I have written a tool which automatically formats and copies files to USB flash drives when they are inserted. It needs the drive to be unmounted to format it.
Normally Nautilus will automatically mount the drive when inserted, so I have to wait for this to happen and then unmount the drive before formatting it. The only problem is that if the user has disabled automounting then my program will wait forever.
I could change the dconf auto-mount setting when my program starts, and then change it back when it exits, but this is not a good solution because the setting wil not be restored if my program crashes.
How can I temporarily prevent Nautilus from automounting USB drives only during the time when my program is running?
You may use
PROGRAM
to check if your program is running to activate that UDEV rule, otherwise it is disabled becausepgrep
returns unsuccessfully.source:
man udev
Add new rules file
/etc/udev/rules.d/90-prevent-auto-mount.rules
Reload rules
BTW,
udisk2
is the one responsible for auto-mountingUPDATE: I have ignored the most important specification: "without making permanent changes to the system". Still solvable I'd say, but not without some deep research work into desktop session life cycle or desktop event handling. That ain't my expertise. ;)
Obviously you know already how to switch off the feature in question. If you're concerned that it is not switched back if your program crashes then there are only two possibilities: Take care that your program does not crash or program a program that watches your program and switches the feature back when your program is not running.
But maybe there is a different solution. You could append a line like this to
/etc/fstab
:Then if the filesystem on the drive carries that label it remains untouched by the automount feature and you can (u)mount it without disturbance. See
man mount
andman fstab
for details. You could also use UUID instead of LABEL, but that depends on your use case and filesystem.