{{Header}} {{Title|title= Shrink Virtual Hard Disk Size }} {{#seo: |description=Free disk space. Decrease the size of a virtual disk. Decrease virtual machine storage capacity. }}
zerofree
procedure.
'''4.''' Shut down.
'''5.''' Virtualizer specific command to release the freed space.
'''6.''' Done.
== Steps ==
{{Tab
|type=controller
|content=
{{Tab
|title= == VirtualBox ==
|image=[[File:Virtualbox_logo.png|25px]]
|addToClass=info-box
|active=true
|content=
[[Undocumented]]. [[unspecific|Unspecific to {{project_name_long}}]]. [[Self_Support_First_Policy|Self Support First Policy]] applies.
}}
{{Tab
|title= == KVM ==
|image=[[File:Kvm-new-logo.png|25px]]
|addToClass=info-box
|content=
A tool called zerofree free is needed to shrink the backing virtual disk file. Since the default are ext4 partitions, the process might be bit involved:
Inside the VM:
'''1.''' Install the package zerofree
in the virtual machine you want to shrink.
{{Install Package|package=
zerofree
}}
'''2.''' Erase what you need in the VM, run apt-get purge, apt-get autoremove, apt-get clean, etc. as needed.
'''3.''' Reboot your virtual machine in recovery mode.
'''4.''' At the command prompt, type
{{CodeSelect|code=
systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && mount -o remount,ro / && zerofree -v /dev/vda1 && shutdown now
}}
'''5.''' Shut down the VM.
On the host:
'''6.''' Open a terminal on the host.
'''7.''' Switch to root.
{{CodeSelect|code=
sudo -s
}}
'''8.''' Change directory to the /var/lib/libvirt/images
folder.
{{CodeSelect|code=
cd /var/lib/libvirt/images
}}
'''9.''' View files inside the /var/lib/libvirt/images
folder.
{{CodeSelect|code=
ls
}}
'''10.''' Create a back up of the disk you want to shrink by moving YourVirtualMachineDisk.qcow2
to YourVirtualMachineDisk.qcow2.backup
.
Note:
* Replace YourVirtualMachineDisk.qcow2
with the actual name of your virtual harddisk.
* Replace YourVirtualMachineDisk.qcow2.backup
with the actual name of your virtual harddisk.
{{CodeSelect|code=
mv YourVirtualMachineDisk.qcow2 YourVirtualMachineDisk.qcow2.backup
}}
'''11.''' Shrink the disk using qemu-img
.
{{CodeSelect|code=
qemu-img convert -O qcow2 -p YourVirtualMachineDisk.qcow2.backup YourVirtualMachineDisk.qcow2
}}
'''12.''' Boot up the VM and see if it is working. If it is, you can delete the backup of the qcow file.
There are ways you can get more creative with this using thin provisioned images that are based on a static qcow2 backing file, a preferred method. However the above is a basic method for getting a shrunken qcow2 disk file.
Credit goes to forum user [https://forums.whonix.org/u/tempest tempest]: https://forums.whonix.org/t/is-it-possible-to-re-shrink-the-qcow2-image-files/13228/3
forum discussion: https://forums.whonix.org/t/is-it-possible-to-re-shrink-the-qcow2-image-files/13228/9
}}
{{Tab
|title= == Qubes ==
|image=[[File:Qubes-logo-blue.png|25px]]
|addToClass=info-box
|active=false
|content=
[[Undocumented]]. [[unspecific|Unspecific to {{project_name_long}}]]. [[Self_Support_First_Policy|Self Support First Policy]] applies.
Refer to the Qubes OS user documentation.
}}
}}
Forum discussion:
https://forums.whonix.org/t/the-workstation-vmdk-gets-bigger-and-bigger/218
= Build from Source Code =
Refer to [[Dev/Build_Documentation|Build and Update {{project_name_short}} from Source Code]] and utilize this setting:
{{CodeSelect|code=
--vmsize 50G
}}
= Future =
Either,
* A) dracut module or
* B) mkosi-initrd systemd unit,
that runs zerofree before mounting the root disk would be useful. Such a feature does not exist yet in any Linux distribution.
= See Also =
* [[Grow Virtual Harddisk]]
= Footnotes =
{{reflist|close=1}}
{{Footer}}
[[Category:Documentation]]