1

Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).

Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.

So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).

Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling rm -r /mnt/point.

Are there any alternatives? I was intrigued by the possibility of using the find command's -xdev option, but I'm not sure how I would actually provide a point of comparison, as find /mnt/point -xdev won't work as the target and its contents are the same device.

Also, using rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an .autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.

  • 2
    Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question. – Raman Sailopal Sep 26 '17 at 10:34
  • What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems. – roaima Sep 27 '17 at 0:48
3

If the directory is a mount point, it will be busy and you shouldn't be able to rename it.

$ sudo mv /mnt /mnt.old
mv: cannot move '/mnt' to '/mnt.old': Device or resource busy

If it's just a regular directory, you should be able to rename it.

$ sudo mv /mnt /mnt.old

If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.

  • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution. – Haravikk Sep 27 '17 at 13:01
1

If you are using Linux

You can test if a directory is a mount point using mountpoint.

To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.

unshare -m --propagation private -- "<delete script>"

Everything in same script:

#!/bin/sh

unshare -m --propagation private -- sh -e <<EOF

if ! mountpoint -q "<path>"; then
    rm -r "<path>"
fi

EOF
0

You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:

rm -f mountpoint/.autodiskmounted    # MacOS cookie
rmdir mountpoint 2>/dev/null

If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)

I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.

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.