1

I have an issue with part of a script that is attempting to check if an attached volume is formatted before formatting.

The grep used in this check returns zero when the volume isnt formatted when using nvme volumes. This because the output of file -s seems to be a long string of characters:

Xs\333d8\374R\352if\253w}$\014\246E\034_I\275\304\2505!\3536U\336\252\274\252\323K\345\334\225;\356\0075T\350ru\003Kwp\353v:\326\263a\251\307\/\001Db\216

Due to this the grep count returns zero and the script assumes the disk is already formatted because an if statement assumes if the count is zero it must be formatted already.

The current check is on the output of whether this is zero:

file -s /dev/nvme | grep -c ': data$'

I would like to know if I can use blkid safely but check to make sure its greater than zero instead of zero to determine if the volume is formatted:

blkid | grep -c /dev/nvme

berimbolo
  • 113
  • 4

2 Answers2

5

You can check the details of a specific device and act based on that:

blkid --match-token TYPE=ext4 /dev/nvme123 || mkfs.ext4 -m0 /dev/nvme123

What this does is:

  1. blkid checks whether /dev/nvme123 is ext4 formatted, and if not it returns non-zero return code.
  2. The double-pipe || runs the second command (mkfs.ext4) only if the first one returned non-zero. If the first one returned 0 it won't run the mkfs.

Hope that helps :)

MLu
  • 24,849
  • 5
  • 59
  • 86
1

Try something like below

# for type in ext4 ext3 ext2 iso9660;do [[ `blkid |grep /dev/vdc|awk '{print $NF}'`  =~ TYPE=\"$type\" ]] && echo matched ;done

Output

matched

List the expected filesystem types and check if match, flag it and use it for further processing.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
  • The script in question only formats ext4 so I only need to check that. If it doesn't show with blkid at all though wont just checking its existence do the same thing? Is it safe to assume if `/dev/nvme` isnt shown by blkid but that is attached that the volume is not formatted? – berimbolo Jul 14 '19 at 16:31
  • In that case no match found and you can format it. above script is just a logic to find if partition have any fs type if true don't do anything else format it. – asktyagi Jul 15 '19 at 02:25