I original setup my main server and virual machines all with Ubuntu. However I have started using Debian and find it leaner than Ubuntu. I am slowly moving my various servers and virtual machines to Debian.
Basically to install the KVM Hypervisor: sudo apt install qemu-kvm qemu-system qemu-utils libvirt-clients libvirt-daemon-system virtinst bridge-utils
bridge-utils is optional.
genisoimage
is a package to create iso imageslibguestfs-tools
is a library to access and modify VM disk imageslibosinfo-bin
is a library with guest operating systems information to assist VM creation
Use the built-in clone facility: sudo virt-clone --connect=qemu://example.com/system -o this-vm -n that-vm --auto-clone
.
To list all defined virtual machines virsh list --all
.
Which will make a copy of this-vm, named that-vm, and takes care of duplicating storage devices.
To dump a virtual machine xml definition to a file: virsh dumpxml {vir_machine} > /dir/file.xml
.
Modify the following xml tags:
uuidgen
to generate a new unique uuid.
To convert the xml file back to a virtual machine definition: sudo virsh define /path_to/name_VM_xml_file.xml
.
The VM xml file can be edited directly using the command virsh edit VM_name
To get virtual image disk information sudo qemu-img info /path_to/name_VM_file.img
A compacted qcow2 image can be created using the following command: sudo qemu-img convert -O qcow2 -c old.qcow2 new_compacted_version_of_old.qcow2
I created a new Debian server on the same hardware as the original Ubuntu server. Only one server could be running at a time using EUFI boot options. I ran into the following problems.
kvm-spice -machine help
shows the allowable configurations on the current KVM server. Simply changing the machine value to one listed from kvm-spice in <type arch=“x86_64” machine=“pc-i440fx-5.2”>hvm</type> corrected this problem.I have not used Windows on a VM now since circa 2021. Just no need. I do have a dual boot on my main desk top that I default to Debian testing and can boot to Windows 11 when I need to use Windows based software. My sons all still use Windows exclusively on their computers and game consoles….. So I still have a family MSOffice 365 subscription. This give access to MSoffice and 1TB of MS Cloud each. I had poor performance on Windows 7, 8/8.1, and 10 running on KVM a few years back. A large frustration was that I could not seem to get more than 2 CPUs functioning on the Windows VM even though I assigned 4. Performance was very poor, with CPU usage usually saturated with any use and relatively high even when idle. I found out early that Windows has limitations on the number of CPUs that could be used; 1 on Home, 2 on professional and 4 on Workstation and more on Server versions, at least that was my understanding. As I did not have a great need for the Windows VM I did not try too hard and basically did not use.
What I recently discovered was that this Windows OS limitation was not on the number of logical CPUs, but rather on the number of sockets configured. Further to this KVM allows for configuration of socket|Cores|Threads. See the picture below. This actually makes sense for limitations on the number of sockets on a paid OS. So there seems to be no limit on the number of cores and threads, only the number of sockets. Sadly, the default KVM topology setup is to assign all the virtual CPUs as sockets with 1 core(/socket) and 1 Thread(/core). When setting the manual CPU topology option to 1 Socket with 4 Cores(/Socket) and 1 Thread(/Core) my Windows 10 could see the 4 cores and performance increase dramatically. Upon further use I seemed to get best performance with 6 cores for the Windows VM. It is basically usable now.
BTW my server hardware configuration is: 1 Socket, 8 Cores (/Socket) & 1 Thread(/Core)
DESKTOP-M41KNMA
My understanding is that Windows Professional only allows one user to be actively logged in at any time either locally or remotely. This limitation was never a concern for me.
There seems to be 4 main ways to backup a KVM virual machine
Note this only works on VMs that are shut down
sudo virsh list –all
to list all KVM virtual machines.sudo virsh dumpxml VM_name | grep -i "source file"
to list the VM source file location noted in the VM XML file.sudo virsh dumpxml vm-name > /path/to/xm_file.xml
to archive/backup the VM XML definition file.sudo cp -p /working/path/VM_image.qcow2 /path/to/
to archive/move the VM file.sudo virsh undefine vm-name --remove-all-storage
to undefine the VM and remove its storage. (Be careful with this one!)sudo virsh define –file <path-to-xml-file>
to import (define) a VM from an XML file.References:
—-
There are perhaps too many of these I will keep this list very short and simple with the most useful options.
sudo virsh nodeinfo
: Virsh display node informationsudo virsh list --all
: Virsh List all domains, the --all
option ensure inactive domains are listedsudo virsh dominfo domain-name
: List information of domain domain-name
or domain domain-id
sudo virsh domiflist domain-name
: List network interface(s) information of domain domain-name
or domain domain-id
sudo virsh domblklist domain-name
: to locate the file of an existing VM of domain domain-name
or domain domain-id
sudo virsh domrename currentname newname
: To rename domainsudo virsh dumpxml domain-name > /dir_tree/kvm_backup/domain-name.xml
: To copy the domain definition of domain-name
to xml filesudo virsh dumpxml domain-name
will list the xml domain definition of domain-name
virsh define --file /dir_tree/kvm_backup/domain-name.xml
: To restore a VM definition from a xml file. The file is normal one created from virsh dumpxml
and domain-name
is given in this definition filesudo virsh pool-list
: List storage poolssudo virsh vol-list --pool pool-name
: List volumes in pool-name
sudo virsh start domain-name
: To start a virtual machine of domain domain-name
sudo virsh shutdown domain-name
: To shutdown a VM of domain-name
or domain-id
(initiate a shutdown now in VM, could take some time to actually stop)sudo virsh destroy domain-name
: To destroy a VM of domain-name
or domain-id
(effectively power down VM, force off, could corrupt a working VM)sudo virsh reboot domain-name
: To reboot (shutdown and restart) a VM of domain-name
or domain-id
sudo virsh suspend domain-name
: To suspend or pause and operating VM of domain-name
or domain-id
, all cpu, device and i/o are paused. But VM remains in operating memory ready for immediate resume / un-pause sudo virsh resume domain-name
: To resume / unpause a suspended / paused VM of domain-name
or domain-id
man virsh --help
: virsh helpman virsh list --help
: virsh list specific helpWhere:
Notes:
How to change the amount of disk space assigned to a KVM *How to Resize a qcow2 Image and Filesystem with Virt-Resize
sudo qemu-img info /path_vm/vm_name.img
sudo qemu-img resize /path_vm/vm_name.img +20G
sudo cp /path_vm/vm_name.img /path_vm/vm_name_backup.img
virt-filesystems --long -h --all -a /path_vm/vm_name.img
, we can also use this to confirm the correct partition to expand.sudo virt-resize --expand /dev/sda1 /path_vm/vm_name_backup.img /path_vm/vm_name.img
The virt-filesystems
command may not be installed by default and can be installed with the following sudo apt install guestfs-tools
cp image.qcow2 image.qcow2_backup
qemu-img convert -O qcow2 image.qcow2_backup image.qcow2
qemu-img convert -O qcow2 -c image.qcow2_backup image.qcow2
Example: A 11GB disk file I shrank without compression basically remained unchanged at 11GB, but with compression to 5.2GB. Time to compress was longer and dependent upon the hardware used.
There seem to be a number of methods to do this.
In all cases the VM (Virtual Machine) must be in shutdown state.
A method is to use the tool set libguestfs
however it is very heavy with many dependencies, so I have decided not to pursue this option.
To check if already installed or not: sudo apt list --installed | grep qemu-utils
To install sudo apt install qemu-utils
The nbd (network block device) kernel module needs to be loaded to mount qcow2 images.
sudo modprobe nbd max_part=16
will load it with support for 8 block devices. (If more blocks are required use 16, 32 or 64 as required.) sudo virsh list --all
. sudo virsh shutdown <domain-name or ID>
. sudo virsh domblklist <domain-name>
to get the full path and file name to the VM image file. ls -l /dev/nbd*
to check if any devices named nbd have already been defined. sudo qemu-nbd -c /dev/nbd0 <image.qcow2>
to create the block devices on the VM.sudo fdisk /dev/nbd0 -l
will list the available partitions in /dev/nbd0.sudo partprobe /dev/nbd0
to update the kernel device list. ls -l /dev/nbd0*
to see available partitions on image. sudo mkdir /mnt/image
. sudo mount /dev/nbd0p1 /mnt/image
or sudo mount -r /dev/nbd0p1 /mnt/image
to mount read only or sudo mount -rw /dev/nbd0p1 /mnt/image
to mount explicitly read-write.When complete clean-up with the following commands.
sudo umount /mnt/image
sudo qemu-nbd -d /dev/nbd0
.sudo virsh start <domain-name>
Links:
sudo modprobe nbd max_part=8
to enable the nbd (network block device) kernel module on hostsudo qemu-nbd --connect=/dev/nbd0 /mnt/kvm/VMname.qcow2
to use qemu-nbd to connect your qcow2 file as a network block devicesudo fdisk /dev/nbd0
to help with finding partitions on the VM file, qcow2sudo fsck /dev/nbd0p1
to fix the corrupted disk on vmsudo qemu-nbd --disconnect /dev/nbd0
to disconnect the disk network block deviceSome Keypoints are:
sudo virsh
to get into virsh, the virtualisation interactive terminal. Once inside virsh:list
to list running VMs, or list --all
to list all defined VMs, running or shutdownedit VM_name
to edit the XML configuration file of the VM names VM_name.sudo virsh define XXX.xml
to add a vm (XXX.xml)into virsh persistently. The VM is not started. The vm xml definition files can be found in /etc/libvirt/qemu
.sudo virsh start VM_name
to start the VM. (Also reboot, reset, shutdown, destroy)sudo virsh help | less
list all the virsh commandsSome links: