Lsblk is a very nice utility installed by default on practically all Linux distributions: we can use it to retrieve a vast range of information about all the block devices attached to the system. In this article we will see how it works and how to use it.

In this tutorial you will learn:

  • How to use the lsblk utility to retrieve information about block devices
  • What is the meaning of the columns displayed in the default utility output
  • How to specify the columns to be displayed and format the output as json or as a list
  • How to display information about a specific device.
main

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software lsblk
Other No other requirements needed
Conventions # - requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux-commands to be executed as a regular non-privileged user

Lsblk basic usage

In its most basic usage, when invoked without any specific option or argument, the lsblk utility will produce a tree-like output including all the block devices attached to the operating system. Here is an example:

NAME                                          MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                                             8:0    0 232.9G  0 disk
├─sda1                                          8:1    0     1G  0 part  /boot
└─sda2                                          8:2    0 231.9G  0 part
  └─luks-5794a0b4-7082-4769-b86b-bd27a544361a 253:0    0 231.9G  0 crypt
    ├─fingolfin_vg-root_lv                    253:1    0    35G  0 lvm   /
    ├─fingolfin_vg-swap_lv                    253:2    0     6G  0 lvm   [SWAP]
    ├─fingolfin_vg-home_lv                    253:3    0    15G  0 lvm   /home
    └─fingolfin_vg-data_lv                    253:4    0   170G  0 lvm   /mnt/data
sr0                                            11:0    1  1024M  0 rom
zram0                                         252:0    0   2.8G  0 disk  [SWAP]

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


The tree-like structure is very useful to identify devices and their partitions, plus how they are structured on the device. In the output above, for example we can see that are two partitions on the sda device: sda1 and sda2.

As we can observe, the former is a “standard” partition: we can identify is as such because we can take a look at the corresponding value in the TYPE column, which in this case is part. We can also observe that the partition is currently mounted at /boot.

The latter, sda2, is also a standard partition, but as we can easily understand from the graph, it has some “children” or “slave” devices. The first one is a LUKS container identified by luks-5794a0b4-7082-4769-b86b-bd27a544361a (this is the device mapper name). Being the system installed on a lvm on luks setup, the luks container itself is marked as a physical volume, and contains some logical volumes mounted on various part of the system.

In the first column of the output we can see information about the device NAME are provided, that is quite easy to understand. Please notice that only the device name is displayed by default, and not its full path: for it to be displayed, instead, we should use the -p option.

The second column is named MAJ:MIN: those are the numbers used by the kernel to internally identify the devices, the first number specifying the device type (8 for example, is used for SCSI disks).

The third column displayed in the default lsblk output is RM: by looking at this column we can see if the device is removable (the value would be 1), or not. In the output above, only one device is marked as removable, sr0, which is an optical drive.

The scope of the fourth column is easily identified by its name: SIZE. In it the size of the corresponding devices is displayed.

The fifth column is RO: this column is used to specify if the device is read-only or not. Like the RM column, the values in the column are used as booleans, so 1 means the device is read-only.

The sixth column of the output is TYPE: as we already mentioned before, this column is used to identify the device or partition type. For example, observing the output of the command, we can see that the crypt value is used to identify the luks container while lvm is used to identify logical volume devices, and disk is used for raw block devices like sda.

The seventh and last column is MOUNTPOINT: this column provides information about the current mountpoint of each partition/block device.

Gathering information about a specific device

As we saw above, if we invoke the lsblk command without any other arguments or options we obtain information about the currently devices attached to the system. What if we want to obtain information about a specific device?

All we have to do is to pass the device we want to gather information about as the argument of the lsblk command. For example, if we only want to inspect the sda1 device, we would run:

$ lsblk /dev/sda1

Notice the we provided the full path of the device, and not only its name. The output produced from the command above, as one would expect, is the following:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda1   8:1    0   1G  0 part /boot

Specifying the columns to be displayed in the lsblk output

We already saw what are the columns included by default in the output of lsblk when it is invoked without any specific option. Those are, however, only a small subset of the available ones. To specify the information we want to be included in the output we must use the -o option (short for --output) and provide a comma-separated list of the columns we want to be included. For example, for the output to include only information about devices names and filesystem types, we could run:

$ lsblk -o NAME,FSTYPE


The command above would return the following output:

