Score:0

KVM - Reduce Storage Size of QCOW2 File To Specific Size

fj flag

I want to reduce the storage size of the VM's running on a Linux 8 physical machine from 2 TB to 400 GB. The actual size of VM's is around 100 GB. But the space we give it as allocated size is too big and I want to reduce it.

What I'm talking about is not reducing the VM's available storage, but reducing storage disk size of VM's disks from 2TB to 400GB.

All the required RPMs are installed and there is nothing extra I need to do inside the VM. However, I should definitely not lose the current data in the VM. How can I do that?

I know that the VM should be kept off during the process. I have no problem with this, if there is a way to do it online I don't have to use this method.

Or can I simply edit this using the XML files that make up the VM?

As you can see from the questions, I'm not an expert on this subject. I would appreciate a detailed answer.

Flash avatar
sr flag
Hello sir,Please include your storage backend (local storage/lvm, or remote NFS, ceph, glusterfs) it matters for the possibilities. "Linux 8" is not a thing, is this "centos linux 8" ? or something else, while it does not matter for functionality much, it does influence software paths and versions.
rndm_acct1 avatar
fj flag
@Flash Hello sir. Oracle Linux 8.7 / LVM + XFS.
Flash avatar
sr flag
Thank you, my answer below stands and should be valid.
Score:0
sr flag

The first thing you should be aware of, thin provisioned disks do not shrink. They may start small but after will only grow. Freed up space inside the VM will get reused before growing the disk however. Different storage backends have different capabilities. For example, NFS does not support thin provision.

To reduce the size of a vm disk you can do the following.

  • Defrag your VM. use 'defraggler' or 'ultradefrag' to get this done.

  • write 0's to the disks free space.

    • On windows this can be done with the 'sdelete' utility. sdelete -z or sdelete -z c: - in case of multiple drives do this once for each.
    • On linux use dd to write a 0 file to the disk which you can after remove. dd if=/dev/zero of=/tmpfile; rm /tmpfile you may need to do this for other paths then root (/) if you have multiple mounts.
  • On the Physical host use qemu-img to 'copy' the drive to a new thin provisioned file. cp image.qcow2 image.qcow2_backup; qemu-img convert -O qcow2 image.qcow2_backup image.qcow2

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.