磁盘分区是指使用磁盘分区工具将磁盘划分为一个或多个独立的区域进行管理。磁盘在分区表中存储了每个分区的位置和大小信息,操作系统根据这些信息将分区视作一个逻辑磁盘。从而防止数据丢失以及增加磁盘空间的利用率。本文将介绍如何管理磁盘分区,包括磁盘分区的创建、改变磁盘分区大小、修改磁盘分区类型和删除分区。
磁盘分区分区表的类型决定了单个分区的最大数量和大小。
磁盘分区表的类型有两种:主引导记录(MBR)和GUID分区表(GPT)。
最大分区数量在MBR中,最大分区数为:“4个主分区”或“3个主分区和1个扩展分区”。其中,扩展分区又可以划分为多个逻辑分区。
在GPT分区表中,最大分区数量为任意多分区,但某些分区工具(例如parted)会限制分区数目。
支持最大的磁盘大小MBR支持的最大磁盘大小为:
如果扇区大小为512字节,磁盘最大为2 TB。
如果扇区大小为4096字节,磁盘最大为16 TB。
GPT支持的最大磁盘大小为:
如果扇区大小为512字节,磁盘最大为8 ZB。
如果扇区大小为4096字节,磁盘最大为64 ZB。
本文以MBR分区类型基于fdisk工具为例进行演示。
查看系统信息。
sudo cat /etc/os-release返回系统信息如下所示。
NAME="Alibaba Cloud Linux"
VERSION="3 (Soaring Falcon)"
ID="alinux"
ID_LIKE="rhel fedora centos anolis"
VERSION_ID="3"
UPDATE_ID="9"
PLATFORM_ID="platform:al8"
PRETTY_NAME="Alibaba Cloud Linux 3 (Soaring Falcon)"
ANSI_COLOR="0;31"
HOME_URL="https://www.aliyun.com/"查看是否安装了fdisk。
fdisk --help若提示无法找到该指令,则执行以下命令安装。
sudo yum install -y util-linux查看fdisk是否安装成功。
fdisk --help打印以下信息,表明fdisk工具安装成功。
Usage:
fdisk [options]
fdisk [options] -l [
...
For more details see fdisk(8).创建分区表本文以在磁盘/dev/vdb下创建分区表为例进行说明。
查看系统磁盘信息。
lsblk返回磁盘信息如下所示。
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 2M 0 part
├─vda2 253:2 0 200M 0 part /boot/efi
└─vda3 253:3 0 39.8G 0 part /
vdb 253:16 0 20G 0 disk 进入fdisk的操作界面。
sudo fdisk /dev/vdb输入m查看所有支持的命令,如下所示。
...
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table输入指令g创建一份GPT分区表,输入指令o创建MBR分区表。输入p指令查看磁盘信息如下所示。
Command (m for help): o
Created a new DOS disklabel with disk identifier 0x34c3f526.
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0x34c3f526输入w指令将分区表信息写入磁盘并退出。
创建分区本文以在磁盘/dev/vdb下创建分区为例进行说明。
进入fdisk的操作界面。
sudo fdisk /dev/vdb输入指令p查看当前磁盘的信息。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0x34c3f526输入指令n创建新分区。
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):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +100M
Created a new partition 1 of type 'Linux' and of size 100 MiB.输入指令p选择主分区类型的分区。
输入1选用默认的分区号。
输入2048设置起始扇区为默认值2048。
说明 起始扇区为默认的2048,截止扇区为起始扇区开始往后+100 M大小的位置。两种方式指定分区的大小。
+sectors:这种方式指定分区的大小为sectors个扇区。
+size:这种方式指定分区的大小为size,例如+100 M则指定分区大小为100 M。
输入+100 M指定分区大小为100 M。
创建的分区如下所示,其大小为100 M。
...
Created a new partition 1 of type 'Linux' and of size 100 MiB.输入指令w将分区信息写入分区表中并退出。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.修改分区大小本文以分区扩容为例,介绍如何修改分区大小。fdisk工具并没有直接提供修改分区大小的指令,因此在使用fdisk修改分区大小时,只能通过删除原有分区并新建一个新的大小的分区来实现,然而,这一操作可能会导致原有数据的丢失。parted是另一种磁盘分区工具,它提供了修改分区大小的相关指令。以下将介绍使用parted工具修改分区大小的方法。
安装parted。
sudo yum install -y parted确认是否安装成功。
sudo parted --help返回结果如下则证明安装成功。
Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run in
interactive mode.
OPTIONs:
-h, --help displays this help message
...进入parted操作界面。
sudo parted /dev/vdb设置parted的单位为MiB。
(parted) unit MiB输入p指令查看所有的分区信息:
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1.00MiB 101MiB 100MiB primary
2 101MiB 201MiB 100MiB primary修改分区大小。
请将NUMBEREND替换为对应的分区号和结束位置。
resizepart NUMBER END分区2的起始位置为101 MiB,若希望将其大小扩展至500 MiB,则结束位置为601 MiB。
(parted) resizepart 2 601MiB
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1.00MiB 101MiB 100MiB primary
2 101MiB 601MiB 500MiB primary输入quit退出parted程序。
进入fdisk的操作界面。
sudo fdisk /dev/vdb输入指令p查看当前磁盘的信息,确认分区大小。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xefabc860
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux
/dev/vdb2 206848 1230847 1024000 500M 83 Linux输入q退出。
修改分区类型使用fdisk修改分区类型。
进入fdisk操作界面。
sudo fdisk /dev/vdb输入p查看当前分区信息,分区信息如下所示。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xefabc860
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux
/dev/vdb2 206848 1230847 1024000 500M 83 Linux输入l查看所有分区类型,分区类型如下所示。
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
......修改分区类型。
以将分区2修改为扩展分区(类型号为5)为例,首先输入指令t,接着输入2,最后输入5即可。
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 5
Changed type of partition 'Linux' to 'Extended'.输入指令p查看分区信息。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xefabc860
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux
/dev/vdb2 206848 1230847 1024000 500M 5 Extended输入w保存分区信息至分区表中,并退出。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.删除分区进入fdisk操作界面。
sudo fdisk /dev/vdb输入p指令查看分区信息。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xefabc860
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux
/dev/vdb2 206848 1230847 1024000 500M 5 Extended以删除分区2为例,首先输入指令d,然后输入需要删除的分区号2,将显示如下信息。
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.输入p指令查看分区信息,此时分区2已被删除。
Command (m for help): p
Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xefabc860
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux输入w指令保存分区信息至磁盘分区表并退出。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.