How to Restore a Broken KVM VM from Backup

How to Restore a Broken KVM VM from Backup #

Sometimes things go wrong with virtual machines — maybe a filesystem corruption or a bad update. When that happens, restoring from a backup is your best friend.

Here’s how I restored my broken KVM VM disk image using weekly backups stored on a NAS share.

The situation #

I have a VM called runner.home.arpa running on KVM, and its disk got corrupted. The VM disk images live at /var/lib/libvirt/images/, and my backups are stored on a NAS mounted at /mnt/backups/runner.home.arpa/.

Kernel couldn’t find the disk as it was corrupted, kernel panic ensued:

Kernel panic on runner.home.arpa VM

The backup folder contains:

  • runner.home.arpa.qcow2 — the full VM disk image backup
  • runner.home.arpa.xml — the VM domain XML config backup

Step 1: Shut down the VM #

The VM in question was in a kernel panic state so I had to forcefully shut it down:

virsh destroy runner.home.arpa

Step 2: Restore the backup disk image #

Copy the backup disk image over the broken one:

cp /mnt/backups/runner.home.arpa/runner.home.arpa.qcow2 \
   /var/lib/libvirt/images/runner.home.arpa.qcow2

Ensure correct ownership and permissions:

chown qemu:qemu /var/lib/libvirt/images/runner.home.arpa.qcow2
chmod 600 /var/lib/libvirt/images/runner.home.arpa.qcow2

Step 3: Start the VM #

Finally, start your VM:

virsh start runner.home.arpa

Check that it boots properly and the filesystem is healthy.

Conclusion #

This shows how important backups are, and making sure they actually are restoreable. This was actually the first time in about three years that I had a VM break on me. Luckily the backups were OK and everything went smoothly!