Novell Home

Automatically Mount USB Hard Drives

Novell Cool Solutions: Tip
By Christian Prause

Digg This - Slashdot This

Posted: 8 Sep 2006
 

Problem:

USB HDDS are not automounted when no GNOME or KDE session is active in SLES/SLED

Solution:

Seen working in: SUSE 10.1, SLES 10, SLED 10

This article describes how to set a fixed mount point for USB Hard Drives and then automatically have them mounted when plugged in.

I tend to be a bit detailed (chatty if you must) there is a short version of what you have to on the bottom of the page.

Recent SUSE systems rely on udev, hal and DBUS to automatically mount Devices. Unfortunately the actual mount is only done if you are logged in a Gnome (gnome-volume-manager) or KDE session. (see http://www.novell.com/documentation/sled10/readme/RELEASE-NOTES.en.html#b554yf2)

But what if you want your drives to be automounted with another window manager or no X at all? It's actually all there, the only thing missing is a mount point and someone to do the mount.

Here's my scenario:

I've got a Notebook running as a little backup/projects/playingaround-server running SLES 10. The book only has about 40G of internal HDD space I added some USB HDD's to compensate.

Since some Services cannot find their data if it is not in the place where they left it (stupid isn't it ;-) I needed the drives to be mounted exactly the same every time they are plugged in, the server is rebooted, etc.

To make the mount points for the USB drives persistent i added the following lines to "/etc/fstab":

LABEL=WORK-USB300  /storage/WORK-USB300 ext3     auto,hotplug,defaults              1 2
LABEL=PRIVATE-USB300  /storage/PRIVATE-USB300 ext3     auto,hotplug,defaults        1 2

I chose to identify the partitions on the disk by Label (LABEL=WORK-USB300). USB drives are actually linked to "/dev/sd-something" device entries by udev. But the order in which they appear is not always preserved. Someday udev might decide to use "/dev/sda" for the drive you used to find under "/dev/sdb". I used yast (system->Partitioner->...) to set the labels of my (ext3) partitions but you could use "e2label" (ext2/3),"reiserfstune -l" or the like for that.

I use the "auto" option in my fstab to be able to mount the drives with "mount -a" wich is quite handy.

The "hotplug" option is important to be able to boot the system without the usb drives attached. Without the "hotplug" option your system will boot into the rescue console when the drives are not present.

The above is pretty low level. For the fancy stuff please "man fstab" and "man mount".

So for now whenever i want my drives i plug them in and run "mount -a". Happy?... not quite. I HAVE TO MANUALLY TYPE "mount -a". NO WAY, GRRR.

udev, the service doing all sorts of plugging in and out detection, was already so kind to create a "/dev/sd-something" device for our USB HDD. Why shouldn't it be able to ALSO mount them?

In /etc/udev/rules.d/ you will find several .rules files. This is what udev does. Whenever it gets an event it runs through those rules an applies them.

In case of a USB HDD it the rules in "/etc/udev/rules.d/60-persistent-storage.rules" are the important ones.

The rule file numbering may be different. It is used to determine the order of the rules.

To automatically run mount -a when a block device (HDD) is added add a file to the /etc/udev/rules.d/ directory.

99-mount.rules:

#run mount -a everytime a block device is added/removed
SUBSYSTEM=="block", run+="/bin/mount -a"

I called the file 99-something to make sure that the "mount" rule is run after the /dev/sd-something device was created. Otherwise the drive could not be there yet.

To activate the new rule do an "/etc/init.d/boot.udev restart" (or "rcudev restart" where it exists).

To learn more about how to write rules for udev please consult: http://www.reactivated.net/writing_udev_rules.html

That's it.

Short Version:

Add your USB drives to "/etc/fstab" using labels to identify the partitions. Use the options "auto" to be able to mount them with "mount -a" and "hotplug" to avoid problems when the drives are disconnected.

Example:

LABEL=WORK-USB300  /storage/WORK-USB300 ext3     auto,hotplug,defaults              1 2
LABEL=PRIVATE-USB300  /storage/PRIVATE-USB300 ext3     auto,hotplug,defaults        1 2

Create a new rule for udev. Place a new file "99-mount.rules" in "/etc/udev/rules.d/" with the following content:

#run mount -a everytime a block device is added/removed
SUBSYSTEM=="block", run+="/bin/mount -a"

Run "/etc/init.d/boot.udev restart" to make the rule available.

end.


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell