In Linux environments utilizing KVM virtualization, managing virtual machine snapshots can be accomplished through the following procedures:
1. Identify Virtual Machine Instance
First, display all available virtual machines:
sudo virsh list --all
Locate the target virtual machine identifier (e.g., web-server-vm).
2. Inspect Snapshots with virsh
Execute the following command to enumerate snapshots for a specific virtual machine:
sudo virsh snapshot-list <vm_identifier>
Example:
sudo virsh snapshot-list web-server-vm
If you encounter the error error: unsupported configuration: internal snapshots not supported with this QEMU binary, this indicates the virtual machine disk format might be raw (which doesn't support snapshots) and requires qcow2 format instead.
3. Verify Virtual Machine Disk Format
Confirm whether the virtual machine disk uses qcow2 format:
sudo qemu-img info /var/lib/libvirt/images/<disk_name>.qcow2 | grep "file format"
If the disk is in raw format, conversion to qcow2 is necesary to enable snapshot functionality.
4. Examine Disk Snapshots Directly (Optional)
Directly inspect snapshot information within the disk file:
sudo qemu-img info /var/lib/libvirt/images/<disk_name>.qcow2
The output's Snapshot list section will display all existing snapshots.
5. Snapshot Creation and Restoration
Based on your requirements, you'll need to create new snapshots and restore specific ones. Here are the detailed steps:
- Create New Snapshot:
sudo virsh snapshot-create-as db-server-vm 20250419-backup
- Restore Specific Snapshot:
sudo virsh snapshot-revert db-server-vm "production-state-20220128"
Operation Details:
Snapshot Creation: The snapshot-create-as command creates a new snapshot named 20250419-backup for the virtual machine db-server-vm.
Snapshot Restoration: The snapshot-revert command rolls back the virtual machine state to the production-state-20220128 timestamp. When snapshot names contain special characters or spaces, it's recommended to enclose them in quotes to prevent parsing issues.
Verification Steps:
Confirm New Snapshot in List:
sudo virsh snapshot-list db-server-vm
Verify that the virtual machine state has been successfully restored.
6. Virtual Machine Cloning
# Power down the virtual machine
$ sudo virsh shutdown db-server-vm
# Force shutdown if unresponsive
$ sudo virsh destroy db-server-vm
# Verify current status
$ sudo virsh list --all
Id Name State
-----------------------------
- db-server-vm shut off
# Perform cloning operation
$ sudo virt-clone --original db-server-vm --name db-server-vm-backup --auto-clone
Cloning completed! New virtual machine named db-server-vm-backup.
# Final verification
$ sudo virsh list --all
Id Name State
-----------------------------
- db-server-vm shut off
- db-server-vm-backup shut off
Troubleshooting Common Issues
- Permission Problems: Ensure you're using
sudoor have swtiched to the root user. - Command Not Found: Enstall required utilities:
sudo yum install -y libvirt-client qemu-img
- Virtual Machine Not Stopped: Certain operations require the virtual machine to be powered off:
sudo virsh shutdown <vm_identifier>
Key Takeaways
- An empty snapshot list indicates no snapshots have been created for that virtual machine.
- Snapshot functionality depends on disk format (must be
qcow2). - The
virsh snapshot-createcommand allows manual snapshot creation.
For specific error messages, provide complete error output for further analysis!