8

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
  • 1
    it is working for me. do you have reloaded the udev rules with " udevadm control --reload-rules" ??? – solsTiCe Jan 11 '15 at 1:08
10

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!

  • Good one! (And no need for silly life hacks! Excellent!) Do you want me to delete the one with the pin? – Fabby Jan 15 '15 at 22:23
  • 1
    Thanks! It's ok with me to leave it for people with no other solution. It's another whole meaning of "hack" :) – Insperatus Jan 16 '15 at 0:56
  • Apparently suspending reverts and unlocks the drive again. – Insperatus Jan 16 '15 at 16:47
  • there is something called a "suspend / wake script" that gets called when suspending / waking up. I suggest you add your solution there as well (I don't have the absolute path to the script handy, just know it exists) – Fabby Jan 16 '15 at 16:51
  • 1
    Yep, I presumed as much as well. Checking here and here for solutions. – Insperatus Jan 16 '15 at 16:58
1

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

  • Unfortunately this does not work for me. – Insperatus Jan 9 '15 at 20:28
  • worked for me (Ubuntu 14.04 on a ThinkPad T420, edited the lib file, like gonzo suggested on launchpad) – mondjunge Jan 20 '15 at 9:42
  • Actually I was screwing up the directory structure, you were on the right track with this answer! – Insperatus Feb 12 '17 at 6:40
1

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:

  1. Use the Ubuntu "Eject" through the dash
  2. Keep that life-hack pin around your PC from now on and push it into the tiny hole next to your piece of plastic to eject the DVD when your laptop is turned off. (Use a piece of adhesive tape to fasten the life-hack pin to the underside of your laptop)
  • 1
    But destroying my cdrom-drive is not really what I would expect as an answer to how to SOFTWARE-lock the tray! Before doing this, I would even take a small piece of cardboard, cut a hole shaped like the button in it and tape it over the eject button so that touching it accidentally e.g. with your knee does not trigger it. – Byte Commander Jan 15 '15 at 19:37
  • @ByteCommander: that's why there is the huge "if all else fails" as the title. Furthermore, the original poster already got a laugh out of it and already upvoted. However, I think your life-hack is worthy of an answer too! If you post it as an answer, I think you'll get 2 upvotes! – Fabby Jan 15 '15 at 19:53
  • Nice thought, but I can't convert it and I don't want to write it again. If it helps anybody it's okay. And you can upvote comments too. By the way, how do you want to know you got your vote from the question poster? – Byte Commander Jan 15 '15 at 20:01
  • @ByteCommander: Upvoting comments doesn't get you any rep though. I only upvote a comment if I don't leave another comment. It's like a pat on the back. And an additional comment is like a big hug! >:) – Fabby Jan 15 '15 at 20:12
  • @ByteCommander: Normally you don't know who up/downvotes, but I saw the upvote come in, went to the question, went to the OP user page and saw "last activity: 1 min". So I deduced it was him/her/it and it gave me a smile as well. – Fabby Jan 15 '15 at 20:15
0

Try this to make the button not eject:

  1. Open: System Settings > Keyboard > Shortcuts > Sound and Media

  2. 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.

  • This didn't work for me as I'm hitting the button on the actual drive and not a keyboard button. – Insperatus Jan 13 '15 at 1:09
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.