Julian I. Kamil

etechcetera

Logo
News and opinions on science, technology, and pop culture

Home
About me
Medium
GitHub
Mastodon
Twitter
WeChat
Blogspot
Credly

Getting closer to the metal with KVM

/ Tags: blog - virtualization - hypervisor - infrastructure

x

In case you haven’t noticed, the Ubuntu Server distribution has made setting up and running the Linux KVM (Kernel Virtual Machine) for virtualization dead simple. You can follow this instruction on LinuxConfig to install and configure a server with the graphical management tools, or if you are like me and prefer the command line interface, simply install the minimal Ubuntu Server along with OpenSSH service and install and configure KVM with only the command line management tools. Not only that this approach reduces server storage space requirement, but it also avoids the need to bring in the full graphical windowing system and results in a simpler, more secure overall server configuration.

KVM turns the Linux kernel into a Type 1 virtual machine monitor, or hypervisor, providing efficient, high performance, closer-to-the-metal platform for paravirtualization that takes advantage of Intel VT-x or AMD-V CPU Hardware Virtual Machine (HVM) instruction sets, while conveniently letting the remaining OS environment to continue to be available and function to run other applications and services.

Creating your first KVM virtual machine

To define a new guest VM, you will need to create a domain XML definition file and run it through the virsh define command. The simplest way to do this without using any graphical tools is by starting with a sample template like the one below, and then editing and modifying it appropriately.

Domain XML file testvm.xml:

<domain type='kvm'>
  <name>testvm</name>
  <uuid>4dea24b3-1d52-d8f3-2516-782e98a23fa0</uuid>
  <memory>131072</memory>
  <vcpu>1</vcpu>
  <os>
    <type arch="i686">hvm</type>
    <boot dev='cdrom'/>
    <boot dev='hd'/>
  </os>
  <clock sync="localtime"/>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <source file='/var/lib/libvirt/images/testvm.img'/>
      <target dev='hda'/>
    </disk>
    <disk type="file" device="cdrom">
      <driver name="qemu" type="raw"/>
      <source file="/var/lib/libvirt/boot/ubuntu-18.04-live-server-amd64.iso"/>
      <target dev="hdc" bus="ide"/>
      <readonly/>
      <address type="drive" controller="0" bus="1" target="0" unit="0"/>
    </disk>
    <interface type='network'>
      <source network='default'/>
      <mac address='24:42:53:21:52:45'/>
    </interface>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'/>
  </devices>
</domain>

Before using the sample file above, you must edit it and change the UUID and the MAC address — which can be generated randomly, see the tips at the end of this article — and make sure it points to valid and existing disk image and ISO image files. Next, issue the following commands to create the guest VM and find out the VNC port number for the console.

# create disk image
sudo mkdir -p /var/lib/libvirt/images
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/testvm.img

# define the guest vm
sudo virsh define testvm.xml

# find out the vnc port number the guest is listening to
sudo netstat -nap | egrep '(kvm|qemu)'

Use a VNC client to connect to the guest VM console and complete the OS installation. Once the OS is installed, you will need to eject the installation media and reorder the disk boot sequence. To do that, issue the following commands:

# eject the installation media
sudo virsh change-media testvm hdc --eject

# edit the guest configuration to reorder boot sequence
sudo virsh edit testvm

And that’s all there is to it… You should now have a guest VM running under the Linux KVM hypervisor in your bare metal server.

Tip: Generating UUIDs

The uuid command line tool generates standards compliant, globally unique identifiers (universally unique IDs or UUIDs) that are computationally difficult to guess and can be appropriately used to tag both short-lived and highly-persistent network objects such as guest virtual machines. If not already installed, you can issue the following command to install the tool:

sudo apt-get update && sudo apt-get -y install uuid

Tip: Generating MAC addresses

There is not a readily available command line tool for generating MAC addresses, but you can easily make one based on the standard random number generator with the following shell script, which you may want to save as make-mac-address.sh in the directory /usr/local/bin:

#! /bin/sh

tr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/\1:/g;s/:$//;s/^/02:/'

More articles

Share your thoughts

Getting closer to the metal with KVM / July 29, 2018