Listing devices with lspci and lsblk

Video 049 of 140 in Virtualization Foundations, in the unit “Storage devices, buses and network cards”. 3:29, with English and Hindi audio.

When you log in to a server, one early question is what hardware it has. Two Linux commands answer most of it. lspci lists the devices on the PCI bus, and lsblk lists block devices, such as disks. Used together, they show the hardware and the storage it holds. This article shows how to run both, how to read their output, and why a disk name alone is not a safe way to pick a disk.

What you will learn

Two lists that answer two questions

The lspci command lists devices on the PCI bus, the internal road where network cards and storage controllers plug in.

The lsblk command lists block devices. A block device is anything that stores data in fixed-size blocks, such as a disk.

Use the two together. lspci tells you what hardware is there, and lsblk tells you which disks it has and how they are split up.

Filter lspci for network and storage cards

A full lspci list is long. The filter below keeps only the lines for network and storage controllers. Each line starts with a bus address, then the device class, then the make and model.

List the network and storage controllers
lspci | grep -i -E 'ethernet|non-volatile|sata|serial attached|raid'

To see which driver runs a network card, add the k option. The line Kernel driver in use names the driver that runs the card.

Show the driver that runs each network card
lspci -k | grep -A 3 -i ethernet

List disks, partitions and filesystem IDs

Plain lsblk lists each disk, with its partitions indented under it.

List disks and their partitions
lsblk

Add the f option to see the filesystem type on each partition, along with its UUID. A UUID is a long ID that is written into the filesystem itself.

Show filesystem types and UUIDs
lsblk -f

Disk names can change, so check before you act

The kernel hands out names such as sda in the order it finds the disks. A name can shift when the hardware changes, so a script that trusts the name may hit the wrong disk.

A UUID stays with the filesystem, wherever the disk is plugged in. Use it to pick a disk, and confirm the size, model and mount point before you format or resize anything. The mount point is the folder where the disk's files appear.

Note: A wrong guess can wipe the wrong data. Formatting or resizing the wrong disk is hard to undo.

Try it yourself

You need a Linux machine. You will see the network and storage hardware, then the disks and the IDs of their filesystems. These commands only list things, so they do not change any disk.

  1. Filter the PCI device list for network and storage controllers.

    lspci | grep -i -E 'ethernet|non-volatile|sata|serial attached|raid'

    What you should see: Each line shows a bus address, the device class, then the make and model.

  2. Find the driver that runs the network card.

    lspci -k | grep -A 3 -i ethernet

    What you should see: The line Kernel driver in use names the driver running the card.

  3. List the disks and their partitions.

    lsblk

    What you should see: Each disk is listed, with its partitions indented under it.

  4. Add the filesystem type and UUID, and note the ID you would use to pick a disk.

    lsblk -f

    What you should see: The -f option adds the filesystem type and the UUID of each filesystem.

Terms

PCI bus
The internal road in a computer where cards such as network cards and storage controllers plug in.
Block device
Anything that stores data in fixed-size blocks, such as a disk.
Partition
A part of a disk that is set aside as its own section.
UUID
A long ID written into a filesystem, which stays the same when the disk moves to another port or slot.
Mount point
The folder where the files of a disk or partition appear.

Check yourself

Which command lists the disks and their partitions?

lsblk. Add the f option to also see the filesystem types and UUIDs.

Why is a UUID safer than a disk name?

A name such as sda can change when the hardware changes, but a UUID stays with the filesystem.

What should you check before you resize a disk?

Its size, its model and its mount point, so you know it is the right disk.

Previous: PCIe: lanes, generations and slots. Next: How a device talks to the CPU: DMA and interrupts.