List Block Devices on FreeBSD lsblk(8) Style

When I have to work on Linux systems I usually miss many nice FreeBSD tools such as these for example to name the few:

  • sockstat
  • gstat
  • top -b -o res
  • top -m io -o total
  • usbconfig
  • rcorder
  • beadm/bectl
  • idprio/rtprio

… but sometimes – which rarely happens – Linux has some very useful tool that is not available on FreeBSD. An example of such tool is lsblk(8) that does one thing and does it quite well – lists block devices and their contents. It has some problems like listing a disk that is entirely used under ZFS pool on which lsblk(8) displays two partitions instead of information about ZFS just being there – but we all know how much in some circles the CDDL licensed ZFS is unloved in that GPL world.

Example lsblk(8) output from Linux system:

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sda 8:0 0 931.5G 0 disk
|-sda1 8:1 0 500M 0 part /boot
`-sda2 8:2 0 931G 0 part
|-vg_local-lv_root (dm-0) 253:0 0 50G 0 lvm /
|-vg_local-lv_swap (dm-1) 253:1 0 17.7G 0 lvm [SWAP]
`-vg_local-lv_home (dm-2) 253:2 0 1.8T 0 lvm /home
sdc 8:32 0 232.9G 0 disk
`-sdc1 8:33 0 232.9G 0 part
`-md1 9:1 0 232.9G 0 raid10 /data
sdd 8:48 0 232.9G 0 disk
`-sdd1 8:49 0 232.9G 0 part
`-md1 9:1 0 232.9G 0 raid10 /data


What FreeBSD offers in this department? The camcontrol(8) and geom(8) commands are available. You can also use gpart(8) command to list partitions. Below you will find output of these commands from my single disk laptop. Please note that because of WordPress limitations I need to change all > < characters to ] [ ones in the commands outputs.

# camcontrol devlist
[Samsung SSD 860 EVO mSATA 1TB RVT41B6Q] at scbus1 target 0 lun 0 (ada0,pass0)

% geom disk list
Geom name: ada0
Providers:
1. Name: ada0
Mediasize: 1000204886016 (932G)
Sectorsize: 512
Mode: r1w1e2
descr: Samsung SSD 860 EVO mSATA 1TB
lunid: 5002538e402b4ddd
ident: S41PNB0K303632D
rotationrate: 0
fwsectors: 63
fwheads: 1

# gpart show
=> 40 1953525088 ada0 GPT (932G)
40 409600 1 efi (200M)
409640 1024 2 freebsd-boot (512K)
410664 984 - free - (492K)
411648 1953112064 3 freebsd-zfs (931G)
1953523712 1416 - free - (708K)



They provide needed information in acceptable manner but only on systems with small amount of disks. What if you would like to display a summary of all system drives contents? This is where lsblk.sh comes handy. While lsblk(8) has many interesting features like --perms/--scsi/--inverse modes I focused to provide only the basic feature – to list the system block devices and their contents. As I have long and pleasing experience with writing shell scripts such as sysutils/beadm or sysutils/automount I though that writing lsblk.sh may be a good idea. I actually ‘open-sourced’ or should I say shared that project/idea in 2016 in this thread lsblk(8) Command for FreeBSD on FreeBSD Forums but lack of time really slowed that ‘side project’ development pace. I finally got back to it to finish it.

The lsblk.sh is generally small and simple shell script which tales less then 400 SLOC.

lsblk


Here is example output of lsblk.sh command from my single disk laptop.

% lsblk.sh
DEVICE MAJ:MIN SIZE TYPE LABEL MOUNT
ada0 0:5b 932G GPT - -
ada0p1 0:64 200M efi efiboot0 [UNMOUNTED]
ada0p2 0:65 512K freebsd-boot gptboot0 -
[FREE] -:- 492K - - -
ada0p3 0:66 931G freebsd-zfs zfs0 [ZFS]
[FREE] -:- 708K - - -




Same output in graphical window.

lolcat


Below you will find an example lsblk.sh output from server with two system SSD drives (da0/da1) and two HDD data drives (da2/da3).

# lsblk.sh
DEVICE MAJ:MIN SIZE TYPE LABEL MOUNT
da0 0:be 224G GPT - -
da0p1 0:15a 200M efi efiboot0 [UNMOUNTED]
da0p2 0:15b 512K freebsd-boot gptboot0 -
[FREE] -:- 492K - - -
da0p3 0:15c 2.0G freebsd-swap swap0 [UNMOUNTED]
da0p4 0:15d 221G freebsd-zfs zfs0 [ZFS]
[FREE] -:- 580K - - -
da1 0:bf 224G GPT - -
da1p1 0:16a 200M efi efiboot1 [UNMOUNTED]
da1p2 0:16b 512K freebsd-boot gptboot1 -
[FREE] -:- 492K - - -
da1p3 0:16c 2.0G freebsd-swap swap1 [UNMOUNTED]
da1p4 0:16d 221G freebsd-zfs zfs1 [ZFS]
[FREE] -:- 580K - - -
da2 0:c0 11T GPT - -
da2p1 0:16e 11T freebsd-zfs - [ZFS]
[FREE] -:- 1.0G - - -
da3 0:c1 11T GPT - -
da3p1 0:16f 11T freebsd-zfs - [ZFS]
[FREE] -:- 1.0G - - -



Below you will find other examples from other systems I have tested lsblk.sh on.

lsblk.examples


While lsblk.sh is not the fastest script on Earth (because of all the needed parsing) it does its job quite well. If you would like to install it in your system just type the command below:

# fetch -o /usr/local/bin/lsblk https://raw.githubusercontent.com/vermaden/scripts/master/lsblk.sh
# chmod +x /usr/local/bin/lsblk
# hash -r || rehash
# lsblk


If I got time which other original Linux lsblk(8) subcommand/option/argument is worth adding to the lsblk.sh script?
🙂


Regards.

EOF

Continue reading...
 
Back
Top