gPT fdisk

出自 Arch Linux 中文维基

GPT fdisk 是由 Rod Smith 編寫的一套用於硬盤分區的文本模式工具集,由 gdiskcgdisksgdiskfixparts 組成。它適用於 GUID(Globally Unique Identifier)分區表(GPT)硬盤,而不是過去常用的 MBR(Master Boot Record)分區表。

gdiskcgdisksgdisk 在功能上一致,但提供了不同類型的用戶界面。gdisk 提供文本交互模式,sgdisk 提供命令行模式,而 cgdisk 有基於 curses 的用戶界面。該篇文章涵蓋了 gdisk(8)sgdisk(8) 工具的使用。

提示:

安裝[編輯 | 編輯原始碼]

安裝 gptfdisk

列出分區[編輯 | 編輯原始碼]

要顯示塊設備上的分區表和分區信息,可以使用如下命令,其中設備名類似於 /dev/sda/dev/nvme0n1/dev/mmcblk0 等:

# gdisk -l /dev/sda

或者使用 sgdisk 命令:

# sgdisk -p /dev/sda

備份和恢復分區表[編輯 | 編輯原始碼]

修改磁盤前,請先備份磁盤的分區表,可以使用備份將相同的分區布局複製到多個設備上。

sgdisk 可以創建一個二進制備份,包含 MBR, GPT 主表頭,GPT 備份表頭和分區表副本。下面示例將 /dev/sda 的分區表信息備份到 sgdisk-sda.bin:

# sgdisk -b=sgdisk-sda.bin /dev/sda

通過下面命令恢復備份:

# sgdisk -l=sgdisk-sda.bin /dev/sda

如果要複製分區到其它磁盤,例如從 /dev/sda 複製到 /dev/sdc:

# sgdisk -R=/dev/sdc /dev/sda

如果兩個磁盤位於同一個計算機,使用下面命令設置隨機的分區 GUID:

# sgdisk -G /dev/sdc

創建分區表和分區[編輯 | 編輯原始碼]

分區操作的第一步是創建一個分區表。創建分區表後,就可以根據目標分區方案來創建分區了。

在開始之前,建議先備份當前的分區表和分區布局。

下面的步驟演示了如何使用 gdisk 來創建分區表和分區。另外,你也可以使用基於 curses 的 cgdisk,但下面的操作步驟就不再適用於你了。對應的用法可參考 cgdisk(8)

gdisk 自動將分區按 2048 個 512 字節扇區(1 MiB)對齊,這一設置兼容所有先進格式化 HDD 和大部分的 SSD

使用 gdisk時, 將要編輯的塊設備作為命令參數。以 /dev/sda 為例:

# gdisk /dev/sda

創建新分區表[編輯 | 編輯原始碼]

警告: 在已經保存數據的磁盤上創建新分區,會刪除磁盤上的所有數據。
提示:在分區前,先確認你的 NVMe 硬盤和先進格式化硬盤是否使用了最佳邏輯扇區大小

在提示中輸入 o 來創建新的 GPT 分區表並清除當前所有的分區數據。如果分區已經創建了你需要的分區表,請跳過此步驟。

創建分區[編輯 | 編輯原始碼]

在提示中輸入 n 命令來創建新分區。你需要提供分區編號,起始扇區位置,末端扇區位置及分區類型。

注意: 對於分區大小和位置的相關問題可參考分區#分區方案

分區編號[編輯 | 編輯原始碼]

分區編號是分配給分區的一個數字,例如:磁盤 /dev/sda 上編號為 1 的分區是 /dev/sda1,類似的有 /dev/nvme0n1 上的 /dev/nvme0n1p1/dev/mmcblk0 上的 /dev/mmcblk0p1。命名方案的細節可參考 Device file#Partition。分區編號不一定與分區在硬盤上的順序匹配,在這種情況下你可以對它們進行排序

建議使用 gdisk 提出的默認編號。

起始和末端扇區[編輯 | 編輯原始碼]

