Lately I’ve been using DeviceKit via DBus in Python as part of my work. The DeviceKit documentation is actually pretty good but I have found that it lacks examples. I’ve had trouble finding much via Google either so I thought it might be nice to post up some short blog articles as I find things out.
I have a need to be able to partition USB compact flash cards and here’s an example that works for me, broken up so I can give some explanation as I go:
import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.DeviceKit.Disks', '/org/freedesktop/DeviceKit/Disks')
iface = dbus.Interface(proxy, "org.freedesktop.DeviceKit.Disks")
cur_dev = bus.get_object('org.freedesktop.DeviceKit.Disks', '/org/freedesktop/DeviceKit/Disks/devices/sdb')
device_int = dbus.Interface(cur_dev, 'org.freedesktop.DeviceKit.Disks.Device')
Lines 3, 4 and 5 are getting and interface to DeviceKit from DBus. Lines 4 and 5 may not be necessary but are useful to have if you don’t know the device name yet and wan tot enumerate through the devices and see what’s there.
Lines 7 and 8 are getting an interface to a specific device, which in this case is a USB card reader on /dev/sdb. I have a 1Gig SanDisk compact flash card, without any partitions, plugged into the card reader.
Now that there is a device interface, I can start using it to partition that card:
device_int.PartitionTableCreate('none', [])
device_int.PartitionTableCreate('mbr', [])
device_int.PartitionCreate(0, 1019215872, '06', '', [], [], 'vfat', [])
device_int.PartitionTableCreate() can take 2 arguments: scheme and options. For more information on the schemes and options allowed see this page as it does a better job than I will at explaining. Here I’ve opted, mainly to just help with the example, to call it twice; once with ‘none’ as the scheme and once with ‘mbr’. The ‘none’ scheme will clear anything that happened to be there before and ‘mbr’ sets up the master boot record at the start of the disk.
Line 3 shows an actual partition being created. The arguments are as follows: offset, size, type, label, flags, options, fstype, fsoptions). The above makes a FAT32 partition, just shy of 1Gig in size.
Normally this would be the end as the partition has been created. However, if you wan to removed partitions the following may be helpful:
cur_dev = bus.get_object('org.freedesktop.DeviceKit.Disks', '/org/freedesktop/DeviceKit/Disks/devices/sdb1')
sdb1_dev = dbus.Interface(cur_dev, 'org.freedesktop.DeviceKit.Disks.Device')
sdb1_dev.PartitionDelete('')
Line 1 gets an interface to the device /dev/sdb1, which is the partition that I have just created and wish to delete. As before, an interface is required to work on the device. Line 2 retrieves one.
Finally the delete is preformed using PartitionDelete(). The disk should now have no partitions.
I’ve only just started using DeviceKit so apologies for missing things or getting things wrong. I’d more than welcome any corrections and tips!
There’s lots of information on DeviceKit Devices which can be found here and I would recommend it as reading for anyone who needs to work with it.