11

I want to know what the easiest way is to determine (without root privilege) whether a block device (say sdb) or any part of it is mounted (and which part of it).

Checking /proc/mounts for sdb is not enough because sdb or one of its partitions may be used by LVM. You can check /sys/block/sdb/sdb*/holders/ but you get dm-x entries which have to be resolved to /dev/mapper names in order to check /proc/mounts. Possible but if there is an easier solution... (which should not require root privilege)

1
  • 2
    I'm curious, were you not just trying to find out if the device is mounted? */holders and */*/holders is good enough to tell you its mounted if the device doesn't exist in the /proc/mounts table. Why do you need to resolve it, further? i.e. cryptsetup adds it to the holders, indicating you shouldn't be messing with the drive.. even if the /dev/mapper isn't mount, or cause encryption corruption. Its just that its being mounted internally through a driver. Also don't forget to check /sys/block/sdb/holders as well, as the drive could be "held" without partitions.
    – Rahly
    Feb 15, 2017 at 3:46

3 Answers 3

14

This is essentially a matter of checking a whole bag of corner cases.

  • A drive can appear in /proc/mounts
  • A drive can be used as swap (use /proc/swaps)
  • A drive can be part of an active LVM pv (use pvdisplay)
  • A drive can be part of a dm-mapper RAID group (use /proc/mdstat)
  • A drive can be directly accessed by an application (e.g. Oracle supports writing directly to a drive or partition instead of a filesystem) (use fuser)
  • A drive can be directly accessed by a virtual machine (use fuser)
  • A drive can be referenced by a loopback device (e.g: mount /dev/sda -o offset=1M /foo) (use losetup -a)

These are just the examples I came up with given a minute and a half to think about it. I'm sure there's a dozen others.

This last example I think is the most interesting and few people know about it. It allows you to mount a filesystem without using partitions. Just specify the starting offset and Linux will transparently create a loopback device. The example above yields the following:

# cat /proc/mounts
...
/dev/loop0 /foo ext4 relatime,data=ordered 0 0

# losetup -a
/dev/loop0 [0005]:2048 (/dev/sda), offset 1048576

Why would you do that? Typically it involves situations where things have previously gone horribly wrong.

Also bear in mind that with the namespacing feature now in mainline (see unshare), different processes can have different views about what's mounted and what isn't. Here things start to get a little bit red-pill.

1

There is much more ways to mount devices, all of them can be combined, so it's like often, up to you to find out.

i.e. what's used by lvm: pvdisplay

So you need include also nfs, cifs, raid, loop, crypt, and so on. A good starting point is always /proc/mounts, as you have already mentioned.

1

For device-mapper devices at least (which include cryptsetup and LVM volumes), there is the "Open count" displayed by dmsetup info. It comes from the DM_TABLE_STATUS ioctl and also appears to be the basis of the o (open) flag shown by lvs. That seems to be pretty comprehensive: for example, in dom0 of my Qubes OS 4.0 system (based on Fedora 25), it detected use by xen_blkback, which isn't mentioned by any of the other answers here or detected by lsblk.

Unfortunately, in brief research, I couldn't find an analogue of this for arbitrary block devices.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.