Shrink QEMU/QCOW2 Images

Shrink QEMU/QCOW2 Images

Phill Luby
16th March 2017

Home Insights Shrink QEMU/QCOW2 Images

Needed to shrink a full directory of QEMU QCOW2 images today that had grown over time with snapshots and general use. While there is information around on the web about it, the focus is on single images and the code provided are just examples.

Here’s a script to keep around that shrinks and compresses all images in a folder, and shows progress:

#!/bin/bash
for imageFile in *.qcow2
do
    echo "Processing $imageFile"
    mv "$imageFile" "$imageFile.orig"
    qemu-img convert -p -c -O qcow2 "$imageFile.orig" "$imageFile"
done
echo "Warning: Test images before deleting the .orig files"

For convenience I stuck this in my QEMU image directory and made it executable. Be sure to test images and then delete the “.orig” files to save space.

Share Article

Insights.

Sponsoring the Legal Tech in Leeds’ Hackathon
Sponsoring the Legal Tech in Leeds’ Hackathon

This is why NewRedo are so proud to have sponsored Legal Tech in Leeds Hackathon

Discover More
Taking a look at Dagger
Taking a look at Dagger

Solomon Hykes is probably most famous for being the founder and former CTO of Docker. Docker revolutionised the way we package, run and distribute server applications, so when Hykes starts a new venture, it's worth checking out.

Discover More
Running Terraform in a Lambda Function
Running Terraform in a Lambda Function

I recently set up a Terraform project which I wanted to run on a regular schedule. There are a number of ways to achieve this, but I decided to package the project as a Lambda function and schedule it with… 

Discover More