Virtualization

From docwiki
Revision as of 19:47, 31 October 2020 by Mond (talk | contribs) (libvirt frontend to KVM)
Jump to: navigation, search


Motivation

With virtual machine you can run a virtual computer inside your real computer. Thus you can save resources by avoiding to have many small machines and you can move these virtual machines between physical hardware in order to allow a higher degree of redundancy. There are a few free software/open source options in and around Linux and also some commercial offerings for virtualization. Here is an overview:

Free Software / Open Source Virtualization

QEMU

Qemu is a software that can emulate a lot of different CPUs and also hardware. It does not require any support from the OS so you can emulate everywhere. The downside is that it is much slower then virtualization with OS and hardware support. The Upside is that you can emulate e.g. an ARM system on your X86 Intel hardware. Etc. Qemu supports among others: x86, x86-64, Spar, ARM, PowerPC, RiscV processors.

Qemu can emulate a full system or just run a binary compiled for a different CPU on another system.

KVM

KVM is the virtualization native to Linux Kernel. It borrows a lot from QEMU and adds native virtualization. The userland tools are similar to that of QEMU. KVM is uses in Googles Cloud but also by IBM and also in parts of Amazons AWS.

XEN

Xen is a dedicated Hypervisor. In order to operate it and have access to hardware devices you need to run a Linux kernel in "Dom0". Xen is older then KVM and customized versions of XEN are used heavily by Amazons AWS Cloud.


Virtual Box

Is a (mostly) open source Virtualization that was developed by SUN and aimed at the desktop. Unfortunately it is now in the Hands of Oracle.

Commercial

  • VMware / ESX VMware was the first software that allowed virtualization of a PC. It is now the market leader in commercial virtualization. In 2004 it was bought by Dell/EMC.
  • Microsoft HyperV This is Microsofts native Virtualization and also powers Microsofts Azure Cloud.
  • Citrix Xen Server A commercial version of Xen,.

Native KVM on Linux

If you are not hooked on a certain platform then KVM is a good and save choice for virtualization.

First we create a virtual hard disk image. (We could also use a real partition or a file that is a 1:1 raw image of a partition). The qcow2 format from QEMU offers a "copy on write" file that only grows as the guest operation system writes data into it:

# create a file mybox.qcow2 with an 8GB image

qemu-img create -f qcow2 mybox.qcow2 8G

# initially the image only takes a few kB.

Now we can boot into our system. E.g. via:

kvm -k de -m 2G mybox.qcow2

Where -k de gives us a german (de) keyboard layout and -m 2G reserves 2G of main memory for our machine. Since our mybox.qcow2 disk is completely empty the system will not boot, but at least we see a window with a virutal PC that tries to boot. Now lets install someting there:

wget https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.6.0-amd64-netinst.iso

kvm -k de -m 2G  --cdrom debian-10.6.0-amd64-netinst.iso -boot d  mybox.qcow2 

And now we are able to install a debian into our virtual machine. In a few minutes of install you will have a fresh install of e.g. debian. You can then stop the machine and keep a copy of that fresh install in order to experiment with it later.

You can even run this without GUI and just with console. For this you need to configure serial console in the guest OS:

# in /etc/default/grub
GRUB_TERMINAL="console serial"
GRUB_CMDLINE_LINUX="console=ttyS0 console=tty0"

Then you can boot into your VM with:

kvm -k de -m 2G  -nographic  -serial mon:stdio  mybox.qcow2 

Network Interfaces

The default way that KVM in the above commands emulates the network is that you have a virtual network card that where clients connect via a kind of NAT. Of course one can specify which type of network one wants. It is possible to create an internal layer2 bridge and also to connect an outside interface via such a bridge so that the virtual machine could have its own IP address on the local LAN.

libvirt frontend to KVM

Using the kvm tool by hand is nice and useful for simple applications e.g. quickly firing up a test machine. When you want to run your virtual machines all the time in the background there are better tools for this. KVM Management Tools Lists a lot of open source and commercial tools. There are dedicated GUIs and web frontends but often the simple virsh CLI tools gets the job done without too much overhead.

virsh can manage KVM virtual machines but also Xen and even vmware ESX and others.


You can use the tool "virt-install" to install a new virtual machine (e.g. from an existing qcow2 file).

# this shows you all running virtual machines
virsh list 
# this one also shows stopped macines:
virsh list --all
# to start one e.g. use:
virsh start mymachine

You can use the virsh commands to managed various aspects of the virtual machines at runtime and you can use them to transparently migrate them to different hardware. Either with a shared storage below or even without one.

libvirt knows about 2 types of machines: permanent and transient. Transient are created on the fly and then forgotten again while permanent will be kept in the inventory even if they are not running.

the virt-install tool allows to install virtual machines. If we want to make a libvirt machine out of our qcow2 image above we could copy it to a place where only libvirt has access e.g. /var/lib/libvirt/images/ and then import it into a machine:

virt-install --name mybox1 --import --disk /space/virt/mybox1.qcow2 --vcpus 1 \
  --memory 2048 --os-type linux --os-variant debian10 --virt-type kvm  --network default
# if we have serial console setup like above we can now connect to the machine with:
virsh console mybox1

Or you can use the graphical console which works with vnx: e.g. with:

virt-viewer -c qemu:///system 

and then select mybox1 to connect to.

Alternatively there is the GUI virsh frontend: virt-manager.

Internally the information about the VM is stored in an XML file. This can be viewed with:

virsh dumpxml mybox1

This xml can be used to create other VMs (you should change name, mac address, etc..) you can also live edit the xml via:

virsh edit mybox1

Exercises