Note: Answer now updated to function under 19.04
I'm on 14.04 and accidentally open my cd/dvd/bluray drive about 20 times a day. I've looked at a few questions here which provided no working solution.
Both this question and this question appear to be outdated -- the offered solution doesn't work.
When I do, eject -i on
I'm met with eject: unable to find or open device for: 'cdrom'
When I do, eject -i on /dev/sr0
I get a more favorable response: CD-Drive may NOT be ejected with device button
however the button is unfortunately still enabled.
The information from /proc/sys/dev/cdrom/info
suggests that locking is possible:
CD-ROM information, Id: cdrom.c 3.20 2003/12/17
drive name: sr0
drive speed: 24
drive # of slots: 1
Can close tray: 1
Can open tray: 1
Can lock tray: 1
Can change speed: 1
Can select disk: 0
Can read multisession: 1
Can read MCN: 1
Reports media changed: 1
Can play audio: 1
Can write CD-R: 1
Can write CD-RW: 1
This answer has a working solution which fixes a UDEV rule to enable locking the drive. I've added some practical info to the solution, allowing one to:
- Disable the optical drive hardware eject button at startup
- Add a keyboard shortcut to eject the optical drive
- Ensure the drive stays locked after waking from suspend
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:Next, edit
/etc/udev/rules.d/60-cdrom_id.rules
and comment out the problematic line:Locate this line:
Now add a
#
(this "comments out" the line, effectively nullifying it without deleting) in front to make it look like this: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
oreject -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:
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:
Paste the following into the new file:
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:
Paste the following into the new file:
Save and close the file, make it executable and you're all set!
For me information here helps: https://bugs.launchpad.net/ubuntu/+source/eject/+bug/885051/comments/6
I edited file in /lib/ directory, not in /etc/ Now in my Ubuntu 14.04 command works: eject -i 1 /dev/sr0
Try this to make the button not eject:
Open: System Settings > Keyboard > Shortcuts > Sound and Media
Select "Eject" and clear the shortcut (by pressing "Backspace").
You can use the shortcut settings to make it do something more useful instead, like open a commonly used application, lock your screen or reset your wifi.
Source:
I have a MacBook Pro running 14.04 and the eject button is right on top of Delete/Backspace, which makes it easily pressed accidentally (it's worse for MacBooks without disc drives where that button is for Power instead). Used the shortcut settings to make the eject button lock my laptop instead. Have used this solution since 12.04 and everything in between.
If all else fails:
Take a pin (not a needle as you'll hurt yourself), push it firmly behind the button on the DVD drive and use it as a lever to yank out the button. Then take a small piece of plastic 1mm wider and larger then the resulting hole, put some super glue on it and glue it on the hole taking care not put it over the tiny hole next to it.
From now on, use the following 2 methods to open the DVD drive:
On this site I found this suggestion if
eject -i 1
doesn't work:sudo echo 1 > /proc/sys/dev/cdrom/lock
to lock (0 instead of 1 to unlock)There are some other options dicussed as well.
Here is a short c source code performing another option: link (notice the bugfix for newer versions in the upper comments!)
But I read in some other forums that all these eject-commands only change the drive's locking status when a disk is inserted and mounted. The only solution seems to be a tool called
cdctl
, but it is not very compatible with many systems, as I studied the comments.