2

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.