Back to blog

Mount USB devices to a fixed mountpoint in Debian


The problems I faced when using USB harddisks or USB pendrives for backup processes is that each time you connect them they will have a different ID.
For example:

I connect USB harddisk “Iomega” to my Debian Squeeze machine which will assign ID /dev/sdc1 to it. The next time I connect the same “Iomega” harddisk, Debian will assign ID /dev/sda1 to it.

You can imagine that backup scripts or programs rely on these IDs because they need to mount the USB device to a specific mountpoint.

Luckily there is Udev, which will execute some rules to identify hardware that is being connected. Each hardware device has some info like manufacturer, serialnumbers, type and so on.

So with Udev we can check our hardware for a unique number and configure a Udev rule to always connect device X with serialnumber Y to /dev/mydevice.
Then our backupscripts can always mount /dev/mydevice to /mnt/backup_device1.

Install Udev if not installed already

apt-get install udev

Connect your hardware device, but DO NOT mount it!
Check which ID is assigned to it:

fdisk -l

Which will output something like (in my case, this is the 1TB Iomega drive):

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1      121601   976760001    7  HPFS/NTFS

So this time my Iomega drive has ID /dev/sda1 assigned.
Execute the following command to acquire the hardware info, ofcourse change the /dev/sda1 to your situation:

udevadm info -a -p $(udevadm info -q path -n /dev/sda1)

Note:
In (most?) cases the udevadm command is called udevinfo, the syntax is the same. So replace udevadm with udevinfo if udevadm is not available on your machine.

This will output a lot of information about the device, search for: ATTRS{serial}==
In my case I have:

ATTRS{serial}=="9E092FFFFFFF"

I use serial because it’s possible you have more then 1 USB device of the same manufacturer and type.
In this case serial is a unique identifier.

Create and edit the file with your favorite editor, I use vi:

touch /etc/udev/rules.d/10-local.rules
vi /etc/udev/rules.d/10-local.rules

Add the following rule and save the file:

BUS=="usb", KERNEL=="sd?1", SYSFS{serial}=="9E092FFFFFFF", NAME="%k", SYMLINK+="usbhddiomega"

This will create the symbolic link /dev/usbhddiomega when connecting your device. Disconnect and connect your device now.

Once connected check if the symbolic link exists:

ls -l /dev/usbhddiomega

Which will output something like:

lrwxrwxrwx 1 root root 4 Nov 17 19:13 /dev/usbhddiomega -> sdc1

As you see Udev created the symbolic link “usbhddiomega” and point it to /dev/sdc1. So it’s possible that the next time you connect your device
the symbolic link points to /dev/sda1.

Now try to mount it:

mkdir /mnt/usbiomega
mount /dev/usbhddiomega /mnt/usbiomega

Check mountpoint:

mount

Which will output something like:

/dev/sdc1 on /mnt/iomega type ext3 (rw)

Done!

Labels: backup debian usb

Drop a note