11

How can I get UUID of a volume that contains the / filesystem? The best thing for that I've found to date is blkid -o list. But this output is human readable and hard to parse. Maybe there is a better way?

I need that to parameterize configuration management with system-specific templates.

NOTE about blkid gotcha for those who may reuse outcomes of my question in future: blkid caches results of previous runs at /etc/blkid.tab . That means that running blkid as non-root user first time will not return any data. Also, running blkid as non-root user after root run will return stale (possibly incorrect) data.

7 Answers7

26

Use findmnt:

$ findmnt /        
TARGET SOURCE       FSTYPE OPTIONS
/      /dev/md127p1 ext4   rw,relatime,stripe=256,data=ordered
$ findmnt / -o UUID
UUID
046a554b-d9f5-4b23-82e6-ffaeb98284aa
$ findmnt / -o UUID -n
046a554b-d9f5-4b23-82e6-ffaeb98284aa

It also has several options to control how it looks up information, and how it presents it (including JSON output!). It's part of the mount package, so available on any Ubuntu installation.

muru
  • 207,228
12

Another solution:

lsblk -nr -o UUID,MOUNTPOINT | grep -Po '.*(?= /$)'
  • -n suppresses the header (not really needed, but safer for parsing)
  • -r makes raw output (makes it safer to parse)
  • -o UUID,MOUNTPOINT include only necessary information
muru
  • 207,228
pLumo
  • 27,991
5

You can use the lsblk command to output the UUID, but you need the device name of the partition (such as /dev/sda2). You can get this by using the df command and trimming the output. Use command substitution to give the device name to lsblk. It appears you need sudo to access the UUID, although the normal output of lsblk does not require it:

sudo lsblk -n -o UUID "$(df -P / | tail -n1 | cut -d' ' -f1)"
Arronical
  • 20,241
4

The best solution I managed to come up with is

blkid -o list | awk '/\/[[:space:]]+/{ print $0 }' | tr -s ' ' | cut -d ' ' -f 4

Feels like non-optimal, but works.

Note: [[:space:]] means Space

Elder Geek
  • 36,752
4

Adjust the value of mountpoint to the current mount point of the file system in question:

dev="$(exec awk -v mountpoint='/' '($2 == mountpoint){ print $1; quit; }' /proc/mounts)" &&
sudo blkid -o export -- "$dev" | sed -ne 's/^UUID=//p'
David Foerster
  • 36,890
  • 56
  • 97
  • 151
1

Here's what I use:

sudo tune2fs -l $(df  / | tail -1 |awk '{print $1}') |grep UUID|awk '{ print $3 }'

tune2fs is in the e2fsprogs package, which I can't remember if it's installed by default.

sudo apt install e2fsprogs

if it isn't installed.

Elder Geek
  • 36,752
John
  • 111
1

Tested to work on 14.04 and 16.04

A simple one-liner that should always produce the UUID of a the root / is

export DRIVE=$(mount | grep ' / ' | awk -F" on " '{print $1}');blkid $DRIVE| cut -d '"' -f2 `

What we are doing here is grepping the output of mount to match the root symbol with spaces on either side / to avoid matching when / is used as a path extension symbol, piping that through awk using " on " as a field separator in order to output only the device name and assigning THAT to the environment variable $DRIVE, then using the output of blkid $DRIVE piped through cut using the " as a field separator and selecting only the second field which strips out everything else, leaving only the UUID.

Note that what belongs after grep in the above command is actually 'space/space' and not '/' as it appears.

This has the benefit of not requiring sudo and will return the appropriate result regardless of how the drive is mounted.

It would be wise to insure that you aren't using the $DRIVE environment variable for anything else prior to attempting this approach echo $DRIVE will return blank line if you aren't using the variable.

Elder Geek
  • 36,752