在 Linux 上弹出 USB 设备

发布于 2024-10-18 20:00:25 字数 853 浏览 1 评论 0原文

我在 python 中通过 dbus 使用 Udisk 来卸载 USB 设备(在本例中是 Kindle),但我想向它发送弹出信号,因为这会将 Kindle 从 USB 模式恢复到其常用接口。

Udisks 不允许我 DriveEject,因为它不是一个实际的驱动器,我认为这是一个功能。给出的错误是:

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

Udisks 文档提到了可弹出属性:

ID_DRIVE_EJECTABLE

驱动器中的介质是否 物理上可弹出。只需将此设置为 1(或0),如果驱动器真正使用(或 没有)可弹出媒体。在 特别是,不需要设置 这适用于 iPod 或 Kindle 设备 需要发送一个 从桌面通过弹出(1)命令 用户会话将提供此选项 对于可移动设备,无论 它们是否可弹出。如果这个 属性未设置,启发式意志 用于确定媒体是否 可弹出(使用光盘驱动器、Zip 或 Jaz 媒体均被考虑 可弹出)。

但没有解释如何从桌面用户会话执行此操作。

(Nautilus 让我可以从桌面 shell 中弹出。“eject”shell 命令将卸载,但不会在没有 sudo 的情况下向设备发送弹出信号)

是否有任何合法的方法可以使用 Udisk 执行此操作,如果没有,如何操作诺特留斯会去做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2024-10-25 20:00:25

所以事实证明我试图弹出分区,而不是驱动器本身,因此“设备不是驱动器错误”。 (也就是说,在/dev/sdc1而不是/dev/sdc上调用udisks弹出)

解决方案是首先使用FilesystemUnmount()卸载分区,然后使用“PartitionSlave”属性找到父驱动器,并调用DriveEject( )对此。

在带有 dbus 的 python 中,假设您已经有一个来自 Udisk 的设备对象用于分区,这看起来像:

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([])

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([])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文