Hook into linux mount/umount event

8

2

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.

Jörg Haubrichs

Posted 2010-03-27T21:25:30.543

Reputation: 181

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 – 2010-03-27T21:39:40.033

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) – Jörg Haubrichs – 2010-03-27T21:45:11.497

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).

– quack quixote – 2010-05-05T06:33:23.560

Answers

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.

mdpc

Posted 2010-03-27T21:25:30.543

Reputation: 4 176

Very bad advise and a maintainance horror if multiple programs would do this. Dont start this way. – Lothar – 2019-06-04T07:43:16.833

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.

d34dh0r53

Posted 2010-03-27T21:25:30.543

Reputation: 371

2inotify 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. – Craig McQueen – 2015-07-30T01:41:13.490

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.

natxo asenjo

Posted 2010-03-27T21:25:30.543

Reputation: 193

1udev 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 – 2014-12-01T11:55:43.927

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. – natxo asenjo – 2014-12-01T18:09:47.940

Or you can just remove it since it's totally outdated :) – Pithikos – 2014-12-01T18:15:53.907

why should I remove anything about something I posted 4 years ago and is easily found through archive.org? If you feel so compelled to do it, just get enough reputation points to edit stuff and do it yourself. I can not be bothered to be honest. As to it being outdated, well, that is your opinion and I suppose you know what people say about opinions ;-) – natxo asenjo – 2014-12-01T19:33:37.963

2the 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 – 2014-12-02T11:51:30.733

I know how the SE sites work. The solution there is not outdated just because you say so. Please stop bugging me or I will have to report you to the moderators. – natxo asenjo – 2014-12-02T17:03:37.360

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 – 2015-07-29T04:59:15.630

@nhinkle, there are no other users, just one user. The examples were perfectly fine in the link which was later easily found in archive.org. Please stop reviving zombie threads. Comments like this one are the reasons people stop posting to the SE sites. It sure will in my case – natxo asenjo – 2015-08-03T00:09:59.687

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 [answer]. – nhinkle – 2015-08-03T00:42:11.080

please do not lecture me on how to be polite. I have re-read my answers and they are not rude or impolite. So if someone flags a post accusing somebody else that's it? A moderator comes in and tells me off? You have got to be kidding me. Please have the professional courtesy of pointing me what was rude and/or impolite in my comments at least before playing god on a site. And any other user with enough points could have edited my answer to add the link to archive.org, like it has already happened if this was of such importance. – natxo asenjo – 2015-08-03T06:20:39.030

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.

Pithikos

Posted 2010-03-27T21:25:30.543

Reputation: 984

"hacky" is usually good enough for most situations. The real world is run by "hacky" solutions. If you do not know that then you have no real world experience using computer systems. Please save your opinions to yourself and stick to the facts. Thanks. – natxo asenjo – 2015-08-03T06:46:16.850

1@natxoasenjo that's why there are good developers and bad developers. – Pithikos – 2015-08-03T10:07:04.840

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? ;-) – natxo asenjo – 2015-08-03T14:03:28.050

and by the way, writing udev rules does not make you a developer, at most a sysadmin of your computer. – natxo asenjo – 2015-08-03T14:12:40.547

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 – 2015-08-04T17:12:18.423