HOWTO: Disable automounting of portable media and manually control automounting

Intro

This howto will allow you to (1) disable automounting of portable media (e.g. USB and flash drives) and (2) give you manual control over the automounting process, so you can select what partitions mount, where they mount, and what programs start automatically.

This hotplug functionality is achieved with the ivman program which uses HAL events to run programs when new hardware changes are detected. This lets you do more than automount external hard drives; for example you can run a script when a printer is attached, etc.

This howto was tested on Ubuntu Jaunty Jackalope.

I. Disable automounting

Run gconf-editor from the command line:

Code:

gconf-editor &

Go to "apps/nautilus/preferences" and uncheck "media_automount" and "media_automount_open". The first option disables the automatic mounting of plugged-in media; the second prevents Nautilus from automatically opening the new device.

II. Set up fstab
You will need to add entries to the fstab file for each device you want to automatically mount. This will vary depending on what you are trying to mount, but here is a rough guide for those new to fstab.

First find out the device name of your drive/partition (e.g. /dev/sdb2). One way to do this is to plug the device in and then run dmesg:

Code:

dmesg

Look for clues about what device was just plugged in toward the bottom of the output.
Another way: if you have not turned off automounting yet, you can plug the device in and run these commands for similar device information:

Code:

mount
df

Once you know the name of your device, find out its UUID (Universally Unique Identifier). This is a unique string that lets you refer to particular drives/partitions which is important when dealing with removable drives (it's better than using /dev/sda2 because those device names may change for removable devices). To find out the UUID for your device, look in this directory:

Code:

ls -l /dev/disk/by-uuid

and find the long number that links to your device. You can also do this with the blkid command or sudo vol_id --uuid /dev/sdb2

Here I'm referring to "/dev/sdb2" by its UUID. This is an ext3 partition; yours may be NTFS (in which case you would use ntfs-3g for read and write support) or something else. If you don't know the partition type, run something like sudo cfdisk /dev/sdb on your device. Many options are available in fstab. Here I'm using "noauto" to prevent automounting of a plugged-in device on startup. You will still be able to allow automounting on startup with the ivman program. I'm also using the "user" option to let an ordinary user mount the device. The "exec" option is disabled in "user" mode, so I re-enabled it (it lets you execute programs on the drive). The "noatime" option is for performance gains (also look into the "relatime" option). For USB flash drives consider the "sync" option for safety reasons (it makes sure that data is written immediately -- in case the USB stick gets unplugged without unmounting first). You can find out more about these options through man mount.

Run it once so that it can create user configuration files in your home directory:

Code:

ivman

You can then stop it with "Ctrl-C".

Before we start using ivman we will need to do some investigating with HAL. The lshal program lists the items in the HAL database, such as your portable devices. We can search for your particular device with grep:

Code:

lshal | grep -A 20 -B 10 "/dev/sdb2"

This will will output 20 lines after and 10 lines before the matching "dev/sdb2" line. You can also search by your volume ID or the UUID. Now look for the line similar to this one:

Use the "info.udi" value you found earlier. Note the use of underscores in the first UUID and dashes in the second. The "exec" line tells ivman what to execute when the device is recognized by HAL. Here we use a mount command, but you can use any command you wish. For example, to mount and open the new device in nautilus use this line instead:

You can even run your own custom scripts this way. For example a script could check if the computer just booted (say through the uptime command) and use that to decide whether or not to start up nautilus on the plugged-in devices.

Add this line to have ivman mount any devices that are already plugged in when it starts. This is great for automounting your drives on startup if they are plugged in (instead of using the 'auto' option in fstab):

You may also want to look at the configuration files in "/etc/ivman/".

Run ivman to test the new configuration until you are satisfied with the behavior. The final step is to run ivman on startup. In GNOME you can go to "System/Preferences/Startup Applications/Add" and add the command "ivman &".

Some more tips. Look into the pmount package to mount drives as user without the need to edit fstab. See this page for a quick pmount example.

Using UUIDs can get tiresome, so try using labels instead. You can get/set drive labels for NTFS partitions with ntfslabel and ext2/3 partitions with e2label. In fstab simply use "LABEL=mylabel" instead of "UUID=..." and then mount with "mount LABEL=mylabel". Finally change your IvmConfigActions.xml file to work with labels as well. Use <ivm:Match name="hal.volume.label" value="mylabel" />. This makes things a lot easier, but of course you'll need to make sure your drive labels are unique and consistent.

To create other rules for custom hardware changes, try this method. Run "lshal > out1.txt" before you plug the device in. Then run "lshal > out2.txt" after you plug it in. Now compare the two outputs with "diff out1.txt out2.txt" and see what has changed. Try to create a rule around the change and launch your own programs/scripts. See this page for more ivman rule examples.

Last edited by adlibre; September 4th, 2009 at 07:44 AM.
Reason: clarification

Just wondering, has this procedure been tested on the most recent version of Ubuntu? I'm looking for a utility that will automatically unmount partitions on external USB/FireWire drives at startup.

I just installed Ubuntu 12.10 on a Mac mini (dual boot with OS X Mountain Lion). On the Mac side, I have a utility that automatically unmounts everything except my Time Machine and media library partitions. I prefer to mount these external drives/partitions only when I'm actually using them. It would be helpful if I could find something in Ubuntu that performs the same function.