没有 sudo 就无法弹出 USB 驱动器?

没有 sudo 就无法弹出 USB 驱动器?

我尝试在命令行中弹出 USB 驱动器,eject /dev/sdg1但出现以下错误:

eject: unable to open `/dev/sdg'

该命令卸载了驱动器,但并未将其弹出。但是,使用 sudo 运行却没有问题。这是错误还是预期行为?我正尝试将其合并到 Python 应用程序中,因此 sudo 并不是一个真正的选项。请注意,umount没有 sudo 也可以正常工作。

这是 USB 闪存盘。我认为它上面只有一个分区

答案1

没有弹出 USB 设备的机制。使用 卸载它(在您的情况下)umount /dev/sdg1并手动删除它。(假设 /dev/sdg1 是 /dev/sdg 上唯一安装的分区)。您可以使用类似命令验证设备上安装了哪些分区lsblkmount | grep sdX其中 sdX 对应于您的设备(在您的情况下为 sdg)。

eject专门用于可移动介质,例如光盘、软盘、磁带、ZIP、JAZ 驱动器,并且完全不支持闪存驱动器。

资料来源:

man eject

答案2

我认为 USB 驱动器中的“弹出”意味着卸载和关闭电源,就像某些 GUI 工具(带有弹出符号)所实现的那样。

您可以查看udisks和工具udisksctl

阅读手册

man udisks
man udisksctl

例如,你会发现命令

   udisksctl mount {--object-path OBJECT | --block-device DEVICE}
             [--filesystem-type TYPE] [--options OPTIONS...]
             [--no-user-interaction]

   udisksctl unmount {--object-path OBJECT | --block-device DEVICE} [--force]
             [--no-user-interaction]

   udisksctl power-off {--object-path OBJECT | --block-device DEVICE}
             [--no-user-interaction]

我觉得

udisksctl unmount --block-device <device> && \
udisksctl power-off --block-device <device>

命令可以做你想做的事。在这种情况下,'sdg' 中的一个分区<device>/dev/sdg1

   power-off
       Arranges for the drive to be safely removed and powered off. On the OS
       side this includes ensuring that no process is using the drive, then
       requesting that in-flight buffers and caches are committed to stable
       storage. The exact steps for powering off the drive depends on the
       drive itself and the interconnect used. For drives connected through
       USB, the effect is that the USB device will be deconfigured followed by
       disabling the upstream hub port it is connected to.

       Note that as some physical devices contain multiple drives (for example
       4-in-1 flash card reader USB devices) powering off one drive may affect
       other drives. As such there are not a lot of guarantees associated with
       performing this action. Usually the effect is that the drive disappears
       as if it was unplugged.

相关内容