虚拟机扩展lvm磁盘
2020-08-08 本文已影响0人
分享放大价值
安装vm后,如果root分区太小,影响使用(如下图,可用磁盘空间只有100M),又不想重新安装vm,可以扩展一下磁盘(前提是安装vm时选择的是lvm格式)

a. 添加硬盘
将vm关机(virsh destroy xxx),在vm的xml配置文件(virsh edit xxx)中新添加一块硬盘,其中 ubuntu16.04-disk2.qcow2 是一个qcow2格式的文件,相当于一块空硬盘,可以通过如下命令创建
qemu-img create -f qcow2 ubuntu16.04-disk2.qcow2 30G

vm开机后(virsh start xxx),使用fdisk -l查看到vm已经有了两个磁盘,/dev/sdc即为新添加的硬盘

b. 创建PV
root@contrail1:~# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
查看当前的pv,能看到 /dev/sdc 是新添加的
root@contrail1:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda5
VG Name ubuntu-vg
PV Size 39.52 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10117
Free PE 0
Allocated PE 10117
PV UUID V3Gksu-cnb0-NiC5-LJNL-1hnc-Ye8U-3Q46Vd
--- Physical volume ---
PV Name /dev/sdb
VG Name ubuntu-vg
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2569
Allocated PE 2550
PV UUID ZYpGAU-XX61-53Ed-A6Mn-pJ1K-bXuG-1ztDK0
"/dev/sdc" is a new physical volume of "30.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 30.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID FYNlZ2-Ion5-zPWW-JIVS-naNk-WCAu-PQU1Ev
将新添加的pv /dev/sdc 扩展到卷组 ubuntu-vg
root@contrail1:~# vgextend ubuntu-vg /dev/sdc
Volume group "ubuntu-vg" successfully extended
c. 扩展到lv

此时查看root分区还没有变化,需要格式化后才能使用

d. 格式化
格式化需要根据磁盘类型使用不同的工具,如果文件系统类型为ext2/ext3/ext4,则使用resize2fs,如下图,本实例:(ubuntu)

如果文件系统类型为xfs,则使用xfs_growfs(centos)


再次查看root分区大小,已经增加了30G
