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.开发者_Go百科
Thanks!
Try man udev
, or man hotplug
.
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
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.
精彩评论