As we know, VMs have an extremely annoying habit of ever-expanding on your host's disk space even if you delete files inside the guest.
Update:
I have since used the "make a copy of empty hard disks" Method 2, and it worked!
So the TLDR is:
- when you are installing a fresh VM, make multiple hard disks and keep backup of the "empty" hard disks, and even better, zip them up. (effectiveness expressed in pictures, later)
- if you already have running VMs, and want to recover the space, then no choice, boot into LiveCD and use zerofree everywhere. But after that - also make a copy of the empty hard disks and zip them up!
Then you can just anytime shutdown your VM can copy the empty hard disk files overwriting the ones filled with crap for swap and /var/log.
To help you visualize, pictures below:
Debian 7 VM hard disk files (Originally main hard disk was about 30 G before cleaning)
Debian 7, the crap hard disks zipped up... note, 2 kb for the "empty swap hard disk"!
Debian 8 zipped up crap hard disks - 181 kb!
End update.
Original post follows:
For me after spending some time, there are Two Things that you can do about it:
- Boot into a LiveCD, use zerofree on the partitions, then use VBoxManage modifyhd --compact on the .vdi files. (10 GB can shrink to 5MB.)
- Even better, in conjunction with that - use multiple .vdi files, and let your VM use multiple hard disks. Who says you can only have one...
By doing that, 30 GBs of space taken up by the VM can be shrunk to 10 GB (or however much space your guest VM is actually taking up. A Debian with KDE could be about 4.8 GB.)
Another tip to remember:
When using your guest, always download stuff into the VirtualBox's Shared Folder. That way, you'll avoid inadvertently expanding the file size of your .vdi.
The more interesting point is the 2nd one. Why and How does that help?
You put your swap, /var/log, and other partitions that expand and contract (have temp stuff deleted) on different .vdi disks. But before you do that, save backups of these disks into another folder. The idea is that: when the in-use .vdis expand to an unearthly size like 10G, overwrite them with the 'empty backup' .vdi files.
Here are the exact steps I used for my specific setup:
(On my Debian8.1 VM, where everything is on LVM, even the swap partition, and all the individual virtual hard disks)
sudo apt-get update && sudo apt-get install -y gparted lvm2 system-config-lvm
sudo gparted
sudo system-config-lvm
ls -lah /dev/mapper
sudo vgchange -a y
sudo zerofree -v /dev/mapper/debian810--vg-root
sudo zerofree -v /dev/mapper/debian810--vg-home
sudo zerofree -v /dev/mapper/debian810--vg-var
sudo zerofree -v /dev/mapper/debian810--vg-tmp
sudo zerofree -v /dev/mapper/debian810--vg-LV_VAR_LOG
sudo dd if=/dev/zero of=/dev/mapper/debian810--vg-LV_SWAP bs=1M
sync
REMOVE CD FROM DRIVE.
cd "c:\Program Files\Oracle\VirtualBox"
VBoxManage.exe modifyhd c:\dev\vm\VIRTUALBOX\debian810\debian810_LV_SWAP.vdi --compact
VBoxManage.exe modifyhd c:\dev\vm\VIRTUALBOX\debian810\debian810_LV_VAR_LOG.vdi --compact
VBoxManage.exe modifyhd c:\dev\vm\VIRTUALBOX\debian810\debian810.vdi --compact
COPY TO SAVED_EMPTY_HARDDISKS/ LV_SWAP and LV_VAR_LOG.vdis.
DONE.
And here's what I used on my debian7 system:
sudo zerofree -v /dev/sda7
sudo zerofree -v /dev/sda6
sudo zerofree -v /dev/sda3
sudo zerofree -v /dev/sda1
sudo vgchange -a y
sudo zerofree -v /dev/mapper/debian750-LV_MYTMP
sudo zerofree -v /dev/mapper/debian750-LV_VAR_LOG
sudo zerofree -v /dev/mapper/debian750-root
sudo dd if=/dev/zero of=/dev/sdb1 bs=1M
sync
then modifyhd --compact on the 4 hard disks, the 3 external hard disks take up 300 MB now.
Because I did not backup them at the start while they were still fresh.
then zip up the swap, tmp and var_log hard disks.
* compressed, these take up 2 MB disk space only!
Method 2:
Since you are using a VM and all hard disks are virtual and you can add and remove hard drives as you please, you can:
- forget about using LVM altogether, just have regular partitions for hard drives, unless you specifically want to learn about how to use and maintain LVM, as I have;
- It will then look like
sudo zerofree -v /dev/sda7 sudo zerofree -v /dev/sda6 sudo zerofree -v /dev/sda3
without needing tovgchange -a y
, which is much more simpler. - Use LVM, but don't put / /home /var /tmp/ etc in separate partitions, just have everything in one partition except for SWAP (which can be on LVM, or non-LVM, but on separate hard disk i.e. separate .vdi file.)
- The reason for this is so that you then don't have to zerofree so many LVs individually as you saw in my specific setup; you will just have to the one
sudo zerofree -v /dev/mapper/debian810--vg-root
anddd if
theSWAP
whereever it is.
Right now I have the 'empty disks' of 5 MB .. 30 MB .. size (5 MB for 'swap' type, 30 MB for 'ext4' type), I have not yet, at time of writing, tried just simply overwriting the in-use .vdis with their 'empty backups', because: so far I just recently used 1) which is to zerofree-modifyhd those disks. So at this moment my disks have not yet become so big for me to actually want to do 2) yet and report the findings here.
Anyway, I'll remark on the effectiveness of 2) in a future post when I get to doing it.
Now, talking about copying over .vdis, there may or may not be those "Virtualbox disk UUID problems"... but I expect not, and even if so, doesn't seem to be too hard to solve. But, as they say... that is left as an exercise to the reader.
Share your experience here if it helped you! :)