Mounting a USB Drive in a Consistent Location on Ubuntu 9.10 (Karmic)

November 18th, 2009 § 2 comments § permalink

This is an obtuse and unabashedly geeky post, but I spent several hours trying to solve this irritating problem, so I’m posting my results here in an attempt to shortcut this issue for others.

The Problem: I have an external USB drive on which I store Virtual Machines which I run with the very excellent VirtualBox. I like to keep it mounted at /media/VirtualMachines. My VirtualBox configuration (stored in ~/.VirtualBox/) points to this location, so it’s vital that it remains consistent between reboots, remounts, updates, and the like. Prior to Ubuntu 9.10, one used to be able  to specify a consistent mount point using the GNOME userspace tools by simply right-clicking on the volume on the desktop, navigating to “Volume”, and choosing a location by typing it in. This was possible, because prior to version 9.10, Ubuntu relied upon hal to provide an abstraction layer between hardware devices and the kernel. There’s a fair amount of code in GNOME which allows for quick configuration of functionality provided by hal. hal is now deprecated in favor of DeviceKit, so some of those tools are gone, and this is one.

The Solution: I spent a fair amount of time googling about trying to come up with an equivalent. Many of the tutorials that I found were either outdated, or involved diving into the internals of udev/DeviceKit. This seemed like it was a problem best-suited to something simpler. It turns out that you can use plain old /etc/fstab to mount a device in a specific place, using the partition’s UUID instead of a block device path. This is handy, because if it’s a removable device, you won’t know what block device it’ll be assigned when you plug it in, or when the system boots. Because it will mount after boot time, you’ll also have to set the user option to allow gnome to mount the device after you log in — otherwise the action will be restricted to the root user. Be sure also to set auto, so that the OS will automatically mount the drive for you without you having to issue the commands yourself.

The relevant line in /etc/fstab looks like this, for me:

UUID=0e408c42-233a-4d0c-a7be-dac2379092d5 /media/VirtualMachines ext3 rw,auto,user,exec 0 0

You can get the UUID of the relevant partition of your drive by using devicekit-drives:

$ devkit-disks --show-info /dev/sdb1 | grep uuid
 by-id:                     /dev/disk/by-uuid/0e408c42-233a-4d0c-a7be-dac238a1a3e8
 uuid:                        0e408c42-233a-4d0c-a7be-dac238a1a3e8
 uuid:

Of course, substitute /dev/sdb1 for the partition you’re trying to mount.