User Tools

Site Tools


Sidebar

kvm

This is an old revision of the document!


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 qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils
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

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

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

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

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).

GUI

virt-manager

virt-manager

Spice

sudo apt install virt-viewer
kvm.1622408822.txt.gz · Last modified: 2021/05/30 14:07 by vissie