leafee98-blog/content/essays/disk-uuid-partuuid-ptuuid.md
2022-12-12 21:52:08 +08:00

57 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "磁盘的 UUID, PARTUUID and PTUUID"
date: 2022-11-18T21:58:06+08:00
tags: []
categories: []
weight: 50
show_comments: true
draft: false
---
在 Linux 中是用 `fdisk``blkid` 可以看到磁盘的 3 种 UUID分别是 UUID、PARTUUID、PTUUID下面将介绍每一种 UUID 的意义。
<!--more-->
```
# fdisk --list-details /dev/sda
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD22EJRX-89B
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: BC266CC5-E480-164A-853B-875A82D7882B
First usable LBA: 2048
Last usable LBA: 3907029134
Alternative LBA: 3907029167
Partition entries starting LBA: 2
Allocated partition entries: 128
Partition entries ending LBA: 33
Device Start End Sectors Type-UUID UUID Name Attrs
/dev/sda1 4096 3907028991 3907024896 0FC63DAF-8483-4772-8E79-3D69D8477DE4 B967BEF5-A7A2-334C-A564-1360313A9BC8
# blkid --probe /dev/sda
/dev/sda: PTUUID="bc266cc5-e480-164a-853b-875a82d7882b" PTTYPE="gpt"
# blkid --probe /dev/sda1
/dev/sda1: LABEL="WDP" UUID="b9ea8994-e79c-4e79-92ea-2c97ca669023" UUID_SUB="87bbaa99-549b-43b9-98fd-55a8e896c195" BLOCK_SIZE="4096" TYPE="btrfs" USAGE="filesystem" PART_ENTRY_SCHEME="gpt" PART_ENTRY_UUID="b967bef5-a7a2-334c-a564-1360313a9bc8" PART_ENTRY_TYPE="0fc63daf-8483-4772-8e79-3d69d8477de4" PART_ENTRY_NUMBER="1" PART_ENTRY_OFFSET="4096" PART_ENTRY_SIZE="3907024896" PART_ENTRY_DISK="8:0"
```
从上面的输出可以看到几种 UUID
1. `fdisk` 输出的 `Disk identifier``blkid` 的 PTUUID 一致,是此分区表的唯一标识,一块磁盘只会在分区时得到一个这样的标识,它的作用是区分不同的磁盘。
2. `fdisk` 的 UUID 和 `blkid` 的 PART_ENTRY_UUID 一致,表示分区表中特定分区的唯一标识,此标识直接从分区表中读取得到。`fdisk` 无需对文件系统的支持就可以拿到这个 UUID即无论此分区是 `ext4` 还是 `btrfs` 又或是 `NTFS` 甚至尚未进行格式化,`fdisk` 只需要从 GPT 的分区表中就能获得这个唯一标识。
3. `blkid` 输出中有一项 `UUID``fdisk` 中没有提及的,它是文件系统级别的唯一标识,需要进入分区并文件系统的元数据中读取,也就是需要对 `ext4` 或者 `btrfs` 等文件系统的支持才能读取到。
除此之外还有一个 UUID 样式的 Type-UUID`blkid` 中是 PART_ENTRY_TYPE又被称为 GUID (Globally unique identifier),它用来记录分区的类型,或者只是提示此分区的用途,但是没有严格的要求(可能不准确)。
在 Linux 内核参数(如在 GRUB 或其他引导程序中指定要挂载根目录的分区)和 fstab如按需要挂载到目录树使用 UUID 的位置都指的是分区表中此分区的 UUID。
## 参考
1. https://unix.stackexchange.com/questions/375548/what-is-uuid-partuuid-and-ptuuid
2. https://en.wikipedia.org/wiki/GUID_Partition_Table