文章目录
- 意义
- 创建分区
- 删除分区
- 格式化
- 小结
意义
- 数据管理方面: 可以把不同类型的数据(如系统文件、用户文件、应用程序文件)分开存放,方便分类管理和备份。一个分区出问题(如文件系统损坏),不会轻易影响其他分区的数据,起到一定的数据隔离作用。
- 系统维护方面: 安装或升级系统时,分区能让操作更方便。方便灵活地分配磁盘空间。
- 性能提升方面: 减少磁盘碎片。不同分区的读写操作相对独立,能降低频繁读写导致的碎片产生几率。提升磁盘 I/O 效率。合理分区可以让磁头更高效地寻道,比如把频繁读写的数据和不常读写的数据分开存放。
创建分区
说明:
目前磁盘分区主要有 MBR 以及 GPT 两种格式:
-
MBR最多支持四个主分区,支持2.2T容量以下的硬盘分区。
-
GPT最多支持128个主分区,没有扩展分区和逻辑盘,支持2.2T容量以上的硬盘分区。
分区工具:
- fdisk:适用于MBR格式
- gdisk:适用于GPT格式(推荐)
- parted:全部适用
若对无分区磁盘则选用其中一种工具完成分区,若对已有分区磁盘创建新分区,则需要使用parted查看磁盘分区格式选择对应工具 (fdisk、gdisk使用方法相似)。
parted 设备名称 print
[root@test ~]# parted /dev/vda print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 316MB 315MB fat16 EFI System Partition boot, esp
2 316MB 1389MB 1074MB xfs
3 1389MB 21.5GB 20.1GB lvm
本文中由于是空磁盘创建分区,故推荐使用gdisk工具创建。
- 执行lsblk,列出系统上的所有磁盘列表
lsblk
[root@test ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 20G 0 disk
├─vda1 253:1 0 300M 0 part /boot/efi
├─vda2 253:2 0 1G 0 part /boot
└─vda3 253:3 0 18.7G 0 part
└─klas-root 252:0 0 18.7G 0 lvm /
vdb 253:16 0 10G 0 disk
- 从回显磁盘列表确认分区磁盘。该列表中vdb磁盘未分区,对vdb创建分区,使用gdisk工具。
gdisk 设备名称
[root@test ~]# gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.5.1
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
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):
- 创建分区,特别注意“ Last sector ”那一行,那行绝对不要使用默认值,直接按enter!要根据分区情况来进行分配,除非你将全部的存储分到当前创建的分区,其它均可默认创建。
Command (? for help): n #增加一个分区
Partition number (1-128, default 1): #默认enter
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: #默认enter
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: 10000000 #根据需求配置
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p #查看创建结果
Disk /dev/vdb: 20971520 sectors, 10.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CDD8C600-2BE0-408F-923F-D3C5540C18E0
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 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 10973500 sectors (5.2 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 10000000 4.8 GiB 8300 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/vdb.
The operation has completed successfully.
- 执行lsblk -f,/dev/vdb1分区已被创建,但是无文件系统类型,需要格式化后才能挂载使用。
[root@test ~]# lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sr0
vda
├─vda1 vfat FAT16 D8F7-B900 293.3M 2% /boot/efi
├─vda2 xfs 8809cf83-3fe8-4301-b79f-368f93ad1fd4 856.3M 16% /boot
└─vda3 LVM2_member LVM2 001 GH0dOK-Djs7-zVIH-xYNI-31hQ-FB9J-SHSQNu
└─klas-root xfs 4540b1b6-e4fc-47c0-9b04-902f16a799bc 14.8G 21% /
vdb
└─vdb1
删除分区
注意:
- 正在使用的分区不能删除,卸载后删除。
- 分区删除会导致数据丢失,功能异常等情况,谨慎操作。
若之前创建的分区存在问题,使用gdisk删除
[root@test ~]# gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.5.1
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): d #删除一个分区,由于只有一个就不需要选择
Using 1
Command (? for help): p #查看删除结果
Disk /dev/vdb: 20971520 sectors, 10.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CDD8C600-2BE0-408F-923F-D3C5540C18E0
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 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)
Number Start (sector) End (sector) Size Code Name
Command (? for help): q #不保存退出,确认需要删除按'w'
格式化
格式化mkfs(make filesystem)应该叫做创建文件系统。
- XFS文件系统
mkfs.xfs /dev/vdb1
[root@test ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=312436 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1249744, 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
Discarding blocks...Done.
- 查看分区创建及文件系统情况,/dev/vdb1的FSTYPE 为xfs,说明已经成功格式化,现在就可以对该分区挂载使用了。
lsblk -f
[root@test ~]# lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sr0
vda
├─vda1 vfat FAT16 D8F7-B900 293.3M 2% /boot/efi
├─vda2 xfs 8809cf83-3fe8-4301-b79f-368f93ad1fd4 856.3M 16% /boot
└─vda3 LVM2_member LVM2 001 GH0dOK-Djs7-zVIH-xYNI-31hQ-FB9J-SHSQNu
└─klas-root xfs 4540b1b6-e4fc-47c0-9b04-902f16a799bc 14.8G 21% /
vdb
└─vdb1 xfs b5eb6622-7c37-47f3-bc22-934892cb997a
EXT4文件系统格式化方式
mkfs.ext4 /dev/vdb1
小结
本文采用gdisk分区工具创建分区,fdisk分区工具和gdisk使用相似。删除分区谨慎操作!分区创建后必须格式化,否则无法使用。