lsblkコマンドでブロックデバイスリストを取得する

恥ずかしながら、いままでlsblkコマンドの存在を知りませんでした。
RHEL系もあるのかな?

パーティション情報が紐付きリストで表示される

# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  1024M  0 rom  
vda    253:0    0    30G  0 disk 
├─vda1 253:1    0  28.1G  0 part /
└─vda2 253:2    0   1.9G  0 part [SWAP]
vdb    253:16   0    30G  0 disk 

-iオプション:紐がascii文字のみで構成されるようになる

# lsblk -i
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  1024M  0 rom  
vda    253:0    0    30G  0 disk 
|-vda1 253:1    0  28.1G  0 part /
`-vda2 253:2    0   1.9G  0 part [SWAP]
vdb    253:16   0    30G  0 disk 

-bオプション:サイズがバイト単位で表示される

# lsblk -b
NAME   MAJ:MIN RM        SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  1073741312  0 rom  
vda    253:0    0 32212254720  0 disk 
├─vda1 253:1    0 30198988800  0 part /
└─vda2 253:2    0  2011168768  0 part [SWAP]
vdb    253:16   0 32212254720  0 dis

-fオプション:ファイルシステムを表示してくる

# lsblk -f
NAME   FSTYPE LABEL MOUNTPOINT
sr0                 
vda                 
├─vda1 ext4         /
└─vda2 swap         [SWAP]
vdb                 

-rオプション:カラム調整や紐付けなどの装飾なしで表示される

# lsblk -r
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom 
vda 253:0 0 30G 0 disk 
vda1 253:1 0 28.1G 0 part /
vda2 253:2 0 1.9G 0 part [SWAP]
vdb 253:16 0 30G 0 disk

-nオプション:ヘッダ情報の表示を抑止

# lsblk -n
sr0     11:0    1  1024M  0 rom  
vda    253:0    0    30G  0 disk 
├─vda1 253:1    0  28.1G  0 part /
└─vda2 253:2    0   1.9G  0 part [SWAP]
vdb    253:16   0    30G  0 disk 

スクリプトで使うには -nrb あたりが良さそう

# lsblk -nrb
sr0 11:0 1 1073741312 0 rom 
vda 253:0 0 32212254720 0 disk 
vda1 253:1 0 30198988800 0 part /
vda2 253:2 0 2011168768 0 part [SWAP]
vdb 253:16 0 32212254720 0 disk 

オプションもいろいろ

組み合わせればブロックデバイスに関しての処理に必要になる一通りの情報が得られそう。

OPTIONS
       -a, --all
              List all block devices.

       -b, --bytes
              Print the SIZE column in bytes  rather  than  in  human-readable
              format.

       -d, --nodeps
              Don't  print  device  holders  or  slaves.   For  example "lsblk
              --nodeps /dev/sda" prints information about the sda device only.

       -D, --discard
              Print information about the discard (TRIM,  UNMAP)  capabilities
              for each device.

       -e, --exclude list
              Exclude the devices specified by a comma-separated list of major
              device numbers.  Note that RAM disks (major=1) are  excluded  by
              default.

       -f, --fs
              Output info about filesystems.  This option is equivalent to "-o
              NAME,FSTYPE,LABEL,MOUNTPOINT".   The  authoritative  information
              about filesystems and raids is provided by the blkid(8) command.

       -h, --help
              Print a help text and exit.

       -i, --ascii
              Use ASCII characters for tree formatting.

       -m, --perms
              Output  info about device owner, group and mode.  This option is
              equivalent to "-o NAME,SIZE,OWNER,GROUP,MODE".

       -l, --list
              Use the list output format.

       -n, --noheadings
              Do not print a header line.

       -o, --output list
              Specify which output columns to print.  Use --help to get a list
              of all supported columns.

       -P, --pairs
              Use key="value" output format.

       -r, --raw
              Use the raw output format.

       -t, --topology
              Output info about block device topology.  This option is equiva‐
              lent    to     "-o     NAME,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-
              SEC,ROTA,SCHED".

地味だけど便利なコマンドだな。昔からあったっけ?