9

is there a way to provide post-mount and pre-umount scripts in Linux?
I am trying to do some scripts on storage media when mounted and before umounting (eg, for synching).

Any help appreciated.

3
  • How is the filesystem being mounted? Things are not mounted automatically presumably you have some program/tool that is performing the mount for you. You need to determine what that is and replace it or fix it.
    – Zoredache
    Mar 27, 2010 at 21:39
  • In my case I normally use the KDE subsystem (presumably that is Dolphin, don't know which component is responsible for the actual mount command) Mar 27, 2010 at 21:45
  • 1
    i wrote about handling automounts with uDev or with HALevt in answers to another question. those would be replacements for the KDE or GVFS subsystem, but the procedure might give you some ideas of where to look. it sounds like you want to tap into the Dbus mounts (desktop/userspace rather than kernel level). May 5, 2010 at 6:33

4 Answers 4

2

If you are trying to do this for people invoking the /sbin/mount and /sbin/umount command, all you'd have to do is make a wrapper program for both and program what you want into these wrapper programs.

A wrapper program being replace the /sbin/mount with the wrapper program that calls the real /sbin/mount (renamed to something like /sbin/mount-real or /sbin/umount-real). To prevent anybody from calling the real program, use group permissions to restrict it and set the SGID bit on the wrapper programs.

If you are looking to fix the mount(2) library call, well that is an effort and probably difficult and probably not advisable as this could cause problems for internal systems processing especially during startup and shutdown.

Even with a wrapper program you'd probably have to thoroughly test it to avoid system interference and made appropriate adjustments.

1
  • 1
    Very bad advise and a maintainance horror if multiple programs would do this. Dont start this way.
    – Lothar
    Jun 4, 2019 at 7:43
0

You can probably use the inotify kernel hook, although a pre-unmount event may be really tricky to catch as I'm not sure if there is an inotify event sent before the directory is umounted.

1
  • 2
    inotify will tell you that a directory has been created (e.g. under /media or /mnt),but not if a filesystem has been mounted on that directory. Jul 30, 2015 at 1:41
0

The simplest solution that is probably going to work forever is to poll the /etc/mtab or /proc/mounts. Once a change to that file is made, it means that either something got mounted or unmounted. Then you have to check there for the device you want.

Udev is not the best solution since it just tells you if a device connected. It doesn't say if the device has actually mounted or not. This might work sometimes but it's a hacky solution that at some point will break and you will be pulling your hair out.

4
  • 1
    @natxoasenjo that's why there are good developers and bad developers.
    – Pithikos
    Aug 3, 2015 at 10:07
  • please come back when you have some real world experience. What you propose is to write some script/program to poll files and you think that is a better idea than to use a well tested api as udev. Who is the bad developper? ;-) Aug 3, 2015 at 14:03
  • and by the way, writing udev rules does not make you a developer, at most a sysadmin of your computer. Aug 3, 2015 at 14:12
  • 1
    @natxoasenjo well if you don't see that the "real world" programs break all the time due to hackish solutions then either you are blind or don't have "real world experience". Udev works fine but has nothing to do with mounting and unmounting. And please stop turning this into a forum thread about who is a programmer. Doesn't help anyone.
    – Pithikos
    Aug 4, 2015 at 17:12
0

you could write a udev rule to do that. I have used something like this for automatically backing up stuff to a usb drive. Fun stuff.

10
  • 1
    udev seems to not have an event for mount/unmount though. It fires just when you connect/disconnect a device. And your link doesn't work..
    – Pithikos
    Dec 1, 2014 at 11:55
  • that was answered 4 years ago. I am not responsible for that site. But you could take a look at archive.org ..., yes, it's still there, finding it will be left as an exercise for the reader. As to your comment about udev, udev lets you write rules and execute actions when those are matched. It's all you need. Dec 1, 2014 at 18:09
  • 3
    the point is to help other people stumbling on the issue, it's not about points or other ego-boosts. Now even if a user goes through all that trouble, the solution there is outdated. So you are wasting people's time for no benefit (except the 1-2 up-votes you have).
    – Pithikos
    Dec 2, 2014 at 11:51
  • 2
    @natxoasenjo the other users are correct, and this is exactly why we encourage answers to quote or paraphrase enough information from linked resources for the answer to stand alone if the links die. A good answer here would have actually shown an example of how to create a udev rule to run a script before/after mounting/unmounting.
    – nhinkle
    Jul 29, 2015 at 4:59
  • 1
    @natxoasenjo I visited the post because your comment was flagged by an unrelated user as being rude or impolite. Stack Exchange has certain expectations for answers, one of which is that they must contain more than a link to an external source. Your answer doesn't meet that requirement. You have an opinion of what is "perfectly fine" which does not match the Stack Exchange expectations for a good answer. Please take some time to read How to Answer.
    – nhinkle
    Aug 3, 2015 at 0:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .