Easy listing block devices using lsblk

ComputerCarriage > Posts > Commands > Easy listing block devices using lsblk
Easy listing block devices using lsblk

In this session we are going to see List block devices using lsblk command

lsblk command list all available device or a block devices. Also read the sysfs filesystem and udev db togather information.
Expect RAM disks prints in tree format. lsblk –help will show list of options that can be used.

by default the NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT columns will be listed. we can also customize the menus. The lsblk command will be very helpful while we adding / removing the disk, to check mounted filesystem of the disks.

1. Listing block devices

Just by typing lsblk command will list the devices that are attached to the server

[root@control ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 31.7G 0 disk
|-sda1 8:1 0 1G 0 part /boot
-sda2 8:2 0 30.7G 0 part |-cl-root 253:0 0 28.6G 0 lvm / -cl-swap 253:1 0 2.2G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
[root@control ~]#

2. Displaying by columns

The –fs option will display with the filesystem that are used

[root@control ~]# lsblk --fs
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
|-sda1 ext4 b2032a80-d579-45d8-bbf8-173736ba334b /boot
-sda2 LVM2_member B4EvZY-cssx-HcFm-osI4-tXKQ-Qvhk-aXfMSW |-cl-root xfs 8cd5b054-12d6-4593-aa00-e6ae1958fa4a / -cl-swap swap cfe666a1-6886-471f-87ff-5099d576dfb2 [SWAP]
sr0
[root@control ~]#

–fs option will display the FSTYPE, LABEL, UUID and mount point

3. Customizing the lsblk output

We can display the custom column using –output option, lsblk –help command will display the options that can be used, the below example will display NAME,FSTYPE,SIZE,MOUNTPOINT, according the requriement add the column.

[root@control ~]# lsblk --output NAME,FSTYPE,SIZE,MOUNTPOINT
NAME FSTYPE SIZE MOUNTPOINT
sda 31.7G
|-sda1 ext4 1G /boot
-sda2 LVM2_member 30.7G |-cl-root xfs 28.6G / -cl-swap swap 2.2G [SWAP]
sr0 1024M
[root@control ~]#

4. Listing empty devices

The option -a will display non-empty devices, i.e., the disk that are currently being used, this will help us to find out when we want to remove the correct disk

root@control ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 31.7G 0 disk
|-sda1 8:1 0 1G 0 part /boot
`-sda2 8:2 0 30.7G 0 part
|-cl-root 253:0 0 28.6G 0 lvm /
`-cl-swap 253:1 0 2.2G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
[root@control ~]#

5. Display in bytes or KB

In few scenarios if we need to display in bytes or kilobytes, -b option is used.

[root@control ~]# lsblk -b
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 34055651328 0 disk
|-sda1 8:1 0 1073741824 0 part /boot
`-sda2 8:2 0 32980860928 0 part
|-cl-root 253:0 0 30651973632 0 lvm /
`-cl-swap 253:1 0 2327838720 0 lvm [SWAP]
sr0 11:0 1 1073741312 0 rom
[root@control ~]#

6. Print devices in JSON format

This format will be useful if we want to set variables in script or generating reports in excel sheets, depending upon the use cases

[root@control ~]# lsblk -J
{
"blockdevices": [
{"name": "sda", "maj:min": "8:0", "rm": "0", "size": "31.7G", "ro": "0", "type": "disk", "mountpoint": null,
"children": [
{"name": "sda1", "maj:min": "8:1", "rm": "0", "size": "1G", "ro": "0", "type": "part", "mountpoint": "/boot"},
{"name": "sda2", "maj:min": "8:2", "rm": "0", "size": "30.7G", "ro": "0", "type": "part", "mountpoint": null,
"children": [
{"name": "cl-root", "maj:min": "253:0", "rm": "0", "size": "28.6G", "ro": "0", "type": "lvm", "mountpoint": "/"},
{"name": "cl-swap", "maj:min": "253:1", "rm": "0", "size": "2.2G", "ro": "0", "type": "lvm", "mountpoint": "[SWAP]"}
]
}
]
},
{"name": "sr0", "maj:min": "11:0", "rm": "1", "size": "1024M", "ro": "0", "type": "rom", "mountpoint": null}
]
}
[root@control ~]#

7. Print in RAW format

The -r will print the device in raw format

[root@control ~]# lsblk -r
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 31.7G 0 disk
sda1 8:1 0 1G 0 part /boot
sda2 8:2 0 30.7G 0 part
sr0 11:0 1 1024M 0 rom
cl-root 253:0 0 28.6G 0 lvm /
cl-swap 253:1 0 2.2G 0 lvm [SWAP]
[root@control ~]#

For more information refer the man page http://man7.org/linux/man-pages/man8/lsblk.8.html

Also read Filesystem extending with lvm https://computercarriage.com/2020/05/12/lvm-howto/

3 thoughts on “Easy listing block devices using lsblk

Leave a Reply