The lsblk Linux command is a useful command which lists information about all or the specified block devices. It queries the /sys virtual file system to obtain the information that it displays. The command displays details about all block devices excluding except RAM disks in a tree-like format by default.
lsblk is installed as part of the util-Linux package. The “util-linux” package contains a large variety of low-level system utilities that are necessary for a Linux system to function. Among others, util-linux contains the fdisk configuration tool and the login program.
Verify the lsblk command in CentOS/Redhat
[root@linuxnix ~]# which lsblk /usr/bin/lsblk [root@linuxnix ~]# rpm -qf /usr/bin/lsblk util-linux-2.23.2-33.el7.x86_64 [root@linuxnix ~]# rpm -ql util-linux | grep -w lsblk /usr/bin/lsblk /usr/share/bash-completion/completions/lsblk /usr/share/man/man8/lsblk.8.gz
Verify the lsblk command in Ubuntu/Debian Linux
sahil@linuxnix:~$ ls -l /bin/lsblk -rwxr-xr-x 1 root root 77280 May 27 2016 /bin/lsblk sahil@linuxnix:~$ dpkg -S /bin/lsblk util-linux: /bin/lsblk sahil@linuxnix:~$ dpkg-query -L util-linux | grep lsblk /bin/lsblk /usr/share/bash-completion/completions/lsblk /usr/share/man/man8/lsblk.8.gz sahil@linuxnix:~$
Example 1: Let’s run the lsblk command without any additional options and understand the output:
[sahil@linuxnix:~] $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 208M 0 loop loop1 7:1 0 456M 0 loop /mnt/rh sr0 11:0 1 1024M 0 rom sda 8:0 0 60G 0 disk |-sda1 8:1 0 512M 0 part /boot `-sda2 8:2 0 59.5G 0 part |-os_vg-root_lv (dm-0) 253:0 0 16G 0 lvm / |-os_vg-swap_01_lv (dm-1) 253:1 0 4G 0 lvm [SWAP] |-os_vg-tmp_lv (dm-4) 253:4 0 2G 0 lvm /tmp |-os_vg-var_lv (dm-5) 253:5 0 10G 0 lvm /var |-os_vg-hpds_lv (dm-6) 253:6 0 6G 0 lvm /var/opt/perf/datafiles `-os_vg-kickstart_lv (dm-7) 253:7 0 4G 0 lvm /kickstart sdd 8:48 0 2G 0 disk sdb 8:16 0 32G 0 disk `-xfer_vg-lvol1 (dm-3) 253:3 0 32G 0 lvm /xfer sdc 8:32 0 24G 0 disk `-kdump_vg-kdump_lv (dm-2) 253:2 0 24G 0 lvm sde 8:64 0 3G 0 disk
Given below is a description of the fields reported in the lsblk output when used without any options:
NAME: The name of the device.
MAJ: MIN: The major and minor number of the device.
RM: This prints 1 if the device is removable (Eg. Cdrom, USB) or 0 if the device is not removable (Eg. Physical volume).
SIZE: The size of the device in human-readable format.
RO: This field has a value of 1 if the device is in read-only mode and 0 otherwise.
TYPE: Displays whether the device is a loopback device, disk, partition or under LVM control.
MOUNTPOINT: The name of the file system on which the device is mounted.
Explanation of each device in lsblk command output
- The devices loop0 and loop1 are loopback devices. The output shows that loop1 is mounted on /mnt/rh. This is an iso file made accessible through the /mnt/rh directory.
df -hTP /mnt/rh Filesystem Type Size Used Avail Use% Mounted on /xfer/isos/rhel-server-7.4-x86_64-boot.iso iso9660 456M 456M 0 100% /mnt/rh
- Next, we have the sr0 device which denotes the cdrom. The RM field for this device is 1 indicating that it’s removable.
- Then we see sda which is a disk device followed by sda1 and sda2 which are partitions.
- sda1 is mounted as /boot file system, and sda2 partition is under LVM control and resulting from this we can view the slave or child logical volumes are using this physical volume in a tree-like format.
- Each of the logical volumes being displayed in the output has a file system mounted on them mentioned in the mount point field.
Example 2: If we do not want to view the output in a tree-like format, then we can use the -l option to display the output in a list format:
[sahil@linuxnix:~] $ lsblk -l NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 208M 0 loop loop1 7:1 0 456M 0 loop /mnt/rh sr0 11:0 1 1024M 0 rom sda 8:0 0 60G 0 disk sda1 8:1 0 512M 0 part /boot sda2 8:2 0 59.5G 0 part os_vg-root_lv (dm-0) 253:0 0 16G 0 lvm / os_vg-swap_01_lv (dm-1) 253:1 0 4G 0 lvm [SWAP] os_vg-tmp_lv (dm-4) 253:4 0 2G 0 lvm /tmp os_vg-var_lv (dm-5) 253:5 0 10G 0 lvm /var os_vg-hpds_lv (dm-6) 253:6 0 6G 0 lvm /var/opt/perf/datafiles os_vg-kickstart_lv (dm-7) 253:7 0 4G 0 lvm /kickstart sdd 8:48 0 2G 0 disk sdb 8:16 0 32G 0 disk xfer_vg-lvol1 (dm-3) 253:3 0 32G 0 lvm /xfer sdc 8:32 0 24G 0 disk kdump_vg-kdump_lv (dm-2) 253:2 0 24G 0 lvm sde 8:64 0 3G 0 disk
Example 3: We cause the -o flag to filter out only those fields for which we’d like the output to be reported. For example, we could type the following command to report just the NAME, SIZE and MOUNPOINT attributes for the block devices.
[sahil@linuxnix:~] $ lsblk -o NAME,SIZE,MOUNTPOINT NAME SIZE MOUNTPOINT loop0 208M loop1 456M /mnt/rh sr0 1024M sda 60G |-sda1 512M /boot `-sda2 59.5G |-os_vg-root_lv (dm-0) 16G / |-os_vg-swap_01_lv (dm-1) 4G [SWAP] |-os_vg-tmp_lv (dm-4) 2G /tmp |-os_vg-var_lv (dm-5) 10G /var |-os_vg-hpds_lv (dm-6) 6G /var/opt/perf/datafiles `-os_vg-kickstart_lv (dm-7) 4G /kickstart sdd 2G sdb 32G `-xfer_vg-lvol1 (dm-3) 32G /xfer sdc 24G `-kdump_vg-kdump_lv (dm-2) 24G sde 3G
Example 4: We can use the -n option to omit the printing of header lines and -d option to print top-level devices only.
[sahil@linuxnix:~] $ lsblk -dn loop0 7:0 0 208M 0 loop loop1 7:1 0 456M 0 loop /mnt/rh sr0 11:0 1 1024M 0 rom sda 8:0 0 60G 0 disk sdd 8:48 0 2G 0 disk sdb 8:16 0 32G 0 disk sdc 8:32 0 24G 0 disk sde 8:64 0 3G 0 disk
Example 5: Using this can be useful in scripts. I’ll demonstrate this by writing a quick one-liner using awk which will print the total storage of the physical volumes attached to the server.
[sahil@linuxnix:~] $ lsblk -dn -o NAME,TYPE,SIZE | awk '/disk/ {sum+=$NF} END {print sum, "GB"}' 121 GB
I’ll just do a quick breakdown of the awk section.
- /disk/ is a search term for the word disk. It’ll filter out all disk devices from the input. We can use regular expressions as well.
- The AWK built-in variable $NF represents the last column in the input (space separated fields) and by using += we iterate over this column and increment the value of the variable sum with the value of $NF in the next row. This continues until the last row is encountered.
- The END block ensures that what follows is executed only once at the end of the awk statement.
- Following the END block, we have a print statement which prints the value of sum obtained after adding the value of $NF over all rows in the input.
- Since we are performing an arithmetic operation on the given field, awk will discard the ‘G’ appended at the end of the SIZE filed in the lsblk output.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021