Today my boss asked me for a method to clearly umount and afterwards eject a CDROM media on the event of trying to eject the tray using the drive button. I googled around a bit and found auto-eject-cdrom, in the public domain, and originally written by Jens Axboe, from SuSe, and Peter Willis, in 2001 and 2005 respectively.
The problem with this little application is that it was unable to eject both my boss drive and mine. It did umount the filesystem but did not eject the tray. So I dig into the code and found that it used plain, old cdrom events, handled by the cdrom keel module. The event was, in fact, defined in <linux/cdrom.h> as:
#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
I found out that using system ("eject"); as my quick and dirty solution after the umount was working ok, but I was losing performance because eject is used for ejecting floppy disks, tapes and several other media. In fact, eject can figure out which media is he working with so he selects the proper method of ejecting, but I was bitten by the curiosity.
So I implemented both the cdrom commands and the SCSI commands for ejecting the tray, in two functions, copypasted from the eject.c, previously apt-get source‘ed and I’m putting this interesting piece of code to the public domain (most likely it should go with a GPL license, since I used GPL’ed code on it) here: auto-eject-cdrom v0.3.
A relevant and important piece of code goes now:
/* Here I use two different methods for ejecting the CDROM. This is for fun only, since I could use system("eject"); but this is the dirty hack. Here I use the only two CDROM-related eject functions from the eject.c file of the eject program by Jeff Tranter (email@example.com) - Note by Jose Parrella */
Installing is just a matter of compiling and copying to /usr/local/sbin and running it as auto-eject-cdrom &. I hope it’s useful for those of you who can not afford to use one of the keys of your keyboard to assign the eject command to it. Enjoy!