分區的起始和末端扇區可以用扇區數或是以千字節(K),兆字節(M),吉字節(G),太字節(T)或是皮字節(P)為單位的位置進行指定。

位置可以以用下方式進行指定:

  • 從硬盤起始端開始計算的絕對值,例如:40M 起始端意味着扇區從硬盤的第 40 MiB 開始。
  • +size-size 為格式的相對位置,例如:+2G 代指默認起始扇區後 2 GiB 的位置,而 -200M 代指最後一個可用扇區往前 200 MiB 的位置。

Pressing the Enter key with no input specifies the default value, which is the start of the largest available block for the first sector and the end of the same block for the last sector.

注意: 確保使用 +size{M,G,T,P} 格式的相對值指定分區大小,且不使用小於 1 MiB 的值,這樣分區就能始終和設備屬性對齊。
提示:為了防備需要創建 BIOS 啟動分區的情況發生,建議在硬盤前 2 TiB 的任意位置保留一個 1 MiB 大小的空餘空間(例如使用 +1M 參數創建首個分區)。

分區類型[編輯 | 編輯原始碼]

可以通過輸入 gdisk 內置類型編碼或手動指定分區類型 GUID 來選擇分區類型。默認的 Linux filesystem(GUID 0FC63DAF-8483-4772-8E79-3D69D8477DE4,gdisk 內置編碼 8300)類型適用於大多數用例。

提示:
常見分區類型
分區類型 掛載點 gdisk 編碼 分區類型 GUID
Linux 文件系統 Any 8300 0FC63DAF-8483-4772-8E79-3D69D8477DE4
EFI 系統分區 Any1 ef00 C12A7328-F81F-11D2-BA4B-00A0C93EC93B
BIOS 啟動分區 None ef02 21686148-6449-6E6F-744E-656564454649
Linux x86-64 根分區 (/) / 8304 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
Linux swap [SWAP] 8200 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
Linux /home /home 8302 933AC7E1-2EB4-4F13-B844-0E14E2AEF915
Linux /srv /srv 8306 3B8F8425-20E0-4F3B-907F-1A25A76F98E8
Linux /var /var1 8310 4D21B016-B534-45C2-A9FB-5C16E091FD2D
Linux /var/tmp /var/tmp1 8311 7EC6F557-3BC5-4ACA-B293-16EF5DF639D1
Linux LVM Any 8e00 E6D6D379-F507-44C2-A23C-238F2A3DF928
Linux RAID Any fd00 A19D880F-05FC-4D3B-A006-743F0F84911E
Linux LUKS Any 8309 CA7D7CCB-63ED-4C53-861C-1742536059CC
Linux dm-crypt Any 8308 7FFEC5C9-2D00-49B7-8941-3EA10A5586B7
  1. systemd-gpt-auto-generator(8) 只會在滿足特定條件的情況下自動掛載分區,詳細信息可參考 systemd#GPT分區自動掛載

重複這一步驟,直到創建完所有需要的分區。

寫入更改到硬盤[編輯 | 編輯原始碼]

提示:為了方便區分,可以使用 c 命令修改分區名稱(分區標籤)。

使用 w 命令來向硬盤寫入分區表並退出。

提示和技巧[編輯 | 編輯原始碼]

在 MBR 和 GPT 之間轉換[編輯 | 編輯原始碼]

提示:更多信息請閱讀 Rod Smith 的 MBR 與 GPT 互轉換說明

gdisksgdiskcgdisk 能將 MBR 和 BSD 盤符 無損轉換為 GPT。轉換時,所有 MBR 主分區和邏輯分區都會變成 GPT 分區,並為每個分區生成正確的分區類型 GUID 和唯一分區 GUID。

轉換之後,需要重新安裝並配置引導加載程序才能正常從 GPT 啟動。

