Saturday, March 7, 2009

How to put a USB drive into /etc/fstab

I recently started rebuilding my Linux PC. It's intended to be the household's primary storage for documents, photos, etc.... So, that means it makes the most sense to attach my external hard drive to that computer.

But the external drive is a USB drive. I'm not really familiar with the inner workings of Linux's support for USB, but I have a feeling that it might not be mapped to the same device file (/dev/xyz) every time I attach it to the system. So, how can I automate backups when I'm not sure that the drive can be mounted on the system the same way every time? Which device do I put into fstab? What if I plug it into a different USB port? Will that make the device different, meaning that I'll need to mount it differently?


So, after doing some searching, I discovered UUIDs. I haven't worked with Linux in a couple years, and I've never seen this before now. But it's exactly what I was looking for.

The UUID is uniquely associated with every block device, so no matter what device my drive is mapped to in the OS, I can always use the UUID to mount it to the same directory.

This is how I set it up on Ubuntu.

First, with the drive plugged in, you can list out the UUIDs of all devices and get the one for the drive:
$ sudo blkid
/dev/sda1: UUID="52f102c5-01b9-41eb-8670-3c080e6ee1b7" SEC_TYPE="ext2" TYPE="ext3"
/dev/sda2: TYPE="swap" UUID="414154c6-7491-49db-8abe-015a22c2d5a1"
/dev/sda4: UUID="4c96575f-329a-4b49-aac0-fdb31ecc6aa4" SEC_TYPE="ext2" TYPE="ext3"
/dev/sda5: UUID="1c7d659c-3725-4a81-8d0e-ba39d977c242" TYPE="ext3"
/dev/sda6: UUID="d3b2dd43-aed0-4ffe-a021-3664435296d2" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdb1: UUID="1f11ebeb-8c10-4b01-a87b-b7db3d3ad54e" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc1: UUID="60406D53406D30C8" LABEL="FreeAgent Drive" TYPE="ntfs"


The last entry is my external hard drive. Now, all I need to do is add an entry to /etc/fstab using the UUID instead of a device path:
# USB FreeAgent drive
UUID=60406D53406D30C8 /mnt/FreeAgent auto defaults 0 0

No comments: