1

I want to automatically mount an LVM logical volume on an external drive as soon as it is connected to the computer.

The solution I choose here is to use udev to detect plug in events and udisks2 to mount the partition. For other alternatives see a more generic approach of logical volume auto-mounting

I am asking for knowing if my approach of udev and udisks2 is doomed or if it is my achievement that failed. The following is a brief sum-up of all my attempt.

The simplest udev rule that does not work

To my mind udev rules seems to be appropriate to assert drive presence and set UDISKS_AUTO for the given partition so udisks2 can automount the partition. Therefore I created the following rule in its own file /etc/udev/rules.d/61-lvm-automount-lv.rules

SUBSYSTEM=="block"\
, ENV{ID_FS_UUID}=="<UUID of my LV>"\
, ENV{UDISKS_AUTO}="1"

Then I reload the rules sudo udevadm control --reload (or even better restart my computer), check that the rule is read and UDISKS_AUTO is set to one for the given partition sudo udevadm test /sys/devices/virtual/block/dm-1 and confirm that in the end of the output I can see UDISKS_AUTO=1

However, when I unlplug/plug the external disk the partition is not mounted automatically.

I tried to find the maximum of information about what's going wrong thanks to journactl, udisksctl monitor but the only useful messages was from udevadm monitor --environment --udev showing that:

UDEV  [3897.839040] change   /devices/virtual/block/dm-1 (block)
.ID_FS_TYPE_NEW=ext4
ACTION=change
DEVLINKS=/dev/mapper/wdhdd0-wd0 /dev/disk/by-uuid/b32cefaa-041e-4ef7-8c82-fe53739aefff /dev/disk/by-label/wd /dev/disk/by-id/dm-name-wdhdd0-wd0 /dev/wdhdd0/wd0 /dev/disk/by-id/dm-uuid-LVM-hwQrCjWSOCCGpJqcdPv3NHaP3OOK6kFQj2Y6j3X51A69FEYNQeHtt8wVnVnlb93N
DEVNAME=/dev/dm-1
DEVPATH=/devices/virtual/block/dm-1
DEVTYPE=disk
DM_ACTIVATION=1
DM_COOKIE=6324838
DM_LV_LAYER=
DM_LV_NAME=wd0
DM_NAME=wdhdd0-wd0
DM_SUSPENDED=0
DM_UDEV_DISABLE_LIBRARY_FALLBACK_FLAG=1
DM_UDEV_PRIMARY_SOURCE_FLAG=1
DM_UDEV_RULES=1
DM_UDEV_RULES_VSN=2
DM_UUID=LVM-hwQrCjWSOCCGpJqcdPv3NHaP3OOK6kFQj2Y6j3X51A69FEYNQeHtt8wVnVnlb93N
DM_VG_NAME=wdhdd0
ID_FS_LABEL=wd
ID_FS_LABEL_ENC=wd
ID_FS_TYPE=ext4
ID_FS_USAGE=filesystem
ID_FS_UUID=b32cefaa-041e-4ef7-8c82-fe53739aefff
ID_FS_UUID_ENC=b32cefaa-041e-4ef7-8c82-fe53739aefff
ID_FS_VERSION=1.0
MAJOR=253
MINOR=1
SEQNUM=6641
SUBSYSTEM=block
TAGS=:systemd:
UDISKS_AUTO=1
USEC_INITIALIZED=5756122

So it seems that udisks2 should automatically mount this partition thanks to UDISKS_AUTO=1 but no. However, I can still execute udisksctl mount -b /dev/wdhdd0/wd0 and now the partition is mounted manually.

Other rules that does not work

Changing the value of UDISKS_SYSTEM since logical volume might be interpret as system volume hence has to be hidden by default

SUBSYSTEM=="block", ENV{ID_FS_UUID}=="b32cefaa-041e-4ef7-8c82-fe53739aefff", ENV{UDISKS_SYSTEM}="0", ENV{UDISKS_AUTO}="1"

Add a rule to unignore the physical volume containing the logical volume with ENV{UDISKS_IGNORE}="0". I understand perfectly why physical volume has to be hidden and not mounted in the filesystem but I though that maybe the logical volume inherit some properties.

Change the order in which rules are loaded, it requires to change the number 61 in the beginning of the name of the file 61-lvm-automount-lv.rules. To set my simple rule earlier in the order of the udev files to be loaded first it requires adding IMPORT{builtin}="blkid" in the beginning of the rule for those who wants to try. Conversely, I tried to put the simple rule in the end.

However these attempts leads to no improvements whatsoever

My last insight about udev

