Is Your Disk Space Running Low? Take It Back From Your VirtualBox Linux System

 

Is Your Disk Space Running Low? Take It Back From Your VirtualBox Linux System

Is Your Disk Space Running Low? Take It Back From Your VirtualBox Linux System

You can run Linux on a virtual machine with VirtualBox, but even after you have been emptying your virtual disks of files, you may find that they are still getting larger. I will walk you through the process of condensing and resizing these volumes to conserve disk space.


Why the VirtualBox Linux Guest Does not Reduce Itself

If you have utilized VirtualBox with Windows guests—the operating system running within the virtual machine is referred to as the "guest" in virtual machine terminology—you are probably accustomed to your VirtualBox disk volumes shrinking when you remove files from them, requiring less space on your physical disk than they actually require to hold the files they contain.


The dynamic allocation feature in VirtualBox is meant to be used for this exact purpose, but it is incompatible with Linux guests. Many customers discover that after employing a Linux guest, their disks expand to their maximum capacity and never contract again as files are removed.


This is because of the way Linux handles filesystems and communicates with the VirtualBox host. For performance-related reasons, Linux does not "zero out" (overwrite with empty data) deleted files, hence VirtualBox is unable to distinguish between removed and active data on a virtual disk.


By zeroing that data yourself and instructing VirtualBox to condense the drive, you can resolve this problem and restore the volume to its original size. This is the method used.


How to Use VirtualBox to Get Back Disk Space in a Linux Virtual Machine

Enabling dynamic allocation is the first step toward recovering disk space from the virtual disks of your Linux guests. You will have to change your disk to a dynamically allocated disk if it is not already.



Next, create a virtual disk backup. You run the risk of losing all the data on your Linux guest if something goes wrong (such as a command that is typed incorrectly or your power cutting out in the middle of the process).

Next, enter the following command into the terminal on your Linux guest:



sudo dd if=/dev/zero of=/var/deleteme
Until the disk is entirely filled, this operation will write zeroed out (empty) data to the file /var/deleteme. The issue that arises from Linux not automatically overwriting deleted data is resolved by doing this, which overwrites all previously deleted files. 


The dd command will erase data without notice, so use caution when using it! Before executing your tasks, thoroughly check your paths and commands.

Depending on the size of the volume, this procedure may take some time, so please be patient and do not stop it. The process will terminate when it is done (maybe with an error stating that it has run out of space). Following this, you can terminate your Linux guest and remove the zeroed-out file:


sudo rm -rf /var/deleteme

sudo shutdown now -h

Your virtual disk's unused space has now been emptied out, allowing VirtualBox to minimize the space on your host and recover it. Using the vboxmanage command to compress the virtual disk image is the last step. Run to accomplish this:


vboxmanage modifymedium disk /path/to/image.vdi -compact
To condense a virtual disk, you must modify the path to the disk at /path/to/image.vdi. If you are using Windows, you must use VBoxManage.exe as follows:
VBoxManage.exe modifymedium disk /path/to/image.vdi -compact

Continue reading for details on how to enable VBoxManage.exe if you can not access it via the Windows command line.

How Was the Linux Command for dd Used? 


In order to identify vacant space in a VirtualBox Linux guest, this method of compacting the guest uses the dd command to write an empty file on disk.  In addition to copying and converting files, the dd (data duplicator) command can also write data. The input file (in this case, /dev/zero, which provides a continuous stream of null or zero-value data) is supplied by the "if" option that is offered to it. This stream of zero data is written to the output file that is specified by the "of" option. The disk will never run out of space since /dev/zero never runs out of null data.


Using Windows' VboxManage.exe


VBoxManage.exe is not accessible from the Windows command line by default. You can add it by calling the complete path to the executable when using it, or by changing the Windows system path to include the installation location for VirtualBox:


 
& "C:/Path/To/VBoxManage.exe" modifymedium disk /path/to/image.vdi -compact
The "&" sign, also known as the call operator, is what carries out the specified command. You can now utilize spaces in the executable's path thanks to this.


Further Details Regarding Guests in VirtualBox


You can both shrink and enlarge your guests' virtual disks as they get bigger. By using snapshots, you can record your virtual machine's state at a specific instant in time and retrieve it at a later time. This can be useful if you wish to roll back and restore the original configuration, for example, after testing a change to the configuration.

VirtualBox is a potent virtualization program used by home users to run outdated applications and games on modern computers or run different operating systems without having to buy a second machine, as well as by professional developers to design and test software. On MacOS, Windows, and Linux hosts, you can run Windows, Linux, ChromeOS, Android, and other operating systems.
 
google-playkhamsatmostaqltradent