如何使用udev查找插入视频媒体(如DVD)的信息

3 投票
1 回答
2450 浏览
提问于 2025-04-15 22:52

我正在尝试把一个应用程序从使用HAL转换为使用纯粹的udev。这个应用是用Python写的,会使用gudev库,不过我也很想看到其他语言的例子。我可以通过以下代码获取所有连接的摄像头等视频设备:

import gudev

client = gudev.Client(["video4linux"])
for device in client.get_devices():
    print device.get_sysfs_attr("name"), device.get_device_name()

这段代码会输出类似这样的内容:

USB2.0 UVC WebCam /dev/video0

我也能获取到块设备的列表,但我想知道怎么:

  1. 判断它是否是CD/DVD驱动器?

  2. 判断如果驱动器支持可移动媒体,当前是否有媒体插入?

  3. 判断媒体的名称/标签是什么(比如DVD的标签是FUTURAMAS1)?

我想要转换的原始代码可以在这个链接找到:http://github.com/danielgtaylor/arista/blob/045a4d48ebfda44bc5d0609618ff795604ee134f/arista/inputs.py

任何帮助都将非常感激!


更新:下面添加了答案。

import gudev

client = gudev.Client(['block'])
for device in client.query_by_subsystem("block"):
    if device.has_property("ID_CDROM"):
        print "Found CD/DVD drive at %s" % device.get_device_file()
        if device.has_property("ID_FS_LABEL"):
            print "Found disc: %s" % device.get_property("ID_FS_LABEL")
        elif device.has_property("ID_FS_TYPE"):
            print "Found disc"
        else:
            print "No disc"

上面的代码会输出类似这样的数据:

Found CD/DVD drive at /dev/sr0
Found disc: Ubuntu_10.04_i386

谢谢你的帮助!

1 个回答

4

看看这个设备的属性:

import gudev

client = gudev.Client(['block'])
for device in client.query_by_subsystem("block"):
    print device
    for device_key in device.get_property_keys():
        print "   property %s: %s" % (device_key, device.get_property(device_key))
    print

撰写回答