【问题标题】:Command line to unmount all external drive on OS X在 OS X 上卸载所有外部驱动器的命令行
【发布时间】:2014-10-29 15:09:06
【问题描述】:

我正在尝试编写一个 shell 脚本来卸载/挂载 OS X 上的所有外部驱动器。

我的磁盘列表如下:

$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage                         499.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           *496.3 GB   disk1
                                 Logical Volume on disk0s2
                                 AE949253-803C-49DC-93EA-7DFC74C2EAA2
                                 Unencrypted
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS Time Machine            999.9 GB   disk2s2
/dev/disk3
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *750.2 GB   disk3
   1:                        EFI EFI                     209.7 MB   disk3s1
   2:       Microsoft Basic Data USB HD 4                749.9 GB   disk3s2
/dev/disk4
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk4
   1:                        EFI EFI                     209.7 MB   disk4s1
   2:       Microsoft Basic Data Backup                  499.9 GB   disk4s2
/dev/disk5
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk5
   1:                        EFI EFI                     209.7 MB   disk5s1
   2:                  Apple_HFS USB HD 1                999.9 GB   disk5s2
/dev/disk6
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *4.0 TB     disk6
   1:                        EFI EFI                     209.7 MB   disk6s1
   2:                  Apple_HFS USB HD 2                4.0 TB     disk6s2
/dev/disk8
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *122.9 GB   disk8
   1:        Apple_partition_map                         32.3 KB    disk8s1
   2:             Apple_Driver43                         28.7 KB    disk8s2
   3:             Apple_Driver43                         28.7 KB    disk8s3
   4:           Apple_Driver_ATA                         28.7 KB    disk8s4
   5:           Apple_Driver_ATA                         28.7 KB    disk8s5
   6:             Apple_FWDriver                         262.1 KB   disk8s6
   7:         Apple_Driver_IOKit                         262.1 KB   disk8s7
   8:              Apple_Patches                         262.1 KB   disk8s8
   9:                  Apple_HFS Windows                 122.9 GB   disk8s9

所以,我写了这样一个脚本:

#!/bin/sh

diskutil list | grep -oE 'disk[0-9]s2' | while read i
do
  Prtl=`diskutil info ${i} | grep Protocol | cut -d ' ' -f 21`
  if [ ${Prtl} = USB ]; then
    diskutil unmount ${i}
  fi
done

我想知道以下几点:

1.有没有不使用diskutil信息来识别外部驱动器的方法?原因是 diskutil info 启动睡眠驱动器,我的脚本很慢。

2.还有更聪明的方法吗?我用谷歌搜索了很多,但我找不到我想要的方法。希望能帮到我...

【问题讨论】:

标签: macos shell disk


【解决方案1】:

不会解决硬盘旋转延迟,但您也可以这样写:

find /dev -name "disk[1-9]" -exec diskutil eject {} \;

或者,这个脚本对我来说效果很好:

osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'

【讨论】:

  • 这个答案当之无愧,感谢苹果脚本。
【解决方案2】:

您可以尝试将system_profiler 用于不同的数据类型。我认为它不会唤醒驱动器。 system_profiler SPUSBDataType 将为您提供所有 USB 设备及其挂载点,system_profiler SPFireWireDataType 为 FireWire 设备提供相同的功能。 system_profiler -listDataTypes 将为您提供所有数据类型。解析出挂载点后,您可以卸载这些驱动器。

仅使用 USB、FireWire 和 Thunderbolt 并不能完美地在不同系统之间移植,因为可能存在不同的连接。要尝试所有操作,您可以执行system_profiler(列出所有数据)然后搜索“Mount Point:”之后的任何内容,然后卸载之后的任何内容。

这是它为 USB 驱动器打印的数据示例:

1394A/USB2.0/eSATA combo drive:

          Product ID: 0x000a
          Vendor ID: 0x0928  (Oxford Semiconductor Ltd.)
          Version: 0.00
          Serial Number: 2009100700000936
          Speed: Up to 480 Mb/sec
          Manufacturer: PI-208
          Location ID: 0xfd500000 / 3
          Current Available (mA): 500
          Current Required (mA): 0
          Capacity: 1 TB (1,000,204,886,016 bytes)
          Removable Media: Yes
          Detachable Drive: Yes
          BSD Name: disk5
          Partition Map Type: GPT (GUID Partition Table)
          S.M.A.R.T. status: Not Supported
          Volumes:
            disk5s1:
              Capacity: 209.7 MB (209,715,200 bytes)
              BSD Name: disk5s1
              Content: EFI
            Clone:
              Capacity: 650.2 GB (650,200,002,560 bytes)
              Available: 101.49 GB (101,489,717,248 bytes)
              Writable: Yes
              File System: Journaled HFS+
              BSD Name: disk5s2
              Mount Point: /Volumes/Clone
              Content: Apple_HFS
              Volume UUID: F42D6E05-C72C-386A-86AD-635A818E1FE3
            LocalBackup:
              Capacity: 339.53 GB (339,526,688,768 bytes)
              Available: 53.88 GB (53,881,970,688 bytes)
              Writable: Yes
              File System: Journaled HFS+
              BSD Name: disk5s3
              Mount Point: /Volumes/LocalBackup
              Content: Apple_HFS
              Volume UUID: 04974080-CB76-3CC3-BC37-274241D1BC0F
            Yosemite:
              Capacity: 9.35 GB (9,350,000,640 bytes)
              Available: 9.31 GB (9,311,920,128 bytes)
              Writable: Yes
              File System: Journaled HFS+
              BSD Name: disk5s4
              Mount Point: /Volumes/Yosemite
              Content: Apple_HFS
              Volume UUID: AE809679-3C2A-3BE4-950F-551663791CE3

【讨论】:

  • 非常感谢。我会采纳你的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2013-08-08
  • 2011-12-30
  • 2014-08-24
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多