关注公众号 “融码一生”,领取全套 PDF / 电子书
- 在安装操作系统的过程中已经对系统硬盘进行了分区,但如果新添加一块硬盘,想要正常使用,需再进行分区。Linux 中有专门的分区命令
fdisk
和parted
。 fdisk
命令较为常用,但不支持大于 2TB 的分区。如果需要支持大于 2TB 的分区,则需使用parted
命令(也能分配较小的分区)- fdisk 命令的格式:
# 列出系统分区
fdisk -l
:<<!
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x89ee0607
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 209715166 209713119 100G 83 Linux
!
# 给硬盘分区
fdisk 设备文件名
- 注意:不要在当前硬盘上尝试使用 fdisk,这会完整删除整个系统;一定要再找一块硬盘,或使用虚拟机。
- 示例:
# 查询本机可以识别的硬盘和分区
fdisk -l
:<<!
Disk /dev/sda:32.2 GB, 32212254720 bytes
# 硬盘文件名和硬盘大小
255 heads, 63 sectors/track, 3916 cylinders
# 共255个磁头、63个扇区和3916个柱面
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: 0x0009e098
Device Boot Start End Blocks ld System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
# 分区 1 没有占满硬盘
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary
# 分区 2 没有占满硬盘
/dev/sda3 281 3917 29203456 83 Linux
# 设备文件名启动分区 起始柱面 终止柱面容量 ID 系统
Disk /dev/sdb: 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: 0x00000000
!
- 使用
fdisk -l
查看分区信息,能看到我们添加的两块硬盘(/dev/sda
和/dev/sdb
)的信息。
-
- 上半部分是硬盘的整体状态,
/dev/sda
硬盘的总大小是 32.2 GB,共有 3916 个柱面,每个柱面由 255 个磁头读/写数据,每个磁头管理 63 个扇区。每个柱面的大小是 8225280 Bytes,每个扇区的大小是 512 Bytes。 - 下半部分是分区的信息,共 7 列:
- 上半部分是硬盘的整体状态,
-
-
- Device:分区的设备文件名
- Boot:是否为启动引导分区,在这里 /dev/sda1 为启动引导分区
- Start:起始柱面,代表分区从哪里开始
- End:终止柱面,代表分区到哪里结束
- Blocks:分区的大小,单位是 KB
- id:分区内文件系统的 ID。在 fdisk 命令中,可以 使用 "i" 查看
- System:分区内安装的系统是什么
-
- 如果这个分区并没有占满整块硬盘,就会提示 "Partition 1 does not end on cyl inder boundary",表示第一个分区没有到硬盘的结束柱面。
-
/dev/sda
已经分配完了分区,没有空闲空间了- 第二块硬盘
/dev/sdb
已经可以被识别了,但没有可分区
- 以硬盘
/dev/sdb
为例来做练习:
# 给 /dev/sdb 分区
fdisk /dev/sdb
:<<!
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xed7e8bc7.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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):m
#交互界面的等待输入指令的位置,输入 m 得到帮助
Command action
#可用指令
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
I 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 verity the partition table
w write table to disk and exit
x extra functionality (experts only)
!
- 注意这里的分区命令是
fdisk /dev/sdb
,因为硬盘并没有分区,使用fdisk
命令就是建立分区。 - 在 fdisk 交互界面中输入 m 可得到帮助,会列出 fdisk 可以识别的交互命令:
命令 | 说 明 |
a | 设置可引导标记 |
b | 编辑 bsd 磁盘标签 |
c | 设置 DOS 操作系统兼容标记 |
d | 删除一个分区 |
1 | 显示已知的文件系统类型。82 为 Linux swap 分区,83 为 Linux 分区 |
m | 显示帮助菜单 |
n | 新建分区 |
0 | 建立空白 DOS 分区表 |
P | 显示分区列表 |
q | 不保存退出 |
s | 新建空白 SUN 磁盘标签 |
t | 改变一个分区的系统 ID |
u | 改变显示记录单位 |
V | 验证分区表 |
w | 保存退出 |
X | 附加功能(仅专家) |
fdisk 创建分区(主分区、扩展分区和逻辑分区)过程
- 硬盘由大量扇区组成,每个扇区的容量为 512 字节。第一个扇区(最重要)保存主引导记录(占用 446 字节)和分区表信息(占用 64 字节),还有 2 个字节供结束符使用。在分区表中,每记录一个分区信息就需占用 16 字节,整个分区表最多只能记录 4 个分区信息,这 4 个分区称为 4 个主分区。
- 第一个扇区的总体情况如下:
- 第一个扇区最多只能创建出 4 个分区吗?不是的,为了解决分区个数不够的问题,还可以进一步将第一个扇区的分区表中 16 个字节(原本要写入主分区信息)的空间用来存储另外一个分区的信息,通常将这 16 个字节的空间称为扩展分区。
- 扩展分区并不是一个真正的分区,而可以看做是一个占用 16 字节分区表空间的指针,用来指向另一个分区。
- 通常用户会选择使用 3 个主分区外加 1 个扩展分区的方法,然后在扩展分区中再创建出多个逻辑分区,从而来满足多分区(大于 4 个)的需求:
- 因此,硬盘分区有 3 种:
-
- 主分区:至少有 1 个,最多有 4 个
- 扩展分区:最多只能有 1 个,且主分区 + 扩展分区总共不能超过 4 个
- 逻辑分区:可以有若干个
(1)fdisk 创建主分区
- 建立一个主分区:
fdisk /dev/vda1
:<<!
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
The old ext4 signature will be removed by a write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x7834aa11.
# jy: 显示当前硬盘的分区列表(目前一个分区都没有)
Command (m for help): p
Disk /dev/vda1: 100 GiB, 107373116928 bytes, 209713119 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
Disklabel type: dos
Disk identifier: 0x7834aa11
# jy: 新建一个分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
# jy: 指定分区类型
Select (default p): p
# jy: 选择分区号, 范围为 1~4, 这里选择 1
Partition number (1-4, default 1): 1
First sector (2048-209713118, default 2048):
# jy: 指定硬盘大小; 可以按照 sector 指定(2048-209713118)。我们对 sector 不熟悉, 可以
# 使用 size{K, M, G} 的方式指定硬盘大小; 这里指定 +5G, 建立一个 5GB 大小的分区
Last sector, +sectors or +size{K,M,G,T,P} (2048-209713118, default 209713118): +5G
Created a new partition 1 of type 'Linux' and of size 5 GiB.
Command (m for help): p
Disk /dev/vda1: 100 GiB, 107373116928 bytes, 209713119 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
Disklabel type: dos
Disk identifier: 0x7834aa11
Device Boot Start End Sectors Size Id Type
/dev/vda1p1 2048 10487807 10485760 5G 83 Linux
!
- 建立主分区的过程是:
-
fdisk 硬盘名 -> n(新建) -> p(建立主分区) -> 1(指定分区号) -> 回车(默认从 1 柱面开始建立分区)-> +5G(指定分区大小)
- 当然,我们的分区还没有格式化和挂载,所以还不能使用。
(2)fdisk 创建扩展分区
- 主分区和扩展分区加起来最多只能建立 4 个,而扩展分区最多只能建立 1 个。
- 扩展分区的建立命令:
fdisk /dev/vda1
:<<!
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# jy: 新建立分区
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
# jy: 这次建立扩展分区
Select (default p): e
# jy: 给扩展分区指定分区号 2
Partition number (2-4, default 2): 2
# jy: 扩展分区的起始 sector; 上节建立的主分区 1 已经占用了 1~10487808 sector, 所以此处
# 从 10487808 开始建立; 注意: 如果没有特殊要求, 则不要跳开柱面建立分区, 应该紧挨着建
# 立分区
First sector (10487808-209713118, default 10487808):
# jy: 这里把整块硬盘的剩余空间都建立为扩展分区
Last sector, +sectors or +size{K,M,G,T,P} (10487808-209713118, default 209713118):
Created a new partition 2 of type 'Extended' and of size 95 GiB.
Command (m for help): p
Disk /dev/vda1: 100 GiB, 107373116928 bytes, 209713119 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
Disklabel type: dos
Disk identifier: 0x7834aa11
Device Boot Start End Sectors Size Id Type
/dev/vda1p1 2048 10487807 10485760 5G 83 Linux
/dev/vda1p2 10487808 209713118 199225311 95G 5 Extended
Command (m for help): w
The partition table has been altered.
Failed to add partition 2 to system: Invalid argument
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
!
- 这里把
/dev/vda1
硬盘的所有剩余空间都建立为扩展分区,即建立一个主分区,剩余空间都建立成扩展分区。 - 扩展分区不能被格式化和直接使用,还要在扩展分区内部再建立逻辑分区。
(3)fdisk 创建逻辑分区
fdisk /dev/vda1
:<<!
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# jy: 建立新分区
Command (m for help): n
# jy: 由于扩展分区已经建立, 所以这里变成了 logical
All space for primary partitions is in use.
# jy: 建立逻辑分区; 不用指定分区号, 默认会从 5 开始分配, 所以直接选择起始柱面
# 注意: 逻辑分区是在扩展分区内部再划分的, 所以柱面是和扩展分区重叠的
Adding logical partition 5
First sector (10489856-209713118, default 10489856):
# jy: 分配 2GB 大小
Last sector, +sectors or +size{K,M,G,T,P} (10489856-209713118, default 209713118): +2G
Created a new partition 5 of type 'Linux' and of size 2 GiB.
# jy: 再建立一个逻辑分区
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (14686208-209713118, default 14686208):
Last sector, +sectors or +size{K,M,G,T,P} (14686208-209713118, default 209713118): +2G
Created a new partition 6 of type 'Linux' and of size 2 GiB.
# jy: 查看一下已经建立的分区
Command (m for help): p
Disk /dev/vda1: 100 GiB, 107373116928 bytes, 209713119 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
Disklabel type: dos
Disk identifier: 0x7834aa11
Device Boot Start End Sectors Size Id Type
/dev/vda1p1 2048 10487807 10485760 5G 83 Linux
/dev/vda1p2 10487808 209713118 199225311 95G 5 Extended
/dev/vda1p5 10489856 14684159 4194304 2G 83 Linux
/dev/vda1p6 14686208 18880511 4194304 2G 83 Linux
# jy: 保存并退出
Command (m for help): w
The partition table has been altered.
Failed to add partition 5 to system: Invalid argument
Failed to add partition 6 to system: Invalid argument
# jy: 要求重新启动, 才能格式化
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
!
- 注意:所有的分区建立过程中如果不保存并退出是不会生效的,所以建立错了也没关系,使用
q
命令不保存退出即可。如果使用w
命令,就会保存退出。 - 有时因为系统的分区表正忙,所以需要重启系统才能使新的分区表生效。
- 可重启很浪费时间;如果不想重启,则使用
partprobe
命令,让系统内核重新读取分区表信息。命令如下:
-
partprobe
-
-
- 如果命令不存在,则请安装
parted-2.1-18.el6.i686
这个软件包 partprobe
命令不是必需的,如果没有提示重启系统,则直接格式化即可
- 如果命令不存在,则请安装
-
关注公众号 “融码一生”,领取全套 PDF / 电子书