{{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. |image=Shrink_Virtual_Harddisk.png }}
* [[Grow_Virtual_Harddisk|Virtual Hard Disk Size Increase]] * [[Shrink_Virtual_Harddisk|Shrink Virtual Hard Disk Size]]
[[File:Shrink_Virtual_Harddisk.png|200px|thumb]] {{intro| Free disk space. Decrease the size of a virtual disk. Decrease virtual machine storage capacity. }} = Introduction = Over the course of many system upgrades, logging, and similar activity, the disk image files can grow substantially. Deleting obsolete files does not automatically shrink the disk size again, because it has grown to accommodate the data. = Overview = Note: When you create a large file or download a large file and then delete it from the {{VM}}, the space will not be freed automatically. Disk shrinking is required. In summary, the following steps are required to shrink the disk. {{IconSet|h1|1}} Inside the {{VM}}: Delete files to free space. {{IconSet|h1|2}} During VM boot: Restart the virtual machine and stop at the [[grub]] boot menu. {{IconSet|h1|3}} During VM boot: At the boot menu, boot into [[Live Mode|live mode]] - [[sysmaint]] session and make a temporary kernel boot parameter change by changing bdev_allow_write_mounted=0 to bdev_allow_write_mounted=1 for this boot only. Elaborated below. {{IconSet|h1|4}} Inside the {{VM}}: Apply the zerofree procedure. {{IconSet|h1|5}} Shut down the VM. {{IconSet|h1|6}} On the host {{os}}: Run a virtualizer-specific command to release the freed space. {{IconSet|h1|7}} Done. = Steps = {{Testers-Only}} Platform-specific. Select your virtualizer. * Non-Qubes: See below. * Qubes: [[Undocumented]]. [[unspecific|Unspecific to {{project_name_long}}]]. [[Self_Support_First_Policy|Self Support First Policy]] applies. Refer to Qubes documentation. A tool called zerofree is needed to shrink the backing virtual disk file. Since the default partitions are ext4, the process might be a bit involved: {{IconSet|h1|1}} Inside the VM: {{IconSet|h1|2}} Install the package zerofree in the virtual machine you want to shrink. {{Install Package|package= zerofree }} {{IconSet|h1|3}} Free space inside the VM. Delete what you no longer need in the VM, and run apt-get purge, apt-get autoremove, apt-get clean, etc. as needed. {{IconSet|h1|4}} During VM boot: {{IconSet|h1|5}} Restart the virtual machine and stop at the [[grub]] boot menu. {{IconSet|h1|6}} At the boot menu, boot into [[Live Mode|live mode]] - [[sysmaint]] session. Alternatively, booting into [[Persistent Mode|persistent mode]] - [[Recovery#Recovery_Mode|recovery mode]] would also be possible. But in that case, additional commands would need to be typed. {{CodeSelect|code= systemctl stop systemd-journald.socket }} {{CodeSelect|code= systemctl stop systemd-journald.service }} {{CodeSelect|code= mount -o remount,ro / }} {{IconSet|h1|7}} Make a [[Grub#Temporary_Kernel_Boot_Parameter_Change|Temporary Kernel Boot Parameter Change]] by changing bdev_allow_write_mounted=0 to bdev_allow_write_mounted=1 for this boot only. Alternatively, advanced users can make [[Grub#Permanent_Configuration_Changes|Permanent Configuration Changes]]. This is more complicated. [https://forums.whonix.org/t/how-to-compress-and-prevent-vdi-from-ballooning-after-each-update-deleting-large-files/22675/11 Forum discussion] {{IconSet|h1|8}} Inside the VM: {{IconSet|h1|9}} [[Grub#Inspect_Kernel_Command_Line|Inspect Kernel Command Line]]. Optional. For verification of kernel boot parameter change. {{CodeSelect|code= cat /proc/cmdline {{!}} grep --color bdev_allow_write_mounted }} Expected output:
bdev_allow_write_mounted=1
{{IconSet|h1|10}} Run zerofree. Platform-specific. Select your virtualizer. {{Tab |type=controller |linkid=virtualizer-choice-zerofree |content= {{Tab |title= == VirtualBox == |image=[[File:Virtualbox_logo.png|25px]] |addToClass=info-box |content= {{CodeSelect|code= sudo zerofree -v /dev/sda3 }} }} {{Tab |title= == KVM == |image=[[File:Kvm-new-logo.png|25px]] |addToClass=info-box |content= {{CodeSelect|code= sudo zerofree -v /dev/vda1 }} }} {{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. }} }} {{IconSet|h1|11}} Shut down the VM. {{IconSet|h1|12}} On the host: {{IconSet|h1|13}} Compact the host VM image. Platform-specific. Select your virtualizer. {{Tab |type=controller |linkid=virtualizer-choice-zerofree |content= {{Tab |title= == VirtualBox == |image=[[File:Virtualbox_logo.png|25px]] |addToClass=info-box |content= Host operating system-specific. * Linux: VBoxManage modifymedium disk "/path/to/disk.vdi" --compact * Windows: VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact [[unspecific|Unspecific to {{project_name_long}}]]. [[Self_Support_First_Policy|Self Support First Policy]] applies. {{IconSet|h1|14}} Done. Forum discussions: * [https://forums.whonix.org/t/how-to-compress-and-prevent-vdi-from-ballooning-after-each-update-deleting-large-files/22675 How to compress and prevent vdi from "ballooning" after each update and deleting large files?] * [https://forums.whonix.org/t/automate-recovering-free-space-from-vdi-disks/22467 Automate recovering free space from VDI disks] }} {{Tab |title= == KVM == |image=[[File:Kvm-new-logo.png|25px]] |addToClass=info-box |content= {{IconSet|h1|14}} Open a terminal on the host. {{IconSet|h1|15}} Switch to root. {{CodeSelect|code= sudo -s }} {{IconSet|h1|16}} Change directory to the /var/lib/libvirt/images folder. {{CodeSelect|code= cd /var/lib/libvirt/images }} {{IconSet|h1|17}} View files inside the /var/lib/libvirt/images folder. {{CodeSelect|code= ls }} {{IconSet|h1|18}} Create a backup 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 hard disk. * Replace YourVirtualMachineDisk.qcow2.backup with the backup name for your virtual hard disk. {{CodeSelect|code= mv YourVirtualMachineDisk.qcow2 YourVirtualMachineDisk.qcow2.backup }} {{IconSet|h1|19}} Shrink the disk using qemu-img. {{CodeSelect|code= qemu-img convert -O qcow2 -p YourVirtualMachineDisk.qcow2.backup YourVirtualMachineDisk.qcow2 }} {{IconSet|h1|20}} Boot up the VM and see if it is working. If it is, you can delete the backup of the qcow2 file. {{IconSet|h1|21}} Done. There are more advanced approaches using thin provisioned images based on a static qcow2 backing file, which is often preferred. However, the above is a basic method for producing a smaller 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 post in "Is it possible to re-shrink the qcow2 image files?"] Forum discussion: [https://forums.whonix.org/t/is-it-possible-to-re-shrink-the-qcow2-image-files/13228/9 Is it possible to re-shrink the qcow2 image files?] }} {{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. }} }} = Build from Source Code = Refer to [[Dev/Build_Documentation|Build and Update {{project_name_short}} from Source Code]] and use this setting: {{CodeSelect|code= --vmsize 50G }} = Future = Either: * {{IconSet|h1|A}} [[dracut|dracut]] module, or * {{IconSet|h1|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. = Forum Discussion = * [https://forums.whonix.org/t/the-workstation-vmdk-gets-bigger-and-bigger/218 The Workstation VMDK gets bigger and bigger] * [https://forums.whonix.org/t/automate-recovering-free-space-from-vdi-disks/22467 Automate recovering free space from VDI disks] = See Also = * [[Grow_Virtual_Harddisk|Grow Virtual Hard Disk Size Increase]] = Footnotes = {{reflist|close=1}} {{Footer}} [[Category:Documentation]]