NAME                                          FSTYPE
sda
├─sda1                                        ext2
└─sda2                                        crypto_LUKS
  └─luks-5794a0b4-7082-4769-b86b-bd27a544361a LVM2_member
    ├─fingolfin_vg-root_lv                    ext4
    ├─fingolfin_vg-swap_lv                    swap
    ├─fingolfin_vg-home_lv                    ext4
    └─fingolfin_vg-data_lv                    ext4
sr0
zram0

For a complete list of the available columns and an explanation of the information they provide, we can invoke the lsblk utility with the --help option:

Available output columns:
        NAME  device name
       KNAME  internal kernel device name
        PATH  path to the device node
     MAJ:MIN  major:minor device number
     FSAVAIL  filesystem size available
      FSSIZE  filesystem size
      FSTYPE  filesystem type
      FSUSED  filesystem size used
      FSUSE%  filesystem use percentage
       FSVER  filesystem version
  MOUNTPOINT  where the device is mounted
       LABEL  filesystem LABEL
        UUID  filesystem UUID
      PTUUID  partition table identifier (usually UUID)
      PTTYPE  partition table type
    PARTTYPE  partition type code or UUID
 PARTTYPENAME  partition type name
   PARTLABEL  partition LABEL
    PARTUUID  partition UUID
   PARTFLAGS  partition flags
          RA  read-ahead of the device
          RO  read-only device
          RM  removable device
     HOTPLUG  removable or hotplug device (usb, pcmcia, ...)
       MODEL  device identifier
      SERIAL  disk serial number
        SIZE  size of the device
       STATE  state of the device
       OWNER  user name
       GROUP  group name
        MODE  device node permissions
   ALIGNMENT  alignment offset
      MIN-IO  minimum I/O size
      OPT-IO  optimal I/O size
     PHY-SEC  physical sector size
     LOG-SEC  logical sector size
        ROTA  rotational device
       SCHED  I/O scheduler name
     RQ-SIZE  request queue size
        TYPE  device type
    DISC-ALN  discard alignment offset
   DISC-GRAN  discard granularity
    DISC-MAX  discard max bytes
   DISC-ZERO  discard zeroes data
       WSAME  write same max bytes
         WWN  unique storage identifier
        RAND  adds randomness
      PKNAME  internal parent kernel device name
        HCTL  Host:Channel:Target:Lun for SCSI
        TRAN  device transport type
  SUBSYSTEMS  de-duplicated chain of subsystems
         REV  device revision
      VENDOR  device vendor
       ZONED  zone model
         DAX  dax-capable device

Some options can also be used, which comprehend a pre-defined set of columns: invoking lsblk with the -f (or --fs) option, for example, is like calling it by specifying the NAME,FSTYPE,LABEL,UUID,FSAVAIL,FSUSE%,MOUNTPOINT columns.

Modifying output format

In the previous examples we saw how the default output produced by the invocation of lsblk command, is a tree-like representation of the block devices attached to the system, and their child or slave devices. There are, however a bunch of options we can use to modify how the output is displayed.

First of all, we can use the -d option (short for --nodeps) to display only parent devices. Here is the result of invoking lsblk with said option:

NAME  MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda     8:0    0 232.9G  0 disk
sr0    11:0    1  1024M  0 rom
zram0 252:0    0   2.8G  0 disk [SWAP]

Another, very interesting option, is -J, or --json: with it we can obtain information about the block devices and their relationships, in the json format:

$ lsblk -J
{
   "blockdevices": [
      {"name":"sda", "maj:min":"8:0", "rm":false, "size":"232.9G", "ro":false, "type":"disk", "mountpoint":null,
         "children": [
            {"name":"sda1", "maj:min":"8:1", "rm":false, "size":"1G", "ro":false, "type":"part", "mountpoint":"/boot"},
            {"name":"sda2", "maj:min":"8:2", "rm":false, "size":"231.9G", "ro":false, "type":"part", "mountpoint":null,
               "children": [
                  {"name":"luks-5794a0b4-7082-4769-b86b-bd27a544361a", "maj:min":"253:0", "rm":false, "size":"231.9G", "ro":false, "type":"crypt", "mountpoint":null,
                     "children": [
                        {"name":"fingolfin_vg-root_lv", "maj:min":"253:1", "rm":false, "size":"35G", "ro":false, "type":"lvm", "mountpoint":"/"},
                        {"name":"fingolfin_vg-swap_lv", "maj:min":"253:2", "rm":false, "size":"6G", "ro":false, "type":"lvm", "mountpoint":"[SWAP]"},
                        {"name":"fingolfin_vg-home_lv", "maj:min":"253:3", "rm":false, "size":"15G", "ro":false, "type":"lvm", "mountpoint":"/home"},
                        {"name":"fingolfin_vg-data_lv", "maj:min":"253:4", "rm":false, "size":"170G", "ro":false, "type":"lvm", "mountpoint":"/mnt/data"}
                     ]
                  }
               ]
            }
         ]
      },
      {"name":"sr0", "maj:min":"11:0", "rm":true, "size":"1024M", "ro":false, "type":"rom", "mountpoint":null},
      {"name":"zram0", "maj:min":"252:0", "rm":false, "size":"2.8G", "ro":false, "type":"disk", "mountpoint":"[SWAP]"}
   ]
}


