Advanced Virtual Machine Operations with Virsh

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.
🔥 TRENDING - Our #1 Selling eBook

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

Only $10 $20
Get Instant Access →

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 host
  • qemu+tcp: Uses TCP for migration.
  • qemu+ssh: Uses SSH for migration
  • qemu+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:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Quite Creating and starting up virtual machines are more than just managing. VMs also rely on fast storage for their […]

For the system administrators and DevOps engineers using KVM as their virtualized environment, controlling virtual machines (VMs) effectively can be […]

A Kickstart installation is an automated method for installing Linux distributions such as Red Hat Enterprise Linux (RHEL), Rocky , AlmaLinux, CentOS, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.