คำสั่ง gdisk: การตั้งค่าพาร์ทิชันบน Linux และการสร้าง ลบพาร์ทิชัน GPT

สภาพแวดล้อมทดสอบตระกูล RHELAlmaLinux 9.8บัญชี root
คำสั่งดิสก์ · การเมานต์gdisk 1.0.7ตำแหน่ง/usr/sbin/gdisk

เมื่อต้องการแบ่งดิสก์แบบ GPT ให้สร้างและลบพาร์ทิชันด้วยคำสั่ง gdisk เมื่อรัน gdisk ตามด้วยชื่อไฟล์อุปกรณ์ หน้าจอแบบโต้ตอบจะเปิดขึ้น แล้วตั้งค่าพาร์ทิชันบน Linux โดยพิมพ์ออปชันตัวอักษรเดียว เช่น p เพื่อดูรายการพาร์ทิชัน และ d เพื่อลบพาร์ทิชัน

gdisk disk.img

ผลลัพธ์ของ gdisk ในบทความนี้ได้จาก disk.img ซึ่งเป็นไฟล์อิมเมจสำหรับฝึกขนาด 3 GiB ที่สร้างด้วย truncate บนดิสก์จริงให้ใส่ชื่อไฟล์อุปกรณ์ เช่น /dev/sdb แทน disk.img

ลำดับงานจนถึงการเมานต์มีดังนี้

  • ลำดับการตั้งค่าพาร์ทิชัน Linux
  • ระบุชื่อไฟล์อุปกรณ์ที่ต้องการแก้ไขข้อมูลพาร์ทิชัน แล้วรันคำสั่ง
  • แสดงรายการพาร์ทิชัน
  • ลบพาร์ทิชันที่ไม่ต้องการ
  • สร้างพาร์ทิชันใหม่
  • บันทึกข้อมูลพาร์ทิชัน (w) แล้วออก

ก่อนบันทึกด้วย w งานที่ทำกับพาร์ทิชันจะยังไม่ถูกเขียนลงดิสก์

1️⃣ ตั้งค่าพาร์ทิชันบน Linux ด้วยคำสั่ง gdisk

gdisk เป็นยูทิลิตีจัดการพาร์ทิชันสำหรับตารางพาร์ทิชัน GPT เนื่องจากแบบ MBR ใช้ดิสก์ที่ใหญ่กว่า 2 TiB ได้ไม่เต็มความจุ ดิสก์ขนาดใหญ่ตั้งแต่ 2 TB ขึ้นไปจึงแบ่งด้วย 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 filesystem

1. หน้าจอเริ่มต้นของ gdisk

นี่คือหน้าจอพื้นฐานที่แสดงหลังรัน gdisk บน disk.img ว่างที่ยังไม่มีตารางพาร์ทิชัน

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. ออปชัน p ของ gdisk: ดูรายการพาร์ทิชัน

ด้านล่างคือรายการที่ดูด้วย p หลังสร้างและบันทึกพาร์ทิชันสามพาร์ทิชัน (1 GiB, swap 512 MiB และส่วนที่เหลือ 1.5 GiB) บน disk.img เดียวกัน

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คำอธิบาย
DiskSectors และความจุทั้งหมดของดิสก์
Sector sizeขนาดของเซกเตอร์ตรรกะหนึ่งเซกเตอร์ (ในบทความนี้คือ 512 bytes)
Disk identifierแสดงข้อมูล GUID
Numberหมายเลขพาร์ทิชัน
Start (sector)เซกเตอร์เริ่มต้นของพาร์ทิชัน
End (sector)เซกเตอร์สุดท้ายของพาร์ทิชัน
Sizeขนาดพาร์ทิชัน
Codeแสดงประเภทพาร์ทิชันเป็นรหัส
Nameการใช้งานของพาร์ทิชัน เปลี่ยนชื่อได้ด้วยคำสั่ง c

3. สร้างพาร์ทิชันด้วย gdisk

