Short Tip: Get UUID of Hard Disks [Update]

shell.png
There is an update to this post available: UUIDs and Linux: Everything you ever need to know.

The Universally Unique Identifier can be used to identify a device independent form its mount point or device name. This is more and more important as many devices today support hot-plugging or are external anyway. Therefore it makes sometimes sense to access a device (for example in fstab) not by device name but by the UUID.

There are several ways to get the UUID. The first one uses the /dev/ directory. While you are on is you might want to check other by-* directories, I never knew of them.

$ ls -l /dev/disk/by-uuid
lrwxrwxrwx 1 root root 10 11. Okt 18:02 53cdad3b-4b01-4a6c-a099-be1cdf1acf6d -> ../../sda2

Another way to get the uuid by usage of the tool blkid:

$ blkid /dev/sda1
/dev/sda1: LABEL="/" UUID="ee7cf0a0-1922-401b-a1ae-6ec9261484c0" SEC_TYPE="ext2" TYPE="ext3"

There you also get the label and other information. Quite usefule.

Btw., if you wonder how “unique” this unique is, here a quote from Wikipedia:

1 trillion UUIDs would have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs.

Pretty unique.

Thanks to Linux By Examples for the initial howto.

31 thoughts on “Short Tip: Get UUID of Hard Disks [Update]”

  1. I use the following command:
    sudo vol_id /dev/hdb5

    and get something like this:
    ID_FS_USAGE=filesystem
    ID_FS_TYPE=ext3
    ID_FS_VERSION=1.0
    ID_FS_UUID=c4e17772-8d5f-4cf5-b254-6ea7684c5d6d
    ID_FS_UUID_ENC=c4e17772-8d5f-4cf5-b254-6ea7684c5d6d
    ID_FS_LABEL=/media/hdb5
    ID_FS_LABEL_ENC=\x2fmedia\x2fhdb5
    ID_FS_LABEL_SAFE=mediahdb5

  2. Alberto, that command was also mentioned at the linked original howto, but Fedora does not provide that binary. So I think that vol_id might be Debian/Ubuntu only and therefore didn’t include it.

  3. If it’s not under “/sbin/vol_id”, have you checked as “/lib/udev/vol_id”? It should be there even for Fedora.

    “/sbin/vol_id” is available in Ubuntu.

  4. Silvio, sorry, I never tried that. But the best would be to start a LiveCD and check the fstab the LiveCD creates on the fly. With the help of that you might find all necessary data to rebuild it, but only by hand.

    I know that grml has some scripts to do that but never looked into them.

  5. nice that UUID has advantages, but what if I clone a disk?
    I have cloned a disk, and opensuse 10.3 can not boot anymore from the clone (identical harddisks). before opensuse 10.2 they did not use UUID and I was happy

  6. tgifriday: If you want to clone a disk UUID won’t work because the clone technique does not clone the UUID afaik.
    In such cases you want to use labels.

  7. Actually, cloning seems to clone UUID, too. At least mirroring does. An example from my machine:

    /dev/sdb1: UUID=”aeb1957f-b184-4810-a203-d7a4875da58a” SEC_TYPE=”ext2″ TYPE=”ext3″
    /dev/sda1: UUID=”aeb1957f-b184-4810-a203-d7a4875da58a” SEC_TYPE=”ext2″ TYPE=”ext3″
    /

    Not a happy day…

    1. UUID aren’t that unique indeed: cloned partitions will have the same UUID. The disk UUID and device letter are likely different though, that’s probably why tgifriday cannot boot anymore, for example because Grub does not know about the new disk UUID.

  8. Not good, there should be a flag/option to choose if you want to clone the UUID as well.

    Nevertheless, this forum entry says how to change the UUID:

    $ uuidgen
    $ tune2fs /dev/hdaX -U numbergeneratedbyuuidgen
    $ vol_id /dev/hdaX
    

    The last step is to verify that everything worked out.

  9. Hehe… I have used the command

    tune2fs -l “” | grep “UUID” | tail –bytes=37

    to simply print out the UUID of a partition. I didn’t realize there was an easier way… 😛

  10. Hi everybody!!

    Somebody knows how can I get UUID or WWLun from disks on HPTru64?

    Best Regards,

    Alejandro Suaza C.

  11. Alejandro, this blog post is about Linux, not about Tru64. If you have problems using Tru64, the best is to ask HP’s customer support or to post the problem (including error messages and a description of what you’ve already tried and why that has failed; not just a question like you did) at an appropriate forum.
    A blog is never the place to discuss a problem in detail.

  12. Thank you Liquidat, you just save my sanity.
    I struggled with restoring grub and then had to deal with a faulty boot sequence because of an erroneous UUID. After running “blkid /dev/sdb2” and inserting the results in fstab all is well.

    Thanks again,
    Dean

  13. Does anybody knows how to get that kind of information from a cdrom? Not only the uuid, but also the label.

  14. oli44: Thanks for pointing out – and the link back to the original plus the name of the original author is all which is needed anyway 🙂

  15. Also, Though I am a newbie
    WHat will happen

    If Virtual Operating System image boots over Host Operating System?

    Being familiar with Ms-Virtual PC.
    Using Ms-Virtual PC software,
    Windows XP + Red Hat Linux 9 operated together
    on 1 Host Windows XP OS

    What will happen to hardware identification like
    MAC Address of NIC(s)
    Hard Disc Idenfitication etc? then?

    May be they will all show same values …

  16. I don’t know, I guess it depends on the virtual machine – and since I’m not familiar with MS virtual PC I can’t help you there.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.