Linux

在斷開連接之前優雅地關閉 USB 磁碟驅動器

  • May 19, 2018

在 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 命令然後使驅動器減速的命令。

這也許還會減少驅動器上的機械應力,因為旋轉磁碟的突然斷電不一定是最佳的。

**編輯:**一個eject /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

實際上,磁碟隨後會斷電。

用於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 訪問權限。

如果你更喜歡 gui,gnome disks有一個按鈕來*“關閉這個磁碟”*。

引用自:https://unix.stackexchange.com/questions/444611