เมื่อพิมพ์ n โปรแกรมจะถามหมายเลขพาร์ทิชัน เซกเตอร์เริ่มต้น และเซกเตอร์สุดท้ายตามลำดับ ด้านล่างเป็นตัวอย่างบน disk.img ที่มีพาร์ทิชันหมายเลข 1 อย่างเดียว โดยปล่อยหมายเลขและเซกเตอร์เริ่มต้นเป็นค่าเริ่มต้น แล้วใส่ +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: 

รหัสประเภทพาร์ทิชันคือ 8200 สำหรับพาร์ทิชัน swap และ 8300 สำหรับระบบไฟล์ Linux ให้ใส่รหัสนี้เพื่อตั้งประเภท

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] เมื่อถูกถามหมายเลข ให้ใส่หมายเลขพาร์ทิชันที่จะลบ ถ้ามีพาร์ทิชันเดียว จะลบทันทีโดยไม่ถามหมายเลข

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 จากตารางออปชันด้วยชิปหรือคีย์บอร์ด เพื่อดูว่าช่องหน่วยความจำและช่องดิสก์ก่อนบันทึกต่างกันอย่างไร

ตัวจำลอง gdiskดิสก์สำหรับฝึก disk.img · 3 GiB
[root@server ~]#
ทำงานเฉพาะในเบราว์เซอร์และไม่เขียนอะไรลงดิสก์จริง ก่อนบันทึกด้วย w การเปลี่ยนแปลงจะอยู่แค่ในหน่วยความจำ และจะหายไปถ้าออกด้วย q

2️⃣ ตัวอย่างการใช้คำสั่ง gdisk ในงานจริง

  • ↪️ งานพาร์ทิชันและข้อผิดพลาดที่ลองบนอิมเมจสำหรับฝึก

ขั้นแรกสร้างอิมเมจสำหรับฝึกสองไฟล์ด้วย truncate ผลลัพธ์ในบทนี้ได้จากอิมเมจ disk.img ขนาด 3 GiB ที่สร้างใหม่ และอิมเมจ data.img ขนาด 1 GiB และคุณสามารถพิมพ์ลำดับเดียวกันนี้ลงใน ตัวจำลองคำสั่ง gdiskได้โดยตรง

truncate -s 3G disk.img
truncate -s 1G data.img

🔲 ตัวอย่างการสร้างและลบพาร์ทิชันด้วย gdisk

รัน gdisk บน disk.img ว่าง แล้วใช้ n สร้างพาร์ทิชัน 1 GiB และพาร์ทิชัน swap 512 MiB ตามลำดับ หมายเลขและเซกเตอร์เริ่มต้นรับค่าเริ่มต้นด้วย 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 อีกครั้ง ใส่ 2 ที่ d ตรวจด้วย 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

ข้อผิดพลาดที่มักเกิดเมื่อใช้ครั้งแรกคือออกด้วย q โดยไม่ใช้ w บน disk.img ที่มีพาร์ทิชันหมายเลข 1 อย่างเดียว ได้เพิ่มพาร์ทิชัน swap ด้วย n และตรวจด้วย p จนเห็นพาร์ทิชันหมายเลข 2 แล้วลองออกด้วย q โดยไม่ใส่ w

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] ก่อนเริ่มงาน ด้านล่างคือผลลัพธ์ของคำสั่งเดียวกันบนอิมเมจ data.img ขนาด 1 GiB ซึ่งแยกจาก disk.img ได้จากบรรทัด [Disk data.img: 2097152 sectors, 1024.0 MiB] และรายการพาร์ทิชันที่ว่างอยู่

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

หลังสร้างหรือลบพาร์ทิชัน จำนวนบรรทัดของรายการพาร์ทิชันและบรรทัด [Total free space] ในผลลัพธ์ของ p จะเปลี่ยนไปพร้อมกัน ทันทีหลังสร้างพาร์ทิชันสองพาร์ทิชันเป็น [Total free space is 3145661 sectors (1.5 GiB)] และหลังลบพาร์ทิชันหมายเลข 2 ขนาด 512 MiB เพิ่มเป็น [Total free space is 4194237 sectors (2.0 GiB)]

