什么是在Linux中检测CDROM介质删除/插入的最简单方法

时间:2022-04-11 11:49:28

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.

在Linux中检测CDROM介质删除和插入的最简单方法是什么?我想写一些简单的代码来处理这个问题。例如,只需绑定一个媒体插入事件并实现处理程序。

Thanks!

谢谢!

3 个解决方案

#1


2  

Try man udev, or man hotplug.

试试man udev,或者man hotplug。

#2


1  

The desktop 'standards' have options for this.

桌面“标准”有此选项。

For example I think Gnome uses dbus for this:

例如,我认为Gnome使用dbus:

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

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.

[..]这些应用程序主要用于桌面环境,以便在发生事件时执行任务,例如在插入USB驱动器时打开文件浏览器或插入摄像机时的图像应用程序。

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.

例如,D-Bus用于在插入音频CD时启动媒体播放器,并通知其他应用程序当前播放的歌曲。

Configuration

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

你可以让你的桌面shell(例如Gnome / nautilus)在偶数发生时调用你的应用程序:

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

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:

对于非桌面版本,man udev确实是你的朋友。有关编写udev规则的信息如下:

http://reactivated.net/writing_udev_rules.html

http://reactivated.net/writing_udev_rules.html

#3


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.

从用户空间最简单的方法是grep mount命令的输出,前提是你已经为自动挂载配置了CDROM(在/etc/fstab中)。但是如果你想要一个异步通知,你可能需要查看udev规则和uevents。

#1


2  

Try man udev, or man hotplug.

试试man udev,或者man hotplug。

#2


1  

The desktop 'standards' have options for this.

桌面“标准”有此选项。

For example I think Gnome uses dbus for this:

例如,我认为Gnome使用dbus:

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

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.

[..]这些应用程序主要用于桌面环境,以便在发生事件时执行任务,例如在插入USB驱动器时打开文件浏览器或插入摄像机时的图像应用程序。

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.

例如,D-Bus用于在插入音频CD时启动媒体播放器,并通知其他应用程序当前播放的歌曲。

Configuration

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

你可以让你的桌面shell(例如Gnome / nautilus)在偶数发生时调用你的应用程序:

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

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:

对于非桌面版本,man udev确实是你的朋友。有关编写udev规则的信息如下:

http://reactivated.net/writing_udev_rules.html

http://reactivated.net/writing_udev_rules.html

#3


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.

从用户空间最简单的方法是grep mount命令的输出,前提是你已经为自动挂载配置了CDROM(在/etc/fstab中)。但是如果你想要一个异步通知,你可能需要查看udev规则和uevents。