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.

Exciting News! Agile Yorkshire Shortlisted for Leeds Digital Festival Awards
Exciting News! Agile Yorkshire Shortlisted for Leeds Digital Festival Awards

a community we’ve proudly supported for over a decade

Discover More
Ada Lovelace Day: A Celebration of Women in Tech!
Ada Lovelace Day: A Celebration of Women in Tech!

This event will explore and promote strategies for increasing accessibility to technology careers for individuals from disadvantaged communities.

Discover More
An A-Level Student's First Journey into the World of Work: Jai Soni's Inspiring Week at NewRedo
An A-Level Student's First Journey into the World of Work: Jai Soni's Inspiring Week at NewRedo

“This week has been an eye-opening experience. I’ve learned so much from Alan and the team, not just about engineering but also about how our work can make a real difference. It’s been incredibly inspiring,”

Discover More