Categories
SysOps

How to export block devices list as JSON

Export block devices list as JSON and parse using jq utility.

List block devices as JSON and print the size using human-readable format.

$ lsblk --json 
{
   "blockdevices": [
      {"name":"loop0", "maj:min":"7:0", "rm":false, "size":"95,6M", "ro":true, "type":"loop", "mountpoint":"/snap/poedit/28"},
      {"name":"loop2", "maj:min":"7:2", "rm":false, "size":"55,5M", "ro":true, "type":"loop", "mountpoint":"/snap/core18/1988"},
      {"name":"loop3", "maj:min":"7:3", "rm":false, "size":"61,7M", "ro":true, "type":"loop", "mountpoint":"/snap/core20/975"},
      {"name":"loop4", "maj:min":"7:4", "rm":false, "size":"55,4M", "ro":true, "type":"loop", "mountpoint":"/snap/core18/1997"},
      {"name":"loop5", "maj:min":"7:5", "rm":false, "size":"162,9M", "ro":true, "type":"loop", "mountpoint":"/snap/gnome-3-28-1804/145"},
      {"name":"loop6", "maj:min":"7:6", "rm":false, "size":"64,8M", "ro":true, "type":"loop", "mountpoint":"/snap/gtk-common-themes/1514"},
      {"name":"loop7", "maj:min":"7:7", "rm":false, "size":"219M", "ro":true, "type":"loop", "mountpoint":"/snap/gnome-3-34-1804/66"},
      {"name":"loop8", "maj:min":"7:8", "rm":false, "size":"138,9M", "ro":true, "type":"loop", "mountpoint":"/snap/chromium/1564"},
      {"name":"loop9", "maj:min":"7:9", "rm":false, "size":"131,6M", "ro":true, "type":"loop", "mountpoint":"/snap/docker/796"},
      {"name":"loop10", "maj:min":"7:10", "rm":false, "size":"215,4M", "ro":true, "type":"loop", "mountpoint":"/snap/rpi-imager/170"},
      {"name":"loop11", "maj:min":"7:11", "rm":false, "size":"217,9M", "ro":true, "type":"loop", "mountpoint":"/snap/gnome-3-34-1804/60"},
      {"name":"loop12", "maj:min":"7:12", "rm":false, "size":"96,3M", "ro":true, "type":"loop", "mountpoint":"/snap/poedit/27"},
      {"name":"loop13", "maj:min":"7:13", "rm":false, "size":"50,7M", "ro":true, "type":"loop", "mountpoint":"/snap/snap-store/481"},
      {"name":"loop14", "maj:min":"7:14", "rm":false, "size":"32,3M", "ro":true, "type":"loop", "mountpoint":"/snap/snapd/11402"},
      {"name":"loop15", "maj:min":"7:15", "rm":false, "size":"156M", "ro":true, "type":"loop", "mountpoint":"/snap/rpi-imager/166"},
      {"name":"loop16", "maj:min":"7:16", "rm":false, "size":"51M", "ro":true, "type":"loop", "mountpoint":"/snap/snap-store/518"},
      {"name":"loop17", "maj:min":"7:17", "rm":false, "size":"32,3M", "ro":true, "type":"loop", "mountpoint":"/snap/snapd/11588"},
      {"name":"loop18", "maj:min":"7:18", "rm":false, "size":"5,5M", "ro":true, "type":"loop", "mountpoint":"/snap/notepad-plus-plus/260"},
      {"name":"loop19", "maj:min":"7:19", "rm":false, "size":"303,1M", "ro":true, "type":"loop", "mountpoint":"/snap/wine-platform-5-stable/16"},
      {"name":"loop20", "maj:min":"7:20", "rm":false, "size":"337,7M", "ro":true, "type":"loop", "mountpoint":"/snap/wine-platform-runtime/216"},
      {"name":"loop21", "maj:min":"7:21", "rm":false, "size":"65,1M", "ro":true, "type":"loop", "mountpoint":"/snap/gtk-common-themes/1515"},
      {"name":"loop22", "maj:min":"7:22", "rm":false, "size":"138,9M", "ro":true, "type":"loop", "mountpoint":"/snap/chromium/1568"},
      {"name":"sda", "maj:min":"8:0", "rm":false, "size":"447,1G", "ro":false, "type":"disk", "mountpoint":null,
         "children": [
            {"name":"sda1", "maj:min":"8:1", "rm":false, "size":"1M", "ro":false, "type":"part", "mountpoint":null},
            {"name":"sda2", "maj:min":"8:2", "rm":false, "size":"513M", "ro":false, "type":"part", "mountpoint":"/boot/efi"},
            {"name":"sda3", "maj:min":"8:3", "rm":false, "size":"732M", "ro":false, "type":"part", "mountpoint":"/boot"},
            {"name":"sda4", "maj:min":"8:4", "rm":false, "size":"445,9G", "ro":false, "type":"part", "mountpoint":null,
               "children": [
                  {"name":"sda4_crypt", "maj:min":"253:0", "rm":false, "size":"445,9G", "ro":false, "type":"crypt", "mountpoint":null,
                     "children": [
                        {"name":"vgubuntu-root", "maj:min":"253:1", "rm":false, "size":"444,9G", "ro":false, "type":"lvm", "mountpoint":"/"},
                        {"name":"vgubuntu-swap_1", "maj:min":"253:2", "rm":false, "size":"980M", "ro":false, "type":"lvm", "mountpoint":"[SWAP]"}
                     ]
                  }
               ]
            }
         ]
      },
      {"name":"sdb", "maj:min":"8:16", "rm":true, "size":"14,8G", "ro":true, "type":"disk", "mountpoint":null,
         "children": [
            {"name":"sdb1", "maj:min":"8:17", "rm":true, "size":"14,8G", "ro":true, "type":"part", "mountpoint":"/media/milosz/3363-6136"}
         ]
      },
      {"name":"sr0", "maj:min":"11:0", "rm":true, "size":"1024M", "ro":false, "type":"rom", "mountpoint":null}
   ]
}