警告:
  • GPT 在硬盤末端存放了第二分區表。該數據結構默認消耗 33 個 512 字節(16.5 KiB)扇區大小。MBR 在硬盤末端沒有類似的數據結構,也意味着如果 MBR 硬盤上的最後一個分區占用了硬盤的最末端,會導致轉換無法完成。如果你遇到了這種情況,需要放棄轉換,並調整最後一個分區的大小。

這篇文章的某些內容需要擴充。

原因: 提供該問題的相關引用。 (在 Talk:GPT fdisk 中討論)
  • 已知基於 Intel 芯片組並在 RAID 模式下運行的筆記本存在備份 GPT 表損壞問題,解決方法是儘可能使用 AHCI 替代 RAID。

要用 sgdisk 將 MBR 分區錶轉換為 GPT,使用 -g/--mbrtogpt 選項:

# sgdisk -g /dev/sda

要將 GPT 轉換為 MBR,使用 -m/--gpttombr 選項,注意最多只能轉換四個分區。

# sgdisk -m /dev/sda

Sort partitions[編輯 | 編輯原始碼]

This applies for when a new partition is created in the space between two partitions or a partition is deleted. /dev/sda is used in this example.

# sgdisk -s /dev/sda

After sorting the partitions if you are not using Persistent block device naming, it might be required to adjust the /etc/fstab and/or the /etc/crypttab configuration files.

注意: The kernel must read the new partition table for the partitions (e.g. /dev/sda1) to be usable. Reboot the system or tell the kernel to reread the partition table.

Recover GPT header[編輯 | 編輯原始碼]

In case main GPT header or backup GPT header gets damaged, you can recover one from the other with gdisk. /dev/sda is used in this example.

# gdisk /dev/sda

choose r for recovery and transformation options (experts only). From there choose either

  • b: use backup GPT header (rebuilding main)
  • d: use main GPT header (rebuilding backup)

When done write the table to disk and exit via the w command.

Expand a GPT disk[編輯 | 編輯原始碼]

After enlarging a disk (e.g. in hardware RAID or a virtual machine disk) the newly added free space will not be immediately usable since GPT keeps data at the end of the disk. You must relocate the backup GPT header to the new end of the disk.

Run sgdisk with the option -e/--move-second-header, e.g.:

# sgdisk -e /dev/sda

Afterwards print the partition table; the total free space should now be increased.

Prevent GPT partition automounting[編輯 | 編輯原始碼]

systemd-gpt-auto-generator(8) will automount partitions following the Discoverable Partitions Specification. Sometimes that may not be desirable.

The automounting can be disabled by setting the partition attribute 63 "do not automount" on the partition.

Start gdisk, e.g.:

# gdisk /dev/sda

Press p to print the partition table and take note of the partition number(s) of the for which you want to disable automounting.

Press x extra functionality (experts only).

Press a set attributes. Input the partition number and set the attribute 63. Under Set fields are: it should now show 63 (do not automount). Press Enter to end attribute changing. Repeat this for all partitions you want to prevent from automounting.

When done write the table to disk and exit via the w command.

Alternatively using sgdisk, the attribute can be set using the -A/--attributes= option; see sgdisk(8) for usage. For example, to set partition attribute 63 "do not automount" on /dev/sda2 run:

# sgdisk -A 2:set:63 /dev/sda

gdisk EFI application[編輯 | 編輯原始碼]

本文內容或本節內容已經過期。

原因: gdisk_x64.efi does not seem to work anymore and is apparently unmaintained. (在Talk:GPT fdisk討論)

There is no package for the EFI version of gdisk, but Rod Smith provides a prebuilt gdisk-1.04 EFI binary on SourceForge. Download gdisk-efi-*.zip and extract the archive. To use it, copy gdisk_x64.efi to the EFI system partition and launch it from your boot loader or UEFI Shell.

gdisk_x64.efi allows you to edit the partition table before the operating system is even booted. It is used the same way as the gdisk binary on Linux.

注意: gdisk_x64.efi cannot access the file system, thus it cannot backup the partition table to a file or restore it from a backup file.

See README-efi.txt for more information.

參閱[編輯 | 編輯原始碼]