KVM backups with Virt-Manager

So if you like to mess around like I do, you’re probably familiar with the idea of virtual machines. I like to use a program called virt-manager under Linux to create and work in VMs. It’s quick and easy, and gets the job done. But you’re probably wondering how you backup said VMs. I’m here to show you.

First list the VMs you currently have available to make sure they’re shut down :

virsh list --all

 Id   Name          State
------------------------------
 52   ActualArch    running
 -    archlinux     shut off
 -    endeavoros    shut off
 -    fedora        running
 -    freebsd13.1   shut off
 -    Gentoo        shut off
 -    manjaro       shut off
 -    ubuntu22.04   shut off
 -    win11         shut off

In my case I’ll be backing up the “fedora” VM. Do this to shut it down :

virsh shutdown fedora

And repeat the listing to verify that it worked:

virsh list --all
 Id   Name          State
------------------------------
 52   ActualArch    running
 -    archlinux     shut off
 -    endeavoros    shut off
 -    fedora        shut off
 -    freebsd13.1   shut off
 -    Gentoo        shut off
 -    manjaro       shut off
 -    ubuntu22.04   shut off
 -    win11         shut off

Backing up a VM contains two components - the domain definition, which is an .xml file, and the actual data file, which is usually in qcow2 format. To see where the data files are located we do the following :

virsh domblklist fedora
Target   Source
---------------------------------------------------------------
 vda      /mnt/D0F0D4BFF0D4ACCA/virtual machines/fedora.qcow2
 sda      -

Now we want to dump the definition file for the Fedora VM to an XML at our backup location:

virsh dumpxml fedora > /location/to/backup/to/fedora.xml

And finally the data file, or hard drive:

cp /mnt/D0F0D4BFF0D4ACCA/virtual machines/fedora.qcow2 /location/to/backup/to

I will show you how to restore these backups in another tutorial coming soon!

3 Likes

Where is the .xml file stored?
If it is in the same place as the qcow2 file, one could just backup the whole partition
I think what you really mean is a snapshot rather than a backup… just one VM

3 Likes

When you do the dumpxml command you pipe it to an actual xml file. It saves it to wherever you tell it to save it to, preferably the location you’re going to ultimately be backing everyting up to. You need both the xml and the qcow2 for it to be a complete backup that’s readable by libvirt when you want to restore it.
Example :

virsh dumpxml fedora > /location/to/backup/to/fedora.xml

If you didn’t do the > fedora.xml it would just dump the contents of the xml to the display. This is for actual backups that you can keep somewhere else in case you have a hard drive failure/etc. Snapshots are good for getting out of breaking something, but I don’t consider them actual backups.

2 Likes

Oh, I get it, it is not stored anywhere, you generate it.

We need the restore tutorial too.

3 Likes

Working on that as we speak :slight_smile:

2 Likes

I am getting ready to follow your steps in backing up my Windows 10 VM in QEMU/KVM as I intend to change distros on this laptop. I want to be able to restore this VM once the Fedora WS 39 is installed. Will it matter that the host OS changes?

Do you have the restore steps you could list here? I have never backed up the VMs in Virt Mgr nor in Oracle. I have a W10 VM there as well that I intend to backup, but wlil need to find a good tutorial for that.

Thanks,
Sheila Flanagan

That will not matter, as long as it is still a Linux kernel
with KVM, and the new OS has to have Qemu and virt-manager installed, of course.

1 Like

I didn’t think it would, but always confirm.

Thanks,
Sheila

1 Like

Strangely, today was my first time doing the manual Timeshift snapshot on this same laptop and when I looked at “Users” I saw libvert-qemu as a user with /var/lib/libvirt as “home” and it was included by default. I did not know that TS looked at VMs and made snapshots.

Now I am wondering if I could actually use that snapshot to restore the VM. Of course, I don’t think that would include any installed apps or personal files, but it never occurred to me to maybe backup the few files I have and then restore from TS once I install a new OS.

I may play with that after backing up using the method in this thread, before restoring. See if you can actually restore a VM from TS…although my past experience in restoring to a different OS has not been stellar with Timeshift.

Sheila

Hi @Sheila_Flanagan ,

@Doron_Beit-Halahmi summarised it perfectly… all you need to backup is the VM (qcow2) file, and the.xml files which configure virt-manager
Any backup utility will do it, as long as you specify the filesystems which contain these 2 files.

But, you dont really want a backup… you want to transfer a VM to another OS. So just copy the qcow2 file and the .xml files across to the new OS. … Eg put them on a usb stick,
setup the new OS, and copy them into it at appropriate locations.

Regards
Neville

I am haivng a few issues getting this VM backed up before new OS install. Following the directions, I get to the “virsh domblklist win10” and it shows the following:

myviolinsings-acer@pop-os-acer:~$ virsh domblklist win10
 Target   Source
-----------------------------------------------------------------
 vda      /var/lib/libvirt/images/win10.img
 sdb      /home/myviolinsings-acer/Win10_22H2_English_x64v1.iso
 sdc      /home/myviolinsings-acer/virtio-win-0.1.217.iso

From there I was able to copy the .xml file (but could NOT send it to external drive) where I will move it later.

But in the example, the .qcow2 files is displayed. Mine is not. So do I just search for it and back it up alongside the .xml file?

I alreaady copied the only files within the VM that I cared about and will have them available once restored.

Thanks,
Sheila

Looks like Windows VMs handle it differently than Linux, I think that .img file is what you’re after. because we know its not the isos. lol

I’m going to have to try this process with a Windows VM and see how different it is. I never did anything for Windows … well … because…Windows. lol

win11qcow2

My windows 11 image is a qcow2 file as well … Your lib virt is making an ass out of me LOL

@Doron_Beit-Halahmi
:rofl:

Isn’t that a Windows “thing?”

So I need to find my source path (hopefully same as yours) and copy that qcow2 file there?

Thanks,
Sheila

it would be the .img file in your case, because your libvirt wants to be all different. So you’ll want to convert the .img file to qcow2 like this

qemu-img convert -f raw '/var/lib/libvirt/images/win10.img' -O qcow2 '/destination/path/win10.qcow2'

And you can thank Windows 10 for being a special snowflake and make you have to work harder to do the same thing LOL … now you may be able to use the img file as is, I’m not sure, never tried it with that.

So using your source path /images mine is EMPTY. Sheesh. Under libvirt the only folder that has anything in it is:

And since I just got into it this morning, I assume everything is in that folder, but no .qcow.

In the VM details console:
Screenshot from 2024-03-24 12-35-52

it’s supposed to be in /pool.

So in command output it shows:
/var/lib/libvert/images/win10.img

But looking in the folder, this does not exist. So trying to convert the .img file is not working.

Sheila

Yes, virt-manager can do an install from either a .iso or a .img file.
but hang on… I thought after the install it would make a .qcow2 file… is @Sheila_Flanagan looking at the right directory?.. I guess it must be, she did it with virsh domblklist and it says vda for the .img file. in /var/lib/libvirt…why there? I thought it kept its virtual disk images somewhere in the home directory by default?
Maybe Shiela moved it?

I looked it up… qemu can use either raw (ie .img) or .qcow2 formats fir its virtual disk… so that must be right… Shiela should move the .img file and the .xml files (and the .iso files if she wants to preserve them). She does not need to convert it to a qcow2 file, and if she did, it would not match what the
.xml files are set to.

But I have searched the entire computer and have not found it as *.img or win10.img

First would have to find the .img file.

Thanks,
Sheila

Is it not there?
That seems to be the home directory of the libvirt user