gdisk 指令:Linux 分割區設定與 GPT 分割區建立、刪除
以 GPT 方式分割磁碟時,用 gdisk 指令建立和刪除分割區。在 gdisk 後面加上裝置檔名稱執行,就會開啟互動畫面,再輸入單一字母的選項來進行 Linux 分割區設定,例如用 p 查看分割區清單、用 d 刪除分割區。
gdisk disk.img文中的 gdisk 輸出是在用 truncate 建立的 3 GiB 練習用映像檔 disk.img 上執行的結果;在實際磁碟上,請把 disk.img 換成 /dev/sdb 這類裝置檔名稱。
掛載前的作業順序如下。
在用 w 儲存之前,對分割區所做的作業都不會寫入磁碟。
1️⃣ 用 gdisk 指令設定 Linux 分割區
gdisk 是處理 GPT 分割表的分割區管理工具。MBR 方式無法用完大於 2TiB 的磁碟,因此 2TB 以上的大容量磁碟會用 GPT 分割,這時就用 gdisk 或 parted 進行分割區作業。
gdisk -[選項] [目標 裝置 檔案]已設定的分割區資訊,可以在 gdisk 或 fdisk 指令後加上 -l 選項來查看。
gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
3 3147776 6291422 1.5 GiB 8300 Linux filesystem1. gdisk 執行畫面
這是在沒有分割表的空白 disk.img 上執行 gdisk 後出現的基本畫面。
gdisk disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help):2. gdisk p 選項:查看分割區清單
以下是在同一個 disk.img 上建立並儲存三個分割區(1 GiB、swap 512 MiB、其餘 1.5 GiB)後,用 p 查看的清單。
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
3 3147776 6291422 1.5 GiB 8300 Linux filesystem| p 輸出項目 | 說明 |
|---|---|
| Disk | Sectors 及磁碟的總容量 |
| Sector size | 一個邏輯磁區的大小(本文為 512 bytes) |
| Disk identifier | 顯示 GUID 資訊。 |
| Number | 分割區編號 |
| Start (sector) | 分割區起始磁區 |
| End (sector) | 分割區結束磁區 |
| Size | 分割區容量 |
| Code | 以代碼顯示分割區類型 |
| Name | 分割區的用途。可用 c 指令變更名稱。 |
3. 用 gdisk 建立分割區
輸入 n 後,會依序詢問分割區編號、起始磁區和結束磁區。以下是在只有 1 號分割區的 disk.img 上,編號和起始磁區保留預設值,結束磁區輸入 +512M 建立 2 號分割區的範例。
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)分割區類型可以用 L 指令查看。
Hex code or GUID (L to show codes, Enter = 8300): L
Type search string, or <Enter> to show all codes:
0700 Microsoft basic data 0701 Microsoft Storage Replica
0702 ArcaOS Type 1 0c01 Microsoft reserved
2700 Windows RE 3000 ONIE boot
3001 ONIE config 3900 Plan 9
4100 PowerPC PReP boot 4200 Windows LDM data
4201 Windows LDM metadata 4202 Windows Storage Spaces
7501 IBM GPFS 7f00 ChromeOS kernel
7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem
8301 Linux reserved 8302 Linux /home
8303 Linux x86 root (/) 8304 Linux x86-64 root (/)
8305 Linux ARM64 root (/) 8306 Linux /srv
8307 Linux ARM32 root (/) 8308 Linux dm-crypt
8309 Linux LUKS 830a Linux IA-64 root (/)
830b Linux x86 root verity 830c Linux x86-64 root verity
830d Linux ARM32 root verity 830e Linux ARM64 root verity
830f Linux IA-64 root verity 8310 Linux /var
8311 Linux /var/tmp 8312 Linux user's home
8313 Linux x86 /usr 8314 Linux x86-64 /usr
8315 Linux ARM32 /usr 8316 Linux ARM64 /usr
8317 Linux IA-64 /usr 8318 Linux x86 /usr verity
Press the <Enter> key to see more codes, q to quit: 分割區類型代碼中,swap 分割區是 8200,Linux 檔案系統是 8300,輸入這個代碼即可設定類型。
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'建立的分割區用 w 寫入磁碟,詢問是否繼續時輸入 Y。
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 disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the 下一個 reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.4. 用 gdisk 刪除分割區
用 [d] 選項刪除分割區。詢問編號時輸入要刪除的分割區編號;若只有 1 個分割區,則不會詢問編號而直接刪除。
Command (? for help): d
Partition number (1-3): 3🔲 gdisk 指令選項
| 選項 | 複製 | 說明 |
|---|---|---|
| -l | 輸出分割區清單後立即結束 | |
| p | 顯示磁碟的分割區清單 | |
| n | 建立新分割區(依序輸入編號、起始磁區、結束磁區) | |
| L | 在詢問類型代碼的欄位顯示分割區類型清單 | |
| 8200 | 輸入分割區類型代碼(n、t 的類型欄位) | |
| w | 將分割區資訊儲存到磁碟後結束 | |
| d | 刪除分割區 |
| 選項 | 複製 | 說明 |
|---|---|---|
| c | 變更分割區名稱 | |
| l | 以清單顯示分割區類型 | |
| q | 不儲存分割區資訊直接結束 | |
| t | 設定分割區類型(包含變更既有分割區的類型) | |
| s | 排序分割區 | |
| ? | 以清單輸出操作指令 |
🔲 gdisk 指令模擬器
在下方模擬器執行 gdisk disk.img,並用按鈕或鍵盤輸入選項表中的 p、n、d、w,即可確認儲存前的記憶體欄和磁碟欄有什麼不同。
2️⃣ gdisk 指令實務範例
首先用 truncate 建立兩個練習用映像檔。本章的輸出取自新建的 3 GiB 映像檔 disk.img 和 1 GiB 映像檔 data.img,相同的步驟也可以在 gdisk 指令模擬器中直接輸入試試。
truncate -s 3G disk.img
truncate -s 1G data.img🔲 gdisk 建立與刪除分割區範例
在空白的 disk.img 上執行 gdisk,用 n 依序建立 1 GiB 分割區和 512 MiB 的 swap 分割區。編號和起始磁區按 Enter 採用預設值,結束磁區輸入 +1G 和 +512M,第二個分割區的類型代碼輸入 8200。
gdisk disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +1G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'輸入 w 之前,先用 p 確認清單。兩個分割區都以輸入的大小和類型代碼建立,空閒空間顯示為 1.5 GiB。
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap確認清單後,輸入 w 和 Y 寫入磁碟。
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 disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the 下一個 reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.刪除也按照相同順序進行。再次執行 gdisk,在 d 輸入 2,用 p 確認只剩 1 號分割區後,再用 w 儲存。
Command (? for help): d
Partition number (1-2): 2
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem🔲 使用 gdisk 時常見的錯誤
初次使用時常犯的錯誤,是沒有 w 就用 q 結束。在只有 1 號分割區的 disk.img 上用 n 新增 swap 分割區,並用 p 確認到 2 號分割區後,不輸入 w 而用 q 結束。
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
Command (? for help): q再用 [gdisk -l] 確認時,2 號分割區不見了,只剩 1 號分割區。即使是 p 清單中出現的分割區,也要用 w 儲存才會寫入磁碟。
gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem指定到其他磁碟的錯誤,可以在作業前先用 [gdisk -l] 確認大小和分割區清單來避免。以下是對 1 GiB 映像檔 data.img 執行相同指令的輸出,從 [Disk data.img: 2097152 sectors, 1024.0 MiB] 這一行和空白的分割區清單,就能和 disk.img 區分。
gdisk -l data.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Disk data.img: 2097152 sectors, 1024.0 MiB
Sector size (logical): 512 bytes
Disk identifier (GUID): E8F9E93B-4299-41F3-8985-37973CBAEFFF
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 2097118
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)
Number Start (sector) End (sector) Size Code Name🔲 確認 gdisk 輸出的變化
建立或刪除分割區後,p 輸出中分割區清單的行數和 [Total free space] 這一行會一起改變。剛建立兩個分割區時是 [Total free space is 3145661 sectors (1.5 GiB)],刪除 512 MiB 的 2 號分割區後增加為 [Total free space is 4194237 sectors (2.0 GiB)]。
Code 欄顯示的就是輸入的類型代碼:按 Enter 採用預設值的 1 號分割區是 8300,輸入 8200 的 2 號分割區是 8200。
是否已儲存,也可以從 gdisk 第一個畫面的 [Partition table scan] 下方分辨。儲存前的空白映像檔顯示 [GPT: not present],用 w 儲存後則出現 [GPT: present] 和 [Found valid GPT with protective MBR; using GPT.] 兩行。
🔢 FAQ & 推薦內容
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。