在创建虚拟机的时候只给了20G磁盘空间大小,但是现在需求变更,想要增加到40G磁盘空间大小,所以需要通过两步扩容磁盘空间。
系统版本是Centos7
根目录挂载的是/dev/mapper/centos_zxy-root
,是LVM逻辑卷,xfs文件类型。
使用fdisk -l查看当前机器所挂载的硬盘个数及分区情况,从下面数据可以知道当前文件系统有一个磁盘dev/sda。
其下面分为/dev/sda1和/dev/sda2,/dev/sda2为LVM卷,可进行扩展。
[root@zxy ~]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x00036ec0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos_zxy-root: 18.2 GB, 18249416704 bytes, 35643392 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
Disk /dev/mapper/centos_zxy-swap: 2147 MB, 2147483648 bytes, 4194304 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
使用lsblk查询磁盘相关分区、所属关系以及LVM情况。
sda2为LVM逻辑卷,sda2下面分为centos_zxy-root、centos_zxy-swap逻辑卷,其中centos_zxy-root挂载到系统根目录。
说明在操作系统任何地方存储文件(除/boot目录外,/boot目录使用的sda1空间),都使用的centos_zxy磁盘空间。
所以本次目标是将centos_zxy扩容到40G。
[root@zxy ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_zxy-root 253:0 0 17G 0 lvm /
└─centos_zxy-swap 253:1 0 2G 0 lvm [SWAP]
sr0 11:0 1 942M 0 rom
1)编辑虚拟机设置->添加磁盘
2)新增硬盘,默认配置点击即可,如下即为添加成功
3)lsblk
执行lsblk查看磁盘状态,可以看到新增了/dev/sdb磁盘20G。
但是对应的centos_zxy-root逻辑卷还没扩展,需要继续手动操作
[root@zxy ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_zxy-root 253:0 0 17G 0 lvm /
└─centos_zxy-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 942M 0 rom
$ mkfs.xfs /dev/sdb
[root@zxy ~]# mkfs.xfs /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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
$ pvcreate /dev/sdb
[root@zxy ~]# pvcreate /dev/sdb
WARNING: xfs signature detected on /dev/sdb at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb.
Physical volume "/dev/sdb" successfully created.
$ vgs
[root@zxy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos_zxy 1 2 0 wz--n- <19.00g 0
$ vgextend centos_zxy /dev/sdb
[root@zxy ~]# vgextend centos_zxy /dev/sdb
Volume group "centos_zxy" successfully extended
使用lvextend命令将新磁盘/dev/sdb扩容到/dev/mapper/centos-root中
[root@zxy ~]# lvextend /dev/mapper/centos_zxy-root /dev/sdb
Size of logical volume centos_zxy/root changed from <17.00 GiB (4351 extents) to 36.99 GiB (9470 extents).
Logical volume centos_zxy/root successfully resized.
[root@zxy ~]# xfs_growfs /dev/mapper/centos_zxy-root
meta-data=/dev/mapper/centos_zxy-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 9697280
查看扩容后的磁盘空间大小,/dev/mapper/centos-root容量扩容完成
[root@zxy ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos_zxy-root 37G 3.0G 35G 9% /
/dev/sda1 1014M 149M 866M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0