Monday, February 1, 2010

On Being Persistent

Those of us who have multiple hard drives in our computers will inevitably boot up one morning to find the naming scheme for these drives has changed. What was once /dev/sda is now /dev/sdb and vice versa. Your computer won't boot and fsck complains about an uknown or mismatched filesystem type. This is especially true after a kernel upgrade. Nothing really can be done about this random renaming, as it is all in the timing. But how can you fix the problem?



Actually, it's a pretty easy fix. Instead of using /dev/sd? in places like grub's menu.lst file and the all important /etc/fstab, use a special label that doesn't change. I find using UUID's to be the best solution.



There are a couple of ways to get the UUID of a hard drive. The easiest is to use the blkid command from the commandline. It lists all the hard drives, along with their UUID and TYPE:



$ blkid
/dev/sdb1: LABEL="ReiserData" UUID="0e9c1455-4993-4ddf-a86a-a3dac116a5cc" TYPE="reiserfs"
/dev/sda1: UUID="a846c306-3114-403d-b893-a3e27704755a" TYPE="ext3" SEC_TYPE="ext2"
/dev/sda3: UUID="c03ce6c7-32ab-41e9-b603-da09acc0fbab" TYPE="ext4"
/dev/sda4: UUID="303889b7-ae78-436f-85ac-da95b2280596" TYPE="ext3"
/dev/sda5: LABEL="/home" UUID="d6db1ae8-30d6-48ee-8a66-90912480e8be" TYPE="ext3" SEC_TYPE="ext2"
/dev/sda6: UUID="71712106-eef9-48c3-840f-20fb77173a9d" TYPE="swap"
/dev/sdb2: LABEL="GAMEY2" UUID="4B89-0200" TYPE="vfat"
/dev/sdc1: UUID="59ac55ab-93f8-4466-804d-57d1148b76e5" TYPE="ext3"
/dev/sdc2: UUID="260834A7083477BF" LABEL="Media" TYPE="ntfs"


You can also get the UUID of paritions by using ls :



$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Feb 1 00:00 0e9c1455-4993-4ddf-a86a-a3dac116a5cc -> ../../sdc1
lrwxrwxrwx 1 root root 10 Feb 1 00:00 260834A7083477BF -> ../../sdb2
lrwxrwxrwx 1 root root 10 Feb 1 00:00 303889b7-ae78-436f-85ac-da95b2280596 -> ../../sda4
lrwxrwxrwx 1 root root 10 Feb 1 00:00 4B89-0200 -> ../../sdc2
lrwxrwxrwx 1 root root 10 Feb 1 00:00 59ac55ab-93f8-4466-804d-57d1148b76e5 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Feb 1 00:00 71712106-eef9-48c3-840f-20fb77173a9d -> ../../sda6
lrwxrwxrwx 1 root root 10 Feb 1 00:00 a846c306-3114-403d-b893-a3e27704755a -> ../../sda1
lrwxrwxrwx 1 root root 10 Feb 1 00:00 c03ce6c7-32ab-41e9-b603-da09acc0fbab -> ../../sda3
lrwxrwxrwx 1 root root 10 Feb 1 00:00 d6db1ae8-30d6-48ee-8a66-90912480e8be -> ../../sda5


You can see that FAT32 and NTFS partitions have different kinds of UUIDs, but it should work for our purposes. Now you just need to replace all references to the various /dev/sd? names with the correct UUID. This will remove all "randomness" from the boot order. Even if you currently have only one hard drive, you really should go to this "persistent" method of naming partitions, as you never know when you might add another.



In the /etc/fstab, replace /dev/sd? with UUID="uuid", like this:




#/dev/sda4
UUID="303889b7-ae78-436f-85ac-da95b2280596" / ext3 defaults 0 1


and in the /boot/grub/menu.lst file, replace it with the /dev/disk/by-uuid path, like this:



kernel /boot/vmlinuz26 root=/dev/disk/by-uuid/303889b7-ae78-436f-85ac-da95b2280596 ro vga=773


The Arch Linux Wiki has a very nice page on Persistent block device naming here: http://goo.gl/GTWT.



No comments:

Post a Comment