CD/DVD tray LOCK/UNLOCK under Linux

LOCK/UNLOCK your CD/DVD under Linux

==== UPDATE1 03.Jan.2013 ====
It looks like when the time passes by, the things get changed
The below program stopped working after some changes in udev rules.
In order to make it work again you have to grab control over your eject button from udevd

comment the following line:
ENV{DISK_EJECT_REQUEST}=="?*", RUN+="cdrom_id --eject-media $tempnode", GOTO="cdrom_end"
in /lib/udev/rules.d/60-cdrom_id.rules,
restart udev

voila, the program is working again.

UPDATE: As you can see in the comments, one reader suggested using eject utility:

Lock the drive: eject -i 1
Unlock the drive: eject -i 0

This works perfectly for me.

When I am home, my kid is playing with my laptop DVD and tries to grab the tray and take it away :).

As you can imagine I am not very happy with this and wanted to find a program to lock the tray but unfortunately did not find any usable.

I found one but it is for Windows, and it costs money. Come on guys you want 15 US$ for 10 rows of code. This is a robbery.

Besides that I am working under Linux and this programs is no good to me.

I am not a programmer but since I was unable to find a suitable program I wrote one.

It is tested under Linux (Fedora 11 if that matters at all).

How to compile:

gcc -o cd-dvd-lock cd-dvd-lock.c

Usage ./cd-dvd-lock lock|unlock

Depending on the parameter it either locks or unlock the tray.

Copy the source below and save it in file called cd-dvd-lock.c

------------ COPY BELOW THIS LINE------------

* cd-dvd-lock.c
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
* The initial developer of the original code is Deyan T. Chepishev
* (C) 2009             Deyan T. Chepishev

* The CD/DVD device is expected to be /dev/cdrom. This is
* usually a symlink to the real device. If you dont want to create a symlink
* just edit the path in the code and point it to your CD/DVD device.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <sys/ioctl.h>

void usage( char a[] );

int main(int argc, char *argv[]){

//The location of cd/dvd device. Edit this if necessary
char device[]="/dev/cdrom";

int fd,lock=9999;

// Set unbuffered output
setvbuf(stdout, NULL, _IONBF, 0);

if( 2 != argc ){

if (0 == strcasecmp(argv[1],"lock")){

if(0 == strcasecmp(argv[1],"unlock")){

if( (0!=lock) && (1!=lock) ){

if (-1 == (fd=open(device,O_RDWR|O_NONBLOCK))){
 printf("Error opening device: \"%s\", ",device);
if (0 != ioctl(fd, CDROM_LOCKDOOR,lock)){
 printf("Error locking device: \"%s\", ",device);

void usage( char a[] ){
printf("\n\nUsage %s [lock|unlock]\n\n", a);

------------ COPY ENDS ABOWE THIS LINE------------


  1. Thanks! This is exactly what i was looking for. I have tested it under ubuntu 9.10 64-bit and it is working perfectly.

  2. or just use eject -i 1|0


  3. Thank you, this is exactly what I was looking for. I read about the "eject -i" method elsewhere, but my version of eject doesn't support it.

    For "not a programmer", your code looks pretty good and you seem to know quite a bit of low-level Linux programming. I knew there had to be a way of doing this but didn't succeed in finding the proper documentation, header files, etc. I'm glad I found your blog then.


  4. BTW: Trivial programs like this aren't protected by copyright law. So there's no need for the GPL header above it.

  5. thanks alot. i was looking for such kind of solution.thanks again

  6. Don't feel stupid, not all versions of eject support locking, mine does not also. I've installed cdctl (cdctl -o[0|1]) which works, but your program works well.

  7. Doesn't work.
    I've tried it on Ubuntu and it doesn't work.

    1. I just updated the post. Please read again.

  8. Hi,

    I'd like to fork this to GitHub, adding a Makefile and some Ubuntu integration to remove the need to manually edit the rule file. I thought I'd check with you first, though, in case you were planning to do the same. If so there's no need for a fork.


    1. Hi,

      I am not planing to to anything further with this program. Its functionality is already existing in the eject utility option "-i". You can go and fork it if you think it will be usable to someone.


  9. Above code has warning during compilation
    " cd-dvd-lock.c:49:10: warning: implicit declaration of function ‘ioctl’ [-Wimplicit-function-declaration]
    if (0 != ioctl(fd, CDROM_LOCKDOOR,lock)) "

    Just Add Header file:
    #include "sys/ioctl.h"

    You can replace ".." with < >

    Then it is compiled properly.

    But it is not locking the CD/DVD drive.

    1. Thank you for your commet.
      Code fixed and can compile now, but considering the age of the post (10 years) I can guess that there are some changes which breaks some functionality :).
      I am not going to track this, because as I said above, this feature already exists as option '-i' in 'eject' util.