Why `lsblk` does not detect UUID but `blkid` does?
Image by Tassie - hkhazo.biz.id

Why `lsblk` does not detect UUID but `blkid` does?

Posted on

If you’re a Linux enthusiast, you’ve probably stumbled upon the `lsblk` and `blkid` commands while trying to identify and manage your system’s disk devices. Both commands are designed to provide information about block devices, but they differ in their approach and output. In this article, we’ll delve into the reasons behind why `lsblk` might not detect UUIDs (Universally Unique Identifiers) while `blkid` does.

What are UUIDs?

Before we dive into the main topic, let’s quickly cover what UUIDs are. A UUID is a unique identifier assigned to a device, such as a hard drive or solid-state drive, to identify it uniquely in a system. UUIDs are often used in Linux to identify devices in configuration files, like `/etc/fstab`, and to mount devices automatically during boot.

Why are UUIDs important?

UUIDs are crucial in ensuring that devices are correctly identified and mounted, regardless of their physical connection order or device naming. Without UUIDs, Linux systems would rely on device names, such as `/dev/sda1` or `/dev/nvme0n1p1`, which can change between reboots or when devices are added or removed. UUIDs provide a stable and persistent way to identify devices.

The `lsblk` command

The `lsblk` command is part of the `util-linux` package and is used to list information about block devices. By default, `lsblk` displays a tree-like view of devices, including their names, sizes, and mount points.

$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 931.5G  0 disk
sda1        8:1    0 931.5G  0 part /
nvme0n1     259:0    0 953.9G  0 disk
nvme0n1p1 259:1    0 953.9G  0 part /home

As you can see, the output doesn’t include UUIDs. So, why doesn’t `lsblk` display UUIDs?

Why `lsblk` doesn’t display UUIDs

The reason `lsblk` doesn’t display UUIDs is due to its design and purpose. `lsblk` is primarily used to provide a quick overview of the block device hierarchy, making it easier to identify devices and their relationships. UUIDs are not essential for this purpose, and including them would clutter the output.

Moreover, `lsblk` relies on the Linux kernel’s sysfs interface to gather information about devices. Sysfs provides a wealth of information about devices, but UUIDs are not part of that information.

The `blkid` command

The `blkid` command is part of the `util-linux` package, just like `lsblk`. However, `blkid` is specifically designed to provide information about block devices, including their UUIDs.

$ blkid
/dev/sda1: LABEL="root" UUID="12345678-1234-1234-1234-1234567890ab" TYPE="ext4" PARTUUID="12345678-01"
/dev/nvme0n1p1: LABEL="home" UUID="98765432-4321-4321-4321-9876543210cd" TYPE="ext4" PARTUUID="98765432-01"

As you can see, `blkid` displays the UUIDs for each device, making it easy to identify devices based on their unique identifiers.

Why `blkid` displays UUIDs

`blkid` is designed to provide a more comprehensive view of block devices, including their UUIDs, labels, and file system types. This information is essential for configuring devices in `/etc/fstab` and other system files.

`blkid` uses the `libblkid` library, which provides a more extensive set of device information, including UUIDs. This library is specifically designed to provide a detailed view of block devices, making it an ideal choice for `blkid`.

Differences between `lsblk` and `blkid`

Now that we’ve explored the reasons behind why `lsblk` and `blkid` behave differently, let’s summarize the main differences between these two commands:

Command Purpose Output UUID Support
`lsblk` Display block device hierarchy Tree-like view of devices No
`blkid` Display detailed information about block devices Comprehensive view of devices, including UUIDs Yes

Conclusion

In conclusion, `lsblk` and `blkid` are two distinct commands that serve different purposes. While `lsblk` provides a quick overview of the block device hierarchy, `blkid` offers a more comprehensive view of block devices, including their UUIDs. Understanding the strengths and weaknesses of each command will help you make the most of your Linux system.

If you need to identify devices based on their UUIDs, `blkid` is the clear choice. However, if you’re looking for a quick and easy way to visualize your device hierarchy, `lsblk` is the way to go.

By mastering these two commands, you’ll be better equipped to manage your Linux system’s devices and ensure that your system boots and runs smoothly.

Additional Resources

If you’re interested in learning more about `lsblk` and `blkid`, here are some additional resources:

FAQs

Here are some frequently asked questions related to `lsblk` and `blkid`:

  1. Q: Why does `lsblk` not display UUIDs?

    A: `lsblk` doesn’t display UUIDs because it’s designed to provide a quick overview of the block device hierarchy, and UUIDs are not essential for that purpose.

  2. Q: Can I use `lsblk` to identify devices based on their UUIDs?

    A: No, `lsblk` does not display UUIDs. Use `blkid` instead.

  3. Q: What is the purpose of `blkid`?

    A: `blkid` is designed to provide a comprehensive view of block devices, including their UUIDs, labels, and file system types.

I hope this article has helped you understand the differences between `lsblk` and `blkid` and why `lsblk` doesn’t detect UUIDs but `blkid` does. If you have any further questions or need clarification, please don’t hesitate to ask.

Frequently Asked Question

Get ready to dive into the world of disk detection and uncover the mysteries behind `lsblk` and `blkid`!

Why does `lsblk` not detect UUID but `blkid` does?

`lsblk` and `blkid` are both utilities for probing and listing block devices, but they use different approaches to detect UUIDs. `lsblk` relies on the `libblkid` library, which caches disk metadata. If the cache is outdated or incomplete, `lsblk` might not detect the UUID. On the other hand, `blkid` bypasses the cache and directly queries the disk, ensuring a more accurate and up-to-date result.

What happens if I run `lsblk` with the `-d` option?

When you run `lsblk` with the `-d` option, it forces a re-scan of the disk metadata, bypassing the cache. This can help `lsblk` detect the UUID, making it more consistent with the result from `blkid`. However, keep in mind that this might take longer and affect system performance.

Can I use `blkid` to list all disk devices, just like `lsblk`?

While `blkid` is primarily designed to extract metadata from a specific device, you can use the `-o` option to list all devices. For example, `blkid -o device,label,uuid` will display a list of devices along with their labels and UUIDs. However, the output format might differ from `lsblk`, so be prepared for a slightly different viewing experience!

Are there any situations where `lsblk` would detect a UUID but `blkid` wouldn’t?

Yes, it’s possible. If the UUID is stored in a location that `blkid` can’t access (e.g., due to permissions or a faulty device), `lsblk` might still detect it through the cache or other means. However, this is relatively rare and usually indicates an underlying system issue that needs attention.

Which command should I use for scripting and automation?

When it comes to scripting and automation, `blkid` is generally a safer choice. Its output is more predictable and reliable, making it easier to parse and process. `lsblk` can still be used, but be prepared to handle potential cache-related issues and inconsistencies.