gpt4 book ai didi

python - 如何使用 dbus 判断 USB 大容量存储设备是否已插入

转载 作者:行者123 更新时间:2023-12-01 05:28:11 26 4
gpt4 key购买 nike

我正在制作一个简单的 python 程序来安装和卸载闪存驱动器,这样我就不必做更多的事情,而只需查看列表并选择我想要安装的内容。现在,我有每个设备的对象和属性的列表。我想知道的是我应该查看哪些属性来判断设备是否是 USB 大容量存储设备。我的第一 react 是查看“可拆卸”,但是我只插入了一个设备(我的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

udisks --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:

udisks --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 大容量存储设备。

最佳答案

如果您可以使用 UDisks2 DBus 接口(interface),答案将非常简单:-)首先,似乎 UDisks2(可能还有 UDisks1,但我还没有安装)仅处理通过 USB 连接的大容量存储设备,完全忽略 USB 连接的 MTP 设备和其他类型的 USB 设备,例如打印机和鼠标。看看:

现在我连接了 6 个 USB 设备:

alex@galene ~ $ /usr/sbin/lsusb | grep -v 'root hub'Bus 001 Device 004: ID 058f:6362 Alcor Micro Corp. Flash Card Reader/WriterBus 001 Device 003: ID 09da:000a A4 Tech Co., Ltd Optical Mouse Opto 510DBus 008 Device 090: ID 04e8:6860 Samsung Electronics Co., Ltd GT-I9100 Phone [Galaxy S II], GT-P7500 [Galaxy Tab 10.1]Bus 008 Device 075: ID 04b8:0007 Seiko Epson Corp. PrinterBus 009 Device 002: ID 058f:6387 Alcor Micro Corp. Flash DriveBus 009 Device 088: ID 1058:1010 Western Digital Technologies, Inc. Elements External HDD

058f:6362 是一款“一体化”读卡器,当前已插入 SD 卡。058f:6387 和 1058:1010 是真正的 USB 大容量存储设备,分别是闪存和外部 HDD。 04e8:6860 是三星 Galaxy Note1(MTP 设备),其余的是鼠标和打印机。

我还有一个内置 SATA 驱动器。

这是当前的 udisksctl status 输出(您可能会看到 Galaxy Note 丢失):

MODEL                     REVISION  SERIAL               DEVICE--------------------------------------------------------------------------Hitachi HDT725040VLA360   V5COA7EA  VFM301R3196R5F       sda     Generic Flash Disk        8.07      8D0147DF             sdb     WDC WD1200BEVS-22RST0     04.01G04  WD-WXE108V72690      sdc     Generic USB CF Reader     1.01      058F312D81B          sde     Generic USB SM Reader     1.02      058F312D81B          sdf     Generic USB MS Reader     1.03      058F312D81B          sdg     Generic USB SD Reader     1.00      058F312D81B          sdd

为了区分可插拔 USB 大容量存储设备和固定驱动器,可以检查“ConnectedBus”和“可移动”属性。请注意,还有另一个属性“MediaRemovable”,它对应于设备的物理介质。以下脚本选择可移动 USB 大容量存储设备并打印其“MediaRemovable”属性

alex@galene ~ $ cat <<EOF | python -> import dbus> bus = dbus.SystemBus()> ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2')> om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')> for k,v in om.GetManagedObjects().iteritems():>     drive_info = v.get('org.freedesktop.UDisks2.Drive', {})>     if drive_info.get('ConnectionBus') == 'usb' and drive_info.get('Removable'):>         print("Device Path: %s, MediaRemovable: %s" % (k, drive_info['MediaRemovable']))> block_devices =  [''.join(chr(c) for c in device[dbus.String(u'org.freedesktop.UDisks2.Block')][dbus.String(u'PreferredDev‌​ice')] if chr(c) != '\x00' ) for device in om.GetManagerObjects().values() if dbus.String(u'org.freedesktop.UDisks2.Block') in device]> EOFDevice Path: /org/freedesktop/UDisks2/drives/Generic_USB_SM_Reader_058F312D81B, MediaRemovable: 1Device Path: /org/freedesktop/UDisks2/drives/Generic_Flash_Disk_8D0147DF, MediaRemovable: 1Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_SD_Reader_058F312D81B, MediaRemovable: 1Device Path: /org/freedesktop/UDisks2/drives/WDC_WD1200BEVS_22RST0_WD_WXE108V72690, MediaRemovable: 0Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_CF_Reader_058F312D81B, MediaRemovable: 1Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_MS_Reader_058F312D81B, MediaRemovable: 1

基本上它解决了您想要解决的问题。有传言说有不可移动 USB 连接的存储设备,但我还没有见过。

关于python - 如何使用 dbus 判断 USB 大容量存储设备是否已插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com