Ubuntu – Automount a logical volume on an external disk drive LVM partitioned

automountexternal-hddlvmudisksusb-drive

Problem

I have an external HDD with one logical volume perfectly working that can be mounted with:
udisksctl mount -b /dev/mapper/wd0hdd-wd0
It will create a new directory under /media/username/wd which is the expected behavior since wd is the label of my disk wd0 the name of my logical volume and username my username.

However, I would like that this logical volume could be mounted automatically (without typing this command) when I plug the external HDD in, just like udisks2 does usually with standard linux partition (non-lvm). [Problem1]

Besides, I have noticed that /media/username/wd belongs to root:root with 755 rights which is problematic since I cannot create files/directory at the root of my partition as a normal user. For standard linux partition, the directory created belongs to username which is expected [Problem2]

Finally, Nautilus does not display the volume in the left pane as usual for standard linux partition instead it is in Other Locations. However, the gnome-extension Place Status Indicator list the new wd correctly below my other drives. [Problem3]

Expected solution

Find a way to tell udisks2 to consider a given logical volume to be auto mounted as a removable media.

Reproduce

Setup an external drive with a single logical partition:

sudo lvmdiskscan # Suppose sdc is the candidate
sudo pvcreate /dev/sdc
sudo vgcreate wd0hdd /dev/sdc1
sudo lvcreate -l 100%FREE -n wd0 wd0hdd
sudo mkfs.ext4 /dev/wd0hdd/wd0

[Facultative] use Gnome-Disk to change the label of the disk to wd

Reboot Ubuntu and see what happens in nautilus and in /media/username when

  • disk plugged-in during boot
  • disk plugged-in after startup has completed

Diagnostic

Tell me what could be useful here.

I tried to find something useful in the log to help track the problem, there is nothing relevant displayed by journactl however there might be some hints about what is going wrong thanks to udisksctl monitor right after I mount the device with udisksctl mount -b /dev/mapper/wd0hdd/wd0

17:49:54.019: Added /org/freedesktop/UDisks2/jobs/2
 org.freedesktop.UDisks2.Job:
   Bytes:              0
   Cancelable:         true
   ExpectedEndTime:    0
   Objects:            ['/org/freedesktop/UDisks2/block_devices/dm_2d5']
   Operation:          filesystem-mount
   Progress:           0.0
   ProgressValid:      false
   Rate:               0
   StartTime:          1553186994019094
   StartedByUID:       0
17:49:54.095: /org/freedesktop/UDisks2/jobs/2: org.freedesktop.UDisks2.Job::Completed (true, '')
17:49:54.095: Removed /org/freedesktop/UDisks2/jobs/2
17:49:54.140: /org/freedesktop/UDisks2/block_devices/dm_2d5: org.freedesktop.UDisks2.Filesystem: Properties Changed
 MountPoints:          /media/mil/wd1
17:49:54.141: /org/freedesktop/UDisks2/block_devices/dm_2d5: org.freedesktop.UDisks2.Block: Properties Changed
 UserspaceMountOptions:        uhelper=udisks2
17:49:54.141: /org/freedesktop/UDisks2/block_devices/sdd: org.freedesktop.UDisks2.PartitionTable: Properties Changed
 Partitions:           ['/org/freedesktop/UDisks2/block_devices/sdd1']

I don't know if this UserspaceMountOptions: uhelper=udisks2 could help me if I manage to change these mount options.


Failed attempts

The HintAuto property and udisks2

The HintAuto is a property that can be overridden thanks to UDISKS_AUTO in udisks2. I spotted that the physical partition udisksctl info -b /dev/sdc1 | grep HintAuto -> HintAuto: true. However, HintAuto is false for the logical device udisksctl info -b /dev/mapper/wd0hdd-wd0 | grep HintAuto -> HintAuto: false

I managed to override the value of HintAuto for the logical volume thanks to a udev rules as discussed in Automount LVM logical volume with a udev rule and udisks2 however it has not solved my problem since udisks2 still does not automount the partition.

Unsatisfactory solution

Use /etc/fstab either by editing directly or using gnome-disk-utility, it just solves the problem to auto-mount at startup but not if I plug the disk latter. Also, it does not solve Problem 2 nor Problem 3 (I thought x-gvfs-show would solve but no) and as far as I know /etc/fstab is for static drive and udisks2 is for all kind of removable drives, so I would prefer a udisks2 solution.

Use awesome shell script started by some daemon detecting when the disk is plugged. Huum well maybe if I have no other choice but I did not find any script solving all the three problems mentioned above. Also I still believe the work could be done by udisks2.

Tell udisks2 to auto mount all the logical volume. That would be undesired since some of my logical volume are other linux system. Anyway I did not find any way to do that.


Motivation

People could ask:
– Why a single LVM partition on the whole disk instead of a single standard partition ? KISS !
– Yes you are right ! However what I try to achieve here is just a simpler scenario of what I want to achieve in the end. I have another disk on which I installed a small ESP partition to multi-boot in EFI mode and another LVM physical volume containing two logical volumes with two different linux and a third huge logical volume for media data storage. Most of the time this disk is just plugged in my personal computer, so I only use the media data storage partition for backup and music however sometimes I go to other places without my computer and only bring my disk, thus I can boot with my own linux, emacs and music player configured as I like and start working in a pleasant environment. In this context, LVM is really useful for this setup to easily resize and manage encryption.

Best Answer

Finally after so many tries the best and only working solution I found to automount logical volume is to use SystemD. Create your <path-to-mount-folder>.mount and <path-to-mount-folder>.automount file under /etc/systemd/system/

media-username-wd.mount

[Unit]
Description=Western Digital external drive mount in media

[Mount]
What=/dev/wdhdd0/wd0
Where=/media/username/wd
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target

media-username-wd.automount

[Unit]
Description=Western Digital external drive automount in media

[Automount]
Where=/media/username/wd

[Install]
WantedBy=multi-user.target

Install these new services

systemctl daemon-reload
systemctl disable media-mil-wd.mount
systemctl enable media-mil-wd.automount

The service .mount is disabled and only the .automount is enabled on purpose. The former is used in the spirit of fstab to mount partition at startup and the latter to automount partition when detected (external drive is plugged). Although the existence of a .mount file is required for every .automount file created, it is not mandatory to enable the service.

Check status

systemctl start media-mil-wd.automount
systemctl status media-mil-wd.automount

I would like to thanks Tomáš Tomeček for its inestimable post Automount with SystemD that could have saved me hours of udev manual reading if only I discovered it sooner.

Finally this solution is now good enough for me it solve my main issue [Problem1] and also [Problem2] which is nice. However it does not resolve [Problem3] but I can live with that for now.

I am still open to any suggestions especially if they are either simpler or allow to solve the [Problem3] that is perfect integration in Nautilus.