Content
Introductory
A VirtualBox with it, we can run virtual machines on our computer, which share the computer's hardware resources with the host operating system. Of these, we are now dealing with the size of the storage devices. The image file of a virtual machine can take up a lot of space by itself, but if we have several machines, we have to reckon with a significant backup storage requirement, so it is advisable to make optimal use of our available storage space. VirtualBox also provides the possibility to use dynamically growing containers, which can be used to create drives that have a fixed size internally (virtually), but the physical image files themselves are only a few megabytes in size when they are created, and grow during use depending on the amount of data placed on them (for example when installing an operating system in the first round). Unfortunately, this is not true backwards, so if, for example, we delete something from the virtual machine, the size of the physical image file of the virtual storage device unfortunately does not decrease. In this description, we will see how we can manually shrink the dynamically growing image files of our VirtualBox virtual machines to their actual size on Linux and Windows systems - thereby saving a lot of space on our computer.
Of course, this method only works with dynamic-sized storage devices, not fixed-size ones, and we can only shrink an image file in the storage if it already had data that was deleted, so the image file size increased but did not shrink back after deletion. . For example, after a recent installation of Windows or Linux, you will not be able to use this method unless you have downloaded several GB of data to the virtual machine and then deleted it later.
In this description, the virtual machine is also compressed / shrunk with Linux / Windows guest and host machines, so each variation takes place. These include skipping links to skip parts we don’t need.
Check virtual storage device
First, we check if the virtual storage device you want to shrink is indeed of the dynamically growing type, because only this can be shrunk. To do this, start VirtualBox and then File In the menu, start Virtual Media Manager ... participate.
On a Linux host:
And on a Windows host:
So on any platform you use VirtualBox, in the virtual media management panel, select the storage device you want to shrink and then click above. Properties icon, and at the bottom of the panel, select Information tab and make sure that "Dynamically Growing Storage" appears in the storage details.
Preparing a virtual machine for shrinking
So, if we have made sure that our target VirtualBox storage device is of dynamically growing type, then we must first prepare the virtual machine to shrink the connected storage device. .vdi file. We can do this in a few steps: we clean up any unnecessary things from the machine that we don’t need (e.g., unused programs, remove unnecessary files, empty the recycle bin, etc.), and then overwrite the empty space on the storage device with zeros with a program designed for that purpose.
To shrink a Windows virtual machine, you can skip to Linux guest system preparation and you can go directly to Preparing Windows guest systems chapter.
Preparing Linux guest systems
Start the Linux virtual machine you want to shrink, and then clean up as root. This can be done equally on Debian and Ubuntu systems.
Clear unused packages and the package cache
As root, let's first update our packages so that the commands that run later can work with the fresh package state:
apt-get update
apt-get upgrade
Then run the following cleaner APT commands:
apt-get autoremove
apt-get clean
The first command removes unused packages, obsolete package dependencies, and so on. The second command clears the locally stored packet cache.
Empty Recycle Bin (optional)
If you are also using a desktop environment on the virtual machine, delete the Recycle Bin as well. This can be done using the recycle bin icon on the desktop, or from the command line with our normal user:
rm -rf ~/.local/share/Trash/*
Install the Zerofree package
A zerofree command to "fill in" the empty space in the storage with zeros. THE zerofree package is not part of the system by default, so install:
sudo apt-get install zerofree
Restart virtual machine in recovery mode
To run the zerofree command, we need to restart the virtual machine. In this example now read only mode, mount the root partition and boot the machine.
I have previously made a description of that how to reset your root password. Now we will need the beginning of this description, where the machine had to be started similarly before. So let's follow what is described there all the way to Start the root console in part. Here we proceed with so much difference that in the description ro Do not overwrite (Read Only) rw but leave ro values (including Debian and Ubuntu). Set the rest the same way, just that ro do not change parts! In fact, here you only need to append "init = / bin / bash" to the end of the line. This will cause you to mount the root (/) partition in read-only mode and get a root prompt right away.
When you have overwritten it, press F10 key to start the configuration.
Find the volume block device to shrink
Once at the root prompt, we need to find the name of the block device we want to shrink / compress. This device mount the root (/) file system in read-only mode. To find out, there are several commands available to list the block devices connected to the machine. Run which of the following is more sympathetic:
df
lsblk
fdisk -l
Find a physical volume
So on one of the Debian virtual machines I have df and that Fdisk command output:
In this example, the root (/) file system is / Dev / sda1 partition is mounted, so here is the physical root partition we are looking for, we will have to use it later for zerofree. Of course, this can be different on other machines, depending on how many hard disks are connected to the machine and how many partitions they have, from which the operating system starts, and so on. So here is a simple example.
Find a logical volume
On an Ubuntu virtual machine (due to the higher resolution) df, lsblk and the output of fdisk commands is also displayed on a single screen:
In this Ubuntu example, accessing the virtual block device of our logical volume is visible in the command output:
/ dev / mapper / ubuntu - vg-ubuntu - lv
So for logical volumes, we need to use this or a similar device name a / dev / mapper structure - for example / Dev / sda3 instead - which we will have to give to the zerofree command. This way, we can be sure that if our logical volume consists of multiple physical volumes, it will also properly populate the connected physical units with zeros.
Filling empty blocks with zeros in zerofree
The last step in preparing the virtual machine for shrinking is to run the zerofree program on the device identified above.
Remaining with the Debian and Ubuntu examples above, the command to apply to a Debian physical volume / partition is:
zerofree -v /dev/sda1
The percentages spin up to 100%, then the output is mine:
And on the Ubuntu logical volume:
zerofree -v /dev/mapper/ubuntu--vg-ubuntu--lv
Here, too, the percentages spin through, then:
If the zerofree command has run, in this run mode a power off command to stop the machine:
poweroff -f
Preparing Windows guest systems
Start the Windows virtual machine that you want to shrink, and then perform a cleanup. In this example, I will demonstrate this on one of my Win10 virtual machines.
Empty trash
After deleting all unnecessary programs and files from the machine, delete the Recycle Bin.
Disk defragmentation
For Windows guest machines, because of the use of FAT / NTFS file systems, it is strongly recommended that you defragment the disk you want to shrink so that Virtualbox can perform the shrink operation faster. You can access Defragmenter by right-clicking the drive icon in the Properties panel on the Tools tab:
Fill in empty blocks with zeros in SDelete
Before shrinking a Windows guest system, we must do the same: overwrite the empty space with zeros using the SDelete program.
Download the program a From Microsoftand then unpack.
There is no need to restart the machine here, as with Linux guest systems, so you can use the program immediately after unpacking. After unpacking, we get both 32-bit and 64-bit versions. Open a command prompt window and look in the directory:
We use the 32-bit or 64-bit version, depending on the architecture of our guest system. The first time you use it, a license window will appear, on which we accept the terms.
To populate the blank space with zeros, run sdelete.exe -z option, as recommended in your help. Assuming you need to use the 64-bit version and shrink the C drive (this is the most common case), our command is as follows:
sdelete64.exe c: -z
This is when the program starts, the percentages spin nicely:
Once done, you can exit the command prompt and shut down the machine.
Shrink a virtual machine using VirtualBox
The shrinking operation takes place on the host operating system. Once you have prepared the virtual machine by performing the above operations and even shutting it down, shrinking can come. Shrinking can be done in the same way on both host operating systems using the command-line switches in VirtualBox.
If you want to shrink a virtual machine on a Windows host, you can skip the next Linux section and go to Shrink a virtual machine on a Windows host chapter.
Shrinking a virtual machine on a Linux host
At a command prompt, go to the directory of the storage unit (.vdi) of the virtual machine that was previously shrunk and check the size of the image file so that it can be compared to the reduced size after shrinking. In this example, I will shrink version 10 of the Debian 1.1 perfect server prepared above (which will be posted soon):
Here, the selected line shows the vdi file of the virtual machine in question, which is currently 9,4 GB in size.
Then shrink the file to vboxmanage command, which is part of the VirtualBox package and is included in the path:
vboxmanage modifymedium disk <vdi fájl> --compact
Then they go through here with 10 percent of the percentages as well. Once you're done, double check your size:
Here we can see that the 9,4 GB vdi file has been reduced to 4,4 GB. That's a pretty nice space saver. Of course, I note that a lot of things have been added to the machine during its operation, and a lot of things have been deleted from it, so it’s not a newly installed machine, so it’s no surprise that the size has dropped so much.
Shrink a virtual machine on a Windows host
In Windows, open a command prompt window. Here we need to access the directory of the VBoxManage.exe command, because in Windows the program directory is not included in the path.
VirtualBox defaults to C: \ Program Files \ Oracle \ VirtualBox \ will be installed in the directory if you did not change your directory during installation. If installed in another directory, use it.
Entering the command directory, first list the virtual HDDs with the following VBoxManage.exe command:
C:\Program Files\Oracle\VirtualBox>VBoxManage.exe list hdds
To do this, it publishes the virtual disks that are connected to a virtual machine, as well as some other information about them:
The capacity data shown here, measured in megabytes, refers to the virtual size, so it's a good idea to look at the actual size of our .vdi file:
So this virtual disk image is about 33 GB in size. Then comes shrinking, which should be done in the same way as in Linux:
VBoxManage.exe modifymedium disk "C:\utvonal\lemezkep.vdi" --compact
With my own example:
VBoxManage.exe modifymedium disk "D:\Egyéb\virtualbox\sajat_gepek\win_10_aztali_gepem.vdi" --compact
It took a little longer than compressing a much smaller Debian virtual machine, but it also went away. Finally, by re-listing the file size, you can see the significant size reduction here as well:
On this machine, the 33 Gbyte size has become 26 Gbyte, which is quite a significant size reduction here as well.
Conclusion
This allows you to compress and shrink disk image files from VirtualBox virtual machines on Linux and Windows systems, as well as Linux and Windows virtual machines. If you have a lot of virtual machines, just like me, you can save a lot of storage space with this shrink. However, there is nothing extra in this, this operation only frees the virtual machines from "unnecessarily accumulated" empty space when using the virtual machine. This process will, of course, restart the next time the machine is started, so it is sometimes worthwhile to compress / shrink our virtual machines. It is advisable to schedule all this after a major cleaning or erasure job in order to get back as much space as possible.
- How to configure VirtualBox for basic systems
- How to configure VirtualBox for graphical desktop operating systems
- Install VirtualBox 5.2 on Debian 9 (stretch) host operating system
- Installing VirtualBox 6.0.x on a Debian 10 (Buster) host operating system
- Install VirtualBox 6.1.x on Debian 10 (Buster) host operating system
- How to install Guest Additions on Debian running VirtualBox
- How to install Guest additions on Debian 11 (Bullseye) running in VirtualBox
- Encyclopedia - VirtualBox
- Manual page - zerofree
- To post registration and login required
- 519 views