Libvirt / kvm basics

installing kvm,

Lets check whether the host supports virtualization

# grep -E 'svm|vmx' /proc/cpuinfo
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl cpuid aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority dtherm ida
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl cpuid aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm tpr_shadow vnmi flexpriority dtherm ida

cpu supports virtualization lets install kvm now

#dnf install  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 vish 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"
# preference over vnc_listen.

Configuring firewalld to allowe 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
#cp centosext.qcow2 /var/lib/libvirt/qemu/
#cp centosext.xml /etc/libvirt/qemu
#virsh list --all
Id Name State
----------------------------------------------------

#virsh define centosext.xml

Domain centosext defined from centosext.xml

#virsh list --all
Id Name State
----------------------------------------------------
- centosext shut off

Starting the kvm guest and accessing.

#virsh start centosext
#virsh console centosext

CentOS Linux 7 (Core)
Kernel 3.10.0-693.17.1.el7.x86_64 on an x86_64

one login:

#when working with virsh command line some times we need to setup multiple vms of the same coniguration for practice/lab purpose this guide will walk you through with the process of cloning and sysprep of vms

# virsh list --all
Id Name State
----------------------------------------------------
- centosext shut off

#sudo virt-clone --original centosext --name centosext1 --file /var/lib/libvirt/images/centosext1.qcow2
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (102400 M requested capacity > 69749 M available)
Allocating 'centosext1.qcow2' | 100 GB 00:01:20
Clone 'centosext1' created successfully.

Checking the cloned kvm guests

#virsh list --all
Id Name State
----------------------------------------------------
- centosext shut off
- centosext1 shut off

Cloning muliple 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
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (102400 M requested capacity > 65534 M available)
Allocating 'centosext2.qcow2' | 100 GB 00:01:20

Clone 'centosext2' created successfully.
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (102400 M requested capacity > 63426 M available)
Allocating 'centosext3.qcow2' 1% [= ] 35 MB/s | 1.5 GB 00:48:08 ETA
Allocating 'centosext3.qcow2' | 100 GB 00:01:27

Clone 'centosext3' created successfully.
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (102400 M requested capacity > 61318 M available)
Allocating 'centosext4.qcow2' | 100 GB 00:01:26

Clone 'centosext4' created successfully.
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (102400 M requested capacity > 59210 M available)
Allocating 'centosext5.qcow2' | 100 GB 00:01:32

Clone 'centosext5' created successfully.

Listing the kvm guets after cloning.

#virsh list --all
Id Name State
----------------------------------------------------
- centosext shut off
- centosext$i shut off
- centosext1 shut off
- centosext2 shut off
- centosext3 shut off
- centosext4 shut off
- centosext5 shut off

printing the kvm gust names

