3

I'm doing automation that will reformat a new disk and mount it. I want my automation to check and make sure the disk isn't in use so I don't break something that was already running (as happened to me today!).

EG, when I make my automation all my machines have /dev/sda and I'm going to automate adding /dev/sdb, but then I try and use a machine that already uses /dev/sdb for something.

Just to be clear, I do not mean does the disk have space. I mean is it mounted at all.

I want some kind of command or function that I can pass a block device to and it will return true or false depending on if the disk is in use.

The disk could be mounted as a whole device (eg "mount /dev/sda /mnt/foo") or it could be partitioned (eg "mount /dev/sda1 /mnt/foo") it could be used in LVM. It could be some other way of using a disk that hasn't been invented yet.

I've tried lsof and fuser, neither worked.

Note, the disk has to be mounted to fail my test. If it's formatted, or part of a volume group but not mounted, I don't care. That's fine.

Is there a better way to do this than checking the partition table and checking if it's part of a volume group and then checking to see if any partitions or lvs are mounted?

Thanks!

Dylan Martin
  • 548
  • 4
  • 14
  • 1
    Automating I'm not sure, but `lsblk` will at least provide you the info you are looking for. – Michael Hampton Aug 11 '18 at 00:14
  • 3
    This looks well answered over on https://unix.stackexchange.com/questions/111779/how-to-find-out-easily-whether-a-block-device-or-a-part-of-it-is-mounted-someh . – Anon Aug 11 '18 at 03:50

0 Answers0