scnr.net

It's not possible? Says who?

Automatically mounting a USB HDD with udev

with one comment

Situation

I have an external USB hard disk. It’s permanently connected to a box running Ubuntu Server Edition 9.04. The only partition on the USB disk should be mounted automatically at system startup. However, if I just add the partition to /etc/fstab it doesn’t work. At the time the system tries to mount all partitions the USB HDD isn’t available yet. So I needed to delay the mounting of the partition until it becomes available.

Solution

I tell udev to mount the partition as it becomes available.

First I configure the partition in /etc/fstab:

1
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx    /media/external-usb-disk    ext3    noauto,noatime,errors=remount-ro    0    0

(replace “xxxxxxxx-xxxx-[...]” with your own, real UUID). I used mounting by UUID instead of device name to avoid problems when the name changes (e.g. due to another USB storage being connected). The noauto option prevents the system from automatically mounting the partition on system startup.

Then I create a new file in /etc/udev/rules.d/ called 80-external-usb-disk.rules:

1
ACTION=="add", ENV{DEVTYPE}=="partition", ENV{ID_FS_UUID}=="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", RUN+="/bin/mount -U $env{ID_FS_UUID}"

Finally, to tell udevd to reload the rule configuration:

1
sudo udevadm control --reload-rules

Now every time a new partition with the specified UUID is added, it is mounted.

Some Notes

I should have read /etc/udev/rules.d/README earlier. It says “Files should be named xx-descriptive-name.rules, [...]“. At first my file didn’t have the “.rules” suffix – it took me some time to figure out why it was ignored. I don’t know whether 80 is a good priority for my rules file – but it works for me.

I could have told udev to assign special device names, like /dev/usbhdd and /dev/usbhdd1. But what for? I can identify the partition by the UUID, the mount point has a fixed name and usually you have symlinks under /dev/disk/by-uuid/ whose name won’t change.

Commandline to get some information about a device:

1
udevadm info --query=all --name=/dev/sdx1

Commandline to watch udev events live:

1
sudo udevadm monitor --environment

Written by johnLate

September 19th, 2009 at 5:04 pm

Posted in Uncategorized

One Response to 'Automatically mounting a USB HDD with udev'

Subscribe to comments with RSS or TrackBack to 'Automatically mounting a USB HDD with udev'.

  1. To find out what the UUID of your hard drive is, use the blkid command. Make sure the hard drive is mounted and run:
    $ sudo blkid
    It will print a list of the block device attributes.

    Bram

    5 Nov 09 at 18:33

Leave a Reply