# Linux Kernel Module Cheat

### 23.5. Snapshot

QEMU allows us to take snapshots at any time through the monitor.

You can then restore CPU, memory and disk state back at any time.

qcow2 filesystems must be used for that to work.

To test it out, login into the VM with and run:

    ./run --eval-after 'umount /mnt/9p/*;./count.sh'

On another shell, take a snapshot:

    ./qemu-monitor savevm my_snap_id

The counting continues.

Restore the snapshot:

    ./qemu-monitor loadvm my_snap_id

and the counting goes back to where we saved. This shows that CPU and memory states were reverted.

The `umount` is needed because snapshotting conflicts with 9P, which we felt is a more valuable default. If you forget to unmount, the following error appears on the QEMU monitor:

    Migration is disabled when VirtFS export path '/linux-kernel-module-cheat/out/x86_64/buildroot/build' is mounted in the guest using mount_tag 'host_out'

We can also verify that the disk state is also reversed. Guest:

    echo 0 >f

Monitor:

    ./qemu-monitor savevm my_snap_id

Guest:

    echo 1 >f

Monitor:

    ./qemu-monitor loadvm my_snap_id

Guest:

    cat f

And the output is `0`.

Our setup does not allow for snapshotting while using initrd.

Bibliography: https://stackoverflow.com/questions/40227651/does-qemu-emulator-have-checkpoint-function/48724371#48724371

- Snapshot internals