This type of output is very useful, among the other things, since it can easily be parsed with more complete programming languages like Python.

Another way to change the displayed output is to use the -l or --list option, which produces an output in the form of a list. When this output is selected, however, the relationships between devices are omitted, since a list, by its nature is “flat”:

$ lsblk -l
NAME                                      MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                                         8:0    0 232.9G  0 disk
sda1                                        8:1    0     1G  0 part  /boot
sda2                                        8:2    0 231.9G  0 part
sr0                                        11:0    1  1024M  0 rom
zram0                                     252:0    0   2.8G  0 disk  [SWAP]
luks-5794a0b4-7082-4769-b86b-bd27a544361a 253:0    0 231.9G  0 crypt
fingolfin_vg-root_lv                      253:1    0    35G  0 lvm   /
fingolfin_vg-swap_lv                      253:2    0     6G  0 lvm   [SWAP]
fingolfin_vg-home_lv                      253:3    0    15G  0 lvm   /home
fingolfin_vg-data_lv                      253:4    0   170G  0 lvm   /mnt/data

To organize the output in “pairs”, instead, we can use the -P or --pairs option:

$ lsblk --pairs
NAME="sda" MAJ:MIN="8:0" RM="0" SIZE="232.9G" RO="0" TYPE="disk" MOUNTPOINT=""
NAME="sda2" MAJ:MIN="8:2" RM="0" SIZE="231.9G" RO="0" TYPE="part" MOUNTPOINT=""
NAME="luks-5794a0b4-7082-4769-b86b-bd27a544361a" MAJ:MIN="253:0" RM="0" SIZE="231.9G" RO="0" TYPE="crypt" MOUNTPOINT=""
NAME="fingolfin_vg-root_lv" MAJ:MIN="253:1" RM="0" SIZE="35G" RO="0" TYPE="lvm" MOUNTPOINT="/"
NAME="fingolfin_vg-data_lv" MAJ:MIN="253:4" RM="0" SIZE="170G" RO="0" TYPE="lvm" MOUNTPOINT="/mnt/data"
NAME="fingolfin_vg-swap_lv" MAJ:MIN="253:2" RM="0" SIZE="6G" RO="0" TYPE="lvm" MOUNTPOINT="[SWAP]"
NAME="fingolfin_vg-home_lv" MAJ:MIN="253:3" RM="0" SIZE="15G" RO="0" TYPE="lvm" MOUNTPOINT="/home"
NAME="sda1" MAJ:MIN="8:1" RM="0" SIZE="1G" RO="0" TYPE="part" MOUNTPOINT="/boot"
NAME="sr0" MAJ:MIN="11:0" RM="1" SIZE="1024M" RO="0" TYPE="rom" MOUNTPOINT=""
NAME="zram0" MAJ:MIN="252:0" RM="0" SIZE="2.8G" RO="0" TYPE="disk" MOUNTPOINT="[SWAP]"

Another very important option that is useful to use especially in scripts, is -n, which is the short for --no-headings. This option modifies the output so that the header line, and the name of the columns are omitted. A concrete use case would be the following. Suppose that in a script we want to get the filesystem type of a specific partition (let’s suppose is sda1), and “store it” in a variable, we could write:

$ fsys="$(lsblk --noheadings -o FSTYPE /dev/sda1)"

Conclusions

In this tutorial we learned to know the lsblk command and how it can be used to retrieve information about block devices attached to the system and their relationships. We learned the meaning of the default columns displayed when the utility is called without any option or arguments, we saw how to get information about a specific device, and the various options we can use to

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions