在断开连接之前正常关闭 USB 磁盘驱动器

在断开连接之前正常关闭 USB 磁盘驱动器

在 Fedora 27 中,当断开外部 USB 磁盘驱动器时,日志会记录如下行:

May 07 22:29:11 usb 2-3.1: USB disconnect, device number 23
May 07 22:29:11 sd 3:0:0:0: [sdb] Synchronizing SCSI cache
May 07 22:29:11 sd 3:0:0:0: [sdb] Synchronize Cache(10) failed:
                            Result: hostbyte=DID_NO_CONNECT driverbyte=DRIVER_OK

对此我们应该做什么呢?

为什么系统/内核在驱动器已断开连接后尝试同步缓存?

是否可以在断开连接之前正常关闭 USB 磁盘?例如,使用发出 Synchronize-Cache 命令然后关闭驱动器的命令。

这也许还会减少驱动器上的机械应力,因为旋转磁盘突然断电不一定是最佳选择。

编辑:Aneject /dev/sdb无效,即上述内核消息在设备拔出时仍然显示,并且磁盘继续旋转。相反,弹出命令会生成以下内核日志消息:

May 18 17:26:06  ldm_validate_partition_table(): Disk read failed.
May 18 17:26:06  Dev sdb: unable to read RDB block 0
May 18 17:26:06   sdb: unable to read partition table
May 18 17:26:06  ldm_validate_partition_table(): Disk read failed.
May 18 17:26:06  Dev sdb: unable to read RDB block 0
May 18 17:26:06   sdb: unable to read partition table

编辑:关闭磁盘电源确实udisksctl power-off --block-device /dev/sdb有效:

May 19 08:08:21  udisksd[9447]: Successfully sent SCSI command SYNCHRONIZE CACHE 
                                to /dev/sdb
May 19 08:08:21  udisksd[9447]: Successfully sent SCSI command START STOP UNIT
                                to /dev/sdb
May 19 08:08:21  kernel: sd 3:0:0:0: [sdb] Synchronizing SCSI cache
May 19 08:08:21  udisksd[9447]: Powered off /dev/sdb - successfully wrote
         to sysfs path /sys/devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.1/remove
May 19 08:08:21  kernel: usb 2-3.1: USB disconnect, device number 60

事实上,磁盘随后就会断电。

答案1

用于udisksctl关闭驱动器电源:

   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.

所以例如

udisksctl power-off --block-device /dev/sdb

您可以以普通用户身份运行该命令,无需 root 访问权限。
如果你喜欢图形用户界面,gnome disks有一个按钮“关闭该磁盘的电源”

相关内容