HOWTO get a disks UUID number

From LinuxReviews
Jump to navigationJump to search

Linux machines name disks according to their placement on the controllers so the first SATA SSD or HDD would be sda, the second sdb and so on. This is impractical when adding and removing disks so Linux also gives drives a unique UUID number.

Why UUID?[edit]

If you add drives partitions using paths like /dev/sdc2 to files like /etc/fstab or /etc/crypttab you'll run into trouble if you swap drives around at a later date. Your /dev/sdc2/ would become /dev/sdb2 if you pull the second drive. Using UUIDs ensures that the reference to the partition you specify remains the same, the drives ID stays the same regardless of SATA port it's attached to or how many drives are present.

blkid will list all your UUIDs[edit]

Simply run blkid to get a nice list of all the drives and storage units present and their UUID numbers. This command can be used as a regular user. blkid has some more advanced options too[1] and many of them require you to run it as the root user.

lsblk will give you a nice-looking tree list[edit]

The command lsblk will give you a nice-looking tree view of the drives and partitions attached to the system. This command can list UUIDs but does not do so by default. The trick as laid out in the secret manual page[2] is to add -o to change the output and +UUID to get it's standard output + UUID numbers. Thus:

lsblk -o +UUID

will produce an output similar to:

Lsblk.jpg

The UUIDs will be listed on the right side of the output. lsblk does not require you to be root.

Kemonomimi rabbit.svg
Note: Both blkid and lsblk are part of the util-linux package. It should be installed by default on most distributions - but it may not be. util-linux is what you need to acquire if you do not have blkid and lsblk

What if you only know the ata output?[edit]

dmesg errors will sometimes refer to something like ata7.00 - how to do turn that into a UUID?

First step would be to find out what Linux calls the drive in question. This small script will show you what ataX kernel entries correspond to actual drive names like sda, sdb and so on:

File: /usr/local/bin/ata-2-driveletter.sh
#!/bin/bash

ls -l /sys/block/sd* \
| sed -e 's^.*-> \.\.^/sys^' \
       -e 's^/host^ ^'        \
       -e 's^/target.*/^ ^'   \
| while read Path HostNum ID
  do
     echo ${ID}: $(cat $Path/host$HostNum/scsi_host/host$HostNum/unique_id)
  done

Running this script will output a short list like

sda: 1
sdb: 2
sdc: 5

If you were interested in UUIDs of ata5.00 which in this case is sdc you could get the information by running blkid | grep sdc or lsblk -o +UUID |grep sdc

notes[edit]

Questions?[edit]


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.