Scripting udev rules to unmount by LABEL

I’ve got a set of external HDDs on usb I use for backup on my home server. I
want them to automatically mount and unmount at /media/backup. They’ve each
got a single (reiserfs) partition labelled BACKUP01, BACKUP02 … and I use
blkid to access the LABEL and create udev rules for only these fs.

I’ve managed to get the mount to work Ok using …
ACTION==“add”, KERNEL==“sd[a-z]1”, PROGRAM=="/sbin/blkid -s LABEL /dev/%k",
RESULT==“LABEL=_BACKUP”, RUN+="/bin/mount -o rw,defaults /dev/%k
/media/backup", OPTIONS=“last_rule”

but the matching umount just doesn’t work …
ACTION==“remove”, KERNEL==“sd[a-z]1”, PROGRAM=="/sbin/blkid -s LABEL
/dev/%k", RESULT==“LABEL=_BACKUP”, RUN+="/bin/umount -l /dev/%k",
OPTIONS=“last_rule”

I’ve tried degugging with
ACTION==“remove”, KERNEL==“sd[a-z]1”, PROGRAM=="/sbin/blkid -s LABEL
/dev/%k", RUN+="/bin/logger '%c]%k]", OPTIONS=“last_rule”

which puts a message in syslog showing %c (the output from PROGRAM) as
blank. The PROGRAM entry works fine for the mount, I’ve run blkid from the
CLI when the device is mounted and it does show the LABEL so it doesn’t
matter if the device is mounted or not for blkid.

Any hints please 'cos I’m stuck.

Alan

Unless I’m missing something here, the very action of physically removing your external backup device will render the unmounting unnecessary. For example, I have a custom udev rule used to mount my backup partition as /media/backup:

SUBSYSTEM==“block”, ATTRS{subsystem_device}==“0x0566”,
ENV{ID_FS_LABEL}==“backup”, SYMLINK+=“backup”

If I plug it in, cat /media/mtab gives:

dean@linux:/home> cat /etc/mtab
/dev/sda6 / ext3 rw,acl,user_xattr 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
debugfs /sys/kernel/debug debugfs rw 0 0
udev /dev tmpfs rw 0 0
devpts /dev/pts devpts rw,mode=0620,gid=5 0 0
/dev/sda7 /home ext3 rw,acl,user_xattr 0 0
/dev/sda1 /windows/C fuseblk rw,noexec,nosuid,nodev,allow_other,default_permissions,blksize=4096 0 0
fusectl /sys/fs/fuse/connections fusectl rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
/dev/sdb2 /media/backup ext3 rw,nosuid,nodev,data=ordered 0 0
/dev/sdb1 /media/disk-1 ntfs rw,nosuid,nodev,uid=1000,utf8 0 0

If I remove it (without manual unmounting), I get:

dean@linux:/home> cat /etc/mtab
/dev/sda6 / ext3 rw,acl,user_xattr 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
debugfs /sys/kernel/debug debugfs rw 0 0
udev /dev tmpfs rw 0 0
devpts /dev/pts devpts rw,mode=0620,gid=5 0 0
/dev/sda7 /home ext3 rw,acl,user_xattr 0 0
/dev/sda1 /windows/C fuseblk rw,noexec,nosuid,nodev,allow_other,default_permissions,blksize=4096 0 0
fusectl /sys/fs/fuse/connections fusectl rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0

Hal takes care of the cleanup.

Similar kind of discussion here:

How to automount USB pendrive and CD/DVD, using udev/HAL/D-BUS | debianHELP

deano ferrari wrote:

>
> Unless I’m missing something here, the very action of physically
> removing your external backup device will render the unmounting
> unnecessary. For example, I have a custom udev rule used to mount my
> backup partition as /media/backup:
>
>> SUBSYSTEM==“block”, ATTRS{subsystem_device}==“0x0566”,
>> ENV{ID_FS_LABEL}==“backup”, SYMLINK+=“backup”
>
<snip>
>
> Hal takes care of the cleanup.
>
<snip>

I’ve not seen HAL do this while I’ve been testing my rules :frowning:

Alan

Fudokai wrote:

> deano ferrari wrote:
>
>>
>> Unless I’m missing something here, the very action of physically
>> removing your external backup device will render the unmounting
>> unnecessary. For example, I have a custom udev rule used to mount my
>> backup partition as /media/backup:
>>
>>> SUBSYSTEM==“block”, ATTRS{subsystem_device}==“0x0566”,
>>> ENV{ID_FS_LABEL}==“backup”, SYMLINK+=“backup”
>>
> <snip>
>>
>> Hal takes care of the cleanup.
>>
> <snip>
>
> I’ve not seen HAL do this while I’ve been testing my rules :frowning:
>
> Alan

I thought I’d just try the ENV{ID_FS_LABEL} you show in your example and
that works for the ACTION==“remove”, tho’ not for the ACTION=“add”.

