Linux Harbour

Technology News of Linux & Open Source


How to build KVM virtual machine on Ubuntu 16.04

KVM (Kernel-based Virtual Machine) is the virtualization solution supported on Ubuntu and other Linux distributions, it is added to mainstream Linux Kernel from version 2.6.20. QEmu is the complete KVM emulator.  Many cloud servers are running as virtual machines, its advantage is rapid deployment. A number of machines can be added in a short time when the need of increasing machine happens. It can also save the purchase, money and physical space.

Bridge Network Interface

A general Linux provides 1 network interface to 1 physical network card (and 1 MAC address). If the Linux system is used as a KVM host, the bridge network interface allows 1 physical network card to serve multiple virtual machines, VM can use different network interfaces with different MAC addresses to share the same physical network card.

In the following example, it demonstrates how to configure eth0 as br0 bridge network interface in the file /etc/network/interfaces.

#auto eth0
#iface eth0 inet static
# The primary network interface
auto br0
iface br0 inet static
# dns-* options are implemented by the resolvconf package, if installed
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0

Preparing a virtual hard drive

Before building a KVM virtual machine, a virtual drive should be prepared for the VM. It is 2 options to build a virtual machine: directly building on Linux host, or building with another Virtualisation Software at office desktop/laptop (eg. VirtualBox) and then uploading to Linux host for file format conversion of a virtual hard drive. On the other hand, 2 options can be mixed, firstly building & uploading a virtual disk image file contains Linux OS in smaller disk size, and then build the 2nd virtual hard drive with a larger disk size on Linux host for file/data storage.

It is recommended to use the mixed method.

Direct building virtual hard disk

QEmu provides ‘qemu-img’ to build up a virtual hard disk image file in various format, QCOW format is developed by the QEmu team. The following command can create a 200GB mydatadisk.qcow2.

$ qemu-img create -f qcow2 mydatadisk.qcow2 200G

Converting the format of virtual hard disk

‘qemu-img’ can also convert the format of a virtual hard disk image file. The following command converts a VDI format virtual hard disk image file mylinuxdisk.vdi in VDI format (the common format used by VirtualBox) to QCOW format file mylinuxdisk.qcow2.

$ qemu-img convert -f vdi mylinuxdisk.vdi -O qcow2 mylinuxdisk.qcow2

Direct building and running KVM Virtual Machine (Linxu OS is installed on VM)

After building a virtual hard disk, ‘virt-install’ can be used to build a KVM machine. In the following example, a new KVM machine mylinuxvm with 2 vCPUs, 1GB Virtual Memory and bridge network interface br0 will be built from mylinuxdisk.qcow2. The parameter –import will skip the Operating System (OS) installation steps.

$ virt-install –name mylinuxvm –vcpus 2 –ram 1024 –import –disk mylinuxdisk.qcow2,format=qcow2 –network bridge=br0

Building a KVM Virtual Machine and Installing Linux OS (Linux OS is not installed in virtual harddisk)

‘virt-install’ with parameter -c to specifiy CDROM image file, for an example, ubuntu-16.04-server-amd64.iso to build a virtual machine, and then a GUI ‘virt-manager’ on GNOME can be used to install the Linux OS to the virtual hard disk image file.

$ virt-install –name mylinuxvm –vcpus 2 –ram 1024 –disk mylinuxdisk.qcow2,format=qcow2 –network bridge=br0 -c ubuntu-16.04-server-amd64.iso

Adding an additional virtual hard disk to a KVM Virtual Machine

‘virsh’ is the administration tool of KVM virtual machine, in the following example, an additional virtual hard disk image file myvdisk2.qcow2 as vdb will be added to a virtual machine mylinuxvm.

$ virsh attach-disk mylinuxvm –subdriver qcow2 –source myvdisk2.qcow2 –target vdb –persistent

Sammy Fung

Sammy is the founder of the Linux Harbour.