jump to navigation

mounting usb drives at boot using udev on debian 2010 March 27 11:31

Posted by diamond in : Tech , trackback

I have an external 1.5TB usb hard disk that i use for backups. If i add it to /etc/fstab as normal, it will fail to mount on boot, as the usb subsystem won’t be initialized that early. This is unsatisfactory. So, here’s what i did to fix this.

  1. I added the drive to /etc/fstab with this line:/dev/disk/by-label/bmopbackup1 /mnt/backup ext3 defaults,noauto 0 0
    Note the noauto option (don’t mount this drive on boot), and the fsck pass number set to zero (don’t check this filesystem for errors on boot). As you can see, i labelled the external hard drive as bmopbackup1, using e2label. It just makes it a little easier to work with.
  2. Next, i needed to check how udev can recognise my drive:
    # udevadm info -q env -n /dev/disk/by-label/bmopbackup1
    ID_VENDOR=WD
    ID_MODEL=15EADS_External
    ID_REVISION=1.75
    ID_SERIAL=WD_15EADS_External_57442D574341565530323131333934-0:0
    ID_SERIAL_SHORT=57442D574341565530323131333934
    ID_TYPE=disk
    ID_INSTANCE=0:0
    ID_BUS=usb
    ID_PATH=pci-0000:00:10.4-usb-0:4:1.0-scsi-0:0:0:0
    ID_FS_USAGE=filesystem
    ID_FS_TYPE=ext3
    ID_FS_VERSION=1.0
    ID_FS_UUID=d519f829-eef8-4651-9a21-70a0552ad933
    ID_FS_UUID_ENC=d519f829-eef8-4651-9a21-70a0552ad933
    ID_FS_LABEL=bmopbackup1
    ID_FS_LABEL_ENC=bmopbackup1
    ID_FS_LABEL_SAFE=bmopbackup1

    As you can see, udev sets ID_FS_LABEL to bmopbackup1, so i can use that to uniquely identify the device to udev. If you don’t have a label on the drive, you could just as easily use ID_FS_UUID.
  3. So, here’s the magic part. I created a udev rules file /etc/udev/rules.d/99-bmopbackup.rules with the following line:
    SUBSYSTEMS=="block", ENV{ID_FS_LABEL}=="bmopbackup1", RUN+="/etc/scripts/bmopbackup-connected"
    This tells udev that when it sees a block device with the filesystem label bmopbackup1, run the specified script.
  4. And finally, I created the /etc/scripts/bmopbackup-connected script:
    #!/bin/bash
    {
    date
    fsck -aC /dev/disk/by-label/bmopbackup1
    ret=$?
    date
    if [ $ret -eq 0 ]; then
    echo "Fsck succeeded, mounting"
    mount /dev/disk/by-label/bmopbackup1
    else
    echo "Fsck failed, not mounting"
    fi
    } &> "/tmp/$(basename "$0").log" &

    If i didn’t want to run fsck on the drive, i could just tell udev to run mount /mnt/backup directly. However, given that this drive is used for backups, i definitely want the drive checked every boot (if i was really paranoid, i’d add the -f to fsck, to force it to run a full filesystem check if it’s marked clean).
  5. If you try something similar to the above and it doesn’t seem to be working, a useful check is to see what udev thinks it should run when a given device is connected:
    # udevadm test /sys/block/sdb/sdb1
    This program is for debugging only, it does not run any program,
    specified by a RUN key. It may show incorrect results, because
    some values may be different, or not available at a simulation run.
    ...
    udevtest: run: '/etc/scripts/bmopbackup-connected'

    To run this test, you have to supply the sysfs path to where your device is currently connected. /dev/disk/by-label/bmopbackup1 is a symlink to /dev/sdb1, so /sys/block/sdb/sdb1 is the equivalent sysfs path.

Comments»

1. maria - 2010 March 29 15:05

bmop!?!?!? :D

2. diamond - 2010 March 29 17:02

Blue Mittens Of Power for evah!