Skip to content

CentOS 磁盘扩容

bash
fdisk -l

可以看到现在的分区情况如下

bash
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        1306     9972736   8e  Linux LVM

创建分区 sda3

bash
fdisk /dev/sda
bash
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
Partition number (1-4): 3   ## 输入 3 表示sda3,因为前面已经有两个了
First cylinder (1306-4568, default 1306):     ## 回车,按默认值
Using default value 1306
Last cylinder, +cylinders or +size{K,M,G} (1306-4568, default 4568):   ## 回车,按默认值
Using default value 4568

Command (m for help): w     ## 输入 w 保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

再次查看分区情况

bash
fdisk -l

可以看到现在的分区情况如下

bash
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        1306     9972736   8e  Linux LVM
/dev/sda3            1306        4568    26206700   83  Linux

重启操作系统

bash
reboot

格式化新分区为 ext4

bash
mkfs -t ext4 /dev/sda3

将物理硬盘分区初始化为物理卷,以便被 LVM 使用,输入指令

bash
lvs
pvcreate /dev/sda3

向卷组中添加物理卷来增加卷组的容量

bash
df -h
bash
vgextend centos-root /dev/sda3

查看可扩展的空间大小

bash
vgdisplay

其中 Free PE / Size 就是可供分配的自由空间,最多有 24.99G,在扩展时输入小于该值

扩充磁盘空间

bash
lvextend -L+24G /dev/mapper/vg_test001-lv_root /dev/sda3

使用 resize2fs 指令来增大或者收缩未加载的“ext”文件系统的大小

bash
resize2fs /dev/mapper/centos-root

如果是 xfs 文件系统

bash
xfs_growfs /dev/mapper/centos-root

Version 4.0 (framework-1.1.4)