Using KVM as hypervisor on CentOS 7

This post describes how to use a CentOS 7 installation as hypervisor for a virtual machine running Ubuntu 14.04 LTS.

These examples is just to show the basics on getting KVM virtualization up and running and should not be put in to production before considering the added value SElinux gives.

Example 1
Since this VM is planned to be a webserver, the VM will only have access to a text console (headless) and there will not be any graphical consoles available through VGA, VNC, Spice or QXL. The VM will be connected to the default network, meaning network traffic from the VM will be NAT based through the host.

Using virt-install to create a headless VM
$ sudo virt-install -n vm-name –description “server for example.com” –os-type=Linux –os-variant=generic –ram=2048 –vcpus=1 –disk path=/var/lib/libvirt/images/vm-hhj.qcow2,bus=virtio,size=10 –graphics none –console pty,target_type=serial –location=/var/lib/libvirt/images/ubuntu-14.04.2-server-amd64.iso –extra-args=console=ttyS0,115200n8 serial –network default

To exit this console view you can use the key combination CTRL + Alt gr + 9
If you are using Putty as SSH client from Windows you can use the key combination CTRL+5 on the Norwegian, Swedish and Finnish keyboard layout.

Example 2
VM with graphical console available through SSH using port forwarding and VNC.

From your local workstation
Create a SSH tunnel from you workstation to the hypervisor server
$ ssh servername.example.com -L 5903:127.0.0.1:5903

Description of the SSH -L option
-L [bind_address:]port:host:hostport
Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

On the hypervisor server
Create a VM with a graphical VNC console
$ sudo virt-install –graphics vnc,port=5903 –noautoconsole –network default –name TestVM –ram 2048 –vcpus=1 –disk path=/var/lib/libvirt/images/TestVM.img,size=5 –location=/var/lib/libvirt/images/ubuntu-14.04.2-server-amd64.iso -v –accelerate –noreboot

From your local workstation (while you have a active SSH session with port forwarding)
Start a VNC connection to localhost port 5903 using krdc or other VNC clients.
The VNC path would then be like
vnc://localhost:5903

Or you can test virt-viewer
$ virt-viewer –connect qemu+ssh://username@example.com/system TestVM

Create file
/etc/polkit-1/localauthority/50-local.d/50-org.example-libvirt-remote-access.pkla

[Remote libvirt SSH access]
Identity=unix-group:wheel
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

You should now be able to install your desired operation system on your new VM.

virsh
Here is a list of useful virsh commands that might come handy when using CentOS as hypervisor.

Start VM
# virsh start vm-name

Stop VM (ACPI)
# virsh shutdown vm-name

If shutdown does not work you can try the destroy command. It is like using the power button on a physical server.
# virsh destroy vm-name

Connecting to the VM and start the installation
# virsh console vm-name

List networks
# virsh net-list

If the network is not active, start it by doing:
# virsh net-start default

List all VMs
# virsh list –all

Remove VM from list
# virsh undefine vm-name

Sources
https://snippets.webaware.com.au/howto/running-qemu-with-port-redirection-through-libvirt/
http://forum.proxmox.com/threads/21194-Port-Forward-with-built-in-NAT-and-PVE-Firewall
http://wiki.libvirt.org/page/SSHPolicyKitSetup
https://www.jethrocarr.com/2012/08/04/virt-viewer-remote-access-tricks/
https://virt-manager.org/download/