在虚拟机安装centos7之后,root分区往往随着安装软件越来越多,空间会不够用。这个时候可以采用动态扩容该分区的办法:
该LVM分区动态扩容分两种情况:
一、扩容原有磁盘/dev/sda
fdisk /dev/sda 对/dev/sda磁盘进行操作(新建分区及格式化)
n
p
回车 默认分区号;
回车 默认磁盘创建开始位置;
回车 默认磁盘创建结束位置;
t 设置分区类型
8e LVM类型
w 保存
重启
ls /dev/sd* 会发现多出一个/dev/sda3
lvm 进入lvm管理
lvm>pvcreate /dev/sda3 创建逻辑卷
lvm>vgextend centos /dev/sda3 //将初始化过的分区加入到虚拟卷组 centos
lvm>vgdisplay -v
lvm>lvextend -L +100G /dev/mapper/centos-root
lvm>pvdisplay //查看卷容量
lvm>quit
使用命令xfs_growfs /dev/mapper/centos-root扩展容量
df -h 进行查看扩容之后的
二、新加磁盘进行扩容
在虚拟机控制台新加磁盘后,并重启虚拟机
[root@bogon ~]# ls /dev/sd* 会发现多出一个/dev/sdb
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <7.00g 0
[root@bogon ~]# pvcreate /dev/sdb
WARNING: ext4 signature detected on /dev/sdb at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb.
Physical volume "/dev/sdb" successfully created.
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <7.00g 0
[root@bogon ~]# vgextend centos /dev/sdb
Volume group "centos" successfully extended
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 26.99g <20.00g
[root@bogon ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <6.20g
swap centos -wi-ao---- 820.00m
[root@bogon ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 6.2G 6.2G 43M 100% /
devtmpfs 908M 0 908M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.6M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 1014M 175M 840M 18% /boot
tmpfs 184M 0 184M 0% /run/user/0
[root@bogon ~]# lvextend -L +20G /dev/mapper/centos-root
Insufficient free space: 5120 extents needed, but only 5119 available
[root@bogon ~]# lvextend -L +19G /dev/mapper/centos-root
Size of logical volume centos/root changed from <6.20 GiB (1586 extents) to <16.20 GiB (4146 extents).
Logical volume centos/root successfully resized.
[root@bogon ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=406016 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1624064, 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 1624064 to 4245504
[root@bogon ~]# df -h