VM spinup using libvirt 2
In a previous blog post, I showed how I pushed VMs to a server. It worked, but I was never happy with the fact that I needed to have root access on the KVM host and there were some other issues as well. This blog post is about the new version, where I only need virsh
access to the host.
There are two scripts, one for download and one for upload. They are available here
Download script
Usage
download_vm.sh vm_to_download qemu+ssh://myserver/system
with
-
vm_to_download
: the name of the VM to download -
qemu+ssh://myserver/system
: The server to use. This is used with the-c
flag ofvirsh
. This is optional.
I use this to get an already functioning machine and download the virtual hard disk and the xml file. That makes for easy editing/templating and upload for later.
Upload script
Usage
upload_vm.sh local_vm new_domain qemu+ssh://myserver/system
with
-
local_vm
: The name of the VM to upload. This is used for the basename of both the qcow and xml files. -
new_domain
: The name of the new domain on the host. -
qemu+ssh://myserver/system
: The server to use. This is used with the-c
flag ofvirsh
. This is optional.
Note that the “name” inside the xml must match the name of the xml file.
About large qcow files
If bandwidth usage is an issue, consider having a user on the virtualisation host with virsh
access, and use the upload script from there. That way it will “upload” from disk to disk.
When it is downloaded, you may want to look into “sparsifying”. This is done using virt-sparsify
.
Some stats:
$ qemu-img info deb10-base.qcow2
image: deb10-base.qcow2
file format: qcow2
virtual size: 50 GiB (53687091200 bytes)
disk size: 37 GiB
...
$ virt-sparsify deb10-base.qcow2 deb10-base-sparse.qcow2 --tmp .
[ 0.1] Create overlay file in . to protect source disk
[ 0.3] Examine source disk
[ 4.0] Fill free space in /dev/sda1 with zero
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 973.5] Clearing Linux swap on /dev/sda5
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[1004.2] Copy to destination and make sparse
[2038.8] Sparsify operation completed with no errors.
...
$ qemu-img info deb10-base-sparse.qcow2
image: deb10-base-sparse.qcow2
file format: qcow2
virtual size: 50 GiB (53687091200 bytes)
disk size: 1.57 GiB
...
My default no-gui base image for debian uses 1.5GB of disk space, and sparsifying, we effectively remove 35GB of swap/cache/noise.