1. 首页 > Linux教程 > 正文

Linux教程FG133-fdisk命令MBR磁盘分区

内容大纲

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

1. fdisk命令概述

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

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

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

# 常用选项
-l:列出所有磁盘的分区表
-u:以扇区为单位显示分区大小

# 查看所有磁盘
# fdisk -l

# 查看特定磁盘
# fdisk -l /dev/vdb

# 进入交互式模式
# fdisk /dev/vdb

2. 基本分区操作

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

# 1. 进入fdisk交互式模式
# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

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

Help:

DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag

Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)

Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes

# 3. 查看当前分区表
Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00000000

# 4. 创建新分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +20G

Created a new partition 1 of type ‘Linux’ and of size 20 GiB.

# 5. 查看分区结果
Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x12345678

Device Boot Start End Blocks Id System
/dev/vdb1 2048 41945087 20971520 83 Linux

# 6. 保存并退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3. 主分区与扩展分区

MBR分区表最多支持4个主分区,或者3个主分区加1个扩展分区。扩展分区可以包含多个逻辑分区。

# 创建3个主分区和1个扩展分区
# fdisk /dev/vdb

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +10G

Created a new partition 1 of type ‘Linux’ and of size 10 GiB.

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (20973568-104857599, default 20973568):
Last sector, +sectors or +size{K,M,G,T,P} (20973568-104857599, default 104857599): +10G

Created a new partition 2 of type ‘Linux’ and of size 10 GiB.

Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3-4, default 3): 3
First sector (41946112-104857599, default 41946112):
Last sector, +sectors or +size{K,M,G,T,P} (41946112-104857599, default 104857599): +10G

Created a new partition 3 of type ‘Linux’ and of size 10 GiB.

Command (m for help): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): e
Partition number (4, default 4): 4
First sector (62918656-104857599, default 62918656):
Last sector, +sectors or +size{K,M,G,T,P} (62918656-104857599, default 104857599):

Created a new partition 4 of type ‘Extended’ and of size 20 GiB.

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x12345678

Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
/dev/vdb2 20973568 41946111 10486272 83 Linux
/dev/vdb3 41946112 62918655 10486272 83 Linux
/dev/vdb4 62918656 104857599 20969472 5 Extended

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

4. 逻辑分区创建

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

在扩展分区内可以创建多个逻辑分区。

# 在扩展分区内创建逻辑分区
# fdisk /dev/vdb

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (62920704-104857599, default 62920704):
Last sector, +sectors or +size{K,M,G,T,P} (62920704-104857599, default 104857599): +5G

Created a new partition 5 of type ‘Linux’ and of size 5 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (73402368-104857599, default 73402368):
Last sector, +sectors or +size{K,M,G,T,P} (73402368-104857599, default 104857599): +5G

Created a new partition 6 of type ‘Linux’ and of size 5 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 7
First sector (83884032-104857599, default 83884032):
Last sector, +sectors or +size{K,M,G,T,P} (83884032-104857599, default 104857599):

Created a new partition 7 of type ‘Linux’ and of size 10 GiB.

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x12345678

Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
/dev/vdb2 20973568 41946111 10486272 83 Linux
/dev/vdb3 41946112 62918655 10486272 83 Linux
/dev/vdb4 62918656 104857599 20969472 5 Extended
/dev/vdb5 62920704 73402367 5240832 83 Linux
/dev/vdb6 73404416 83886079 5240832 83 Linux
/dev/vdb7 83888128 104857599 10484736 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

5. 分区删除与修改

学习交流加群风哥QQ113257174

使用fdisk命令可以删除和修改现有分区。

# 删除分区
# fdisk /dev/vdb

Command (m for help): d
Partition number (1-7, default 7): 7

Partition 7 has been deleted.

Command (m for help): d
Partition number (1-6, default 6): 6

Partition 6 has been deleted.

# 修改分区类型
Command (m for help): t
Partition number (1-5, default 5): 5
Hex code or alias (type L to list all): 82

Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x12345678

Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
/dev/vdb2 20973568 41946111 10486272 83 Linux
/dev/vdb3 41946112 62918655 10486272 83 Linux
/dev/vdb4 62918656 104857599 20969472 5 Extended
/dev/vdb5 62920704 73402367 5240832 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

6. 实战案例

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

# 场景:新添加了一块50GB的磁盘,需要创建分区并挂载

# 1. 查看新磁盘
# fdisk -l /dev/vdb

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00000000

# 2. 创建分区
# fdisk /dev/vdb

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +20G

Created a new partition 1 of type ‘Linux’ and of size 20 GiB.

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (41945088-104857599, default 41945088):
Last sector, +sectors or +size{K,M,G,T,P} (41945088-104857599, default 104857599): +20G

Created a new partition 2 of type ‘Linux’ and of size 20 GiB.

Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (3-4, default 3): 3
First sector (83888128-104857599, default 83888128):
Last sector, +sectors or +size{K,M,G,T,P} (83888128-104857599, default 104857599):

Created a new partition 3 of type ‘Extended’ and of size 10 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (83890176-104857599, default 83890176):
Last sector, +sectors or +size{K,M,G,T,P} (83890176-104857599, default 104857599): +5G

Created a new partition 5 of type ‘Linux’ and of size 5 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (94373760-104857599, default 94373760):
Last sector, +sectors or +size{K,M,G,T,P} (94373760-104857599, default 104857599):

Created a new partition 6 of type ‘Linux’ and of size 5 GiB.

Command (m for help): t
Partition number (1-6, default 6): 5
Hex code or alias (type L to list all): 82

Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x12345678

Device Boot Start End Blocks Id System
/dev/vdb1 2048 41945087 20971520 83 Linux
/dev/vdb2 41945088 83888127 20971520 83 Linux
/dev/vdb3 83888128 104857599 10484736 5 Extended
/dev/vdb5 83890176 94373759 5241792 82 Linux swap / Solaris
/dev/vdb6 94375808 104857599 5240896 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 3. 格式化分区
# mkfs.xfs /dev/vdb1
# mkfs.xfs /dev/vdb2
# mkfs.xfs /dev/vdb6
# mkswap /dev/vdb5

# 4. 挂载分区
# mkdir -p /data1 /data2 /data3
# mount /dev/vdb1 /data1
# mount /dev/vdb2 /data2
# mount /dev/vdb6 /data3
# swapon /dev/vdb5

# 5. 配置永久挂载
# echo ‘/dev/vdb1 /data1 xfs defaults 0 0’ >> /etc/fstab
# echo ‘/dev/vdb2 /data2 xfs defaults 0 0’ >> /etc/fstab
# echo ‘/dev/vdb6 /data3 xfs defaults 0 0’ >> /etc/fstab
# echo ‘/dev/vdb5 swap swap defaults 0 0’ >> /etc/fstab

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

风哥经验总结

1. fdisk命令只适用于MBR分区表,对于GPT分区表,建议使用gdisk命令。

风哥提示:

2. 在创建分区时,建议使用+size{K,M,G,T,P}格式指定分区大小,这样更直观。

3. 逻辑分区的编号从5开始,无论主分区有多少个。

from PG视频:www.itpux.com

4. 分区操作完成后,必须使用w命令保存更改,否则所有操作都会丢失。

5. 在生产环境中,分区前应做好数据备份,避免误操作导致数据丢失。

联系我们

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

微信号:itpux-com

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