In the last post from this series I’ve shown how to show some info about the UDisks daemon, now lets search for devices.
This first method that we’ll see is the EnumeraDevices
>>> import dbus
>>>
>>> bus = dbus.SystemBus()
>>> proxy = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
>>> iface = dbus.Interface(proxy, "org.freedesktop.UDisks")
>>>
>>> devs = iface.EnumerateDevices()
>>> print devs
dbus.Array([dbus.ObjectPath('/org/freedesktop/UDisks/devices/fd0'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdb'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sr0'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sda1'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sda2'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdc1'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdb1'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sda'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdb3'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdc'), dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdb2')], signature=dbus.Signature('o'))
>>> devs[0]
dbus.ObjectPath('/org/freedesktop/UDisks/devices/fd0')
>>> type(devs[0])
<type 'dbus.ObjectPath'>
>>> dir(devs[0])
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
The EnumerateDevices method is used to list all storage device connected to your PC. It does not take any arguments and return a list of dbus.ObjectPath. According to the DBus documentation an ObjectPath behaves like strings and doesn’t have any other utility besides uniquely identify an Object. By the dir(devs[0]) output you can see that it’s a mere string. This ObjectPath values will be usefull to get the volume object and the interface, we’ll see this in a near future.
The next method is the EnumerateDeviceFiles, which enumerate all device files (including symlinks):
>>> for i in iface.EnumerateDeviceFiles(): print i
...
/dev/fd0
/dev/sdb
/dev/disk/by-id/ata-SAMSUNG_HD161GJ_S14DJA0Z108748
/dev/disk/by-id/scsi-SATA_SAMSUNG_HD161GJS14DJA0Z108748
/dev/disk/by-id/wwn-0x50024e9201d01d37
/dev/disk/by-path/pci-0000:00:1f.5-scsi-1:0:0:0
/dev/sr0
/dev/disk/by-path/pci-0000:00:1f.5-scsi-0:0:0:0
/dev/sda1
/dev/disk/by-id/ata-MAXTOR_STM31000340AS_9QJ3JDHV-part1
/dev/disk/by-id/scsi-SATA_MAXTOR_STM31000_9QJ3JDHV-part1
/dev/disk/by-id/wwn-0x5000c500113ce6fc-part1
/dev/disk/by-uuid/659c06c3-9b37-4be3-915b-545998d37f7a
/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part1
/dev/sda2
/dev/disk/by-id/ata-MAXTOR_STM31000340AS_9QJ3JDHV-part2
/dev/disk/by-id/scsi-SATA_MAXTOR_STM31000_9QJ3JDHV-part2
/dev/disk/by-id/wwn-0x5000c500113ce6fc-part2
/dev/disk/by-uuid/eba43f19-65f0-45e0-b24e-68db4b3fbe19
/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part2
/dev/sdc1
/dev/disk/by-id/usb-Kingston_DataTraveler_2.0_5B7616A2C81C-0:0-part1
/dev/disk/by-uuid/48D5-32CD
/dev/disk/by-path/pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0-part1
Some of this output is trunked.
The next useful method is EnumerateDeviceByDeviceFile, which finds a device ObjectPath based in a file:
>>> iface.FindDeviceByDeviceFile('/dev/sda1')
dbus.ObjectPath('/org/freedesktop/UDisks/devices/sda1')
>>> iface.FindDeviceByDeviceFile('/dev/disk/by-uuid/48D5-32CD')
dbus.ObjectPath('/org/freedesktop/UDisks/devices/sdc1')
>>> iface.FindDeviceByDeviceFile('/error/error')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.6/dbus/proxies.py", line 140, in __call__
**keywords)
File "/usr/lib/pymodules/python2.6/dbus/connection.py", line 620, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.UDisks.Error.Failed: No such device
>>>
As shown above, it raises a DBus Exception in case the file doesn’t refer to a device.
Next is the FindDeviceByMajorMinor which, given the major and minor device number (more informations about it here) returns the device ObjectPath:
>>> import os
>>> info = os.stat('/dev/sda1')
>>> iface.FindDeviceByMajorMinor(os.major(info.st_rdev), os.minor(info.st_rdev))
dbus.ObjectPath('/org/freedesktop/UDisks/devices/sda1')
>>>
There are many other methods in this interface but this are the ones that you’ll use the most. For all the other ones take a look at the documentation here.
Comments
comments powered by Disqus