0x0034's Blog.

kvm qcow2磁盘离线扩容

字数统计: 1.1k阅读时长: 5 min
2022/01/17

以 tmp_ka30 虚拟机为例, 源机器磁盘大小40GB

准备操作

关闭指定虚拟机

qcow2的磁盘扩容,貌似不能在线扩容,如果有能在线扩容的方法,wanxialianwei@gami.com 告诉我,感激不尽

1
2
3
4
5
6
7
8
9
10
11
12
# 关闭指定虚拟机
[root@jzbj tmp]# virsh stop tmp_ka30

# 查看虚拟机状态
[root@jzbj tmp]# virsh list --all
Id 名称 状态
----------------------------------------------------
16 k120-anolis_ka29 running
26 daily-anolis_ka28 running
63 h331-anolis_ka28 running
- tmp_ka30 关闭

寻找img文件路径

/home/lib/libvirt/images/tmp_ka30.img 就是本例的镜像文件路径

1
2
3
4
5
6
7
8
9
10
11
12
# 查看qcow2 镜像路径
[root@jzbj tmp]# virsh dumpxml tmp_ka30
...
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/home/lib/libvirt/images/tmp_ka30.img'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='ua-box-volume-0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</disk>

移动qcow2 镜像

一定要记得备份文件, 谨记. 问就是血泪教训.

1
[root@jzbj images]# mv tmp_ka30.img tmp_ka30.img.origin

开始扩容

创建新的磁盘镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@jzbj images]# qemu-img create -f qcow2 tmp_ka30.img  50G
Formatting 'tmp_ka30.img', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 lazy_refcounts=off
# 检查磁盘镜像
# 显示qcow2文件虚拟容量50G,实际目前是空的稀疏文件,所以只占用了196K空间
[root@jzbj images]# qemu-img info tmp_ka30.img
image: tmp_ka30.img
file format: qcow2
virtual size: 50G (53687091200 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false

检查原镜像信息

这一步的主要作用是确定要迁移的磁盘

1
2
3
4
5
[root@jzbj images]# virt-filesystems -a tmp_ka30.img.origin --all --long -h
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 20G -
/dev/sda1 partition - - 83 20G /dev/sda
/dev/sda device - - - 40G -

resize磁盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@jzbj images]# virt-resize tmp_ka30.img.origin tmp_ka30.img --expand /dev/sda1
[ 0.0] Examining tmp_ka30.img.origin
**********

Summary of changes:

/dev/sda1: This partition will be resized from 20.0G to 50.0G. The
filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’
method.

**********
[ 2.5] Setting up initial partition table on tmp_ka30.img
[ 2.7] Copying /dev/sda1
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 70.5] Expanding /dev/sda1 using the ‘resize2fs’ method

Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.

启动,验证扩容

检查扩容

1
2
3
4
5
[root@jzbj images]# virt-filesystems -a tmp_ka30.img --all --long -h
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 50G -
/dev/sda1 partition - - 83 50G /dev/sda
/dev/sda device - - - 50G -

启动虚拟机,检查

1
2
3
4
5
6
7
8
9
10

启动虚拟机 确认磁盘信息
[root@ka30 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 964M 0 964M 0% /dev
tmpfs 981M 0 981M 0% /dev/shm
tmpfs 981M 8.4M 973M 1% /run
tmpfs 981M 0 981M 0% /sys/fs/cgroup
/dev/vda1 50G 2.0G 45G 5% /
tmpfs 197M 0 197M 0% /run/user/1001

去除镜像空洞

当磁盘的实际使用量和磁盘文件大小不一致的时候需要去除磁盘空洞

1
2
3
4
5
6
7
8

# 备份磁盘文件
mv tmp_ka30.img tmp_ka30.img.origin

# 去除磁盘镜像空洞
# --tmp 的目的是 默认的tmp目录大小并不能支撑到镜像除去空洞, 所以需要指定一个足够大的磁盘来存储临时文件
# 当然, 如果磁盘足够大 当我没说
virt-sparsify --tmp /ssd1t/kvm/images/ tmp_ka30.img.origin -f qcow2 tmp_ka30.img

接着就是重启虚拟机来验证, 后面省略了.

虚拟机在/etc/fstab 中写入错误无法启动的恢复流程

查看虚拟机镜像的磁盘分区

1
2
virt-filesystems -a tmp_ka30.img
# 输出: /dev/sda1 , 也就是本例中tmp_ka30.img镜像中的磁盘分区

挂载镜像分区

1
2
# 将tmp_ka30.img镜像的/dev/sda1磁盘分区 挂载到机器的/mnt/sysmount 目录上
guestmount -a tmp_ka30.img -m /dev/sda1 /mnt/sysmount

根据需求修改系统文件

这边就需要根据个人需求了, vm的文件系统已经挂载到/mnt/sysmount, 修改即可.

卸载分区

1
umount mnt/sysmount
CATALOG
  1. 1. 准备操作
    1. 1.1. 关闭指定虚拟机
    2. 1.2. 寻找img文件路径
    3. 1.3. 移动qcow2 镜像
  2. 2. 开始扩容
    1. 2.1. 创建新的磁盘镜像
    2. 2.2. 检查原镜像信息
    3. 2.3. resize磁盘
  3. 3. 启动,验证扩容
    1. 3.1. 检查扩容
    2. 3.2. 启动虚拟机,检查
  4. 4. 去除镜像空洞
  5. 5. 虚拟机在/etc/fstab 中写入错误无法启动的恢复流程
    1. 5.1. 查看虚拟机镜像的磁盘分区
    2. 5.2. 挂载镜像分区
    3. 5.3. 根据需求修改系统文件
    4. 5.4. 卸载分区