ช่อง Code แสดงรหัสประเภทตามที่ใส่ไว้ พาร์ทิชันหมายเลข 1 ที่รับค่าเริ่มต้นด้วย Enter เป็น 8300 และพาร์ทิชันหมายเลข 2 ที่ใส่ 8200 เป็น 8200

การบันทึกแล้วหรือยังดูได้จากใต้ [Partition table scan] ในหน้าจอแรกของ gdisk ด้วย อิมเมจว่างก่อนบันทึกแสดง [GPT: not present] และหลังบันทึกด้วย w แสดงบรรทัด [GPT: present] และ [Found valid GPT with protective MBR; using GPT.]

🔢 FAQ & เนื้อหาแนะนำ

เมื่อพิมพ์ q โปรแกรม gdisk จะปิดโดยไม่เขียนการเปลี่ยนแปลงลงดิสก์ การเปลี่ยนแปลงด้วย n หรือ d อยู่แค่ในหน่วยความจำจนกว่าจะพิมพ์ w จึงถูกเขียนลงดิสก์ ถ้าทำผิดให้ออกด้วย q แล้วเริ่มใหม่

ตามบรรทัด [Warning: The kernel is still using the old partition table.] ในผลลัพธ์ของ w หลังบันทึกทันทีเคอร์เนลยังใช้ตารางพาร์ทิชันเดิมต่อไป ตารางใหม่จะมีผลหลังรีบูต หรือหลังรัน partprobe หรือ kpartx

gdisk จัดตำแหน่งเริ่มต้นของพาร์ทิชันเป็นหน่วยละ 2048 เซกเตอร์ และบรรทัด [Partitions will be aligned on 2048-sector boundaries] ในผลลัพธ์ของ p คือการตั้งค่านี้ ดังนั้นเซกเตอร์ 34~2047 จึงว่าง และพื้นที่ว่าง 2014 เซกเตอร์บน disk.img ที่สร้างสามพาร์ทิชันก็คือตรงนี้

🔗 เนื้อหาแนะนำ
ขั้นต่อไป การตั้งค่าหน่วยความจำ swap (Swap Memory): 2 วิธี พาร์ทิชัน & ไฟล์ | คำสั่ง swapon, mkswap บทความที่สร้างพื้นที่ swap ซึ่งใช้พื้นที่ดิสก์เหมือนหน่วยความจำ ด้วยสองวิธีคือพาร์ทิชัน swap และไฟล์ swap แล้วใช้งานด้วย mkswap และ swapon อ่านบทความ → เมานต์หน่วยความจำ swap ด้วยคำสั่ง swapon

ℹ️ ประกาศเรื่องลิงก์แอฟฟิลิเอต
เนื้อหาของเว็บไซต์นี้มีลิงก์แอฟฟิลิเอต หากผู้เข้าชมซื้อสินค้าหรือบริการผ่านลิงก์เหล่านี้ เว็บไซต์นี้จะได้รับค่าคอมมิชชันจากผู้ขาย ในกระบวนการนี้ ราคาที่ผู้ซื้อจ่าย(ราคาจะลดลงเมื่อมีโปรโมชัน↓)จะไม่สูงขึ้นแต่อย่างใด ราคา ส่วนลด และข้อมูลสต็อกที่แสดงเป็นข้อมูล ณ เวลาที่เขียนและอาจต่างจากความเป็นจริง โปรดตรวจสอบกับผู้ขายอีกครั้งก่อนซื้อ การเลือกและการประเมินสินค้าเขียนตามเกณฑ์ของเราเอง และการได้รับค่าคอมมิชชันหรือไม่ไม่มีผลต่อลำดับการแนะนำหรือเนื้อหาการประเมิน

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *

Prove your humanity: 3   +   5   =