修改磁盘和分区的UUID

我在 使用tar备份和恢复树莓派系统 过程中,需要修改SD卡的磁盘分区UUID:

  • 原先 树莓派5使用nvme存储启动 时是通过 dd 命令clone磁盘的,导致NVMe磁盘和SD卡的磁盘UUID和PARTUUID完全一致

  • 当需要从SD卡启动时,两个磁盘UUID一致会导致即使设置了SD卡启动也会将NVMe的分区挂载为根分区(因为NVMe性能更快,所以相应UUID挂载会先于SD卡)

fdisk 修改msdos分区UUID

fdisk 提供了 x 命令 ( export ),在这个命令下一级子命令提供了 i 命令来修订分区ID:

fdisk修订msdos分区ID
# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/mmcblk0: 59.48 GiB, 63864569856 bytes, 124735488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x57a11afa

Device         Boot   Start       End   Sectors  Size Id Type
/dev/mmcblk0p1         8192   1056767   1048576  512M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      1056768 124735487 123678720   59G 83 Linux

Command (m for help): x

Expert command (m for help): i

Enter the new disk identifier: 0x57a11afb

Disk identifier changed from 0x57a11afa to 0x57a11afb.

Expert command (m for help): r

Command (m for help): w

The partition table has been altered.
Syncing disks.

gdisk 修改GPT分区UUID

备注

这段我还没有实践,以后有机会试试

gdisk修订GPT分区ID
$ sudo gdisk /dev/sda
[sudo] password for mook: 
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): x                                       # enter x to change to experts menu

Expert command (? for help): c                                # enter c to change PARTUUID
Partition number (1-2): 2                                     # enter the number of the partition you want to change
Enter the partition's new unique GUID ('R' to randomize): r 
New GUID is 76349364-D66C-4C19-B422-237A0D2DB9F5

Expert command (? for help): m                                # enter m to go back to main menu

Command (? for help): w                                       # enter w to write the change to disk

Command (? for help): q                                       # enter q to exit gdisk
$

tune2fs 修改磁盘UUID

备注

这段我还没有实践,以后有机会试试

tune2fs修订磁盘UUID
tune2fs -U $(uuidgen) /dev/sdb1

参考