How To Resize Qcow2 Disks Image
In in this post we will see How to resize qcow2 disks image used in KVM virtual images, Actually situation is like that, Today I clone one of my base image which i need to work on, But further working on same I need to resize its Disk size.
In this case , we are going to ShutDown virtual machine, while resizing Disk used in KVM virtual machine.
SetUP
We can use any Guest KVM virtual machine for this setup. So mentioning what Host machine used for this Setup
root@jarvis:~# lsb_release -d Description: Ubuntu 18.04 LTS root@jarvis:~# uname -r 4.15.0-23-generic root@jarvis:~# kvm --version QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.4) Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers
For this first we have to shutdown Virtual Machine, its always good to poweroff virtual machine from login prompt. otherwise you can also shutodown or destroy virtual Machine through virsh command as well.
once your machine is in shutoff state as of mine in below output, we can start resize procedure.
root@jarvis:~# virsh list --all| grep gpw - gpw shut off
To know disk used in this Vitual Machine, we could use virsh domblklist command like below.
root@jarvis:~# virsh domblklist gpw Target Source ------------------------------------------------ hda /data/vm_images/geekpills_wordpress/system.img
Then we could check Disk size and can analyse how much we need to increase, as per my requirement I need to increase(I suggest not to decrease disk size, if you don’t know much about qcow2 format and how to troubleshoot these cases). decreasing could dangerous in case Disk consumption is more what you trying to decrease.
root@jarvis:~# qemu-img info /data/vm_images/geekpills_wordpress/system.img image: /data/vm_images/geekpills_wordpress/system.img file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 1.2G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false
Once we understand what is requirement we could resize disk like below.
root@jarvis:~# qemu-img resize /data/vm_images/geekpills_wordpress/system.img +10G Image resized.
Once we resize we could see resize size in qemu-img command.
root@jarvis:~# qemu-img info /data/vm_images/geekpills_wordpress/system.img image: /data/vm_images/geekpills_wordpress/system.img file format: qcow2 virtual size: 30G (32212254720 bytes) disk size: 1.2G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false
And now , its time to check Disk size in Virtual machine.
root@jarvis:~# virsh console gpw Connected to domain gpw Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-693.17.1.el7.x86_64 on an x86_64 localhost login: root Password: Last login: Sun Jul 29 20:34:05 on ttyS0 [root@localhost ~]# fdisk -l| grep /dev/sd[a-z]: Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Thanks for the article. Have a question. if I attached the qcow2 file as a nbd device ( qemu-nbd)
Will this nbd device will automatically recognize the new size change post size expansion
Also in a circurmstance say the Qcow2 size is reaching mximum volume size set by the OS, How do I increase the size to the Qcow2 file. Are there any strategies available for this
I am sure, nbd will pick-up new size of Qcow2 image, might we need to remount it only. i need to test and check, only then i could commit it to anyone.