Virtualization With KVM and Libvirt
Virtualization With KVM and Libvirt
Virtualization With KVM and Libvirt
In what follows, we will create an isolated network with two virtual machines connected to it.
The network will be created using the Linux ethernet bridge mechanism. The virtual storage
Virtualization with KVM and libvirt
devices will be created using the QEMU qcow2 format. This format will be used because it
provides the mechanism of backing file, i.e., the same image can be used as a base for several
Introduction
virtual machines.
The KVM mechanism is best described in its own main web page1:
Table 1 – Raw and qcow2 QEMU disk image types
Raw Qcow2
KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86
hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable Raw is default format if no specific format is Qcow2 is an open-source format developed
kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specified while creating disk images. Raw as an alternative to the VMWare vmdk and
specific module, kvm-intel.ko or kvm-amd.ko. disk images do not have special features like Oracle Virtualbox vdi formats. Qcow2
compression, snapshot, etc. On the other provides features like compression, snapshot
Using KVM, one can run multiple virtual machines running unmodified Linux or Windows hand, raw disk images are faster than other and backing file.
images. Each virtual machine has private virtualized hardware: a network card, disk, disk image types.
graphics adapter, etc.
Exercises 2) Create a directory named after your student number under /opt and grant full access
permission to it for all system users:
1) KVM can be used by simply passing the --enable-kvm command line parameter to # mkdir /opt/student number
QEMU. In this first exercise, we will compare the performance between the execution of a # chmod 777 /opt/student_number
program in an emulated machine and its execution on a fully virtualized machine.
Move all files to that directory and, from now on, keep working on that directory:
1.1) Download the arcom_vm.img and launch the distribution using QEMU in emulation # mv * /opt/student_number
mode: # cd /opt/student_number
$ qemu-system-x86_64 arcom_vm.img
3) Create the arcom-vm1.qcow2 and arcom-vm2.qcow2 volumes (both backed by the
1.2) In the emulated machine, run /root/stress 100, where 100 is the number of arcom-vm.qcow2 volume) to be used by the virtual machines:
iterations executed by the program, and measure its execution time using a clock (host # qemu-img convert -O qcow2 arcom-vm.img arcom-vm.qcow2
application, smartphone, etc.). Note that this is the advisable procedure since time # qemu-img create -f qcow2 -o backing_file=arcom-vm.qcow2 arcom-vm1.qcow2
measurements in emulators and virtual machines (VMs) may be very inaccurate in several # qemu-img create -f qcow2 -o backing_file=arcom-vm.qcow2 arcom-vm2.qcow2
situations. # qemu-img info arcom-vm1.qcow2
Execution time:________________________________ The following script will be used to create a bridge with two virtual interfaces (vnet1 and
1.3) Shut down the virtual machine and relaunch QEMU with --enable-kvm parameter: vnet2) connected to it:
$ qemu-system-x86_64 –enable-kvm arcom_vm.img #!/bin/sh
set -x
Determine, by trial and error, the number of iterations to obtain an execution time ip tuntap add vnet1 mode tap
approximately equal to the one obtained before ip tuntap add vnet2 mode tap
Virtualization with KVM and libvirt 1/14 Virtualization with KVM and libvirt 2/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
# Adding the interface into the bridge is
# done by setting its master to bridge_name
ip link set vnet1 master kbr0
ip link set vnet2 master kbr0
Save the above script as ifup and enable execution permission for its owner (chmod u+x
ifup).
The following script will be used to delete all interfaces created by the ifup script:
#!/bin/sh
set -x
Virtualization with KVM and libvirt 3/14 Virtualization with KVM and libvirt 4/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
# virt-manager
Create the first virtual machine using the command line tool virt-install2:
You should be presented with a graphical window, with a list of virtual machines. You should
virt-install --name arcom-kvm1 --ram 64 --graphics vnc --disk path=arcom-vm1.qcow2
be able to find the previously created VM:
--import --network network=mynet1,model=virtio
The virtual machine is started and the virt-install command blocks until the machine is
powered off. In order to power off the machine, you must connect to it (using the VNC client)
and execute the poweroff command (still in the virtual machine).
Afterward, the machine can be restarted, stopped and powered off using the virsh tool.
To list all virtual machines managed through libvirt:
# virsh list --all
The VM is kept in memory but it won't be scheduled for execution. If you try to use the VM’s
terminal, you will get no response from it. To resume execution of the virtual machine:
# virsh resume arcom-kvm1
# virsh list --all
If the guest operating system supports the Advanced Configuration and Power Interface
(ACPI), a software shutdown can be requested:
# virsh shutdown arcom-kvm1
The same information can be obtained directly from the corresponding XML file:
cat /etc/libvirt/qemu/arcom-kvm1.xml
The virsh and virt-install utilities are particularly useful for scripting and for quick
checks. On the other hand, the virt-manager utility provides a more user-friendly
environment. Create the second VM using the virt-manager utility:
2
The –import parameter is used to build a guest around an existing disk image (the default is to install from a
given installation source). The device used for booting is the first device specified via "--disk" or "--filesystem".
Virtualization with KVM and libvirt 5/14 Virtualization with KVM and libvirt 6/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
Virtualization with KVM and libvirt 7/14 Virtualization with KVM and libvirt 8/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
Appendix – Virtualization packages for fedora
(from https://docs.fedoraproject.org/en-US/quick-docs/getting-started-with-virtualization/)
Run the following command to install the mandatory and default packages in the
virtualization group:
To finish the exercise, power off and delete your virtual machines:
# virsh list --all
# virsh destroy arcom-kvm1
# virsh destroy arcom-kvm2
# virsh list --all
# virsh undefine arcom-kvm1
# virsh undefine arcom-kvm2
# virsh list --all
Virtualization with KVM and libvirt 9/14 Virtualization with KVM and libvirt 10/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
Appendix - Creation of the system image
Finally, unmount the file system and detach the network block device:
Obtain the kernel and root file system archive files: # umount m
# qemu-nbd -d /dev/nbd0
$ wget http://www.dee.isep.ipp.pt/~jes/arcom/Lab8-KVM/bzImage
$ wget http://www.dee.isep.ipp.pt/~jes/arcom/Lab8-KVM/rootfs.tar.xz
The provided bzImage and rootfs.tar.xz were built with Buildroot, using the configurations in
qemu_x86_64_defconfig.
$ mkdir rootfs
$ tar xvf rootfs.tar.xz -C rootfs
$ dd if=/dev/zero of=arcom-vm.img bs=1M count=16
# modprobe nbd
# qemu-nbd --format=raw --connect=/dev/nbd0 arcom-vm.img
Use fdisk to create a single partition on /dev/ndb0, marked as bootable (‘a’ command),
using all disk space. After that operation, the output of ls -l /dev/ndb0* should present a
new device, /dev/nbd0p1.
Copy the Linux kernel to the /boot directory in the first partition:
# mkdir m
# mount /dev/nbd0p1 m
# mkdir -p m/boot/extlinux
# cp bzImage m/boot
DEFAULT linux
SAY Now booting the kernel from SYSLINUX...
LABEL linux
KERNEL ../bzImage
APPEND root=/dev/sda1
The file above will be used by the Extlinux bootloader. The bootloader is installed by the
following command:
# extlinux --install m/boot/extlinux
# dd if=/usr/share/syslinux/mbr.bin of=/dev/nbd0
3
As of Syslinux 6.03, "pure 64-bits", compression and/or encryption are not supported
(https://www.syslinux.org/wiki/index.php?title=Filesystem#ext)
Virtualization with KVM and libvirt 11/14 Virtualization with KVM and libvirt 12/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019
Appendix –Additional virt-install examples Bibliography
• Chirammal, Humble Devassy, Mastering KVM Virtualization, Packt Publishing, 2016
Creation of a VM based on a external kernel image (kernel=…), specification of kernel • USE LLC, Virtualization Guide, openSUSE Leap 15.0, 2018
parameters (kernel_args=…), and with no graphical output (--graphics none).
The --boot parameter can also be used to create a virtual machine that is started each time
the host starts.
Virtualization with KVM and libvirt 13/14 Virtualization with KVM and libvirt 14/14
ARCOM – MEEC – ISEP – 2018/2019 ARCOM – MEEC – ISEP – 2018/2019