KVM

From The World according to Vissie
Jump to navigation Jump to search

Install on Debian

At some point I will have to re-write this how-to. I used this as a base:

https://linuxhint.com/install_kvm_debian_10/

Old:

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,

sudo modprobe vhost_net
echo "vhost_net" | sudo  tee -a /etc/modules
lsmod | grep vhost
vhost_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 :
#111
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

https://www.linux-kvm.org/page/SPICE
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


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

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


GUI

Try the GUI stuff some time....

virt-manager

virt-manager 

Spice

sudo apt install virt-viewer