Reset/Clear guest RAM when it reboots in QEMU to reduce RAM snapshot size
During installation 4GB+ of RAM can be used by the guest. Most of the time the RAM usage is much lower than this.
After installation completes the system is rebooted and then a snapshot is taken. In theory the snapshot should be very small because the system has only just booted, however it appears that QEMU thinks all the RAM is still in use and saves it to the snapshot. This might not be unexpected because on bare metal the RAM is preserved between reboots on modern systems. However, assuming that it is not relied upon by the guest OS, we don't need it to happen and can save some time.
Some ideas to solve this:
- Use the virtio memory balloon
- Use the -no-reboot switch and restart the QEMU process if it exits unexpectedly.
- Patch QEMU to clear (some of) the RAM when the guest initiates a reboot.
#3 Updated by rpalethorpe almost 3 years ago
- Status changed from New to In Progress
I think the virtio-balloon driver can force the kernel to reclaim that memory. Although I suppose dropping the caches would be quicker.
Anyhoo I have implemented ballooning and it seems to work to some extent, please see the bugzilla entry and https://github.com/os-autoinst/os-autoinst/pull/1020
#4 Updated by rpalethorpe almost 3 years ago
Also from the bugzilla thread:
Alexander Graf from comment #19
QEMU's RAM migration code treats all-0 pages special, so maybe it's enough to just make as many pages as we can contain all zeros?
For that, it should be enough to do something like
$ dd if=/dev/zero of=/dev/null bs=4G count=1
and wait for it to crash ;). Or the slightly nicer way might be to run
$ dd if=/dev/zero of=/dev/garbage bs=1M; rm -f /dev/garbage
which however will be limited by the /dev tmpfs size.