CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Disk & Storage / blkid

blkid

Show filesystem type, UUID and label of devices

Beginnerdiskfilesystemfstab

blkid identifies what is on a block device: filesystem type, UUID, label, and partition UUIDs. It's the standard way to get the UUID for /etc/fstab and to check, in a script, whether a disk is still empty.

Syntaxblkid [OPTIONS] [DEVICE...]

Watch it run

You want the type, UUID and label of every filesystem on the instance in one go.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
sudo blkid

One line per device with every tag found (long lines trimmed here). Partitions also carry a PARTUUID from the GPT table. The BIOS boot partition has only a PARTUUID: there's no filesystem on it.

Anatomy of a command

$
commandsudo

blkid probes the raw device; without root it can only show possibly stale cached data.

How it works

libblkid reads small regions of the device and looks for magic numbers: the ext4 superblock magic 0xEF53 at byte 1080, XFS's 'XFSB' at offset 0, swap's signature at the end of the first page, LUKS, LVM, RAID and partition-table headers. When it recognises one it parses the superblock for the UUID, label, block size and version. Results are cached in /run/blkid/blkid.tab; as root, blkid re-validates them, while a normal user only gets the cache. udev runs the same probe when a device appears and stores the results, which is where lsblk -f and the /dev/disk/by-uuid and by-label symlinks come from. UUID is the filesystem's own ID (changes with mkfs); PARTUUID belongs to the GPT partition entry and survives reformatting. If no signature is found blkid prints nothing and exits with status 2.

Options you'll actually use

-s TAGShow only this tag: UUID, TYPE, LABEL, PARTUUID, BLOCK_SIZE
-o valuePrint only values (one per line)
-o exportPrint KEY=value lines you can eval or source in shell
-o full / listDefault NAME: KEY="value" format / a table
-L LABELPrint the device that has this label
-U UUIDPrint the device that has this UUID
-t NAME=valueList devices matching a tag, e.g. -t TYPE=xfs
-pLow-level probe, bypassing the cache (shows more detail)
-c /dev/nullIgnore the cache file

Common mistakes

Running blkid without sudo and trusting the result

As a normal user blkid only reads its cache, which can be stale or empty for new devices.

sudo blkid, or lsblk -f which reads udev's data
Using PARTUUID and UUID interchangeably

UUID belongs to the filesystem and changes when you run mkfs; PARTUUID belongs to the partition table entry and doesn't.

Use UUID= in fstab for filesystems; PARTUUID= mainly for kernel root= parameters
Cloning a disk or volume and mounting both copies on one host

Both have the same UUID: mount by UUID becomes ambiguous and XFS refuses the second mount.

Give the copy a new UUID: tune2fs -U random /dev/X (ext4) or xfs_admin -U generate /dev/X (xfs)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create an ext4 image file labelled 'lab' and print only its UUID.

  2. 2

    Show lab.img's tags as shell variables, then load them and echo the type.

  3. 3

    Check whether an empty file has any filesystem signature, using only the exit code.

Related commands

lsblkList disks, partitions and where they are mountedmkfsCreate a filesystem on a disk or partitionmountAttach a filesystem to the directory tree
PreviousfsckNextdd
Linux / Disk & Storage / blkid

blkid

Show filesystem type, UUID and label of devices

Beginnerdiskfilesystemfstab

blkid identifies what is on a block device: filesystem type, UUID, label, and partition UUIDs. It's the standard way to get the UUID for /etc/fstab and to check, in a script, whether a disk is still empty.

Syntaxblkid [OPTIONS] [DEVICE...]

Watch it run

You want the type, UUID and label of every filesystem on the instance in one go.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
sudo blkid

One line per device with every tag found (long lines trimmed here). Partitions also carry a PARTUUID from the GPT table. The BIOS boot partition has only a PARTUUID: there's no filesystem on it.

Anatomy of a command

$
commandsudo

blkid probes the raw device; without root it can only show possibly stale cached data.

How it works

libblkid reads small regions of the device and looks for magic numbers: the ext4 superblock magic 0xEF53 at byte 1080, XFS's 'XFSB' at offset 0, swap's signature at the end of the first page, LUKS, LVM, RAID and partition-table headers. When it recognises one it parses the superblock for the UUID, label, block size and version. Results are cached in /run/blkid/blkid.tab; as root, blkid re-validates them, while a normal user only gets the cache. udev runs the same probe when a device appears and stores the results, which is where lsblk -f and the /dev/disk/by-uuid and by-label symlinks come from. UUID is the filesystem's own ID (changes with mkfs); PARTUUID belongs to the GPT partition entry and survives reformatting. If no signature is found blkid prints nothing and exits with status 2.

Options you'll actually use

-s TAGShow only this tag: UUID, TYPE, LABEL, PARTUUID, BLOCK_SIZE
-o valuePrint only values (one per line)
-o exportPrint KEY=value lines you can eval or source in shell
-o full / listDefault NAME: KEY="value" format / a table
-L LABELPrint the device that has this label
-U UUIDPrint the device that has this UUID
-t NAME=valueList devices matching a tag, e.g. -t TYPE=xfs
-pLow-level probe, bypassing the cache (shows more detail)
-c /dev/nullIgnore the cache file

Common mistakes

Running blkid without sudo and trusting the result

As a normal user blkid only reads its cache, which can be stale or empty for new devices.

sudo blkid, or lsblk -f which reads udev's data
Using PARTUUID and UUID interchangeably

UUID belongs to the filesystem and changes when you run mkfs; PARTUUID belongs to the partition table entry and doesn't.

Use UUID= in fstab for filesystems; PARTUUID= mainly for kernel root= parameters
Cloning a disk or volume and mounting both copies on one host

Both have the same UUID: mount by UUID becomes ambiguous and XFS refuses the second mount.

Give the copy a new UUID: tune2fs -U random /dev/X (ext4) or xfs_admin -U generate /dev/X (xfs)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create an ext4 image file labelled 'lab' and print only its UUID.

  2. 2

    Show lab.img's tags as shell variables, then load them and echo the type.

  3. 3

    Check whether an empty file has any filesystem signature, using only the exit code.

Related commands

lsblkList disks, partitions and where they are mountedmkfsCreate a filesystem on a disk or partitionmountAttach a filesystem to the directory tree
PreviousfsckNextdd