Enable Locking the Drive
(note: if eject -i on
already works, you may skip ahead to "Lock the Drive on Startup")
First, copy /lib/udev/rules.d/60-cdrom_id.rules
to /etc/udev/rules.d/
like so:
cp /lib/udev/rules.d/60-cdrom_id.rules /etc/udev/rules.d/
Next, edit /etc/udev/rules.d/60-cdrom_id.rules and comment out the problematic line:
sudoedit /etc/udev/rules.d/60-cdrom_id.rules
Locate this line:
ENV{DISK_EJECT_REQUEST}=="?*", RUN+="cdrom_id --eject-media $devnode", GOTO="cdrom_end"
Now add a #
(this "comments out" the line, effectively nullifying it without deleting) in front to make it look like this:
# ENV{DISK_EJECT_REQUEST}=="?*", RUN+="cdrom_id --eject-media $devnode", GOTO="cdrom_end"
Now save and close by pressing Ctrl+X, then Y to confirm, followed by Enter to accept the current file name. Don't worry that it appears as a strange temporary file name, that's just how sudoedit works.
Now you should be able to disable the optical drive hardware button (essentially we're locking the drive) with this:
eject -i on /dev/sr0
~or~ eject -i 1 /dev/sr0
They do the same.
Lock the Drive on Startup
To make this more useful, I wanted this command to take effect upon startup. I used the GUI "Startup Applications" program (preinstalled in Ubuntu, find it with Dash) to accomplish this.
Open the program and then click the "Add" button, a new dialogue box opens.
Type a name (I went with the descriptive "Lock optical drive") and within the "Command:" field enter bash -c 'eject -i on /dev/sr0'
Click "Add" to complete and then close the program.
Add a Keyboard Shortcut
Now the optical drive is locked upon startup. But how will I open the drive when I need to use it?! To make it simple, I put the commands into an "eject" keyboard shortcut which unlocks the drive, ejects the drive, then relocks the drive. This way I can still easily access the drive but the hardware button is never a problem.
Here's how to accomplish this keyboard shortcut:
- Open the "Keyboard" program found within the Dash.
- Click the "Shortcuts" tab
- Click on "Custom Shortcuts" at the bottom of the list
- Click on the "+" sign, a new dialogue box will open
- Name the shortcut (I used "Unlock, Eject, Relock CD")
Enter this into the "Command:" field:
bash -c 'eject -i off /dev/sr0 && eject /dev/sr0 && eject -i on /dev/sr0'
Click "Apply"
Click to the right of your shortcut's name where it says "Disabled". Once you click it, "Disabled" changes to "New Accelerator":
Press the key combination you want to use. I used Ctrl+Alt+E
You can then test the shortcut immediately. If all is well close and you're done!
Lock Drive Upon Wake From Suspend (pre-systemd method)
I've noticed my drive becomes unlocked again upon resuming from suspend so I created a script to ensure the drive stays locked in this case.
Create the script file:
sudoedit /usr/lib/pm-utils/sleep.d/99lock-optical
Paste the following into the new file:
#!/bin/sh
# lock the optical drive upon resume from suspend
case "${1}" in
resume|thaw)
eject -i 1 /dev/sr0
;;
esac
Lock Drive Upon Wake From Suspend (systemd method)
I'm using 19.04 now and noticed my drive was becoming unlocked upon resuming from suspend. This method works to make it stay locked:
Create the script file:
sudoedit /lib/systemd/system-sleep/00start_my_connection
Paste the following into the new file:
#!/bin/sh
if [ $1 = post ]
then eject -i 1 /dev/sr0
fi
Save and close and you're all set!
In MATE at least it seems clicking the eject button just unmounts the device. To unmount and power off the drive on the command line, you can use udisksctl
First list the drives with lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:0 1 15.2G 0 disk
└─sdb1 8:1 1 15.2G 0 part /media/zanna/4C45-110F
mmcblk0rpmb 179:24 0 4M 0 disk
mmcblk0boot0 179:8 0 4M 1 disk
mmcblk0boot1 179:16 0 4M 1 disk
mmcblk0 179:0 0 29.1G 0 disk
├─mmcblk0p1 179:1 0 512M 0 part /boot/efi
├─mmcblk0p2 179:2 0 26.7G 0 part /
└─mmcblk0p3 179:3 0 2G 0 part [SWAP]
I want to remove the drive /dev/sdb
so I would do
udisksctl unmount -b /dev/sdb1
udisksctl power-off -b /dev/sdb
Best Answer
umount removes the device from the file system (mount point).
Eject and Safely remove both do basically the same thing on a flash drive.
sync the data , then un-mount (remove the drive from the file system).
In addition, eject would eject the media (think CD/DVD).
It is sort of a bug that both these options show with flash drives.
The expected behavior would be
Safely Remove - USB/Flash drive - sync data && unmount ; user then physically removes the drive
Eject - CDROM/DVD - unmount && eject physical media