15

Using Python, I scan Unix mounted volumes for files, then add or purge the filenames in a database, based on their existence. I've just realised that if the volume being scanned is unmounted for some reason, the scan will assume every filename on that volume should be purged! Yikes. Is there any better way to mount volumes or any suggestions at all?

About the only thing I can think of is to put a permanent dummy file on each volume which I check for before scanning, thereby ensuring that the volume is only scanned if the dummy file can be located.

1

2 Answers 2

21

You can use mountpoint to check if the given directory is a mount point. Example

mountpoint /mnt/foo; printf "$?\n"
/dev/foo is a mountpoint
0

mountpoint /mnt/bar; printf "$?\n"
/dev/bar is not a mountpoint
1

As the return value indicates, this can easily by used in an if statement in a script.

3
  • 1
    Wow, and Python even has the ismount method. Thank you Commented Jun 10, 2015 at 10:39
  • I'd recommend using a different approach. mountpoint(1) from util-linux-2.26 doesn't seem to do what you expect. From util-linux-2.26/sys-utils/mountpoint.1: mountpoint checks whether the given directory or file is mentioned in the /proc/self/mountinfo file. This means it only "sees" mount points that are already mounted, which pretty much defeats the initial purpose. Also, FWIW, mountpoint(1) is Linux-specific. Commented Jun 10, 2015 at 16:10
  • 1
    I agree it's Linux specific. In general I don't think there's a better alternative to parsing the mount or df output. I understand the question that the OP wants to check if that directory is a mount point at this moment and that's what mountpoint does. Commented Jun 10, 2015 at 16:46
9

The classic answer is that a directory is a mount point if the st_dev field returned by stat is different from the value in its parent, or if it's the root directory. This is the criterion that most tools use (find -xdev, rsync -x, du -x, …). This is provided as the os.path.ismount function in Python.

This method has both false negatives and false positives due to a number of complex ways of arranging filesystems. For example, on Linux, you can bind mount a (part of a) filesystem on a subdirectory of itself, creating a mount point where the parent directory is on the same filesystem. Conversely, some filesystems with subvolumes (at least btrfs) present a different st_dev value on each subvolume, but a subvolume root isn't a mount point. Network filesystems can cause both false positives and false negatives because a client can mount the same server directory on different paths, and possibly via different exports.

This won't help you directly, because if a directory is empty, that doesn't mean that it was a mount point at some other time. If you want to detect mount points, you need to do it at indexing time. You may want to keep separate mount points in separate indexes, and only update indexes whose roots are present and non-empty. Beware that a directory that is sometimes a mount point may be non-empty even when the usual filesystem isn't mounted, either because some other filesystem is mounted there or simply because there are files in that directory (the files in a directory are hidden while this directory is a mount point).

2
  • Also beware of btrfs and potential other file systems with subvolume features. In those cases the device field will be different and yet it's neither a mount point (not in mtab or similar) nor is it a separate partition. Commented Jun 10, 2015 at 23:56
  • There's an edge case here, which is /: as you know, that's its own parent directory, so it fails this test despite being a mount point. Another edge case is on platforms such as Linux that support bind-mount of a directory elsewhere on the same filesystem. Commented Apr 27, 2024 at 5:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.