1

Dear friends and colleges

we have servers with rhel 7.5 version , and each server include disks with filesystem and disks without filesystem

usually sda - is the OS disk , and sdb is disk that already have filesystem,

with the following lsblk syntax we can capture the disks list with filesystem type

lsblk --fs  -o NAME,FSTYPE
NAME             FSTYPE
sda
├─sda1           xfs
└─sda2           LVM2_member
  ├─VG-LV_root xfs
  ├─VG-LV_swap swap
  └─VG-LV_var  xfs
sdb              ext4
sdc  
sdd

I want to know how to match all the disks that are without filesystem

expected output

sdc
sdd

so we try the following syntax

lsblk --fs  -o NAME,FSTYPE |  awk '$2 == "" {print $1}'

but we get the following

sda
sdc
sdd

so we get sda in spite sda have filesystem ( XFS )

so how to capture only the real disks that are without filesystem - sdc / sdd

5
  • make a hash of the disks, then populate each disk hash with its partitions, any hash key that is empty at the end is a disk without a file system. That's one way of doing it. Don't be afraid to write more than a line of code, it's often much easier to use many lines than to try to cram it all one one. Commented Nov 8, 2020 at 21:18
  • better if you can post your solution so all folks here will enjoy -:) Commented Nov 8, 2020 at 21:20
  • I did, the only part that remains is you doing the coding of it. I don't generally write people's code for them unless it's an actually interesting problem. As I said, this problem is much easier if you use more lines of code because then you can test each item, decide whether it's a disk or a partition, or something you don''t want, then put it in the hash if it passes, as key or key value, then at the end, loop through the hash looking for the empty keys. Commented Nov 8, 2020 at 21:21
  • Other ways is to take the first item, the sda alone on a line, then use that and test with lsblk, if it returns more than 2 lines, it's got partitions, if it returns only two lines, the headers and the sda item, it's got no partitions, aka fiile systems. I'm sure others can come up with different ways of doing it. but it's all the same idea, look for the items with no partitions. You could also skip lsblk and just use /proc/partitions as well, using the same tests and hash methods/. Commented Nov 8, 2020 at 21:29
  • yes, I know, a partition isn't actually the same as a file system, but in this context, the assumption is if someone made a partition on a server, they also put a file system on that. Commented Nov 8, 2020 at 21:37

1 Answer 1

7

If your lsblk supports the --json output format, and if you have jq installed, you could parse that for block devices that (a) have no children (i.e. are unpartitioned) and (b) have no defined filesystem themselves:

lsblk --fs --json |
  jq -r '.blockdevices[] | select(.children == null and .fstype == null) | .name'

You must log in to answer this question.