最近在使用Raspberry Pi Zero W
,在创建系统镜像的时候,使用如下命令,发现非常缓慢,时间往往以小时计算:
1 2 3 |
$ diskutil unmountDisk /dev/disk2 $ sudo dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/disk2 |
如果要解决这个问题,那么可以使用如下方式:
1 2 3 |
$ diskutil unmountDisk /dev/rdisk2 $ sudo dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/rdisk2 bs=1m |
注意两个命令的区别,一个是 /dev/disk2
,一个是 /dev/rdisk2
, 两者的区别可以通过如下命令来查看:
1 |
$ man hdiutil |
可以看到如下介绍:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
.......................... 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
速度差别的情况,就不是太好理解了。
后面 bs=1m
参数也很重要,要求拷贝写入的时候整块 (1MB
) 写入(否则是逐个字节操作,写入次数非常多,性能很差),这样才能起到加速作用。