KVM notes

- 3 mins read

KVM notes

Check if the CPU supports virtualization:

egrep -c '(vmx|svm)' /proc/cpuinfo
lscpu | grep Virtualization
  • Install KVM
sudo apt install qemu-kvm qemu-utils libvirt-daemon-system libvirt-clients virtinst virt-manager
  • Check Virtualization
systemctl status libvirtd
systemctl --now enable libvirtd
  • Check qemu and virsh:
kvm --version
virsh --version
  • Components that can be used
sudo virt-host-validate
  • Check IOMMU
dmesg | grep -e DMAR -e IOMMU
  • Edit /etc/default/grub if WARN (IOMMU not enabled)

GRUB_CMDLINE_LINUX="intel_iommu=on" add this line to /etc/default/grub and sudo update-grub

  • Other devices
GRUB_CMDLINE_LINUX="cgroup_enable=devices  group_enable=freezer intel_iommu=on"
  • User permissions
sudo adduser $USER libvirt
sudo usermod -a -G libvirt $USER
  • Network
sudo virsh net-list --all
sudo virsh net-start default
sudo virsh net-autostart default
systemctl restart libvirtd
sudo virsh net-info default

Default is NAT network. Use Bridge network or macvtap for VMs to get IP from the router.

  • Create Bridge network debian
sudo apt install bridge-utils
sudo nano /etc/network/interfaces

auto br0
iface br0 inet static
    address 192.168.1.1/24
    gateway 192.168.1.254
    bridge_ports eth0 eth1
    bridge_stp on

brctl show (shows the bridge configuration and ports) ip addr show (shows the IP addresses assigned to the bridge and its ports) arp -n (shows the ARP cache for the bridge)

  • VirtManager GUI

Enable XML editing virt-viewer

  • Notes

Image path = /var/lib/libvirt/images

virt-install is a command line tool which provides an easy way to provision operating systems into virtual machines. virt-viewer is a lightweight UI interface for interacting with the graphical display of virtualized guest OS. It can display VNC or SPICE, and uses libvirt to lookup the graphical connection details. virt-clone is a command line tool for cloning existing inactive guests. It copies the disk images, and defines a config with new name, UUID and MAC address pointing to the copied disks. virt-xml is a command line tool for easily editing libvirt domain XML using virt-install’s command line options. virt-bootstrap is a command line tool providing an easy way to setup the root file system for libvirt-based containers.

  • Create a VM
virt-install \
--name=centosVM \
--ram=4096 \
--disk path=/var/kvm/images/centos7.img,size=15 \
--vcpus=2 \
--os-type=linux \
--os-variant=rhel7 \
--graphics none \
--location 'http://ftp.iij.ad.jp/pub/linux/centos/7/os/x86_64/' \
--extra-args console=ttyS0
Starting install...
Retrieving file vmlinuz... | 6.5 GB 00:17
  • virsh commands
virsh <command> <domain-name> [options]
virsh start VM1
virsh suspend  VM1
virsh shutdown VM1
virsh list --all
virsh console VM1
virsh dominfo VM1
  • Edit VM Share Filesystem
sudo virt-xml VM1 --edit \
    --memorybacking source.type=memfd,access.mode=shared
sudo virt-xml VM1 --add-device \
    --filesystem driver.type=virtiofs,source.dir=/home/madhu,target.dir=host_home    

driver.type=virtiofs: The hypervisor driver used to provide the filesystem, in this case, virtiofs. source.dir=/shared: The directory on the host computer you want to share with the Linux guest virtual machine. target.dir=/shared_server: The arbitrary string used to identify the shared directory to be mounted within the guest. I named it ‘shared_server’

mkdir -v ~/shared_server
sudo mount -v -t virtiofs shared_server ~/shared_server

Edit /etc/fstab add

shared_server /home/emu/shared_server virtiofs defaults 0 0

remount sudo systemctl daemon-reload

  • Disk serial
sudo hdparm -I /dev/sda | grep Serial
  • Video resolution

´´´xml ´´´

Set resolution in UEFI-BIOS - esc - esc

libguestfs-tools

sudo apt install libguestfs-tools