解決macOS High Sierra使用dd命令向USB裝置拷貝資料非常緩慢的問題
最近在使用Raspberry Pi Zero W
,在建立系統映象的時候,使用如下命令,發現非常緩慢,時間往往以小時計算:
$ diskutil unmountDisk /dev/disk2 $ dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/disk2
如果要解決這個問題,那麼可以使用如下方式:
$ diskutil unmountDisk /dev/rdisk2 $ dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/rdisk2
注意兩個命令中的區別,一個是/dev/disk2
,一個是/dev/rdisk2
,兩者的區別可以通過如下命令來檢視:
$ man hdiutil
可以看到如下介紹:
.......................... DEVICE SPECIAL FILES Since any /dev entry can be treated as a raw disk image, it is worth not- ing which devices can be accessed when and how. /dev/rdisk nodes are character-special devices, but are "raw" in the BSD sense and force block-aligned I/O. They are closer to the physical disk than the buffer cache. /dev/disk nodes, on the other hand, are buffered block-special devices and are used primarily by the kernel's filesystem code. It is not possible to read from a /dev/disk node while a filesystem is mounted from it, but anyone with read access to the appropriate /dev/rdisk node can use hdiutil verbs such as fsid or pmap with it. Beware that information read from a raw device while a filesystem is mounted may not be consistent because the consistent data is stored in memory or in the filesystem's journal. The DiskImages framework will attempt to use authopen(1) to open any device which it can't open (due to EACCES) for reading with open(2). Depending on session characteristics, this behavior can cause apparent hangs while trying to access /dev entries while logged in remotely (an authorization panel is waiting on console). Generally, the /dev/disk node is preferred for imaging devices (e.g. convert or create -srcdevice operations), while /dev/rdisk is usable for the quick pmap or fsid. In particular, converting the blocks of a mounted journaled filesystem to a read-only image will prevent the volume in the image from mounting (the journal will be permanently dirty). ................................
根據介紹,rdisk
屬於原始裝置(rawdisk
),不必經過作業系統的檔案系統緩衝處理,相當於直接操作硬體,速度非常快。但是像macOS High Sierra
這種出現20x
速度差別的情況,就不是太好理解了。
參考連結
- macOS Sierra : dd to USB is very slow and can't seem to use /dev/rdisk
- macOS High Sierra (10.13)製作Windows+7/">Windows 7安裝U盤
- Why is “/dev/rdisk” about 20 times faster than “/dev/disk” in Mac OS X