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