【发布时间】:2014-01-04 21:06:51
【问题描述】:
我正在制作一个简单的 python 程序来挂载和卸载闪存驱动器,这样我就不需要做更多的事情,只需查看一个列表并选择我想要挂载的内容。现在,我有每个设备的对象和属性列表。我想知道的是我应该查看哪些属性来判断设备是否是 USB 大容量存储设备。我的第一直觉是看“可拆卸”,但是我只插入了一个设备(我的 Kindle),但我得到了两个可拆卸的设备。我注意到的两个属性不同的是 /dev/sdb 有一个空白的“IdLabel”和“IdUsage”。这是我现在拥有的代码:
#!/usr/bin/python
import dbus
def get_devices():
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object('org.freedesktop.UDisks',
'/org/freedesktop/UDisks')
ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
proplist = []
for device in ud_manager.EnumerateDevices():
device_obj = bus.get_object('org.freedesktop.UDisks', device)
device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
proplist.append(device_props.GetAll('org.freedesktop.UDisks.Device'))
for device_props in proplist:
print '----------'
print device_props['IdLabel']
print device_props['DeviceFile']
print device_props['IdUsage']
if device_props['DriveCanDetach']:
print 'Device is detachable'
else:
print 'Device is not detachable'
get_devices()
我说的两个输出是:
Kindle
/dev/sdb1
filesystem
Device is detachable
----------
/dev/sdb
Device is detachable
udisk 的结果 --show-info /dev/sdb1
Showing information for /org/freedesktop/UDisks/devices/sdb1
native-path: /sys/devices/pci0000:00/0000:00:1c.3/0000:04:00.0/usb3/3-1/3
-1:1.0/host7/target7:0:0/7:0:0:0/block/sdb/sdb1
device: 8:17
device-file: /dev/sdb1
presentation: /dev/sdb1
by-id: /dev/disk/by-id/usb-Kindle_Internal_Storage_90D42204345600JR
-0:0-part1
by-id: /dev/disk/by-uuid/386D-5422
by-path: /dev/disk/by-path/pci-0000:04:00.0-usb-0:1:1.0-scsi-0:0:0:0-
part1
detected at: Sat 04 Jan 2014 03:56:35 PM EST
system internal: 0
removable: 0
has media: 1 (detected at Sat 04 Jan 2014 03:56:35 PM EST)
detects change: 0
detection by polling: 0
detection inhibitable: 0
detection inhibited: 0
is read only: 0
is mounted: 0
mount paths:
mounted by uid: 0
presentation hide: 0
presentation nopolicy: 0
presentation name:
presentation icon: multimedia-player
automount hint:
size: 1432346624
block size: 512
job underway: no
usage: filesystem
type: vfat
version: FAT32
uuid: 386D-5422
label: Kindle
partition:
part of: /org/freedesktop/UDisks/devices/sdb
scheme: mbr
number: 1
type: 0x0b
flags:
offset: 8192
alignment offset: 0
size: 1432346624
label:
uuid:
udisk 的结果 --show-info /dev/sdb
Showing information for /org/freedesktop/UDisks/devices/sdb
native-path: /sys/devices/pci0000:00/0000:00:1c.3/0000:04:00.0/usb3/3-1/3
-1:1.0/host7/target7:0:0/7:0:0:0/block/sdb
device: 8:16
device-file: /dev/sdb
presentation: /dev/sdb
by-id: /dev/disk/by-id/usb-Kindle_Internal_Storage_90D42204345600JR
-0:0
by-path: /dev/disk/by-path/pci-0000:04:00.0-usb-0:1:1.0-scsi-0:0:0:0
detected at: Sat 04 Jan 2014 03:56:34 PM EST
system internal: 0
removable: 1
has media: 1 (detected at Sat 04 Jan 2014 03:56:34 PM EST)
detects change: 1
detection by polling: 1
detection inhibitable: 1
detection inhibited: 0
is read only: 0
is mounted: 0
mount paths:
mounted by uid: 0
presentation hide: 0
presentation nopolicy: 0
presentation name:
presentation icon: multimedia-player
automount hint:
size: 1432354816
block size: 512
job underway: no
usage:
type:
version:
uuid:
label:
partition table:
scheme: mbr
count: 1
drive:
vendor: Kindle
model: Internal Storage
revision: 0100
serial: 90D42204345600JR
WWN:
detachable: 1
can spindown: 0
rotational media: Yes, unknown rate
write-cache: unknown
ejectable: 0
adapter: Unknown
ports:
similar devices:
media:
compat:
interface: usb
if speed: 480000000 bits/s
ATA SMART: not available
所以我想知道检查是否存在 IdLabel 和 IdUsage 是否足以假设某物是 USB 大容量存储设备。
【问题讨论】: