更改 GPT 分区的类型代码

为什么要更改硬盘分区的类型代码?事情的起因是这样的。
Ventoy 是一个制作可启动 U 盘的开源工具,但是有个问题,它创建的 EFI 分区,格式为 FAT16,分区类型代码却是0700(正确的应该是 EF00)。macOS 上因为会自动挂载该 EFI 分区, 如果同时插入两个 U 盘,则第二个会无法正确挂载。
做了一些搜索后,我选择 GPT fdisk。它是开源软件(GPL),而且跨平台,支持 Linux, macOS, Windows。
准备工具
以 macOS 举例,下载 gdisk-1.0.10.pkg。安装后即可使用 gdisk,注意需要使用 root 权限执行。
# /dev/disk2 为目标硬盘
sudo gdisk /dev/disk2
输入 ? 查看使用帮助
Command (? for help): ?
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu
修改分区类型代码
具体步骤如下。
- p查看硬盘分区表(主要目的是确认是否选对了硬盘)。
- t修改分区类型代码。
- 输入分区编号 2。需要按实际情况输入,可以根据分区表中的 Size 和 Name 区分。
- 输入分区类型编码 ef00。ef00 表示的是 EFI 分区,输入L可查看所有分区编码。
- p查看硬盘分区表(确认一下修改是否正确)。
- w写入分区表并退出
- y确认执行。
至此,修改完成。操作示例如下。
sudo gdisk /dev/disk2
Command (? for help): p
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       976707591   465.7 GiB   0700  Ventoy
   2       976707592       976773127   32.0 MiB    0700  VTOYEFI
Command (? for help): t
Partition number (1-2): 2
Current type is 700 (Microsoft basic data)
Hex code or GUID (L to show codes, Enter = 700): ef00
Changed type of partition to 'EFI system partition'
Command (? for help): p
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       976707591   465.7 GiB   0700  Ventoy
   2       976707592       976773127   32.0 MiB    EF00  VTOYEFI
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/disk2.
各操作系统的硬盘选择方法
# N for disk number
# macOS
gdisk /dev/diskN
# Windows
# Terminal 需要以管理员权限模式执行
# PhysicalDriveN,N 表示硬盘编号,从0开始
gdisk64.exe \\.\PhysicalDriveN
# Linux
gdisk /dev/sdN
# FreeBSD
# adN or daN
gdisk /dev/adN
阅读其它文章