List block devices as JSON, exclude loop devices, print the name, filesystem type and size using bytes.

$ lsblk --output NAME,FSTYPE,SIZE --exclude 7 --bytes --json
{
   "blockdevices": [
      {"name":"sda", "fstype":null, "size":480103981056,
         "children": [
            {"name":"sda1", "fstype":null, "size":1048576},
            {"name":"sda2", "fstype":"vfat", "size":537919488},
            {"name":"sda3", "fstype":"ext4", "size":767557632},
            {"name":"sda4", "fstype":"crypto_LUKS", "size":478795530240,
               "children": [
                  {"name":"sda4_crypt", "fstype":"LVM2_member", "size":478778753024,
                     "children": [
                        {"name":"vgubuntu-root", "fstype":"ext4", "size":477748002816},
                        {"name":"vgubuntu-swap_1", "fstype":"swap", "size":1027604480}
                     ]
                  }
               ]
            }
         ]
      },
      {"name":"sdb", "fstype":null, "size":15931539456,
         "children": [
            {"name":"sdb1", "fstype":"vfat", "size":15927345152}
         ]
      },
      {"name":"sr0", "fstype":null, "size":1073741312}
   ]
}

Display all available information about specific filesystem as JSON, ignore dependencies and print the size using human-readable format.

$ lsblk --output-all --nodeps --json /dev/mapper/sda4_crypt 
{
   "blockdevices": [
      {"name":"sda4_crypt", "kname":"dm-0", "path":"/dev/mapper/sda4_crypt", "maj:min":"253:0", "fsavail":null, "fssize":null, "fstype":"LVM2_member", "fsused":null, "fsuse%":null, "fsver":"LVM2 001", "mountpoint":null, "label":null, "uuid":"LUpQBF-ziSa-tjbo-oS02-TuxS-mQLK-6rX2Cm", "ptuuid":null, "pttype":null, "parttype":null, "parttypename":null, "partlabel":null, "partuuid":null, "partflags":null, "ra":128, "ro":false, "rm":false, "hotplug":false, "model":null, "serial":null, "size":"445,9G", "state":"running", "owner":"root", "group":"disk", "mode":"brw-rw----", "alignment":0, "min-io":512, "opt-io":0, "phy-sec":512, "log-sec":512, "rota":false, "sched":null, "rq-size":128, "type":"crypt", "disc-aln":0, "disc-gran":"512B", "disc-max":"2G", "disc-zero":false, "wsame":"0B", "wwn":null, "rand":false, "pkname":null, "hctl":null, "tran":null, "subsystems":"block", "rev":null, "vendor":null, "zoned":"none", "dax":false}
   ]
}

List only children devices.

$ lsblk --json | jq  --raw-output  '.. |  .children? | .[]? | .name'
sda1
sda2
sda3
sda4
sda4_crypt
vgubuntu-root
vgubuntu-swap_1
sdb1

Display basic information about mounted filesystems with exception of those mount point starts with /snap/ directory.

$ lsblk --fs --json | jq --raw-output --compact-output '.. | select(.mountpoint? and .mountpoint!=null and (.mountpoint|startswith("/snap/")|not)) | [.name,.fstype,.mountpoint]' 
["sda2","vfat","/boot/efi"]
["sda3","ext4","/boot"]
["vgubuntu-root","ext4","/"]
["vgubuntu-swap_1","swap","[SWAP]"]
["sdb1","vfat","/media/milosz/3363-6136"]

Display some basic information on available block devices with exception of loop devices.

$ lsblk --output-all --exclude 7 --json | jq --raw-output --compact-output '.. | select(.name? and .fstype?) | {name,fstype,mountpoint,sched}'
{"name":"sda2","fstype":"vfat","mountpoint":"/boot/efi","sched":"mq-deadline"}
{"name":"sda3","fstype":"ext4","mountpoint":"/boot","sched":"mq-deadline"}
{"name":"sda4","fstype":"crypto_LUKS","mountpoint":null,"sched":"mq-deadline"}
{"name":"sda4_crypt","fstype":"LVM2_member","mountpoint":null,"sched":null}
{"name":"vgubuntu-root","fstype":"ext4","mountpoint":"/","sched":null}
{"name":"vgubuntu-swap_1","fstype":"swap","mountpoint":"[SWAP]","sched":null}
{"name":"sdb1","fstype":"vfat","mountpoint":"/media/milosz/3363-6136","sched":"mq-deadline"}
{"name":"sde","fstype":"xfs","mountpoint":null,"sched":"mq-deadline"}

Display mounted block devices with less then 20GB of available space and over 50% of used space.

$ (echo -e "Device;Free space (bytes);Used space%"; lsblk --output-all --exclude 7  --bytes --json | jq --raw-output --compact-output '.. | select(.fsavail?) | select(.fsavail | tonumber / 1024 / 1024 / 1024 < 20)  | select(."fsuse%" | rtrimstr("%") | tonumber > 50) | [.name, .fsavail, ."fsuse%" ] | join(";")')  | column  -t --separator ";" 
Device  Free space (bytes)  Used space%
sda3    252354560           59%