Extending Qcow2 partition

Taken from https://blog.khmersite.net/2020/11/how-to-expand-qcow2/

Let’s check what is the current size.

# qemu-img info hosta.qcow2
image: hosta.qcow2
file format: qcow2
virtual size: 30 GiB (32212254720 bytes)
disk size: 1.18 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
Let’s add 20GiB to this disk to make it to 50GiB.

# qemu-img resize hosta.qcow2 +20G
Image resized.
Next we need to resize the filesystem on this disk. There are a few ways to do this such as attaching this hostq.qcow2 to another VM and expand it there, or just manually grow the LVM volume group in my case. However, I found a really cool way by using the virt-resize command.

# cp hosta.qcow2 hosta-orig.qcow2
Then carefully instruct the virt-resize to expand the correct partition. On my server, /dev/sda1 is used for /boot and /dev/sda2 is used as a LVM physical volume.

There is a nice trick to display the disk filesystem on a (KVM) VM.

# virt-filesystems --long -h --all -a hosta.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1014M -
/dev/cl/root filesystem xfs - - 27G -
/dev/cl/swap filesystem swap - - 2.0G -
/dev/cl/root lv - - - 27G /dev/cl
/dev/cl/swap lv - - - 2.0G /dev/cl
/dev/cl vg - - - 29G /dev/sda2
/dev/sda2 pv - - - 29G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 8e 29G /dev/sda
/dev/sda device - - - 50G -
From the above output, we can see that the /dev/sda has already been expanded to 50GiB. But the /dev/sda2 is still only 29GiB (1GiB has been allocated to /dev/sda1 for the Boot partition.) So let’s expand the /dev/sda2.

# virt-resize --expand /dev/sda2 hosta-orig.qcow2 hosta.qcow2
[ 0.0] Examining hosta-orig.qcow2

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 29.0G to 49.0G. The LVM PV
on /dev/sda2 will be expanded using the ‘pvresize’ method.

[ 3.7] Setting up initial partition table on hosta.qcow2
[ 4.8] Copying /dev/sda1
[ 6.9] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 30.3] Expanding /dev/sda2 using the ‘pvresize’ method

Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
Let’s verify our result.

# virt-filesystems --long -h --all -a hosta.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1014M -
/dev/cl/root filesystem xfs - - 27G -
/dev/cl/swap filesystem swap - - 2.0G -
/dev/cl/root lv - - - 27G /dev/cl
/dev/cl/swap lv - - - 2.0G /dev/cl
/dev/cl vg - - - 49G /dev/sda2
/dev/sda2 pv - - - 49G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 8e 49G /dev/sda
/dev/sda device - - - 50G -
On the background, virt-manager actually created a temporary VM, perform the disk expansion, virt-filesystem check and remove that VM upon completion.


Trackback specific URI for this entry

This link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.

No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.