For the system administrators and DevOps engineers using KVM as their virtualized environment, performing complex virtual machine (VM) operations effectively can be challenging without the right tools. This is where virsh
, a command-line interface provided by Libvirt, shines as a powerful solution.
Tasks that otherwise require CLI work such as snapshots, backup/restore, and live/offline VM migration between hosts can be performed through virsh. These are necessary to keep uptime as high as possible, safe data and enterprise grade workloads. This guide delves into these intricate details and other features of KVM so you can take your KVM virtualization to the next level, making it faster, more efficient, and highly available.
Why virsh?
- Centralized Control:
virsh
provides consolidation of virtual machines, network, and storage management into a single command-line interface, thus streamlining operations across hypervisors supported by Libvirt. - Scriptability and Automation: Being a command-line tool,
virsh
is ideal for integration into automation and scripted workflows, enabling repetitive tasks to be executed reliably and efficiently. - Remote Management: Use
virsh
to connect to remote servers, making it a practical choice for managing headless environments or virtualized data centers. - Advanced Troubleshooting: Access detailed diagnostic information for VMs, networks, and storage pools to identify and resolve issues promptly.
Mastering KVM Virtualization - The Ultimate eBook
From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today
1. KVM Snapshots
List VM snapshots:
sudo virsh snapshot-list <vm-name>
Create a new VM snapshot:
sudo virsh snapshot-create-as <vm-name> \ --name <snapshot-name> \ --description "Add description or comments here."
Common options to use:
--disk-only
– Capture disk state but not vm state--quiesce
– Quiesce guest’s file systems--atomic
– Require atomic operation--live
– Take a live snapshot--no-metadata
– Take snapshot but create no metadata
List VM snapshots:
sudo virsh snapshot-list <vm-name>
Revert VM snapshot:
virsh snapshot-revert <vm-name> <snapshot-name>
Delete VM snapshot:
virsh snapshot-delete <vm-name> <snapshot-name>
Edit snapshot:
sudo virsh snapshot-edit <vm-name> <snapshot-name>
2. VM Migration
List all VMs to identify one to migrate:
sudo virsh list --all
Migrate a VM to different host:
sudo virsh migrate --domain <vm-name> \ --desturi qemu+ssh://<user>@<host>/system \ --migrateuri tcp://<host> \ --live --auto-converge --verbose
Useful common options:
--domain
– domain name, id or uuid--desturi
– connection URI of the destination host--migrateuri
– migration URI, usually can be omitted. Useful if destination hypervisor listens on a specific port or URI:--live
– Ensures the migration occurs while the VM is running without interruption.--auto-converge
– force convergence during live migration--verbose
– display the progress of migration--change-protection
– prevent any configuration changes to domain until migration ends--offline
– Ensures the VM is migrated while it’s powered off.--dname <string>
– rename to new name during migration (if supported)--copy-storage-all
– migration with non-shared storage with full disk copy--copy-storage-inc
– migration with non-shared storage with incremental copy (same base image shared between source and destination)--persistent
: Ensures the VM remains persistent after migration in destination hostqemu+tcp
: Uses TCP for migration.qemu+ssh
: Uses SSH for migrationqemu+tls
: Uses a TLS connection for secure migration.--copy-storage-all:
Copies all storage associated with the VM during migration.--p2p
: Uses a peer-to-peer migration mode.--bandwidth <number>
: Migration bandwidth limit in MiB/s.--xml /path/to/override.xml
: Specifies a custom XML file for migration.
Get information about ongoing migration:
sudo virsh domjobinfo --domain <vm-name>
Get information about completed migration:
sudo virsh domjobinfo --domain <vm-name> --completed
Abort ongoing migration using domjobabort:
sudo virsh domjobabort --domain <vm-name>
More guides from our team: