(kvm) qemu virsh
migrate guest to new host
old# virsh list
old# virsh list --all
old# virsh dumpxml Void > Void.xml
old# cp Void.xml Void.qcow2 /mnt ; sync
...
new# virsh create Void.xml # disappears after shutdown, or
new# virsh define Void.xml # persistent, not started
persistent domain
migrate
learn properly
note that if the source and target hypervisor differ more in terms of versions, compilation
settings etc it might be safer to try the change first with the virt-manager GUI
pay attention to the keyboard and mouse options, e.g. PS/2 vs USB vs VirtIO...and maybe to
graphics options, e.g. Virtio vs. QXL
no default network?
shell# virsh
net-list --all
net-start default
net-autostart default
net-list --all
LVM raw disk to qcow2 file...
See Converting kvm guests from lvm to qcow2, base images and snapshots
...and back?
cd /var/lib/libvirt/images/
qemu-img convert -O raw alpine.qcow2 alpine.raw
ls -l alpine.raw
-rw------- 1 root root 1811160064 Nov 2 12:01 alpine.raw
# ^^^^^^^^^^
lvcreate -L 1811160064b -n alpine guests
dd if=alpine.raw of=/dev/guests/alpine bs=4M
After migration edit xml or virsh attach-disk or switch disk
in virt-manager
squeeze images, save disk space
consider starting with BleachBit to trim out temporary files etc (often readily available in your distribution)
then guestfs-tools: virt-sparsify
• if enough RAM available, speed up by TMPDIR=/your/path/to/tmpfs/ramdisk ...e.g.
mount -t tmpfs -o size=30g tmpfs /mnt
• if not enough free space in /tmp use any TMPDIR big enough (e.g. /tmp on a small subvolume)
• ! on SuSE also install libguestfs-appliance rpm-devel
• in Fedora also make sure libvirt has write access to TMPDIR, or you get
virt-sparsify: error: libguestfs error: could not create appliance through libvirt.
without (big) guestfs-tools?
cp image.qcow2 image.qcow2.previous
# without compression, faster but bigger
qemu-img convert -O qcow2 image.qcow2.previous image.qcow2
# with compression, smaller but slower
qemu-img convert -O qcow2 -c image.qcow2.previous image.qcow2
but do sparsify before compression for best (much smaller) results
many disk ops with
qemu-img
all at once
from raw LVM to sparsified then compressed QCOW2 file for back up
convenience, slight speed up with ramdisk
mount -t tmpfs -o size=40g tmpfs /mnt
mkdir /mnt/tmp
cd /mnt/
qemu-img convert -O qcow2 /dev/guests/suse suse-uncompressed.qcow2
TMPDIR=/mnt/tmp virt-sparsify suse-uncompressed.qcow2 suse-sparsified.qcow2
qemu-img convert -O qcow2 -c suse-sparsified.qcow2 ~/backups/suse.qcow2
sync
cd
umount /mnt
virsh dumpxml suse > ~/backups/suse.xml
Depending on the partition sizes and layout, e.g. if /tmp is big enough, simply...
cd /var/lib/libvirt/images/
rm -rf /tmp/tmp && mkdir /tmp/tmp && for i in *2 ; do TMPDIR=/tmp/tmp virt-sparsify $i /tmp/tmp.qcow2 && sync && qemu-img convert -O qcow2 -c /tmp/tmp.qcow2 $i && sync && ls -lh $i; done && rm -rf /tmp/tmp /tmp/tmp.qcow2
...could be enough if you don't need the intermediate files
If /tmp is too small because of tmpfs and small RAM or small btrfs slice size use some suitable filesystem, e.g. /var/tmp instead
On new Fedora 38 install it seems you need
# virt-sparsify: error: libguestfs error: could not create appliance through
# libvirt. Try running qemu directly without libvirt using this environment variable
export LIBGUESTFS_BACKEND=direct
Note if migrating from bleeding edge host to LTS host, xml could include too
fancy virtual hardware features.
In that case try to create new machine and just use the disk image.
diff backups
mkdir old new
guestmount -a /backups/week/host.qcow2 -i --ro old/
guestmount -a /backups/day/host.qcow2 -i --ro new/
diff -r old new | less
# see new items only
diff -r old new 2>/dev/null | grep '^Only in new' | less
See also:
Virt-Manager like a Pro
virt-sysprep
RaspBerry ARMv8 64bit as server
usb redir
use your QEMU wiser with Quickemu
even live blockcopy