Ubuntu – How to disable laptop optical drive eject button and assign eject to a keyboard shortcut

ejectoptical;

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

Best Answer

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!

Related Question