开发者

Ejecting USB devices on linux

开发者 https://www.devze.com 2023-02-12 09:56 出处:网络
I\'m using Udisks via dbus in python to unmount a usb device (in this case a Kindle), but I\'d like to send the eject signal to it, as this kicks the Kindle back from USB mode into its usual interface

I'm using Udisks via dbus in python to unmount a usb device (in this case a Kindle), but I'd like to send the eject signal to it, as this kicks the Kindle back from USB mode into its usual interface.

Udisks won'开发者_开发百科t let me DriveEject, as it's not an actual drive, and I gather this is a feature. The error given is:

dbus.exceptions.DBusException: org.freedesktop.UDisks.Error.Failed: Device is not a drive

The Udisks documentation mentions the ejectable property:

ID_DRIVE_EJECTABLE

Whether the media in the drive is physically ejectable. Only set this to 1 (or 0) if the drive truly uses (or doesn't) ejectable media. In particular, it is not necessary to set this for e.g. iPod or Kindle devices where it is necessary to send a command via eject(1) since the desktop user session will offer this option for removable devices regardless of whether they are ejectable. If this property is not set, a heuristic will be used to determine if the media is ejectable (drives using optical, Zip or Jaz media are considered ejectable).

but doesn't explain how to do this from the desktop user session.

(Nautilus lets me eject just fine from the desktop shell. The 'eject' shell command will unmount but won't send the eject signal to the device without sudo)

Is there any legitimate way to do this with Udisks, and if not, how does nautlius go about doing it?


So it turns out I was trying to eject the partition, not the drive itself, hence the "Device is not a drive error". (Which is to say, calling udisks eject on /dev/sdc1 rather than /dev/sdc)

The solution was to first unmount the partition using FilesystemUnmount(), then find the parent drive using the "PartitionSlave" property, and call DriveEject() on this.

In python with dbus, assuming you already have a device object from Udisks for the parition, this looks something like:

dev_if = dbus.Interface(device_object, 'org.freedesktop.UDisks.Device')
dev_if.FilesystemUnmount([])
device_props = dbus.Interface(device_object, dbus.PROPERTIES_IFACE)
drive = device_props.Get('org.freedesktop.UDisks.Device', "PartitionSlave")
drive_obj = self.bus.get_object("org.freedesktop.UDisks", drive)
drive_if = dbus.Interface(drive_obj, 'org.freedesktop.UDisks.Device')
drive_if.DriveEject([])
0

精彩评论

暂无评论...
验证码 换一张
取 消