3. What's the correct setup to automount the HD when the plug is rebooted?
If you know for sure that your external hard disk will always be powered before the plug and will always be connected to it, all you need is an fstab entry, i.e. a line in /etc/fstab - something like:
/dev/sda1 /media/verbatim/samba ext2 auto 0 0
This would tell your plug to mount the partition /dev/sda1 of drive /dev/sda under the directory /media/verbatim/samba . This directory must exist beforehand.
I have instead opted to go with a combination of fstab entries + udev rules. This has the advantage that, if I power up / connect the drive after the plug boots, it will be recognised and mounted as soon as it is connected.
My fstab entries (One for each partition in the USB HD) are as follows:
UUID=3a3a2235-543d-4e3a-985e-3ef4345db427 swap swap noauto 0 0
UUID=b2f32134-4122-3e4f-b0f4-ad9f63e68dac /media/verbatim/samba ext3 noauto 0 0
A couple of notes on the above:
- "noauto" tells fstab NOT to attempt to mount the partition at bootup - this will be done by udev. The fstab entries serve just as definitions for the filesystems. These definitions will be used by fstab.
- Instead of /dev/sda1, dev/sda2, I have opted to use the UUID of the partitions. This is safer in the sense that /dev/sda means the first partition of the first hard drive. The UUID notation is more specific and will work only with the specific partitions in the specific drive. You can get the UUIDs of the partitions by typing the following after physically connecting the drive:
userid@myplug:/etc/udev/rules.d$ ls -l /dev/disk/by-uuid
After that, I wrote a shell file, /etc/udev/rules.d/v-h-d.sh with the following contents:
#!/bin/sh
# To be called by udev rule for Verbatim external USB disk drive
if [ $1 = "1" ] ; then
swapon /dev/usbhd$1
elif [ $1 = "4" ] ; then
mount /dev/usbhd$1
fi
This file (permissions=-rwxrwx---, owner=root, group=root) will be called by udev when the drive is connected.
After that, I added the following udev rule (file /etc/udev/rules.d/90-verbatim-hard-drive.rules):
KERNEL=="sd*" , ACTION=="add", SUBSYSTEMS=="usb", ATTRS{manufacturer}=="Verbatim", ATTRS{product}=="Desktop USB Drive", ATTRS{serial}=="1705000dd575", SYMLINK+="usbhd%n" RUN+="/etc/udev/rules.d/v-h-d.sh %n"
This file (permissions = -rw-r--r--, owner=root, group=root) essentially tells udev to add a symbolic link under /dev called usbhd1, usbhd2 etc and then call the above shell file, for each partition matching the criteria of my drive. The question of course is which criteria to use, and how to find the ones available in the first place. This is one of the things the udevadm utility does:
userid@myplug:/etc/udev/rules.d$ udevadm info .name=/dev/sda1 --attribute-walk
quoting from the output of udevadm:
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
I have tested the above setup and found it to work both when the USB HD is connected and running when the plug is started, and when it is connected after the plug is booted.
Hope this helps.
Alf