Root-Server Tutorial

ROOT-SERVER TUTORIAL

Edit and Shrink a qcow2 Snapshot (with QEMU on Windows or Another OS)

Learn how to edit the contents of a qcow2 virtual server snapshot and shrink the snapshot file to the required size.

Author:

NaN

Last updated:

03/04/2023

Introduction

This tutorial describes how to edit the contents of a snapshot of a qcow2 virtual server and shrink the snapshot file to the required size.

This can be useful if a snapshot containing large amounts of data is to be stored in a different location. Shrinking the snapshot file size reduces the time it takes to upload the file to a virtual server.

The aim of this tutorial is to show a method that works on a Windows host, but this tutorial may also be useful for those using other operating systems.

Requirements

This method requires enough hard disk space to store the unedited disk image file in addition to a copy from which the unnecessary data will be removed.

It is assumed that the snapshot image has already been downloaded and is in qcow2 format. The user should be sufficiently familiar with the Linux CLI to navigate a file system and selectively delete unwanted files.

Step 1 - Download and install QEMU

Download the binaries for QEMU and install them.

Step 2 - Download a Live-ISO of a Linux distribution

For this tutorial, we will use Alpine Linux, a lightweight Linux distribution. For the purpose of this tutorial, download the "Virtual" x86_64 ISO file.

Step 3 - Start a virtual machine

Use QEMU to start a virtual machine with Alpine Linux as OS and the qcow2 snapshot file you would like to resize as the virtual hard disk, by executing the following command:

qemu-system-x86_64.exe -m 2048 -smp 4 -boot d -cdrom alpine-virt-3.17.3-x86_64.iso -drive if=virtio,driver=qcow2,discard=on,file=diskimage.qcow2

Please note that you might need to adjust this command to include the path to your QEMU binary, the Alpine ISO and the qcow2 image. If you are relying on a Linux distribution other than Alpine or are using a newer version of Alpine, you will also need to adjust the command accordingly.

Note that the -m 2048 option specifies that QEMU should reserve 2GB of RAM for the virtual machine and -smp 4 assigns four virtual CPU cores to the VM. This may be reduced or increased depending on your needs and system configuration. The -boot d parameter sets the boot device to the cdrom ISO, which is provided following the -cdrom parameter.

The -drive parameter instructs QEMU to attach the virtual hard disk as a virtio device, backed by the qcow2 driver, and to pass the discard instruction to the driver.

For compatibility reasons, this command does not enable hardware accelerated virtualization, so the execution may be slower than expected. You can try -accel whpx or -accel haxm on Windows and -accel kvm on Linux if you want to activate hardware acceleration. If this doesn't work, just use the slower software emulation by forgoing those command arguments.

Step 4 - Edit the hard disk from inside Alpine Linux

  1. Log in as root with no password

  2. Change the keymap if you're not using a US keyboard layout:

    setup-keymap

    Type setup, press the tab key to get the hyphen, type keymap.

    For a German keyboard layout, enter 'de' twice.

  3. You can list the partitions with:

    fdisk -l

  4. Mount the partition you want to work on, in our example /dev/vda1:

    mount /dev/vda1 /mnt

  5. Delete the files that you want to purge from the snapshot:

    cd /mnt

    ...use cd, find, rm, etc. to search and delete unnecessary files...

    cd /

  6. Mark the empty space in the filesystem as unused by running:

    fstrim -v /mnt

  7. Unmount the partition:

    umount /mnt

  8. Turn off the virtual machine:

    poweroff

Step 5 - Expunge the unused space from the snapshot image

Now that the empty space in the disk image file has been marked as unallocated, "converting" the disk image with qemu-img will yield a smaller copy without the freed space. Run the following command to receive a smaller, trimmed qcow2 image:

qemu-img.exe convert -f qcow2 -O qcow2 diskimage.qcow2 diskimage-trimmed.qcow2

Again, you may need to specify the correct paths for your specific situation.

The qcow2 file format supports compression, which can further reduce the size of the snapshot file. This can optionally be enabled by adding the modifier -c, but please note that this will increase the conversion time significantly:

qemu-img.exe convert -f qcow2 -O qcow2 -c diskimage.qcow2 diskimage-trimmed.qcow2

Conclusion

After completing this tutorial, you have two copies of the snapshot disk image: the original one with the changes you made in the VM, and the copy that was shrunk and does not contain any deleted files or unused space. You can now delete the larger image file as the smaller image file does not depend on it. Use the smaller file with your netcup Server, it should be much easier and faster to upload thanks to the reduced file size.

Licence

MIT

Copyright (c) 2023 netcup

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the licence indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate licence and I have the right under that licence to submit that work with modifications, whether created in whole or in part by me, under the same licence (unless I am permitted to submit under a different licence), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the licence(s) involved.

Published 03/04/2023 by NaN

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.