Friday, March 19, 2021

Oracle VIrtualBox 6 -- How to Shrink an Image File of a Linux Guest OS


You want to shrink an dynamically allocated image file of a virtual machine with Linux guest


To shrink a file on the host operating system, you first need to zero the high end of the disk in the guest operating system inside the vm

1. In the guest operating system remove as much files as possible to make as much free space as possible

2. In the guest operating system run dd command to zero the disk space on the mount point
$ dd if=/dev/zero of=temp.file
This will create a temp file filled with zero bytes. The file will keep filling as large as the underlying disk size and free space allows

Once dd completes creating the temp file, delete the temp file

$ rm temp.file
3. Shutdown the virtual machine
4. In the host operating system use VBoxManage.exe on Windows, or vboxmanage command on Linux/Unix, to shrink the image file:
VBoxManage.exe modifymedium disk "<image_file>" --compact

No comments: