Linux 硬盘分区管理
Linux 硬盘分区管理
摘要:本文系统介绍了 Linux 硬盘分区管理的核心概念与实用工具。首先阐述了硬盘分区的必要性,包括数据隔离、分类整理、降低风险等。随后详细对比了 MBR(主引导记录)和 GPT(GUID 分区表)两种主流分区方案,重点讲解了 MBR 的 2TB 容量限制、4个主分区规则以及 GPT 的大容量、多分区优势。文章核心部分通过大量 Shell 命令示例,演示了如何使用 fdisk 管理 MBR 磁盘(查看、创建、删除、扩展分区),以及使用 gdisk、parted 管理 GPT 磁盘。最后,介绍了 wipefs 工具清除磁盘标识的方法,并详细说明了通过 /etc/fstab 配置文件实现文件系统持久化挂载的完整流程,确保服务器重启后数据盘自动挂载,保障业务连续性。
硬盘为什么要分区?
- 隔离系统与数据
系统装 C 盘,文件、软件放其他分区,重装系统只清 C 盘,个人数据完好无损。
- 便于文件分类整理
按用途划分:软件盘、游戏盘、资料盘、备份盘,查找打理更清晰。
- 降低数据丢失风险
单个分区损坏、中毒出错,不会影响整块硬盘所有数据。
- 满足系统启动规则
Windows 必须有活动主分区才能引导开机,整块裸盘无法直接装系统启动。
- 适配分区表规范
MBR/GPT 分区格式是系统识别硬盘的基础,无分区无法格式化、正常存取文件。
- 权限与用途区分
可单独设置分区权限、加密分区,也能分开存放公私文件。
一、MBR分区方案
- 最大磁盘容量:2TB(超出不认)
分区表限制
- 最多 4 个主分区
- 或 3 主分区 + 1 扩展分区
- 扩展分区内可划分多个逻辑分区
512 字节扇区拆分
前446 字节:系统引导程序(GRUB)
中间64 字节:分区表(每 16 字节一个分区,最多 4 个)
最后2 字节:55 AA 有效启动标志
*** 分区规则***
主分区:可直接装系统、设为活动分区引导
扩展分区:不能直接使用,仅当容器
逻辑分区:在扩展分区里划分,用于存数据
fdisk工具:用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况进行划分分区。
[root@sever ~ 18:58:21]# fdisk -h
用法:
fdisk [选项] <磁盘> 更改分区表
fdisk [选项] -l <磁盘> 列出分区表
fdisk -s <分区> 给出分区大小(块数)
选项:
-b <大小> 扇区大小(512、1024、2048或4096)
-c[=<模式>] 兼容模式:“dos”或“nondos”(默认)
-h 打印此帮助文本
-u[=<单位>] 显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
-v 打印程序版本
-C <数字> 指定柱面数
-H <数字> 指定磁头数
-S <数字> 指定每个磁道的扇区数
查看分区
#方法1:
[root@sever ~ 10:04:52]# fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
磁盘 /dev/sdb:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 578C7D63-5DDF-4275-86CF-D70A8CD934E8
# Start End Size Type Name
1 2048 4196351 2G Microsoft basic data01
#方法2:
[root@sever ~ 10:04:57]# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 578C7D63-5DDF-4275-86CF-D70A8CD934E8
# Start End Size Type Name
1 2048 4196351 2G Microsoft basic data01
命令(输入 m 获取帮助):q
创建分区
[root@sever ~ 14:07:09]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x51be97b2 创建新的 DOS 磁盘标签。
#创建分区 -n
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
#有空闲剩余默认为p 主区
Select (default p):
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB
#w保存退出 q不保存退出
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@sever ~ 14:07:32]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 59G 0 part
├─centos-root 253:0 0 38.3G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 18.7G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 2G 0 part
#若未更新分区表,可以通过partprobe 更新最新的分区表
删除分区
[root@sever ~ 14:10:55]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
#删除命令
命令(输入 m 获取帮助):d
已选择分区 1
分区 1 已删除
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@sever ~ 14:11:05]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 59G 0 part
├─centos-root 253:0 0 38.3G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 18.7G 0 lvm /home
sdb 8:16 0 20G 0 disk
扩展分区演示
#从第四个开始默认是扩展分区 类型为e
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
已选择分区 4
起始 扇区 (14682112-41943039,默认为 14682112):
将使用默认值 14682112
Last 扇区, +扇区 or +size{K,M,G} (14682112-41943039,默认为 41943039):
将使用默认值 41943039
分区 4 已设置为 Extended 类型,大小设为 13 GiB
命令(输入 m 获取帮助):
#扩展分区后,默认创建逻辑分区
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (14684160-41943039,默认为 14684160):
将使用默认值 14684160
Last 扇区, +扇区 or +size{K,M,G} (14684160-41943039,默认为 41943039):+2G
分区 5 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x51be97b2
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 14682111 3145728 83 Linux
/dev/sdb4 14682112 41943039 13630464 5 Extended
/dev/sdb5 14684160 18878463 2097152 83 Linux
#扩展分区为逻辑分区父容器
#扩展分区sdb4 是逻辑分区仓库,自身无文件系统,不能作为挂载点
#sdb4头部空白扇区存放逻辑分区索引表,系统靠它找到里面的 sdb5、sdb6…逻辑分区
二、GPT方案
- 分区理论最大18 ZiB
- 分区表双备份,不易损坏
- 不用纠结主分区名额,随便新建分区(不区分主区,扩展分区,逻辑分区),128个分区
- 删除任意单个分区互不影响,删一个不会连带删别的
gdisk工具
gdisk工具用于管理采用GPT分区方案的磁盘分区,主要用于管理磁盘容量超过2T的磁盘。
#安装工具
[root@sever ~ 14:33:36]# yum install -y gdisk
#命令大多与fdisk工具类似
#查看分区表
[root@sever ~ 14:35:57]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 25605123-B133-46D6-B04D-F130810A81E0
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
[root@sever ~ 14:33:46]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
#输入o 可以将上面的mbr方案转换成GPT
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
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
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.
[root@sever ~ 14:34:20]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 2G 0 part
#查看类型
[root@sever ~ 14:34:26]# blkid /dev/sdb
/dev/sdb: PTTYPE="gpt"
#更改分区名称
[root@sever ~ 14:40:32]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): c
Using 1
Enter name: data01
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 25605123-B133-46D6-B04D-F130810A81E0
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4196351 2.0 GiB 8300 data01
#查看分区详细信息
Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: A3F304FD-49B6-4DFA-AEF8-25C13F20C665
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'
#删除分区
Command (? for help): d
Using 1
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.
[root@sever ~ 14:43:50]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
三、wipefs工具
作用:查看和清楚磁盘管理信息
[root@sever ~ 14:45:15]# wipefs /dev/sdb
offset type
----------------------------------------------------------------
0x200 gpt [partition table]
[root@sever ~ 14:45:22]# wipefs -a /dev/sdb
/dev/sdb:8 个字节已擦除,位置偏移为 0x00000200 (gpt):45 46 49 20 50 41 52 54
/dev/sdb:8 个字节已擦除,位置偏移为 0x4fffffe00 (gpt):45 46 49 20 50 41 52 54
/dev/sdb:2 个字节已擦除,位置偏移为 0x000001fe (PMBR):55 aa
/dev/sdb: calling ioclt to re-read partition table: 成功
[root@sever ~ 14:45:34]# wipefs /dev/sdb
[root@sever ~ 14:45:45]#
四、parted工具
不演示交互式方案,命令基本同上
#非交互式方案
#设置磁盘分区管理方案
[root@sever ~ 15:38:26]# parted /dev/sdb mklabel gpt
信息: You may need to update /etc/fstab.
#查看分区
[root@sever ~ 15:38:50]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
#创建分区 unit MiB统一单位 data01 分区名称 xfs 文件系统类型 起始加结尾
[root@sever ~ 15:42:01]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.
[root@sever ~ 15:42:32]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 2049MiB 2048MiB xfs data01
#调整分区大小,可以缩容/扩容
[root@sever ~ 15:42:54]# parted /dev/sdb unit MiB resizepart 1 5121
信息: You may need to update /etc/fstab.
[root@sever ~ 15:45:29]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 5121MiB 5120MiB xfs data01
#删除分区
[root@sever ~ 16:03:23]# parted /dev/sdb rm 1
信息: You may need to update /etc/fstab.
[root@sever ~ 16:04:32]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
五、文件系统持久化挂载
为什么要做持久化挂载
- 临时挂载重启失效
mount 设备 目录
只是临时挂载,服务器重启就消失,业务数据丢挂载直接崩。
-
生产必须开机自动挂载,网站、日志、数据盘需要开机直接挂上目录,不用手动敲命令。
-
UUID 挂载比 /dev/sdb 更稳定,磁盘插拔、顺序变动,设备名sdb/sdc会乱,
UUID 永远不变,不会挂错盘。
*** 环境准备***
利用 parted 创建一个分区,并格式化为xfs文件系统。
[root@sever ~ 18:46:55]# parted /dev/sdb mklabel gpt
信息: You may need to update /etc/fstab.
[root@sever ~ 18:47:06]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.
[root@sever ~ 18:47:32]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
*** 持久化挂载***
当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /etc/fstab文件中添加一个条目。
/etc/fstab 是以空格分隔的文件,每行具有六个字段。
- **第一个字段指定设备。**可以使用UUID或device来指定设备。
- **第二个字段是目录挂载点。**通过它可以访问目录结构中的块设备。挂载点必须存在;如果不存在,请使用mkdir命令进行创建。
- 第三个字段包含文件系统类型,如xfs或ext4 。
- 第四个字段是挂载选项,以逗号分隔的。 defaults是一组常用选项。详细信息参考mount(8) 。
- 第五个字段指定dump命令是否备份设备。
- 第六个字段指定fsck顺序字段,决定了在系统启动吋是否应运行fsck命令,以验证文件系统是否干净。 该字段中的值指示了 fsck的运行顺序。 对于XFS文件系统, 请将该字段设为0 ,因为XFS并不使用fsck来检查自己的文件系统状态。 对于ext4 文件系统,如果是根文件系统, 请将该字段设 为 1 ; 如果是其他ext4 文件系统, 则将该字段设为2。 这样, fsck就会先处理根文件系统,然后同步检查不同磁盘上的文件系统,并按顺序检查同一磁盘上的文件系统。
示例:
[root@sever ~ 18:48:46]# blkid /dev/sdb1
/dev/sdb1: UUID="e3297f53-528f-4fbe-927f-8df6918bdcee" TYPE="xfs" PARTLABEL="data01" PARTUUID="e5d1daca-fece-49ee-9e36-11aa4c6cd622"
[root@sever ~ 18:48:51]# mkdir /data01
# 最后一行增加一个条目
UUID="e3297f53-528f-4fbe-927f-8df6918bdcee" /data01 xfs defaults 0 0
[root@sever ~ 18:48:57]# vim /etc/fstab
[root@sever ~ 18:49:53]# mount /data01
[root@sever ~ 18:50:03]# df -h /data01
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 33M 2.0G 2% /data01
[root@sever ~ 18:50:09]# reboot
[root@sever ~ 18:51:27]# df -h /data01
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 33M 2.0G 2% /data01
取消持久化挂载
删除/etc/fstab中对应条目即可。
更多推荐


所有评论(0)