Difference between revisions of "How to Shrink a VirtualBox Virtual Machine"
(Created page with "== How to Shrink a VirtualBox Virtual Machine == '''NOTE: In this guide we are using a Linux HOST and a Linux GUEST.''' By default, VirtualBox creates dynamic disks that gro...")
Revision as of 12:26, 11 April 2019
- 1 How to Shrink a VirtualBox Virtual Machine
How to Shrink a VirtualBox Virtual Machine
NOTE: In this guide we are using a Linux HOST and a Linux GUEST.
By default, VirtualBox creates dynamic disks that grow over time as you add data. However, if you delete data from the virtual machine later, you’ll notice that the disk doesn’t automatically shrink. But you can manually shrink a dynamic disk using a hidden command.
Step One: Ensure You’re Using a Dynamic Disk
This process only works for dynamic disks, which can grow and shrink in size. If you have a fixed size disk you want to shrink, you can first convert it to a dynamic disk.
Step Two: Write Zeros to the Disk in The Virtual Machine
If you have Linux installed in as GUEST in the virtual machine use built-in commands to zero the free space on the drive. We’ll use Ubuntu as the example here, but the process will be similar on other Linux distributions.
To do this, you’ll first need to install the zerofree utility inside the virtual machine. It should be available in your Linux distribution’s software repositories. For example, you can install it on Ubuntu by running the following command at the terminal inside your virtual machine:
sudo apt install zerofree
The manual for zerofree indicates that if you want to run this on a filesystem, then that filesystem must be either mounted readonly, or unmounted. When you are running Ubuntu, it is certain that the root partition /, which is /dev/sda1 on your computer, is mounted thus the command will give a failure.
If you want to run zerofree on /dev/sda1 you will need to either boot from an alternate device, uch as a bootable USB or ISO image, or perform a chroot to an alternate linux image. The link that you reference has specific instructions for you, on how to run this program on your root partition. Please re-read their instructions on how to use this program in Linux: Boot and access Grub before the OS loads
- Choose 'Advanced Options' from the grub menu
- Enter a recovery mode session
- Use the 'root' login
- Identify the disk
- Run zerofree
- Halt the machine
- Powerdown and restart the VM
Another successful solution (TESTED OK)
- Boot using a Live CD,
- Install zerofree
- Run zerofree
- Halt the machine
Step Three: Find the VBoxManage Command
The command ('vboxmanage') will be available in the Linux terminal If you’re using VirtualBox on macOS or Linux, you can just open a Terminal window and run the vboxmanage command normally, as you would any other command.
Step Four: Locate the Path to the Disk You Want To Compact
Run the following command in the command prompt window to view a list of all the virtual hard disks on your computer:
vboxmanage list hdds
Step Five: Compact the Disk
To compact the disk, you’ll need to use vboxmanage with the correct command. Just run the following command, replacing the path to the disk VDI file with the path to the disk on your system which you found using the above command. Be sure to enclose the path of the file in quotes if it contains a space anywhere in its path.
It should look like this:
vboxmanage modifymedium disk "/home/koan/vmware/path/to/disk.vdi" --compact
VirtualBox will compact the disk and you can immediately boot up the virtual machine afterwards, if you like. How much space you end up saving depends on how much empty space there was to recover.
Credits : Chris Hoffman is Editor in Chief of How-To Geek https://www.howtogeek.com/312883/how-to-shrink-a-virtualbox-virtual-machine-and-free-up-disk-space/