#sudo virsh list --all | awk '{print $2}' | grep -v Name | tail -n 5
centosext1
centosext2
centosext3
centosext4

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 {}
[ 0.0] Examining the guest ...
[ 35.6] Performing "abrt-data" ...
[ 35.6] Performing "backup-files" ...
[ 42.0] Performing "bash-history" ...
[ 42.0] Performing "blkid-tab" ...
[ 42.2] Performing "crash-data" ...
[ 42.2] Performing "cron-spool" ...
[ 42.3] Performing "dhcp-client-state" ...
[ 42.3] Performing "dhcp-server-state" ...
[ 42.3] Performing "dovecot-data" ...
[ 42.4] Performing "logfiles" ...
[ 43.4] Performing "machine-id" ...
[ 43.4] Performing "mail-spool" ...
[ 43.5] Performing "net-hostname" ...
[ 43.7] Performing "net-hwaddr" ...
[ 43.9] Performing "pacct-log" ...
[ 44.0] Performing "package-manager-cache" ...
[ 44.6] Performing "pam-data" ...
[ 44.7] Performing "passwd-backups" ...
[ 44.7] Performing "puppet-data-log" ...
[ 44.7] Performing "rh-subscription-manager" ...
[ 44.8] Performing "rhn-systemid" ...
[ 44.9] Performing "rpm-db" ...
[ 44.9] Performing "samba-db-log" ...
[ 45.0] Performing "script" ...
[ 45.0] Performing "smolt-uuid" ...
[ 45.0] Performing "ssh-hostkeys" ...
[ 45.1] Performing "ssh-userdir" ...
[ 45.1] Performing "sssd-db-log" ...
[ 45.2] Performing "tmp-files" ...
[ 45.3] Performing "udev-persistent-net" ...
[ 45.3] Performing "utmp" ...
[ 45.4] Performing "yum-uuid" ...
[ 45.4] Performing "customize" ...
[ 45.5] Setting a random seed
[ 45.6] Setting the machine ID in /etc/machine-id
[ 46.1] Performing "lvm-uuids" ...
[ 0.0] Examining the guest ...
[ 11.7] Performing "abrt-data" ...
[ 11.8] Performing "backup-files" ...
[ 18.1] Performing "bash-history" ...
[ 18.1] Performing "blkid-tab" ...
[ 18.3] Performing "crash-data" ...
[ 18.3] Performing "cron-spool" ...
[ 18.4] Performing "dhcp-client-state" ...
[ 18.5] Performing "dhcp-server-state" ...
[ 18.5] Performing "dovecot-data" ...
[ 18.5] Performing "logfiles" ...
[ 19.5] Performing "machine-id" ...
[ 19.6] Performing "mail-spool" ...
[ 19.6] Performing "net-hostname" ...
[ 19.9] Performing "net-hwaddr" ...
[ 20.1] Performing "pacct-log" ...
[ 20.2] Performing "package-manager-cache" ...
[ 20.7] Performing "pam-data" ...
[ 20.8] Performing "passwd-backups" ...
[ 20.8] Performing "puppet-data-log" ...
[ 20.9] Performing "rh-subscription-manager" ...
[ 20.9] Performing "rhn-systemid" ...
[ 21.0] Performing "rpm-db" ...
[ 21.0] Performing "samba-db-log" ...
[ 21.1] Performing "script" ...
[ 21.1] Performing "smolt-uuid" ...
[ 21.1] Performing "ssh-hostkeys" ...
[ 21.2] Performing "ssh-userdir" ...
[ 21.3] Performing "sssd-db-log" ...
[ 21.3] Performing "tmp-files" ...
[ 21.4] Performing "udev-persistent-net" ...
[ 21.4] Performing "utmp" ...
[ 21.5] Performing "yum-uuid" ...
[ 21.5] Performing "customize" ...
[ 21.6] Setting a random seed
[ 21.7] Setting the machine ID in /etc/machine-id
[ 22.2] Performing "lvm-uuids" ...
[ 0.0] Examining the guest ...
[ 8.9] Performing "abrt-data" ...
[ 9.0] Performing "backup-files" ...
[ 15.2] Performing "bash-history" ...
[ 15.3] Performing "blkid-tab" ...
[ 15.4] Performing "crash-data" ...
[ 15.5] Performing "cron-spool" ...
[ 15.6] Performing "dhcp-client-state" ...
[ 15.6] Performing "dhcp-server-state" ...
[ 15.6] Performing "dovecot-data" ...
[ 15.6] Performing "logfiles" ...
[ 16.7] Performing "machine-id" ...
[ 16.7] Performing "mail-spool" ...
[ 16.7] Performing "net-hostname" ...
[ 17.0] Performing "net-hwaddr" ...
[ 17.2] Performing "pacct-log" ...
[ 17.3] Performing "package-manager-cache" ...
[ 17.9] Performing "pam-data" ...
[ 17.9] Performing "passwd-backups" ...
[ 18.0] Performing "puppet-data-log" ...
[ 18.0] Performing "rh-subscription-manager" ...
[ 18.1] Performing "rhn-systemid" ...
[ 18.2] Performing "rpm-db" ...
[ 18.2] Performing "samba-db-log" ...
[ 18.3] Performing "script" ...
[ 18.3] Performing "smolt-uuid" ...
[ 18.3] Performing "ssh-hostkeys" ...
[ 18.4] Performing "ssh-userdir" ...
[ 18.4] Performing "sssd-db-log" ...
[ 18.5] Performing "tmp-files" ...
[ 18.6] Performing "udev-persistent-net" ...
[ 18.6] Performing "utmp" ...
[ 18.7] Performing "yum-uuid" ...
[ 18.7] Performing "customize" ...
[ 18.8] Setting a random seed
[ 18.9] Setting the machine ID in /etc/machine-id
[ 19.4] Performing "lvm-uuids" ...
[ 0.0] Examining the guest ...
[ 9.0] Performing "abrt-data" ...
[ 9.2] Performing "backup-files" ...
[ 15.6] Performing "bash-history" ...
[ 15.6] Performing "blkid-tab" ...
[ 15.8] Performing "crash-data" ...
[ 15.9] Performing "cron-spool" ...
[ 15.9] Performing "dhcp-client-state" ...
[ 16.0] Performing "dhcp-server-state" ...
[ 16.0] Performing "dovecot-data" ...
[ 16.0] Performing "logfiles" ...
[ 17.0] Performing "machine-id" ...
[ 17.1] Performing "mail-spool" ...
[ 17.1] Performing "net-hostname" ...
[ 17.4] Performing "net-hwaddr" ...
[ 17.6] Performing "pacct-log" ...
[ 17.7] Performing "package-manager-cache" ...
[ 18.3] Performing "pam-data" ...
[ 18.3] Performing "passwd-backups" ...
[ 18.4] Performing "puppet-data-log" ...
[ 18.4] Performing "rh-subscription-manager" ...
[ 18.5] Performing "rhn-systemid" ...
[ 18.6] Performing "rpm-db" ...
[ 18.6] Performing "samba-db-log" ...
[ 18.6] Performing "script" ...
[ 18.6] Performing "smolt-uuid" ...
[ 18.7] Performing "ssh-hostkeys" ...
[ 18.7] Performing "ssh-userdir" ...
[ 18.8] Performing "sssd-db-log" ...
[ 18.8] Performing "tmp-files" ...
[ 19.0] Performing "udev-persistent-net" ...
[ 19.0] Performing "utmp" ...
[ 19.0] Performing "yum-uuid" ...
[ 19.1] Performing "customize" ...
[ 19.2] Setting a random seed
[ 19.3] Setting the machine ID in /etc/machine-id
[ 19.7] Performing "lvm-uuids" ...

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