Users repeatedly complain about lost data on USB thumb drives after they unplugged them without unmounting. We run Ubuntu 14.04 LTS on our machines. Auto-mounting is enabled.
As I am tired of remembering them to do a safe removal before unplugging, I want to make Ubuntu display an alert whenever a mounted USB drive is unplugged.
I thought of adding an udev
rule that runs notify-send
on removal in case the drive is still mounted. How can I determine if the USB drive was mounted upon removal?
Best Answer
I like Fabby's approach, but it is good to teach people about this bad habit (They are not working all the time on controlled machines). This is similar to an OSX feature, as described here.
There is a small difference that you may use it to know if it was a safe remove:
udev variable
ID_PART_TABLE_TYPE=dos
is set forsdx
disk node (NOT partition nodesdxY
) in unsafe remove.Where it is not set in safe remove
Noticed by monitoring udev events:
Safe remove
Unsafe remove
Create a udev rule (change
useranme
)Reload rules
Another way, you may use a script (python) that connect to
udisks
DBUS. It has all needed info you need about partition mount/unmount, disks plug/unplug...Reference/Source: Gentoo Wiki: Udisks - USB_Thumb_Drive_Example