USB hard drive being given different device names every boot

Q I have a Seagate 250GB USB hard drive attached to my Dell Inspiron laptop running Ubuntu 7.04. There is no problem with the drive being recognised, but every time I boot up, it appears at a different place in dev - anything from /dev/sdb to /dev/sdh and all points in between to date. This makes auto mounting of the various partitions I've defined on the drive awkward, to say the least. Is there any way I can get this drive to always appear as a specific device in /dev so that I can add it into /etc/fstab and save myself the frustration of having to find out where Ubuntu thinks the drive is today and then manually mounting all the partitions?

Or is there some way of entering the partitions into /etc/fstab so that, whatever their /dev location, they will always mount at the same point in /media? Ubuntu does mount the partitions on startup, but as most of them are 40GB it's not too helpful having them called '40GB disc-1' '40GB disc-2' etc. I need them to automount in more descriptive locations.

A It is possible to tell fstab to mount partitions by their unique ID instead of the traditional /dev/sd? node. If you look in /dev/disk/by-id, you will see symbolic links to the various partitions using their IDs. These IDs stay the same and are updated to point to the correct node when udev (the Linux device handler) starts. You could use these in /etc/fstab instead of the traditional node names, but these are really intended for use with fixed disks.

The advantage for fixed disks is that if you add another partition, causing the numbering to change, /etc/fstab will still work, and Ubuntu uses this system by default. When using removable disks, it is best if you avoid putting them in /etc/fstab at all. In that case the Gnome Volume Manager will automount them when they are detected, at boot or any time later. However, this leads to the problem you have, so we use another udev feature to ensure the device names do not change. Udev supports rules that can, among other things, specify the /dev node name for a particular device or filesystem. This persistent naming means that you can also give your devices meaningful names, like /dev/ extdisk for your external disk and /dev/camera for, well, I'm sure you get the idea. To use these rules, you need to be able to uniquely identify the device, and the udevinfo command helps here. If your disk is currently /dev/sda, run

udevinfo --attribute-walk --path /block/hda | less

If you want to query a specific partition, say sda1, use /block/sda/sda1 for the path. Looking through the output you will see lines like

ATTRS{model}=="model_code"
ATTRS{vendor}=="manufacturer"

that can be used to identify the device. Then edit or create the file /etc/udev/rules.d/10-local.rules and add the line

BUS=="usb", KERNEL=="sd*",
ATTRS{model}=="model_code", ATTRS{vendor}=="manufacturer", NAME:="extdisk%n",
SYMLINK+="%k"

the items using == are tests, all of them must match for the rule to apply. The first two make sure you are dealing with a USB disk device or partition, the next two are copied directly from the udevinfo output. The next item sets the node name to /dev/extdisk for the disk and /dev/extdiskN for the Nth partition. The final part creates symlinks to the devices using the original names that would have been used without this rule. This rule applies to the disk and any partitions on it, but you can also match individual partitions, which is useful for things like cameras, MP3 players or memory cards in your card reader that appear as a single partition. For example

BUS=="usb", KERNEL=="sd?[0-9]",
ATTRS{model}=="model_code", ATTRS{vendor}=
="manufacturer", NAME:="camera",
SYMLINK+="%k"

In this case only partitions are mounted by the pattern sd[a-z][0-9], which means sd followed by any single letter then any single number, and the partition appears as /dev/camera instead of /dev/camera1. There is plenty of useful information on writing udev rules at www.reactivated.net/udevrules.php You may still find the Gnome Volume Manager mounts with names like /dev/disk-n. The easiest solution to this is to give the partitions labels when you create them, then the volume manager will use these. To name an ext3 partition when you create it, use

mke2fs -j -L Volume_Name /dev/sda1

and to add or change a volume name on an existing filesystem

tune2fs Volume_Name /dev/sda1

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar