5

What's the simplest way to detect CDROM media removal and insertion in Linux? I want to write some simple code to handle this. For example, just bind an event for media insertion and implement the handler.

Thanks!

Mat
  • 202,337
  • 40
  • 393
  • 406
flypen
  • 2,515
  • 4
  • 34
  • 51

3 Answers3

2

Try man udev, or man hotplug.

monsieur_h
  • 1,360
  • 1
  • 10
  • 20
1

The desktop 'standards' have options for this.

For example I think Gnome uses dbus for this:

http://www.linuxcertification.co.za/linux-training-sysfs-udev-hald-dbus

[..] These applications are mainly used by desktop environment to carry out tasks when an event occurs such as open the file browser when a USB drive is inserted or image application when a camera is inserted.

D-Bus is used for example to launch media players when a audio CD is inserted and to notify other applications of the currently playing song for example.

Configuration

You could just have your desktop shell (e.g. Gnome/nautilus) call your application when the even happens:

http://library.gnome.org/users/user-guide/stable/gosnautilus-61.html.en

Non-desktop

For the non-desktop version of this, man udev is indeed your friend. Info on writing udev rules is here:

http://reactivated.net/writing_udev_rules.html

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
0

The simplest way from user space is to grep the output of the mount command, provided you have configured the CDROM for automount ( in /etc/fstab).But if you want to have an asynchronous notification, you might need to look into udev rules and uevents.

itisravi
  • 3,406
  • 3
  • 23
  • 30