6

I run several VM's on KVM/ubuntu, which all get started with the -snapshot parameter (the VM's only compute several things which can be destroyed after reboot).

Out of the docs I've read, the changes are not written back to the image, but stored in temp-files and these are deleted after shutdown.

Now, I wonder where on the filesystem these "temp-files" are stored?

5 Answers 5

3

Try use lsof to find snapshot storage:

# ps aux | grep kvm
$PID ?        Sl   5289:40 /usr/bin/kvm ...
# lsof -n -p $PID
COMMAND   PID USER   FD   TYPE   DEVICE        SIZE     NODE NAME

or one command:

# lsof -n -p $(ps ax | grep [k]vm | awk '{print $1}' | head -1)
2

I just wondered myself where "-snapshot" stores its changes, and eventually found out where: It seems to use the same algorithm as "mktemp".

That is, if you set the environment variable TMPDIR to the path of some directory, then it stores its temporary data there.

Otherwise, it stores them in /tmp.

Therefore, if you want to save temporary snapshots in /var/tmp/kvm rather than in /tmp, run qemu as follows:

$ TMPDIR=/var/tmp/kvm qemu-system-x86_64 -enable_kvm ...

Alternatively, you can "export" the variable first:

$ export TMPDIR=/var/tmp/kvm
$ qemu-system-x86_64 -enable_kvm ...

This might be better if you start multiple VMs, because you don't need to specify the variable for every invocation.

Also, remember to also add "-no-shutdown" to the qemu options if you want to be able to keep the changes rather than losing them when the VM shuts down.

With -no-shutdown, the vm enters a stopped state after shutdown, but the vm process stays alive.

This allows you to connect to the KVM monitor console and issue the "commit" command, which writes all changes from the temporary file back to the VM image file. After that, you issue the "quit" command which actually terminates the VM process.

Without "-no-shutdown", you do not have the chance to issue the "commit" command, and without it all changes in the temporary file are lost once the VM process shuts down.

And another note: Do not wonder that no file shows up in the directory specified by $TMPDIR once the VM has started with "-snapshot".

The file is there, but qemu deletes the file immediately after it has created it, and therefore it does not show up in a directory listing.

But the qemu process still holds a reference to the file, and so the file is kept alive until the qemu process terminates. Only then the "deleted" file will be truly deleted, and the space occupied by it will be returned as free space to the filesystem.

1

Here's a little more concise version of the lsof command used above. This variant has several improvements. One being that it will list ALL the KVM VM's.

$ lsof -n -p $(pgrep kvm | tr '\012' ,)

You could then grep the output looking for .img and .qcow2 files if you knew you were using those extensions to name your VM images.

$ sudo lsof -n -p $(pgrep kvm | tr '\012' ,) |grep -E ".img|.qcow2"
qemu-kvm  3836 root   11u   REG              253,0 10737418240 19169281 /var/lib/libvirt/images/vm1.img
qemu-kvm  3895 root   11u   REG              253,0 48318382080 19890183 /kvm/vm2.img
qemu-kvm  3924 root   11u   REG              253,0 10737418240 19890185 /kvm/vm3.img
qemu-kvm 13594 root   11u   REG              253,0 43881005056 69173572 /var/lib/libvirt/images/vm4.qcow2

You could also go about this and query libvirtd to find out what storage pools it has available.

$ sudo virsh pool-list
Name                 State      Autostart 
-----------------------------------------
default              active     yes       

$ sudo virsh vol-list default
Name                 Path                                    
-----------------------------------------
vm1-d-disk0        /var/lib/libvirt/images/vm1-d-disk0   
vm2-d-disk0        /var/lib/libvirt/images/vm2-d-disk0   
vm3.img            /var/lib/libvirt/images/vm3.img         
...

So the default path would look to be /var/lib/libvirt/images for VMs that are using the default pool. That would be the same directory that temporary files related to -snapshot activity would reside as well.

0

Since I was examing the same and couldn't find them anywhere on the harddrive I would assume they stay in memory? That would also explain, why they aren't persistent.

If you want to write them to disk, use the save/restore commands. Here you can provide a path.

0

The snapshots are stored wherever you configure them to be. on my F14 it's in /var/lib/libvirt/images, but you can play with storage domains in virt-manager or virsh, however you like.

2
  • it is not about snapshots taken by hand, to store the actual state of the vm, its about where the temp-files are stored by kvm when starting with the -snapshot parameter
    – bmaeser
    Feb 23, 2011 at 16:25
  • and that is what my answer refers to exactly
    – dyasny
    Feb 23, 2011 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .