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 / lsblk

lsblk

List disks, partitions and where they are mounted

Beginnerdiskpartitionscloud

lsblk prints every block device as a tree: disks, their partitions, LVM volumes and loop devices, with size and mount point. It is how you find a volume you just attached, check whether it has a filesystem, and see why df didn't grow after you resized a disk.

Syntaxlsblk [OPTION]... [DEVICE]...

Watch it run

You've logged in to an EC2 instance and want to see its disks, partitions and mount points.

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

The tree shows disks and the partitions on them. Loop devices are read-only snap packages. nvme0n1 is the 30G root volume (Ubuntu puts BIOS boot, EFI and /boot on partitions 14-16); nvme1n1 is a second EBS volume with a filesystem directly on it, no partitions.

Anatomy of a command

$ ,
commandlsblk

List block devices from sysfs as a tree. No root needed.

How it works

lsblk doesn't open the disks. It reads /sys/block and /sys/dev/block: every block device the kernel knows has a directory there with its size (in 512-byte sectors), major:minor numbers, removable and read-only flags, and holders/slaves links that let lsblk draw partitions, LVM and RAID as a tree. Filesystem type, label and UUID come from the udev database in /run/udev/data, which udev fills by probing each device with libblkid when it appears. Mount points come from /proc/self/mountinfo. On AWS Nitro instances every EBS volume is an NVMe device (nvme0n1, nvme1n1...), whatever name you chose in the console (/dev/sdf); the volume ID is in the SERIAL column. A partition's size is fixed in the partition table, so after you grow a cloud disk lsblk shows a bigger disk but the same partition until you run growpart.

Options you'll actually use

-fFilesystem view: FSTYPE, FSVER, LABEL, UUID, FSAVAIL, FSUSE%, MOUNTPOINTS
-o COLSChoose columns: NAME,SIZE,TYPE,FSTYPE,UUID,MOUNTPOINTS,MODEL,SERIAL...
-dDisks only, no partitions
-e 7Exclude a major number; 7 = loop devices
-pPrint full paths (/dev/nvme1n1) instead of bare names
-bSizes in bytes (for scripts)
-JJSON output, e.g. for jq
-nNo header line
-r / -lRaw or list output without tree characters, easier to parse
-SShow SCSI devices (sd*) with vendor, model and transport

Common mistakes

Formatting /dev/nvme1n1 because the console said 'attached as /dev/sdf'

On Nitro instances NVMe names follow discovery order, not the console name. You might format the wrong volume.

lsblk -d -o NAME,SIZE,SERIAL and match the serial to the volume ID
Running resize2fs right after growing the EBS volume

The partition still has its old size, so resize2fs finds nothing to grow ('The filesystem is already ... blocks long. Nothing to do!').

sudo growpart /dev/nvme0n1 1 && sudo resize2fs /dev/nvme0n1p1
Parsing the default lsblk tree output with awk in a script

Tree characters and empty columns shift fields around.

lsblk -rno NAME,SIZE,MOUNTPOINT or lsblk -J | jq

Interview questions

Practice

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

  1. 1

    Write the commands that list all block devices on a server, then list them again without the loop devices. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  2. 2

    Write the command that shows each device's filesystem type, label and UUID. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  3. 3

    Write the command that prints only disk names and sizes in bytes, with no header and no partitions. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  4. 4

    Write the commands to grow partition 1 of /dev/nvme0n1 and its ext4 filesystem after the cloud volume was enlarged (the sandbox disk can't be resized).

Related commands

blkidShow filesystem type, UUID and label of devicesfdiskView and edit a disk's partition tablemkfsCreate a filesystem on a disk or partitionmountAttach a filesystem to the directory treedfReport used and free space per filesystem
PreviousduNextfdisk
Linux / Disk & Storage / lsblk

lsblk

List disks, partitions and where they are mounted

Beginnerdiskpartitionscloud

lsblk prints every block device as a tree: disks, their partitions, LVM volumes and loop devices, with size and mount point. It is how you find a volume you just attached, check whether it has a filesystem, and see why df didn't grow after you resized a disk.

Syntaxlsblk [OPTION]... [DEVICE]...

Watch it run

You've logged in to an EC2 instance and want to see its disks, partitions and mount points.

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

The tree shows disks and the partitions on them. Loop devices are read-only snap packages. nvme0n1 is the 30G root volume (Ubuntu puts BIOS boot, EFI and /boot on partitions 14-16); nvme1n1 is a second EBS volume with a filesystem directly on it, no partitions.

Anatomy of a command

$ ,
commandlsblk

List block devices from sysfs as a tree. No root needed.

How it works

lsblk doesn't open the disks. It reads /sys/block and /sys/dev/block: every block device the kernel knows has a directory there with its size (in 512-byte sectors), major:minor numbers, removable and read-only flags, and holders/slaves links that let lsblk draw partitions, LVM and RAID as a tree. Filesystem type, label and UUID come from the udev database in /run/udev/data, which udev fills by probing each device with libblkid when it appears. Mount points come from /proc/self/mountinfo. On AWS Nitro instances every EBS volume is an NVMe device (nvme0n1, nvme1n1...), whatever name you chose in the console (/dev/sdf); the volume ID is in the SERIAL column. A partition's size is fixed in the partition table, so after you grow a cloud disk lsblk shows a bigger disk but the same partition until you run growpart.

Options you'll actually use

-fFilesystem view: FSTYPE, FSVER, LABEL, UUID, FSAVAIL, FSUSE%, MOUNTPOINTS
-o COLSChoose columns: NAME,SIZE,TYPE,FSTYPE,UUID,MOUNTPOINTS,MODEL,SERIAL...
-dDisks only, no partitions
-e 7Exclude a major number; 7 = loop devices
-pPrint full paths (/dev/nvme1n1) instead of bare names
-bSizes in bytes (for scripts)
-JJSON output, e.g. for jq
-nNo header line
-r / -lRaw or list output without tree characters, easier to parse
-SShow SCSI devices (sd*) with vendor, model and transport

Common mistakes

Formatting /dev/nvme1n1 because the console said 'attached as /dev/sdf'

On Nitro instances NVMe names follow discovery order, not the console name. You might format the wrong volume.

lsblk -d -o NAME,SIZE,SERIAL and match the serial to the volume ID
Running resize2fs right after growing the EBS volume

The partition still has its old size, so resize2fs finds nothing to grow ('The filesystem is already ... blocks long. Nothing to do!').

sudo growpart /dev/nvme0n1 1 && sudo resize2fs /dev/nvme0n1p1
Parsing the default lsblk tree output with awk in a script

Tree characters and empty columns shift fields around.

lsblk -rno NAME,SIZE,MOUNTPOINT or lsblk -J | jq

Interview questions

Practice

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

  1. 1

    Write the commands that list all block devices on a server, then list them again without the loop devices. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  2. 2

    Write the command that shows each device's filesystem type, label and UUID. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  3. 3

    Write the command that prints only disk names and sizes in bytes, with no header and no partitions. (The practice sandbox has no block devices, so lsblk prints nothing there: write the commands.)

  4. 4

    Write the commands to grow partition 1 of /dev/nvme0n1 and its ext4 filesystem after the cloud volume was enlarged (the sandbox disk can't be resized).

Related commands

blkidShow filesystem type, UUID and label of devicesfdiskView and edit a disk's partition tablemkfsCreate a filesystem on a disk or partitionmountAttach a filesystem to the directory treedfReport used and free space per filesystem
PreviousduNextfdisk