DEV Community

Cover image for Virtual Machine Snapshot
Yero Bâ
Yero Bâ

Posted on

Virtual Machine Snapshot

Understanding QEMU/KVM Snapshots: A Developer's Guide

Snapshots are one of the most powerful features in virtualization, allowing you to capture and restore VM states instantly. But they're also one of the most misunderstood. Let's dive into what they are, when to use them, and how to avoid common pitfalls.

What Is a Snapshot?

A snapshot preserves the complete state of a virtual machine at a specific moment in time. This includes:

  • VM memory state
  • Disk state
  • Configuration settings

QEMU/KVM leverages the qcow2 disk format to track changes efficiently using copy-on-write technology.

Types of Snapshots

Internal Snapshots

Stored directly within the qcow2 disk image file, containing both disk and VM state.

Example workflow:

  1. Day 0: Fresh Ubuntu VM in myvm.qcow2
  2. Day 1: Create snapshot named before-updates
  3. Day 2: Install updates and applications
  4. Options: Rollback to Day 1 state or delete snapshot to save space

External Snapshots

Created as separate files that track only changes after the snapshot point.

Example workflow:

  1. Day 0: Base image myvm.qcow2
  2. Day 1: Create external snapshot snap.qcow2
  3. Day 2: All changes saved to snap.qcow2
  4. Cleanup: Merge snapshot back to base or lose changes if deleted

Critical Warning: Snapshots ≠ Backups

Snapshots are NOT backups! Here's why:

  • Dependent on the original disk image
  • If base image corrupts, all snapshots become unusable
  • Designed for temporary use only
  • Increase risk of data loss when used as sole recovery method

Pros and Cons

Advantages

  • Speed: Nearly instantaneous creation and restoration
  • Testing safety: Perfect for experimenting with system changes
  • Flexibility: Multiple restore points for different configurations
  • Efficiency: Uses sparse allocation to minimize space usage

Disadvantages

  • Performance impact: Each active snapshot adds I/O overhead
  • Growing disk usage: Snapshots expand as changes accumulate
  • Chain dependency: Corruption affects all subsequent snapshots
  • Management complexity: Long chains become difficult to maintain

When to Use Snapshots

Good Use Cases

  • Software testing and updates
  • Security testing with potentially malicious software
  • Training environments that need consistent reset points
  • Development sandboxes with easy rollback
  • Before applying system patches

Bad Use Cases

  • Production database servers
  • Long-running production VMs
  • Long-term data preservation
  • Mission-critical systems
  • As your only recovery strategy

Managing Snapshots

Command Line Operations

Create internal snapshot:

virsh snapshot-create-as --domain vm_name --name "snapshot_name" --description "Description" --atomic
Enter fullscreen mode Exit fullscreen mode

Create external snapshot:

virsh snapshot-create-as --domain vm_name --name "snapshot_name" --disk-only --atomic
Enter fullscreen mode Exit fullscreen mode

List snapshots:

virsh snapshot-list vm_name
Enter fullscreen mode Exit fullscreen mode

Restore snapshot:

virsh snapshot-revert vm_name snapshot_name
Enter fullscreen mode Exit fullscreen mode

Delete snapshot:

virsh snapshot-delete vm_name snapshot_name
Enter fullscreen mode Exit fullscreen mode

Using Virt-Manager GUI

  1. Open Virt-Manager and select your VM
  2. Click "Show virtual hardware details"
  3. Select View → Snapshots
  4. Use the + button to create new snapshots
  5. Select and click "Run" to restore snapshots

Storage Behavior

QEMU/KVM snapshots use qcow2's copy-on-write mechanism:

base.qcow2 ← snapshot1.qcow2 ← snapshot2.qcow2 ← current_state.qcow2
Enter fullscreen mode Exit fullscreen mode

Only changed blocks are written to snapshot layers, creating efficient storage chains.

Maintenance Best Practices

  • Regularly delete unneeded snapshots
  • Monitor disk usage with qemu-img info --backing-chain
  • Commit changes to consolidate long chains
  • Check snapshot chain integrity periodically
  • Limit chain depth to 10 or fewer snapshots
  • Never manually delete snapshot files

Troubleshooting: VirtualBox to QEMU Migration

When converting VDI files to qcow2, you might encounter boot failures. Here's the fix:

1. Proper Conversion

qemu-img convert -p -f vdi -O qcow2 source.vdi destination.qcow2
Enter fullscreen mode Exit fullscreen mode

2. Critical VM Settings

  • Disk Controller: Use SATA or IDE (not Virtio unless configured)
  • CPU Configuration: Enable "Copy host CPU configuration"

These settings prevent hardware detection issues that cause boot failures.

Conclusion

Snapshots are powerful tools for development and testing workflows, but they require careful management. Use them for short-term testing and experimentation, but always maintain proper backups for production data. With the right approach, snapshots can significantly improve your virtualization workflow while keeping your systems safe.

Remember: snapshots are for convenience, backups are for survival.

Top comments (0)