Script to Dump xml Configuration Files

Most people today use the libvirt API and/or its front end Virtual Machine Manager (virt-manager) to configure and manage virtual machines. Both create xml configuration files. Unfortunately you can’t just copy them or edit them using your favorite editor.

virt-manager or libvirt store a copy of the VM xml configuration files in /etc/libvirt/qemu/ for kvm or qemu guests. But when you open a configuration file, you get the following warning:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit centos8
or other application using the libvirt API.
-->
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  <name>centos8</name>
...

Great! Now what?

Sure I can open the configuration by typing for example:

sudo virsh edit centos9

But that command opens the configuration in the terminal, using the default editor for the user. This is probably “vi” or “vim”. Not sure how many of you are familiar with either of them. Here is a way to find out which default editor you have:

echo $EDITOR

When using vi or vim, once you opened the xml configuration using the virsh edit command, you can save the content to a new file as follows:

  1. Hit the : (colon) key
  2. Type the following: wq /path/to/filename.xml

This will write a clean xml file, without the warning, to the path/file you specified and close the editor.

I got more than a dozen VMs configured and that procedure would be rather tedious. So I wrote a short bash script:

#!/bin/bash
# Copyright (C) 2024 Heiko Sieger
# https://heiko-sieger.info
#
# This program is free software: you can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software
# Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# See <https://www.gnu.org/licenses/>.
#######################################################
# This bash script dumps the xml configurations of all 
# VMs into separate vm_name.xml files. It uses the name
# of the VM as file name and adds the suffix ".xml".
#
# Syntax: export-xml.sh <directory>
#
# The optional parameter <directory> specifies the
# (relative) path where the vm_name.xml files are to be
# saved. If not specified, the default directory is
# ~ (user home).
set -ux
directory="${1:-~}"
sudo virsh list --all | awk 'NR>2 && $2 != "" {print $2}' | while read -r vm_name; do
	sudo virsh dumpxml "$vm_name" > "$directory/$vm_name.xml"
done

The above bash script lists all VMs and dumps their xml configuration into separate vm_name.xml files. Copy and paste the above script into an empty text file named export-xml.sh and make it executable:

chmod u+x export-xml.sh

To run the script, type in a terminal:

export-xml.sh <directory>

where <directory> is the (relative) path of the folder where you want to store the xml config files (default is the user’s home directory). It will ask for your sudo password.

It’s advisable to make a backup of your VM configurations to a different storage place. This allows you to easily restore the configuration in case you messed it up (see further below).

libvirt Storage Locations

What should be a trivial task like finding the location of your qcow2 images requires at least two steps. Assuming you use the out-of-the-box “default” settings, you need to run the following commands to get that very basic information:

sudo virsh pool-dumpxml default | grep -i path
    <path>/var/lib/libvirt/images</path>

If you plan on changing the storage default, have a look at this tutorial at UnixArena: “Linux KVM – Change libvirt VM image store path – Part 10“. Or this tutorial: “Relocate an existing libvirtd (KVM) images directory“.

You can find the storage information for the “default” pool also in the /etc/libvirt/storage/default.xml configuration file, next to configuration files for other pools or media. Most or all of the libvirt configuration files can be found under the /etc/libvirt/ folder and its sub-folders. But most of them should not be edited directly (see also the linked tutorials above).

Restoring the xml Configuration

The VM xml configuration files created above can be backed up and used to once again fire up a VM. After you made sure you have the backup (created using my script above), you need to first “undefine” the old configuration before you restore it. The following command wipes out the configuration of vm_name and deletes the original /etc/libvirt/qemu/vm_name.xml file:

sudo virsh undefine vm_name

To (re)define the VM using the xml dump we created, use the following command (replace “/path/to” with the path to the xml file and “vm_name” with the name of the VM):

sudo virsh define /path/to/vm_name.xml

The above command will recreate the vm_name.xml file under /etc/libvirt/qemu/ and the VM will show up once again in Virtual Machine Manager.

Summary

libvirt has become a powerful API for creating and managing virtual machines. But it’s not perfect. I personally don’t like their default choices. Trying to figure out where things are configured or stored can be time consuming and erroneous.

Making a backup of the VM configurations using virsh dumpxml can be useful in case you need to recover a configuration. Make sure to keep the backup up-to-date and store it in a save place (preferably on a different drive).

Author: Heiko Sieger

The day has 24 hours. If that isn't enough, I also use the night.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.