My last idea was to make udev start the shell command I use to mount the LV manually /usr/bin/udisksctl mount -b /dev/wdhdd0/wd0 at the right time that is after the PV has been successfully mounted. So I naively tried the following rule with name 991-mountthatstupidcomputer.rules to be evaluated last:

ACTION=="add"\
, SUBSYSTEM=="block"\
, ENV{ID_FS_UUID}=="<UUID of my PV>"\
, RUN{program}+="/usr/bin/udisksctl mount -b /dev/wdhdd0/wd0"

According to the man page of udev about RUN

add a program to the list of programs to be executed after processing all the rules for a specific event

So it should have been done ! But no once again automount failed and I could see why in the log. In fact the script is executed right after udev has considered the event add on the given PV has been finished. However, that was without counting on the nasty dmsetup thing I did not know about. It seems that all the LVM setup is delayed so when udev execute my script the PV is not yet mounted. In Ubuntu, I could find the usage of dmsetup in the rules:

  • 55-dm.rules:52
  • 95-dm-notify.rules:12

That the point where I am stuck right now. There might be a solution based on this DM_COOKIE that dm_setup seems to use but I gave up while I was looking at the dm_setup manual. It definitely seems to me that I have dug to deep here for a problem as simple as mounting a partition. I might have missed something simpler somewhere. Also, I would like to stick with standard tools and not too much scripting all over my system that will broke sooner or latter.

Goal

Ultimately, I would like that a given logical volume could be automatically mounted by my system exactly as standard (non-lvm) partitions are usually mounted. I am open to any suggestion on the best way to do that. I oriented this question with udev and udisks2 because it seems to do a perfect job for standard partition under Ubuntu, so I would like to achieve the same thing for LVM.

Edit

My /var/log/syslog right after plugging in the disk with the simplest rule in the beginning:

[12766.403419] usb 1-1: new high-speed USB device number 9 using xhci_hcd
[12766.553014] usb 1-1: New USB device found, idVendor=1058, idProduct=1003
[12766.553020] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[12766.553024] usb 1-1: Product: External HDD    
[12766.553029] usb 1-1: Manufacturer: Western Digital 
[12766.553032] usb 1-1: SerialNumber: 57442D574D41565531333130343938
[12766.553817] usb-storage 1-1:1.0: USB Mass Storage device detected
[12766.554314] scsi host5: usb-storage 1-1:1.0
mtp-probe: checking bus 1, device 9: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
mtp-probe: bus: 1, device: 9 was not an MTP device
upowerd[2160]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0
upowerd[2160]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1
kernel: [12767.564246] scsi 5:0:0:0: Direct-Access  WD 15EARS External  1.75 PQ: 0 ANSI: 4
kernel: [12767.565075] sd 5:0:0:0: Attached scsi generic sg1 type 0
kernel: [12767.565196] sd 5:0:0:0: [sdc] 2930277168 512-byte logical blocks: (1.50 TB/1.36 TiB)
kernel: [12767.565482] sd 5:0:0:0: [sdc] Write Protect is off
kernel: [12767.565487] sd 5:0:0:0: [sdc] Mode Sense: 23 00 00 00
kernel: [12767.565740] sd 5:0:0:0: [sdc] No Caching mode page found
kernel: [12767.565749] sd 5:0:0:0: [sdc] Assuming drive cache: write through
kernel: [12767.620230]  sdc: sdc1
kernel: [12767.621669] sd 5:0:0:0: [sdc] Attached SCSI disk
systemd[1]: Starting LVM2 PV scan on device 8:33...
lvm[31988]:   1 logical volume(s) in volume group "wdhdd0" now active
systemd[1]: Started LVM2 PV scan on device 8:33.
  • This should work automatically. As far as I know, there is nothing special about LVs in this context - they are just another block device. But the place to look for problems/error messages are the system logs in /var/log, specifically message and syslog. What do those say? – Faheem Mitha Mar 26 at 17:39
  • 1
    I'd also recommend checking out udiskie. That's what I use. It's packaged for Debian, and thus for Ubuntu. And it sounds like you are using Ubuntu. – Faheem Mitha Mar 26 at 17:45
  • Yes thanks for your advice I'll edit the main post with the content of syslog right after the disk is plugged in. Also I have seen udiskie while I was browsing the documentation and all the help I could find about udisks and udev, but I hav not tried udiskie yet so I'll try that latter and report my experience. – Émilien Mar 26 at 18:00
  • However I have no /var/log/messages or anything similar maybe it has been removed from Ubuntu 18.04 – Émilien Mar 26 at 18:09
  • 1
    Check /var/log/syslog. – Faheem Mitha Mar 26 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.