1. 首页 > Linux教程 > 正文

Linux教程FG134-gdisk命令GPT磁盘分区

内容大纲

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

1. gdisk命令概述

gdisk命令是Linux系统中用于管理GPT分区表的工具,它是fdisk命令的GPT版本,支持创建、删除、修改GPT分区等操作。

from PG视频:www.itpux.com

# 安装gdisk
# dnf install -y gdisk

# 基本语法
# gdisk [选项] 设备

# 常用选项
-l:列出磁盘的分区表

# 查看GPT磁盘
# gdisk -l /dev/sda

# 进入交互式模式
# gdisk /dev/sda

2. 基本分区操作

以下是使用gdisk命令进行基本分区操作的步骤:

# 1. 进入gdisk交互式模式
# gdisk /dev/sda

GPT fdisk (gdisk) version 1.0.9

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

Creating new GPT entries.

# 2. 查看帮助信息
Command (? for help): ?

b back up GPT data to a file
c change a partition’s name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition’s type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu

# 3. 创建新分区
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-209715166, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): ef00
Changed type of partition to ‘EFI System’

# 4. 创建第二个分区
Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-209715166, default = 1050624) or {+-}size{KMGTP}:
Last sector (1050624-209715166, default = 209715166) or {+-}size{KMGTP}: +100G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):

# 5. 查看分区结果
Command (? for help): p
Disk /dev/sda: 1073741824 sectors, 512.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 12345678-90AB-CDEF-1234-567890ABCDEF
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 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 838860800 sectors (400.0 GiB)

Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System
2 1050624 210450431 100.0 GiB 8300 Linux filesystem

# 6. 保存并退出
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 /dev/sda.
The operation has completed successfully.

3. GPT分区特点

GPT分区表相比MBR分区表有以下特点:

# GPT分区特点

# 1. 支持更大的磁盘容量
# 查看GPT磁盘信息
# gdisk -l /dev/sda

Disk /dev/sda: 1073741824 sectors, 512.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 12345678-90AB-CDEF-1234-567890ABCDEF
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 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 838860800 sectors (400.0 GiB)

# 2. 支持更多的分区
# 创建多个分区
# gdisk /dev/sda

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-209715166, default = 210450432) or {+-}size{KMGTP}:
Last sector (210450432-209715166, default = 209715166) or {+-}size{KMGTP}: +100G

Command (? for help): n
Partition number (4-128, default 4): 4
First sector (34-209715166, default = 419837952) or {+-}size{KMGTP}:
Last sector (419837952-209715166, default = 209715166) or {+-}size{KMGTP}: +100G

Command (? for help): n
Partition number (5-128, default 5): 5
First sector (34-209715166, default = 629225472) or {+-}size{KMGTP}:
Last sector (629225472-209715166, default = 209715166) or {+-}size{KMGTP}:

Command (? for help): p

Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System
2 1050624 210450431 100.0 GiB 8300 Linux filesystem
3 210450432 419837951 100.0 GiB 8300 Linux filesystem
4 419837952 629225471 100.0 GiB 8300 Linux filesystem
5 629225472 209715166 700.0 GiB 8300 Linux filesystem

Command (? for help): w

4. 分区类型管理

gdisk命令支持管理GPT分区的类型,通过类型代码来指定分区的用途。

# 查看可用的分区类型
# gdisk /dev/sda

Command (? for help): l

0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM
a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap
a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
aa00 Apple HFS/HFS+ ab00 Apple boot af00 Apple RAID
af01 Apple RAID offline af02 Apple labels af03 Apple TV recovery
af04 Apple Core Storage be00 Solaris boot bf00 Solaris root
bf01 Solaris /usr & Apple ZFS bf02 Solaris swap bf03 Solaris backup
bf04 Solaris /var bf05 Solaris /home bf06 Solaris alternate sector
bf07 Solaris reserved 1 bf08 Solaris reserved 2 bf09 Solaris reserved 3
bf0a Solaris reserved 4 bf0b Solaris reserved 5 c001 HP-UX data
c002 HP-UX service ef00 EFI System ef01 MBR partition scheme
ef02 BIOS boot f800 Ceph OSD f801 Ceph DM-Metadata
f802 Ceph journal ffff Unknown

