Tue, March 6, 2018 ยท 2 min read

Libvirt / kvm basics

Libvirt / kvm basics

installing kvm,

Lets check whether the host supports virtualization

# grep -E 'svm|vmx' /proc/cpuinfo

cpu supports virtualization lets install kvm now

#dnf install qemu-kvm qemu-img libvirt virt-install libvirt-client libvirt-python

verifying that that kernel is loaded with kvm

#lsmod | grep kvm
kvm_intel             200704  3
kvm                   581632  1 kvm_intel
irqbypass              16384  4 kvm

we can control libvirtd daemon from virsh command.

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     cipher_default                 running
 -     centosext                      shut off
 -     centosext1                     shut off
 -     centosext2                     shut off
 -     centosext3                     shut off
 -     centosext4                     shut off
 -     centosext5                     shut off
 -     chefnode1                      shut off
 -     chefnode2                      shut off
 -     chefnode3                      shut off
 -     chefnode4                      shut off

Enabling vnc access kvm hosts, when setup kvm hosts from -vnc option it will listen on loopback 127.0.0.1 to make vnc access available on all interfaces edit /etc/libvirt/qemu.conf and set vnc_listen=0.0.0.0.

#grep vnc_listen /etc/libvirt/qemu.conf
vnc_listen = "0.0.0.0"

Configuring firewalld to allow vnc ports,

#firewall-cmd --get-active-zones
public
  interfaces: enp9s0

#firewall-cmd --zone=public --permanent --add-port=5900-6100/tcp
success

#firewall-cmd --zone=public --permanent --list-ports
5900-6100/tcp

Creating vms from virsh console

#virt-install --name centos7 --ram 512 --file=/var/lib/libvirt/images/centos.qcow2 --file-size=3 --vnc --cdrom=/home/cipher/centos.iso

Clone and sysprep of vms

#sudo virt-clone --original centosext --name centosext1 --file /var/lib/libvirt/images/centosext1.qcow2

Cloning multiple kvm guests at once

#for i in $(cat vms); do sudo virt-clone --original centosext --name centosext"$i" --file /var/lib/libvirt/images/centosext$i.qcow2; done

Sysprep will remove any initial configurations set on vm

#sudo virsh list --all | awk '{print $2}' | grep -v Name | tail -n 5 | xargs -I {} sudo virt-sysprep -d {}

Lets set hostnames and root passwords

#virt-sysprep -d centosext1 --hostname centosext.lab.com --root-password password:decipher

Renaming a virtual machine domain with virsh.