You could write a udev rule like
ACTION=="remove", KERNEL=="sd[b-z][0-9]", SUBSYSTEM=="block", RUN+="umount-removed.sh"
Which will trigger the unplug event and then umount the file system from that script
MNT_POINT=`df | grep "$DEVNAME" | awk '{print $6}'`
#If still mounted
if [ ! -z "$MNT_POINT" ];then
logger -i -t usbrm -p daemon.notice "The device $DEVNAME is still mounted"
#Umount fs
umount $DEVNAME
if [ $? -eq 0 ];then
logger -i -t usbrm -p daemon.notice "$DEVNAME successfully unmounted from $MNT_POINT"
else
logger -i -t usbrm -p daemon.err "Impossible to umount $DEVNAME from $MNT_POINT. Aborting..."
fi
fi
You can see the log in /var/daemon.log.
Edit : Forgot to precise that udev use a specific name space so you have to configure it to share mount point with userspace.
To do it locate the file systemd-udevd.service
on your system and duplicate it to /etc/systemd/system/systemd-udevd.service
and replace content to MountFlags=shared
umount
the file system before removing the stick, did you?udevmonitor
. The problem is that the script which is triggered can not do anything (It tries to unmount the dev file and removes the mount point).udev
-rules that automatically unmounts filesystems when the underlaying blockdevice vanishes, but that won't protect you from any data loss. Best way to go is have your users eject their block devices properly. Hit them! :)