Where did you find the info about ENV{ID_FS_LABEL}?

Alan

If you do

udevinfo -e

you will get all available attributes for all your devices. For example, a segment of my output to illustrate:

P: /devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1
N: sda1
S: disk/by-id/scsi-SATA_TOSHIBA_MK8032G_X5CX6959T-part1
S: disk/by-id/ata-TOSHIBA_MK8032GSX_X5CX6959T-part1
S: disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part1
S: disk/by-uuid/CAB0555EB0555255
S: disk/by-label/IBM_PRELOAD
S: disk/by-id/edd-int13_dev80-part1
E: ID_VENDOR=ATA
E: ID_MODEL=TOSHIBA_MK8032GS
E: ID_REVISION=AS11
E: ID_SERIAL=SATA_TOSHIBA_MK8032G_X5CX6959T
E: ID_SERIAL_SHORT=X5CX6959T
E: ID_TYPE=disk
E: ID_BUS=scsi
E: ID_ATA_COMPAT=TOSHIBA_MK8032GSX_X5CX6959T
E: ID_PATH=pci-0000:00:1f.2-scsi-0:0:0:0
E: ID_EDD=int13_dev80
E: ID_FS_USAGE=filesystem
E: ID_FS_TYPE=ntfs
E: ID_FS_VERSION=3.1
E: ID_FS_UUID=CAB0555EB0555255
E: ID_FS_UUID_ENC=CAB0555EB0555255
E: ID_FS_LABEL=IBM_PRELOAD
E: ID_FS_LABEL_ENC=IBM_PRELOAD
E: ID_FS_LABEL_SAFE=IBM_PRELOAD

There is a hint about using these environment attributes here:

automatic backups on a removable device

You may have to google ‘ENV{ID_FS_LABEL}’ and similar for further info. (I know the info concerning udev attributes is sketchy). The main thing is to be able to uniquely identify the correct partition with your string matching. Hope this helps.

deano ferrari wrote:

>
> If you do
>
> udevinfo -e
>
> you will get all available attributes for all your devices. For
> example, a segment of my output to illustrate:
>
>> P:
>> /devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1
>> N: sda1
>>
>> S: disk/by-id/scsi-SATA_TOSHIBA_MK8032G_X5CX6959T-part1
>>
>> S: disk/by-id/ata-TOSHIBA_MK8032GSX_X5CX6959T-part1
>>
>> S: disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part1
>>
>> S: disk/by-uuid/CAB0555EB0555255
>>
>> S: disk/by-label/IBM_PRELOAD
>>
>> S: disk/by-id/edd-int13_dev80-part1
>>
>> E: ID_VENDOR=ATA
>>
>> E: ID_MODEL=TOSHIBA_MK8032GS
>>
>> E: ID_REVISION=AS11
>>
>> E: ID_SERIAL=SATA_TOSHIBA_MK8032G_X5CX6959T
>>
>> E: ID_SERIAL_SHORT=X5CX6959T
>>
>> E: ID_TYPE=disk
>>
>> E: ID_BUS=scsi
>>
>> E: ID_ATA_COMPAT=TOSHIBA_MK8032GSX_X5CX6959T
>>
>> E: ID_PATH=pci-0000:00:1f.2-scsi-0:0:0:0
>>
>> E: ID_EDD=int13_dev80
>>
>> E: ID_FS_USAGE=filesystem
>>
>> E: ID_FS_TYPE=ntfs
>>
>> E: ID_FS_VERSION=3.1
>>
>> E: ID_FS_UUID=CAB0555EB0555255
>>
>> E: ID_FS_UUID_ENC=CAB0555EB0555255
>>
>> E: ID_FS_LABEL=IBM_PRELOAD
>>
>> E: ID_FS_LABEL_ENC=IBM_PRELOAD
>>
>> E: ID_FS_LABEL_SAFE=IBM_PRELOAD
>>
>>
>>
>
>
> There is a hint about using these environment attributes here:
>
> ‘automatic backups on a removable device’
> (http://blog.bofh.it/debian/id_217)
>
> You may have to google ‘ENV{ID_FS_LABEL}’ and similar for further info.
> (I know the info concerning udev attributes is sketchy). The main thing
> is to be able to uniquely identify the correct partition with your
> string matching. Hope this helps.
>
>
Thanks deano - I’ll look into this.

For the moment I’ve got something working but I’m not really happy with it
but I’ve got a life to live.

You mentioned HAL and I had a look around the web for info on HAL (not much
better than udev unfortunately :frowning: ). The result is that I’m now not sure
whether I should be using udev rules or HAL.

  • one of the other little niggles I’ve had is that I’ve got a SATA HDD caddy
    system which I’d also like to hot swap. When I’ve re-plastered the wall
    where I’ve been banging my head recently maybe I’ll find time to look into
    that :-).

Alan