How do I disable laptop optical drive eject button and assign eject to a keyboard shortcut?

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!

Tags:

Eject

Optical