Table of Contents

Prev  
 Next
, , , , , , , , , , , , , , ,

KVM Setup

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.

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:

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

Copy to New Server

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.


Windows10 on KVM

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.


KVM Backup

There seems to be 4 main ways to backup a KVM virual machine

  1. Copy the main file(s) while the VM is running
    1. Not recommended as file corruption will probably occur as the VM operation may and probably will modify the file during the copy process
  2. Shutdown the VM first and then copy the file. Start the VM again after the copy is completed.
  3. Use virsh backup-begin command to allow live full backup
  4. Live backup using snapshot
    1. Create a snapshot of the VM and direct all changes to the snapshot allowing safe backup of main VM file
    2. Active back commit the snapshot and verify back commit worked

KVM Offline Backup

Note this only works on VMs that are shut down

  1. sudo virsh list –all to list all KVM virtual machines.
  2. sudo virsh dumpxml VM_name | grep -i "source file" to list the VM source file location noted in the VM XML file.
  3. sudo virsh dumpxml vm-name > /path/to/xm_file.xml to archive/backup the VM XML definition file.
  4. sudo cp -p /working/path/VM_image.qcow2 /path/to/ to archive/move the VM file.
  5. sudo virsh undefine vm-name --remove-all-storage to undefine the VM and remove its storage. (Be careful with this one!)
  6. sudo virsh define –file <path-to-xml-file> to import (define) a VM from an XML file.

References:

—-

KVM Cheat Sheet

There are perhaps too many of these I will keep this list very short and simple with the most useful options.

Where:

Notes:


KVM QEMU Commands

Change the Disk Allocated Size

How to change the amount of disk space assigned to a KVM *How to Resize a qcow2 Image and Filesystem with Virt-Resize

The virt-filesystems command may not be installed by default and can be installed with the following sudo apt install guestfs-tools

How To extend/increase KVM Virtual Machine (VM) disk size

Shrink the Disk File

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.

How to mount VM virtual disk on KVM hypervisor

There seem to be a number of methods to do this.

In all cases the VM (Virtual Machine) must be in shutdown state.

libguestfs method

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.

tl;dr; libguestfs

Mount a qcow2 image directly

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.

When complete clean-up with the following commands.

Mount a qcow2 image with LVM

Links:

Some Keypoints are:

Some links:

Home Server Index

Prev  
 Next