Table of Contents

KVM

Install

apt-get install qemu-kvm virtinst virt-manager bridge-utils libosinfo-bin
apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager    #ubuntu 20.04
sudo adduser $USER kvm 
#apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker
sudo kvm-ok                                                                       #check      
systemctl status libvirtd.service
https://github.com/LuyaoHuang/virsh-bash-completion                               #bash completion

Network

Set your network devices on OS level such as (example with 2 network interfaces “enp4s0” and “eno1” which is bridge slave interface for bridge “vmbr0”):

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
 version: 2
 renderer: networkd
 ethernets:
   enp4s0:
     dhcp4: no
     dhcp6: no
     addresses: [192.168.1.100/24]
     gateway4: 192.168.1.1
     mtu: 1500
     nameservers:
       addresses: [192.168.1.1]
       search: [local]
   eno1:
     dhcp4: no
     dhcp6: no
 bridges:
   vmbr0:
     dhcp4: no
     dhcp6: no
     interfaces: [eno1]
     addresses: [10.0.0.100/24]
     gateway4: 10.0.0.1
     mtu: 1500
     #nameservers:
     #  addresses: [8.8.8.8,8.8.4.4]
     #parameters:
     #  stp: true
     #  forward-delay: 4

Commands

virsh list                                                       #show running vm´s
virsh list --all                                                 #show all vm´s 
virsh undefine vm1
virsh destroy vm1                                                #forcefully and immediately stop a given domain - the vm won´t be destoyed, just stopped.
virsh start vm-name
virsh autostart test                                             #
virsh reboot vm-name
virsh shutdown vm-name
virsh suspend vm-name
virsh resume vm-name
virsh domrename foo bar                                          #rename vm from foo to bar
virsh autostart vm-name
virsh autostart --disable vm-name
virsh domrename $OLD_NAME $NEW_NAME
virsh domrename --domain oldname newname                         #rename domain/ vm       
virsh edit vm-name                                               #edit vm properties
osinfo-query os                                                  #list available os when specify os by "--os-variant" - package "libosinfo-bin" is required!
virsh net-list                                                   #list running network configs
virsh net-list --all                                             #list all network configs
virsh dumpxml vm-name                                            #xml-config to stdout
virsh dumpxml master > master.xml                                #backup vm
virsh define master.xml                                          #restore vm 
virt-manager                                                     #gui to show, manage and create vm´s

snapshot

virsh snapshot-list vm-name                                                 #list all snaps       
virsh snapshot-list vm-name--tree 
virsh snapshot-create-as vm-name
virsh snapshot-create-as --domain vm-name --name snap-name
virsh snapshot-create-as vm-name
virsh snapshot-create-as vm-name "snap1" --diskspec sda,file=/var/backups/snapshots/snap1-vm-name.qcow2 --disk-only --atomic
virsh snapshot-info --domain vm-name --snapshotname snap-name
virsh snapshot-revert --current vm-name
virsh snapshot-delete --current vm-name
virsh snapshot-delete --domain vm-name --snapshotname snap-name
virsh snapshot-create-as --disk-only vm-name
virsh snapshot-edit --rename  --domain vm-name --snapshotname snap-name     #rename
virsh snapshot-delete --current --metadata vm-name                          #delete "disk-snapshot"
qemu-img info --backing-chain test.qcow2
qemu-img info test.qcow2
qemu-img convert -O output_format source destination
virsh domblklist vm-name
virsh blockcommit vm-name sda --active --verbose --pivot

Change Path Of "backing file"

rename-backup-path.sh
#!/bin/bash
 
BACKINGFILE="/kvm/images/master.qcow2"
QCOW2_FILE_TO_CHANGE="master.1586464836"
 
sudo qemu-img rebase \
  -f qcow2 \
  -u \
  -b $BACKINGFILE \
  $QCOW2_FILE_TO_CHANGE

Other:

sudo qemu-img rebase \
  -f qcow2 \
  -b $NEW_BACKING_FILE \
  $QCOW2_FILE_TO_CHANGE

Merge snapshot and backing file into standalone disk

cp test1-base new-master                                 #test1-base = base-disk
qemu-img rebase -b new-master test1-master-0             #snapshot="test1-master-0"
qemu-img commit test1-master-0
qemu-img commit new-master                               #output if success: "Image committed"
Image committed.
qemu-img info new-master

Backup

virsh blockcopy --domain kmaster sda \
  /data/images/backup/copy.qcow2 \
  --wait --verbose --shallow \
  --pivot

Pool

virsh pool-list --all                                            #show also inactice pool
virsh pool-list                                                  #show active storage pool
virsh pool-list --details                                        #show details like disk usage
virsh pool-info poolname                                         #detailed pool info
virsh pool-autostart poolame
virsh pool-start poolname
virsh pool-undefine poolname                                     #delete pool "default"
virsh pool-destroy poolname                                      #set inactive
virsh pool-edit poolname
virsh pool-define-as --name default --type dir --target /media/work/kvm
virsh pool-refresh --pool default
virsh pool-refresh --pool kvm-vm

Volume

virsh vol-list default
virsh vol-list default --details
virsh vol-list kvm-vm --details
virsh vol-delete volume-name.qcow2  --pool default
virsh vol-create-as default test_vol2.qcow2 2G
virsh vol-clone --pool default vol1.qcow2 vol2.qcow2
qemu-img resize volume-name.qcow2 30G && virsh pool-refresh --pool poolname0 #vm has to be shutdown!

Pool refesh an volume list:

pool-vol-ref.sh
#!/bin/bash
 
virsh pool-refresh --pool poolname0
virsh pool-refresh --pool poolname1
virsh vol-list poolname0 --details
virsh vol-list poolname1 --details

clone domain/ vm

Simple Guest Clone

clone-vm.sh
#!/bin/bash
 
#author:  Thomas Roehm
#version: 1.0
 
echo ""
echo "Overview VM´s:"
echo ""
virsh list --all
echo ""
read -p "Please enter the name of the vm you want to clone: " SOURCEVM
echo ""
read -p "Please enter the name of the cloned vm: " DESTVM
echo ""
 
virt-clone \
--original $SOURCEVM \
--auto-clone \
--name $DESTVM
 
STATUS=$(echo $?)
 
echo ""
 
if [ $STATUS -eq 0 ]
then
        echo "VM \"$DESTVM\" cloned."
else
        echo "Problem - VM not cloned!"
fi

Log

journalctl -a -b -k -u libvirtd.service
systemctl status libvirtd.service

https://computingforgeeks.com/virsh-commands-cheatsheet/