LinuxCommandLibrary

blkid

Identify block device attributes

TLDR

List all partitions

$ sudo blkid
copy

List all partitions in a table, including current mountpoints
$ sudo blkid [[-o|--output]] list
copy

Get the UUID of the filesystem on a partition
$ sudo blkid [[-s|--match-tag]] UUID [[-o|--output]] value [/dev/sdXY]
copy

SYNOPSIS

blkid [options] [device...]
blkid -L <label>
blkid -U <uuid>
blkid -p <device>

PARAMETERS

-c
    Specify an alternative cache file to use. The default is usually /etc/blkid.tab or /run/blkid/blkid.tab.

-D
    Show all devices (default is to skip devices that don't have filesystems or partitions).

-g
    Perform a garbage collection on the libblkid cache. This removes entries for non-existent devices.

-h
    Display a help message and exit.

-k
    List all known filesystem types and their internal signatures that libblkid recognizes.

-L
    Look up a device by its filesystem label. This is a common way to find a device without knowing its UUID or path.

-l
    List only devices that are labeled or have a UUID.

-M
    Display information about supported metadata types. Useful for debugging libblkid.

-o
    Specify the output format. Common formats include 'full' (default), 'value' (only attribute values), 'device' (only device names), 'export' (shell export format), or 'udev'.

-p
    Probe a specific device directly, ignoring the cache. This ensures the most current information but can be slower.

-s
    Show only the specified tag (e.g., 'UUID', 'LABEL', 'TYPE'). Can be specified multiple times.

-t
    Search for devices with a specific tag and value pair (e.g., 'TYPE=ext4').

-U
    Look up a device by its filesystem UUID. This is the most reliable way to find a device.

-V
    Display version information and exit.

-w
    Write the current libblkid cache to the specified file.

-x
    Exclude the specified tag from the output. Can be specified multiple times.

-z
    Don't use the cache and don't write to the cache. Similar to -p for all devices, but does not update the cache.

DESCRIPTION

The blkid command is a utility used to identify and print the attributes (such as UUID, LABEL, TYPE, PARTUUID, PARTLABEL, etc.) of block devices. It is primarily used for scripting, configuration, and debugging tasks related to filesystems and partitions. blkid can retrieve information from the libblkid cache (which is faster) or by directly probing the block devices (which ensures up-to-date information but can be slower).

The unique identifiers provided by blkid, especially UUIDs (Universally Unique Identifiers) and PARTUUIDs (Partition UUIDs), are crucial for persistent device naming. This is vital in contexts like /etc/fstab, where devices need to be referenced reliably regardless of their dynamic kernel-assigned names (e.g., /dev/sda1, /dev/nvme0n1p1) which can change between reboots or hotplugging events. By using these identifiers, system configurations remain robust and consistent, ensuring that the correct filesystems are mounted every time.

CAVEATS

Root privileges are often required to probe block devices directly, especially for devices that are not publicly accessible or for updating the libblkid cache. If blkid is run without root privileges, it might only be able to display information from the cached data, which could be stale. Direct probing (`-p` option) or ignoring the cache (`-z` option) can ensure accuracy but may take longer.

CACHE MANAGEMENT

blkid primarily relies on a cache file (usually /etc/blkid.tab or /run/blkid/blkid.tab) to quickly retrieve device information. This cache is built by probing devices at boot or when the system detects changes. While faster, it can become outdated if devices are added/removed without proper system updates. The -p (probe) and -z (no cache) options are available to bypass or ignore this cache, ensuring the most current device attributes are reported.

PERSISTENT NAMING

One of blkid's most significant contributions is facilitating 'persistent naming' of block devices. Instead of relying on volatile device names like /dev/sda1 (which can change if hardware is reordered), UUIDs and labels provide stable, unique identifiers. This is vital for configuring /etc/fstab, which ensures that the correct filesystems are always mounted to their designated mount points, regardless of how the kernel enumerates the devices.

HISTORY

blkid is part of the util-linux project, a collection of essential utilities for Linux. It emerged as a more robust and programmatic way to identify block devices compared to earlier, less reliable methods that often depended on device path (e.g., /dev/sda1). Its development was crucial for systems that use UUIDs and labels for persistent device naming, becoming a cornerstone for modern Linux filesystem management and ensuring system stability across reboots and hardware changes. It provides the underlying functionality for utilities like mount and system configurations like /etc/fstab to reliably locate filesystems.

SEE ALSO

findfs(8), lsblk(8), mount(8), fstab(5), partprobe(8)

Copied to clipboard