「KVM」- 扩容 qcow2 镜像、磁盘扩容、零重启

  CREATED BY JENKINSBOT

问题描述

我们的虚拟机是从虚拟机模板创建的,磁盘默认仅有 10G 空间,无法容纳更多数据,因此需要进行扩容。

注意事项,错误的磁盘分区调整将导致数据的丢失,如果无足够经验,请先进行数据备份,防止数据丢失。

该笔记将记录:在 KVM 中,扩容 .qcow2 操作系统镜像文件的方法,以及常见问题的处理方法。

解决方案

第一阶段、磁盘扩容

通过 virsh 扩容(**推荐**):

# virsh qemu-monitor-command --domain tmpl-ubuntu-1804  --hmp "info block"
drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2 ro=0 drv=qcow2 encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-ide0-0-0: removable=1 locked=0 tray-open=0 io-status=ok [not inserted]

# virsh qemu-monitor-command --domain tmpl-ubuntu-1804  --hmp "block_resize drive-virtio-disk0 20G"

注意事项,请勿缩小磁盘,如果需要缩小磁盘,请先缩小内部文件系统。(这里不再展开讨论)

第二阶段、调整文件系统

接下来,需要进入虚拟机,进行操作系统磁盘空间扩容。该阶段的场景比较多(有人使用 LVM,有人使用 Ext4,有人使用 XFS),我们无法枚举,这里仅记录我们的场景。

我们使用 LVM 进行磁盘管理,上层是 XFS 文件系统,所以扩容操作过程如下:

pvresize "/dev/vda2" # 扩大 PV 空间

vgs # 能够看到 VG 空间已经扩大

lvextend -l +100%FREE /dev/centos/root # 将空间全部分配
lvdisplay # 查看扩容后的大小,能够看到 LV 已扩大

xfs_growfs /dev/centos/root # 该虚拟机为 XFS 文件系统,需要 xfs_growfs 命令

方法二、通过 qemu-img resize 扩容

我们**不推荐**这种做法(在操作系统中,磁盘空间未改变,需要额外处理)

第一步、停止虚拟机实例

第二步、查看磁盘信息

# virsh domblklist tmpl-ubuntu-1804
 Target   Source
-----------------------------------------------
 vda      /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2

# qemu-img info /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2
image: /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 2.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

如果虚拟机磁盘包含快照(snapshot)信息,则需要删除快照:

# virsh snapshot-list tmpl-ubuntu-1804
 Name        Creation Time               State
--------------------------------------------------
 snapshot1   2019-04-16 08:54:24 +0300   shutoff

# virsh snapshot-delete --domain tmpl-ubuntu-1804 --snapshotname snapshot1
Domain snapshot snapshot1 deleted

第三步、虚拟机磁盘扩容

# qemu-img resize /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2 +5G # 增加 5G 空间
Image resized.

# qemu-img info /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2
image: /var/lib/libvirt/images/tmpl-ubuntu-1804.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 2.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

或者,使用 virsh blockresize 重置:

# virsh blockresize rhel8 /var/lib/libvirt/images/rhel8.qcow2 40G

第四步、启动虚拟机并验证

# lsblk 
 NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 sr0            11:0    1 1024M  0 rom  
 vda           252:0    0   40G  0 disk 
 ├─vda1        252:1    0    1G  0 part /boot
 └─vda2        252:2    0   29G  0 part 
   ├─rhel-root 253:0    0 26.9G  0 lvm  /
   └─rhel-swap 253:1    0  2.1G  0 lvm  [SWAP]

第五步、扩容文件系统

How To resize an ext2/3/4 and XFS root partition without LVM | ComputingForGeeks
How to extend root filesystem using LVM on Linux | ComputingForGeeks

参考文献

Increasing the Size of a qcow2 Image Under KVM – Natural Born Coder
Linux KVM – How to Add/Resize Virtual disk on fly? Part 7 – UnixArena
How To extend/increase KVM Virtual Machine (VM) disk size | ComputingForGeeks