Search code examples
macosshelldisk

Command line to unmount all external drive on OS X


I am trying to write a shell script to unmount/mount all external drive on OS X.

My disk list is as follows:

$ 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

So, I write a script like:

#!/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

I'd like to know the followings:

1.Is there a way to identify external drive without using diskutil info? The reason is diskutil info starts up sleeping drive, my script is very slow.

2.Are there any much more smart way? I have Googled so much, but I can't find a way which I want. Hope to help me...


Solution

  • You could try using system_profiler with different data types. I don't think it wakes up the drives. system_profiler SPUSBDataType will give you all USB devices and their mount points, and system_profiler SPFireWireDataType does the same for FireWire devices. system_profiler -listDataTypes will give you all data types. Once you parse out the mount points, you can unmount those drives.

    It's not perfectly portable across different systems to just do USB then FireWire then Thunderbolt because there could be different connections. To try everything, you could do system_profiler (which lists all data) then search for anything following "Mount Point:" and unmount whatever is after that.

    This is an example of the data it prints out for a USB drive:

    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