command to check the block devices attach in linux machine

 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.

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]

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

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]"}
   ]
}


Comments

Popular posts from this blog

Essential Linux Commands That Every User Should Know

Command for services in linux machine