sys·ad·min·ol·o·gy [sis-ad-mih-nol-uh-jee]

noun

  1. The scientific study of system administration and related phenomena.

Monday, 31 March 2014

Specifying UUIDs with mkfs

After loosing a RAID50 drive on a SAN, I found myself having to recreate partitions on a group of download mirrors. They run Debian 7 (Wheezy) and so make use of UUIDs in /etc/fstab. (I know they're not the only distribution that do!)

Given I had 6 hosts to do this on, I didnt want to have to run blkid, and then update the UUID in /etc/fstab for each. Even doing some sed foo to replace it seemed a bit over the top. Fortunately, mkfs.extX allows you to specify the UUID of the partition you are formatting.



mke2fs .... [ -U UUID ] ....

So given an fstab entry like this:

UUID=d71ae821-febb-4bbe-b208-5539bd824436 /var/www ext3 noatime 0 3

This is what I ended up with:

MYUUID=$(grep UUID /etc/fstab | grep www | awk -F" " '{print $1}' | sed 's/UUID=//'); mkfs.ext3 -U ${MYUUID} /dev/xvdb1

For a quick run down of what the  commands do:

  1. first we look for the string UUID in /etc/fstab. 
  2. Then we grep that output for www which ensured I only got the UUID of the partition in question. Obviously if "www" occurred in more than one entry, this would not be sufficient. 
  3. I use AWK to seperate the UUID from the rest of the fstab entry
  4. Then finally (for this section of the command at least) I use sed to remove the "UUID=" leaving me with the UUID only. cut could have been used here as we know the length of UUID= in all cases.
  5. Then the mkfs.ext3 is issued, using -U to specify the UUID gleaned from the previous command.


I was then able to paste that command into each respective console. Finally, running

mount -a

on each host, restored the partition, ready to have content copied on to it.

Looking back, I could have automated fdisk too.... Ah well! Next time!

No comments:

Post a Comment