0% found this document useful (0 votes)
47 views

Resizing KVM Qcow2 Images - Maublog

Uploaded by

R2D2 Sanshiro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Resizing KVM Qcow2 Images - Maublog

Uploaded by

R2D2 Sanshiro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

&

blog
;
code
a
contact

Resizing KVM
qcow2 images
While expanding qcow2 images is easy, nding an easy way to shrink them was not.

Recently, my Matrix VM ran out of disk space, while my miscellaneous hosting VM had way
too much space.

Since resizing images is often useful, I decided to make a fairly simple tutorial on how to
expand and shrink the images.

Expanding images
This is fairly straightforward. Due to the design of qcow2 images, you don’t even need to
have the disk space available right away.

1. Shut down the virtual machine

2. Resize the image with


qemu-img resize image.qcow2 +SIZE

where SIZE is the size (e.g. 10G for 10 gibibytes).

3. Boot into an external live OS and resize the partition. The easiest way to do this is to use a
GParted live image and virt-manager to connect to the VM.

Shrinking images
This is a bit more di cult. You will also need to have disk space to t both images at the
same time. For example, you want to resize a 100gb allocation to 50gb, your current image
shows 60gb used from outside (the qcow2 image) and has 30gb actually used inside. For
the change, you’ll need to have 50gb space for the resized image and 60gb for the existing
image, so 110gb total.

1. Resize the partition (see step #3 of expanding images)

2. If you managed to resize the partition from within the virtual machine (and thus didn’t shut
it down already for resizing), shut it down now.

3. KVM/QEMU images are stored in /var/lib/libvirt/images by default. It should be root-


only, so sudo su is acceptable in this case.

Create the new smaller image:

qemu-img create -f qcow2 -o preallocation=metadata newimage.qcow2


NEW_SIZE

where NEW_SIZE is the size (50G for the example at the start).

4. Resize the image by copying the old image into the new one.

virt-resize oldimage.qcow2 newimage.qcow2

If the image created in the previous step is larger than the combined partitions on the old
image, virt-resize will inform you of a surplus and create a new partition. You can still
terminate the process without data loss and go back to step #3 to create a smaller image.

If the image is smaller than the partitions, virt-resize will fail and inform you how much
space needs to be added. In this case, you must create a larger image in step #3.

5. Start your VM. There may be some disk errors related to the stored block lengths. fsck
should be able to automatically x them.

If virt-resize created an extra partition, you can now use a partition editor to delete it and
add the space to another partition.
6. Once you have veri ed that the VM is working as expected, you can safely remove the old
image.

Fri Apr 6, 2018


500 Words

Read in about 3 Min

Tutorial

  Fixing GRUB not loading 


Comments
2 Comments Maunium 🔒 
1 Login

 Favorite t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Михаил Н • 2 years ago


I've done this much faster with virt-sparsify --in-place disk.qcow2:
http://libguestfs.org/virt-...
3△ ▽ • Reply • Share ›

David Konsumer > Михаил Н • 2 years ago


This is a great tip. I didn't know about virt-sparsify, and it
was faster, and can do other cool stuff like sparsify raw
images, or convert between formats (I converted a raw
image very quickly to clean & sparse qcow2.) On top of
that, it has a nicer CLI UI:

△ ▽ • Reply • Share ›

Copyright © 2016-2018 Tulir Asokan. Licensed under CC BY 4.0 . Powered by Hugo .

You might also like