“每个字节都需要一个温暖的家~”——来自每块硬盘的临终遗言
在Linux系统中,磁盘分区是系统管理和存储配置的基础操作之一。无论是为新硬盘分配空间,还是调整现有分区,掌握分区工具的使用都是每个运维人员和开发者的必备技能。本文将带你从零开始,学习如何使用 fdisk和parted 工具进行磁盘分区。
1 磁盘分区简介
磁盘分区是将一块物理硬盘划分为多个逻辑部分的过程。每个分区可以独立格式化并挂载到文件系统中,用于存储数据。常见的分区表类型有:
- MBR:Master Boot Record,即主引导分区表,其使用32标识扇区数,分区不超过2TB,同时扩展分区不超过4个
- GPT:GUID patition table,即全局唯一标识分区表,最大支持128个主分区
主分区:也称为主磁盘分区,主分区中不能再划分其它类型的分区
扩展分区:主分区以外分区,不能直接使用,必须在扩展分区中划分若干逻辑分区,每块磁盘最多只可以划分一个扩展分区
逻辑分区:从扩展分区中划分,可以直接使用
分区编号:主分区和扩展分区的分区编号为1-4,逻辑分区的分区编号从5开始
2 fdisk分区
#进入交互模式分区管理界面
fdisk /dev/sda
##交互模式输入出错时,可以使用“ctrl+Backspace”撤销
Command (m for help): m //输入m查看帮助信息
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
##参数解释
p 显示当前磁盘的分区
n 添加一个新分区
e 扩展分区
p 主分区
l 逻辑分区
d 删除一个分区
t 修改分区类型
l 显示所⽀支持的所有类型
w 保存退出
q 不保存退出
Command (m for help):
Command (m for help): p //#输入p可以查看分区信息
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b5a7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 548 4194304 83 Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3 548 679 1048576 82 Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4 679 1332 5250586 83 Linux
Command (m for help):
##解释:
Device:表示分区的设备文件名称
Boot:表示是否是引导分区,如是,则有“*”标识
Start :表示该分区在硬盘中的起始位置(柱面数)
End :表示该分区在硬盘中的结束位置(柱面数)
Blocks:表示分区的大小,以Blocks(块)为单位,默认大小为1024字节
Id:表示分区对应系统ID号,83表示Linux中的EXT4分区、82表示Swap分区、8e表示LVM逻辑卷
2.1 创建主分区
[root@redhat6 ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n //输入n创建新分区
Command action
e extended
p primary partition (1-4)
p //输入p创建主分区(e是创建扩展分区,p是创建主分区)
Partition number (1-4): 1 //分区编号,默认为1
First cylinder (1-261, default 1): //分区的起始扇区,使用默认即可,直接回车
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +500M
//结束扇区,可直接写+size{K,M,G,T,P}
Command (m for help): p //打印分区表
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1edbdfd2
Device Boot Start End Blocks Id System
/dev/sdb1 1 65 522081 83 Linux
Command (m for help): w //保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@redhat6 ~]#
2.2 创建扩展分区
[root@redhat6 ~]# fdisk /dev/sdb //进入分区交互
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n //增加一个分区
Command action
e extended
p primary partition (1-4)
e //创建一个扩展分区
Partition number (1-4): 2 //设置分区编号
First cylinder (66-261, default 66): //直接回车
Using default value 66
Last cylinder, +cylinders or +size{K,M,G} (66-261, default 261): +500M //设置结束扇区
Command (m for help): p //打印分区列表
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1edbdfd2
Device Boot Start End Blocks Id System
/dev/sdb1 1 65 522081 83 Linux
/dev/sdb2 66 130 522112+ 5 Extended
Command (m for help): w //保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@redhat6 ~]#
2.3 创建逻辑分区
[root@redhat6 ~]# fdisk /dev/sdb //进入分区交互模式
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n //创建新分区
Command action
l logical (5 or over)
p primary partition (1-4)
l //创建逻辑分区
First cylinder (66-130, default 66): //直接回车
Using default value 66
Last cylinder, +cylinders or +size{K,M,G} (66-130, default 130): +100M
//设置结束扇区
Command (m for help): p //打印分区列表
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1edbdfd2
Device Boot Start End Blocks Id System
/dev/sdb1 1 65 522081 83 Linux
/dev/sdb2 66 130 522112+ 5 Extended
/dev/sdb5 66 79 112423+ 83 Linux
Command (m for help): w //保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@redhat6 ~]#
2.4 刷新分区信息
#刷新分区信息
partprobe
[root@redhat6 ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
[root@redhat6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2G 0 disk
├─sdb1 8:17 0 509.9M 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 109.8M 0 part
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part /
├─sda3 8:3 0 1G 0 part [SWAP]
└─sda4 8:4 0 5G 0 part
sr0 11:0 1 1024M 0 rom
[root@redhat6 ~]#
2.5 删除分区
[root@redhat6 ~]# fdisk /dev/sdb //进入分区交互模式
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): d //删除分区
Partition number (1-5): 5 //选择删除的分区编号
Command (m for help): p //打印分区列表
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1edbdfd2
Device Boot Start End Blocks Id System
/dev/sdb1 1 65 522081 83 Linux
/dev/sdb2 66 130 522112+ 5 Extended
Command (m for help): w //保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@redhat6 ~]#
3 parted分区
[root@redhat6 ~]# parted /dev/sdb //1.进入parted分区工具
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt //2.设置 GPT 分区表:将磁盘/dev/sdb的分区表类型设置为GPT
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes //3.输入yes确认继续操作
(parted) print //4.查看当前分区信息:打印当前磁盘的分区信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
(parted) mkpart print 0k 100M //5.创建分区:命名为 "print",从0KB到100MB
Warning: You requested a partition from 0.00B to 100MB.
The closest location we can manage is 17.4kB to 100MB.
Is this still acceptable to you?
Yes/No? yes //6.确认继续操作
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i //7.提示分区未对齐:直接忽略
(parted) p //8.查看当前分区信息:打印当前磁盘的分区信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 100MB 100MB print
(parted) quit //9.完成分区,退出
[root@redhat6 ~]#
#查看分区信息:lsblk
[root@redhat6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2G 0 disk
└─sdb1 8:17 0 95.4M 0 part
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 4G 0 part /
├─sda3 8:3 0 1G 0 part [SWAP]
└─sda4 8:4 0 5G 0 part
sr0 11:0 1 1024M 0 rom
[root@redhat6 ~]#
4 结语
掌握以上操作后,你可以轻松管理磁盘分区,为系统配置存储空间。大家在实际生产黄金分区时踩过哪些有趣的坑?欢迎在评论区分享你的血泪史~
完美的分区方案就像爱情——需要精心规划,也要留有弹性空间。