How to Create Persistent KVM Domains Using virsh: Fixing Disappearing VMs Issue


1 views

When using virsh create domain.xml, the virtual machine (VM) disappears after shutdown because this command only creates transient domains. Unlike virt-manager which creates persistent domains by default, virsh create doesn't automatically register the domain in libvirt's configuration.

Use virsh define followed by virsh start instead of just virsh create:

# First define (register) the domain
virsh define /path/to/your/domain.xml

# Then start it
virsh start your-domain-name

Transient domains (virsh create):

  • Temporary existence
  • Disappear after shutdown/reboot
  • Good for testing configurations

Persistent domains (virsh define):

  • Permanently registered in libvirt
  • Survive host reboots
  • Appear in virsh list --all

If you already created a transient domain you want to keep:

# Dump the running transient domain's XML
virsh dumpxml transient-domain-name > persistent.xml

# Define it as persistent
virsh define persistent.xml

Check your domain appears in the complete list:

virsh list --all

The output should show your domain with either "shut off" or "running" state, proving it's persistent.

For domains that should start automatically with the host:

virsh autostart domain-name

This creates a symlink in /etc/libvirt/qemu/autostart/ pointing to your domain's XML file.

Complete example from scratch:

# Create a new persistent domain
virsh define ubuntu-vm.xml
virsh start ubuntu-vm

# Configure to start automatically
virsh autostart ubuntu-vm

# Verify
virsh list --all

When using virsh create somefile.xml, you're creating a transient domain that only exists until shutdown. This differs fundamentally from virt-manager's default behavior which creates persistent domains. The XML format itself isn't the problem - it's the creation method.

Key differences in libvirt:

  • Transient domains (virsh create): Exist only in memory, no libvirt config tracking
  • Persistent domains (virt-manager default): Registered in libvirt's configuration

Here's the proper workflow for persistent VMs:

# First define (register) the domain
virsh define /path/to/your/vm.xml

# Then start it
virsh start your-vm-name

# Verify persistence
virsh list --all

When working with XML definitions:

# Dump XML from existing VM (great for templates)
virsh dumpxml existing-vm > template.xml

# Edit carefully (use libvirt-sanitizer if needed)
vim template.xml

# Define the new persistent VM
virsh define template.xml

If your VM still disappears:

  • Check permissions in /etc/libvirt/qemu/
  • Verify storage pools are persistent
  • Ensure no duplicate UUIDs exist

Bash script for batch creation:

#!/bin/bash
for vm in *.xml; do
  name=$(xmllint --xpath 'string(/domain/name)' $vm)
  virsh define $vm
  virsh start $name
done