# 修改分区类型
Command (? for help): t
Partition number (1-5): 5
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to ‘Linux LVM’

Command (? for help): p

Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System
2 1050624 210450431 100.0 GiB 8300 Linux filesystem
3 210450432 419837951 100.0 GiB 8300 Linux filesystem
4 419837952 629225471 100.0 GiB 8300 Linux filesystem
5 629225472 209715166 700.0 GiB 8E00 Linux LVM

Command (? for help): w

5. 实战案例

下面通过一个完整的实战案例来演示如何使用gdisk命令进行GPT磁盘分区。

# 场景:新添加了一块1TB的磁盘,需要创建GPT分区并配置

# 1. 查看新磁盘
# gdisk -l /dev/sdb

Disk /dev/sdb: 2097152000 sectors, 1000.2 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 00000000-0000-0000-0000-000000000000
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 2097151966
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097151933 sectors (1000.2 GiB)

# 2. 创建GPT分区表
# gdisk /dev/sdb

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

# 3. 创建EFI系统分区(如果需要)
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-2097151966, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-2097151966, default = 2097151966) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): ef00
Changed type of partition to ‘EFI System’

# 4. 创建根分区
Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-2097151966, default = 1050624) or {+-}size{KMGTP}:
Last sector (1050624-2097151966, default = 2097151966) or {+-}size{KMGTP}: +500G

# 5. 创建数据分区
Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-2097151966, default = 1049645056) or {+-}size{KMGTP}:
Last sector (1049645056-2097151966, default = 2097151966) or {+-}size{KMGTP}: +400G

# 6. 创建交换分区
Command (? for help): n
Partition number (4-128, default 4): 4
First sector (34-2097151966, default = 1887461376) or {+-}size{KMGTP}:
Last sector (1887461376-2097151966, default = 2097151966) or {+-}size{KMGTP}:

Command (? for help): t
Partition number (1-4): 4
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to ‘Linux swap’

# 7. 查看分区结果
Command (? for help): p

Disk /dev/sdb: 2097152000 sectors, 1000.2 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 12345678-90AB-CDEF-1234-567890ABCDEF
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 2097151966
Partitions will be aligned on 2048-sector boundaries
Total free space is 0 sectors (0 bytes)

Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System
2 1050624 1049645055 500.0 GiB 8300 Linux filesystem
3 1049645056 1887461375 400.0 GiB 8300 Linux filesystem
4 1887461376 2097151966 100.0 GiB 8200 Linux swap

# 8. 保存并退出
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 /dev/sdb.
The operation has completed successfully.

# 9. 格式化分区
# mkfs.xfs /dev/sdb2
# mkfs.xfs /dev/sdb3
# mkswap /dev/sdb4

# 10. 挂载分区
# mkdir -p /mnt/root /mnt/data
# mount /dev/sdb2 /mnt/root
# mount /dev/sdb3 /mnt/data
# swapon /dev/sdb4

# 11. 验证挂载
# df -h
# free -h

风哥经验总结

1. gdisk命令是管理GPT分区表的专用工具,相比fdisk,它支持更大的磁盘容量和更多的分区。

风哥提示:

2. 对于大于2TB的磁盘,必须使用GPT分区表,否则无法充分利用磁盘空间。

学习交流加群风哥微信: itpux-com

3. 如果系统使用UEFI启动,必须创建EFI系统分区(类型代码EF00)。

4. 在创建分区时,gdisk会自动进行分区对齐,这有助于提高磁盘性能。

更多学习教程公众号风哥教程itpux_com

5. GPT分区表有备份,这提高了分区表的可靠性,减少了数据丢失的风险。

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息