Safely Deleting a Directory Only If It ISN'T a Mount Point
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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.
filesystems mount directory rm unmounting
add a comment |Â
up vote
1
down vote
favorite
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.
filesystems mount directory rm unmounting
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
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
filesystems mount directory rm unmounting
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.
filesystems mount directory rm unmounting
filesystems mount directory rm unmounting
edited Sep 26 '17 at 23:45
Jeff Schaller
32.3k849110
32.3k849110
asked Sep 26 '17 at 10:16
Haravikk
2741315
2741315
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
add a comment |Â
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
2
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
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
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
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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
add a comment |Â
up vote
3
down vote
accepted
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
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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.
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.
answered Sep 27 '17 at 0:00
BowlOfRed
2,335612
2,335612
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
add a comment |Â
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
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
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
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
edited Sep 26 '17 at 12:42
answered Sep 26 '17 at 10:23
sebasth
6,14321643
6,14321643
add a comment |Â
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Sep 26 '17 at 16:32
answered Sep 26 '17 at 15:08
roaima
40.2k547110
40.2k547110
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394501%2fsafely-deleting-a-directory-only-if-it-isnt-a-mount-point%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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