记录下在MacOS中制作Linux USB启动盘的操作步骤。

操作步骤

查看磁盘挂载分区

使用命令diskutil list查看所在U盘的分区,找到U盘的挂载点,此处挂载点为/dev/disk2

1
2
3
4
5
6
7
8
9
$ diskutil list
...
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *31.0 GB disk2
1: DOS_FAT_32 UNTITLED 31.0 GB disk2s1
$

卸载U盘挂载

使用diskutil unmountDisk命令,卸载U盘的挂载。

1
2
3
$ diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful
$

如果不卸载挂载点就写入启动盘,则会提示dd: /dev/disk2: Resource busy

使用dd来来写入iso

使用dd命令来将CentOS写入启动盘,

1
sudo dd if=~/carl_workSpace/software/os/CentOS-7-x86_64-DVD-1810.iso of=/dev/rdisk2 bs=1m

注意:

  1. 此处~/carl_workSpace/software/os/CentOS-7-x86_64-DVD-1810.iso是我本地CentOS的路径,需要替换成实际的路径
  2. /dev/rdisk2就是上面diskutil list列出的U盘挂载点, 并且注意,此处磁盘前面多了个r,是rdisk2,而不是disk2rdisk2disk2的原始盘,目的是可以更快速的写入。

写入需要花费几分钟时间,期间可以使用CTRL + T来查看写入进度,显示如下:

1
2
3
109+0 records in
108+0 records out
113246208 bytes transferred in 7.430910 secs (15239884 bytes/sec)

也可以使用iostat来查看磁盘写入进度

1
2
3
4
5
6
7
8
$ iostat -w 5
disk0 disk2 cpu load average
KB/t tps MB/s KB/t tps MB/s us sy id 1m 5m 15m
42.68 14 0.58 849.97 0 0.00 7 4 89 3.84 3.42 2.67
450.16 15 6.50 1024.00 7 7.19 3 3 94 3.70 3.39 2.67
85.34 124 10.33 1024.00 9 8.80 6 4 90 3.64 3.39 2.67
$

最终完成后,dd命令输出:

1
2
3
4376+0 records in
4376+0 records out
4588568576 bytes transferred in 539.126637 secs (8511115 bytes/sec)

写入完成后,Macos会有一个提示框,提示“此电脑不能读取您插入的磁盘。”
disk_was_not_readable.png

USB启动盘不能被Macos正常读取,但是可以用来被当作启动盘安装CentOS。

使用diskutil list可以查看到此时U盘的分区信息。

1
2
3
4
5
6
7
8
$ diskutil list
...
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *31.0 GB disk2
1: 0xEF 8.9 MB disk2s2
$

弹出U盘

使用“磁盘工具”APP或者命令diskutil eject弹出U盘。

1
diskutil eject /dev/disk2

延伸

Macos中/dev/disk和/dev/rdisk的区别

首先查看man hdiutil的描述:

Since any /dev entry can be treated as a raw disk image, it is worth noting 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.

/dev/rdisk是原始读取模式,没有经过文件系统的文件缓存机制,因此速度比/dev/disk速度更快。

下面以918M大小的CentOS-7-x86_64-Minimal-1810.iso为例来比较/dev/rdisk/dev/disk的写入速度。两者的命令分别为

1
2
3
4
5
6
7
8
9
10
11
# 写入/dev/rdisk的速度
$ sudo dd if=CentOS-7-x86_64-Minimal-1810.iso of=/dev/rdisk2 bs=1m
918+0 records in
918+0 records out
962592768 bytes transferred in 106.192945 secs (9064564 bytes/sec)
# 写入/dev/disk的速度
sudo dd if=CentOS-7-x86_64-Minimal-1810.iso of=/dev/disk2 bs=1m
918+0 records in
918+0 records out
962592768 bytes transferred in 3016.605565 secs (319098 bytes/sec)

可以看到写入/dev/rdisk花费了106秒,而写入/dev/disk花费了3016秒, 差别巨大。

Reference

留言