User Tools

Site Tools


Sidebar

kvm

Install

Debian

From: https://linuxhint.com/install_kvm_debian_10/

Some network advanced stuff: https://www.linuxtechi.com/install-configure-kvm-debian-10-buster/

sudo apt install --no-install-recommends qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst dnsmasq qemu-utils ovmf
#  Some apps does not get installed with --no-install-recommends and seemds to be added: qemu-utils dnsmasq
# ovmf is for uf
sudo systemctl status libvirtd.service
sudo virsh net-list --all
sudo modprobe vhost_net

As we can see in above output, default network is inactive so to make it active and auto-restart across the reboot by running the following commands,

sudo virsh net-start default
sudo virsh net-autostart default

Stop all iptables rules

I had issues where these rules were interfering with my iptables manual rules. This worked

So edit your network (virsh net-edit) to <forward mode='open'/>.

If you want to offload the mechanism of “virtio-net” and want to improve the performance of KVM VMs then add “vhost_net” kernel module on your system using the beneath command,

echo "vhost_net" | sudo  tee -a /etc/modules
lsmod | grep vhost
host_net               24576  0
vhost                  49152  1 vhost_net
tap                    28672  1 vhost_net
tun                    49152  2 vhost_net

Note: If you want a normal user to use virsh commands then add that user to libvirt and libvirt-qemu group using the following commands

sudo adduser pkumar libvirt
sudo adduser pkumar libvirt-qemu

To refresh or reload group membership run the followings,

newgrp libvirt
newgrp libvirt-qemu

Setup Bridges Network

sudo vim /etc/network/interfaces

  
# This file describes the network interfaces available on your system

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
# allow-hotplug enp0s31f6
#iface enp0s31f6 inet dhcp
#auto enp0s31f6

auto br0
iface br0 inet dhcp
    bridge_ports enp0s31f6
    bridge_stp off
    bridge_fd 0
    bridge_maxwait 0   

Allow ping in host

sudo groupadd unpriv_ping
sudo usermod --append --groups unpriv_ping vissie
getent group unpriv_ping | cut -f 3 -d :
sudo vim /etc/sysctl.conf

...

net.ipv4.ping_group_range = 1003 1003

...

sysctl -p

or

sudo sysctl -w net.ipv4.ping_group_range='0 2147483647'

Create HDDs

# create a file "disk_image" with format qcow2 and 40GB of max space
qemu-img create -f qcow2 disk_image.img 40G

Running a VM

sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img

Port Forward

QEMU can forward ports from the host to the guest to enable e.g. connecting from the host to an SSH server running on the guest. For example, to bind port 10022 on the host with port 22 (SSH) on the guest, start QEMU with a command like:

qemu-system-x86_64 disk_image -nic user,hostfwd=tcp::10022-:22

SPICE

From: https://www.linux-kvm.org/page/SPICE</color>

sudo apt install spice-client-gtk

You wanted copy and paste between host and guest right? ;) We need to add a virtio-serial device to the guest, and open a port for the spice vdagent. We also need to install the spice vdagent in guest. Be sure the agent is running (and for future, started automatically). First the guest side, since the guest is running.

sudo apt install xserver-xorg-video-qxl spice-vdagent qemu-guest-agent
sudo systemctl start spice-vdagent
sudo systemctl enable spice-vdagent
vissie@mycomputer$ spicy -h 127.0.0.1 -p 5900

VNC

sudo apt install tigervnc-viewer
sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img -vnc 127.0.0.1:0
sudo netstat -npl | grep qemu
vissie@mycomputer$ vncviewer 127.0.0.1:0

Some commands

Working with domains

virsh list --all
virsh start server01
virsh vncdisplay server01
remote-viewer spice://localhost:5900
virt-manager
sudo virsh edit debiantesting
sudo virsh undefine paulawin10 ## Kill or destroy a domain

Upgrading

To increase the number of CPUs:

virsh setvcpus <vm_name> <vcpu_count> --config

If you get an error that you exceeded the maximum number, first do:

virsh setvcpus <vm_name> <vcpu_count> --config --maximum

Then repeat the above:

virsh setvcpus <vm_name> <vcpu_count> --config

To increase the memory size:

virsh setmaxmem <vm_name> <memsize> --config
virsh setmem <vm_name> <memsize> --config

I'll test this at some point too:

sudo virsh setvcpus --domain test --maximum 2 --config
sudo virsh setvcpus --domain test --count 2 --config
sudo virsh reboot test

Please remember to SWITCH OFF the VM, and back on for the changes to take affect.

error: Requested operation is not valid: cannot undefine domain with nvram

sudo virsh undefine --nvram nameofvm

List all supported os-variants

apt-get install libosinfo-bin
osinfo-query os

Start a install

sudo virt-install --name kvmdeb \
   --os-type linux \
   --os-variant debian10 \
   --ram 2048 \
   --disk /kvm/disk/kvmdeb.img,device=disk,bus=virtio,size=20,format=qcow2 \
   --graphics vnc,listen=0.0.0.0 \
   --noautoconsole \
   --hvm \
   --cdrom /kvm/iso/debian-10.5.0-amd64-netinst.iso \
   --boot cdrom,hd
qemu-system-x86_64 \
  -boot d \
  -cdrom "Bliss-v14.10-x86_64-OFFICIAL-opengapps-20221027.iso" \
  -bios /usr/share/ovmf/x64/OVMF.fd \
  -nodefaults \
  -enable-kvm \
  -smp 8 \
  -device intel-hda \
  -device hda-duplex \
  -device virtio-vga-gl \
  -net nic \
  -net user,hostfwd=tcp::5555-:5555 \
  -cpu host \
  -m 4096 \
  -display sdl,gl=on \
  -hda Android.img

Stop a running domain

virsh managedsave kvmdeb --verbose

Change network settings

To get a list of all domains and ip detail run:

sudo virsh net-dhcp-leases default

If you need the mac adress, try:

sudo virsh domifaddr viswin10

You can edit network settings by editing the default network with

sudo virsh net-edit default

As an example, here are my network settings for my 2 VMs (the lines starting with <host are what you're looking for):

<network>
  <name>default</name>
  <uuid>d836a341-605b-4ba8-a6ce-edfd7a756bc1</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3d:52:bf'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
      <host mac='52:54:00:b4:7e:ed' name='qemu-windows' ip='192.168.122.11'/>
      <host mac='52:54:00:46:d0:e8' name='qemu-mint' ip='192.168.122.12'/>
    </dhcp>
  </ip>
</network>

After making the desired changes, run

sudo virsh net-destroy default && sudo virsh net-start default && sudo systemctl restart libvirtd.service

to restart the network (best done with no VMs running).

Or…..

sudo virsh net-update default add-last ip-dhcp-host "<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip='192.168.122.10'/>" --live --config

Convert

qemu-img convert -f vdi -O qcow2 ./Lineage.vdi ./Lineage.qcow2

GUI

virt-manager

virt-manager

Spice

sudo apt install virt-viewer
kvm.txt · Last modified: 2023/06/01 00:52 by vissie