For a while now I have been using VirtualBox to run various windows guest operating systems on my Linux host. One thing that I have always been wary of when using them is how the disk image gradually grows over time. This is because on the host operating system the disk container uses a sparse file so that the virtual disk can appear to be bigger than it actually is, but it can only do this when the guest operating system is not using the space, and the data is effectively set to zero. When you delete a file on the guest operating system it doesn’t bother to set the file contents back to zero, it just removes the record of where the files data was located.
This is not really a problem and most people know that you can use a tool such as sysinternals SDelete to zero the free space. Then using the command
vboxmanage modifyhd --compact <diskimage.vdi>
the space can be reclaimed.
However in windows there is one file in particular called pagefile.sys (also known as the swap file) that is constantly being written to and then subsequently erased. Its possible that windows does this intelligently and re-uses the same sectors of the virtual disk for the pagefile, but it seems more likely to me that windows doesn’t do this, after all it isn’t aware that its really writing to a virtual disk.
What made sense to me was to create a second virtual disk specifically for the pagefile. I made the second disk image immutable. What this means is that all writes to the disk are stored in a differencing image which is thrown away when the guest machine reboots. Because of this I can ensure that it never gets any bigger. This also has some other advantages.
Firstly I can use the same immutable image for all my Virtual machines. These currently include; Windows XP, Windows 7, Windows 8.1, and Windows 10. Secondly because the image is immutable it allows all guests to use the image while all running at the same time.
Thirdly the fun part, I can host the differencing images to the immutable base image in tmpfs meaning that the guests get the advantage of having very fast i/o for their pagefile.sys. This is the advantage I like the most because it certainly seems to improve performance.