Ubuntu – Prevent USB Drives from auto-remounting after remove

automountusb

When I press "safely remove drive" in Nautilus as user to my USB 3.0 Transcent Storejet 25H3 drive it reconnects immediately.

How can I safely remove it without that behaviour?

I think the only solution is to disable the automounting in Ubuntu. I did that now with:
How to disable automount in nautilus's preferences

But it is a shame, cause now my external music library doesn't automount anymore.

Isn't there a solution where harddisks don't remount on safely remove?!

Best Answer

Valid for this Ubuntu at least:

$ lsb_release -d  
Description:    Ubuntu 14.04.1 LTS


I have found that:

$ udisksctl mount --block-device /dev/$device

... will mount /dev/xxx in the same style as automount,
that is - in /media/$USER/Disk_Label-or-UUID/ with the last level dir auto-created.

$ udisksctl unmount --block-device /dev/$device;  

... will umount the above, but not '-eject' memory card/USB reader content.

$ gvfs-mount --eject "file:///media/$USER/DISK_LABEL"  

... lastly appears to be equal to 'Safely remove' or 'Eject' - and that WITHOUT an immediate re-mount.

Note: $ udisksctl power-off --block-device /dev/$device;
... would otherwise be a 'natural' option, but is hampered by an immediate re-mount.


EDIT --- you could use this script to "umount all" without having to dig out the names:

#! /bin/bash
#
for device in $(mount | grep "/media/$USER/" | cut -d" " -f1); do
        echo Unmounting $device 
        udisksctl unmount --block-device $device
done

if you prefer eject, change the -f1 into -f3 in the cut command and the line with udiskctl with

gvfs-mount --eject "file://$device"

(I think --- you can have problem with correctly quoting labels with blanks in it; and you'll have warnings if the device has multiple partitions mounted).



$ lsblk

... will display all the available block devices (except ram disks, no sudo required).
RAM and loop --> lsblk --all .



How do I "automount" a labelled disk?

Bring up the Dash (hit the 'Super' key) and type 'startup appl' and 'Startup Applications' should appear, click on it. Click Add and type anything you want in 'Name' and 'Comment' fields. Then fill in 'Command' with your version of:

/bin/bash -c '/bin/sleep 5 && /usr/bin/udisksctl mount -b /dev/disk/by-label/GreenWD_3TB'

... where /dev/disk/by-label/GreenWD_3TB is anything suitable you find from
the display of ls -lR /dev/disk/* | less



More:
How to access gvfs mounts from command line?
How to mount drive in /media/userName/ like nautilus does using udisks

Related Question