Discovering the BlockID in Centos



This is just a quick article,

I needed to discover the block ID on both Centos and Ubuntu today and needed this to be injected into a script, I came up with the following:

When you use Centos or Ubuntu / Debian you can use the “blkid” command to discover drive UUID and other useful information..

However if you only need the UUID for a drive (example: In my case to push into a bash script) I made the following:

Centos:

blkid | grep /dev/xvda1 | tail -n 1 | awk -F ‘”‘ ‘{print $4 }’

So instead of something like:

[[email protected] pxe]# blkid

/dev/mapper/VolGroup00-LogVol01: TYPE=”swap”

/dev/mapper/VolGroup00-LogVol00: UUID=”87a00e6a-ac53-4a3d-bf7d-2192184f11b2″ TYPE=”ext3″

/dev/xvda1: LABEL=”/boot” UUID=”c099a807-bcb8-4310-9fd9-1b6dcd234f25″ TYPE=”ext3″

/dev/VolGroup00/LogVol00: UUID=”87a00e6a-ac53-4a3d-bf7d-2192184f11b2″ TYPE=”ext3″

/dev/VolGroup00/LogVol01: TYPE=”swap”

/dev/loop0: LABEL=”Ubuntu-Server 10.04.2 LTS i386″ TYPE=”iso9660″

/dev/xvdb: UUID=”16ce5d9c-2c8d-4603-831e-dfa8c200c3a2″ SEC_TYPE=”ext2″ TYPE=”ext3″

/dev/xvda3: UUID=”02ea7d12-a80f-4075-9938-947b05be210c” TYPE=”ext3″

You will end up with:

c099a807-bcb8-4310-9fd9-1b6dcd234f25

which as you can see is the /dev/xvda1 UUID.

Likewise Ubuntu:

blkid | grep /dev/xvda1 | tail -n 1 | awk -F ‘”‘ ‘{print $2 }’

This is great for grabbing the UUID and pushing it into a variable within some script, like I have 😀

Note that I don’t take responsibility of the use of this script, I admit I wrote that in a few minutes and might not be the best option, but it works for me.

If you notice, the ubuntu and centos one differs by the print command, this is because my ubuntu instance didn’t have a LABEL section, you can adjust the $2 to be whatever section you like…

All its doing is delimiting by the ” symbol….

Enjoy and happy scripting 🙂

–Karl.