The Xen way
What is Xen?
In terms of open source hypervisor main players are: KVM and Xen, these projects can be used on a personal laptop as well as in commercial. There are also: Virtual Box which is good if you are using Windows and bhyve for these that are die hard BSD fans. From commercial solution should be mentioned: VMware ESXi and Microsoft Hyper-V. In this article, I’m going to describe some basics concepts of Xen hypervisor. Usually, Xen is considered as difficult to configure and manage, but In this article, I would like to overturn this myth. My goal is to make a short introduction and highlight basic concepts and show some examples. One thing that I didn’t want to do is to provide a complex overview of each tool, installation etc, rather than I will provide more links to better and more focused on one topic articles.
Xen not exactly type 1 hypervisor.
In general, there are two types of hypervisors: type 1 and 2. This distinction is not something new, it came in 1974 Formal Requirements for Virtualizable Third Generation Architectures, but after years of hardware improvement and introduction technologies like intel VT-d, IOMMU this criterium is not as clear. To do not introduce more theory about CPU protection rings and x86/ARM virtualization features, it can be assumed simplified definition of type 1-2 hypervisors:
- Type one hypervisor is running natively on bare metal hardware, and VMs run directly in this environment.
- Type two hypervisor run on the conventional operating system (i.e as a kernel module) with other parallel OS processes, also other VMs run as another process.
If we compare Type-1 hypervisor concepts with Xen architecture, we can see a similarity. However, there is one difference: Dom0 VM. In Xen concept device drivers and other tools are moved from hypervisor to Dom0. This VM behave as a normal operating system and is used to the management of VMs and drivers.
Some theory
As for start lets take a look at sample VM configuration file. In open source Xen VM are created based on configuration files. To better understand how Xen create, manage and works with VMs, template configuration file is really useful (it show many options). For the first view, it can look really complicated and large, but in practice, we usually need only a few properties to deploy and manage most of the VMs.
Xen installation: transform your OS to Dom0
Installation of Xen hypervisor for most of Linux distribution is really straightforward and can be done using packet management tool.
As of Fedora/Ubuntu and other new distros, GRUB will automatically choose to boot Xen first if Xen is installed. After installation reboot system and check if xen was installed correctly:
Last command xl list
show all VMs that currently running on Xen hypervisor. After installation, our Linux turns into VM Dom0.
More details about installation on Debian-based systems including disk configuration can be found at Ubuntu official wiki
Xen tool stack
As it was said before, after installation of Xen hypervisor, new level of dependencies is created. Starting from bottom we have hardware, microkernel of hypervisor, and VMs including special by default running Dom0. One main concept of virtualization is isolation, because of that interaction between hypervisor Dom0 and other guests (In the Xen world they are called DomU) is really limited. To be able to manage hypervisor and other VMs Dom0 has running Xend daemon process which is collecting calls from different sources, and communicate with kernel using /dev/xen
. To be able to send any message from kernel to the hypervisor there is only one method available: ‘hypercalls’. From the other hand, there are many available tools to communicate with xend daemon process: there is interface for scripts languages that is used to run shell based interfaces like xl, also there exist API that can be run from C code.
Because the easiest way to manage Xen from Dom0 is by using xl command line tool, in next few paragraphs I will describe basics use-cases and different parts of stack that can be controlled using xl.
Below is a diagram that shows dependencies between different Xen components.
DOMAIN SUBCOMMANDS
The first group of commands that are used to VM management is domain subcommand. There are executed on Dom0 but their target is DomU. To manage subdomains xl toolchain allows:
- management of VM lifecycle (create, pause, reboot, shutdown)
- list running VMs with stats (list)
- VM migration (migrate)
- Debugging and Administration: dumping core, console (dump, console)
XEN HOST SUBCOMMANDS
The second group is used to manage host, getting information logs or even debug information:
- hypervisor messaged (dmesg)
- information about available and assigned memory, numa node etc (info)
- usage of CPU by different (top)
- the amount of memory claimed by different guests (claims)
SCHEDULER AND CPUs SUBCOMMANDS
Xen comes with numbers of schedules, its type can be specified during boot time using command line arguments (sched=). By default, scheduling is done by algorithm based on credits. Also for scheduling important is configuration of CPU, that also can be done using xl toolchain.
- manage scheduling algorithms (sched-credit, sched-rtds)
- pin CPU to domain or assign CPU-pool to VMs [including Dom0]
- migrate of VM between pools/CPUs
DEVICE DRIVERS
This group is used mainly to manage external devices that are connected to DomU. An example of that can be PCI/USB devices that are passed through VM, block devices like physical disks, CD-ROM. Most of them are hotplug.
- List all assignable devices PCI/USB/Network/Block (pci-assignable-list, pci-attach, usb-list, usbdev-attach, network-list, network-attach, block-list, block-attach )
OTHER: CACHE MONITORING and TRANSCENDENT MEMORY
- Starting from Intel Haswell, servers provide monitoring capability in each logical processor to measure specific shared resource metric, as L3 cache occupancy. That can be done using (psr-cmt-attach, psr-cmt-show, psr-cmt-detach)
- Transcendent Memory from the other hand is feature which can provide resource savings by allowing otherwise underutilized resources to be time-shared between multiple virtual machines. By definition, Transcendent Memory is: a collection of idle physical memory in a system and an interface providing indirect access to that memory (tmem-list, tmem-freeze, tmem-set)
Fast ramp up
After I described basics concepts of Xen and tools that can be used for administration or profiling, is time to see how it looks in practice. to deploy VM we will need few things:
- Storage for image of VM
- Installation medium
- Network configuration
- A configuration file that will be created based on previous points.
Storage management
Xen can work with file based images like qemu qcow2
, and also repository type which can be created based on physical partition on a disk. Usually, VMs are create using second approach, the reason for that is keeping repository based VM images is safer than taking care of different files.
I will describe these two approaches, starting from LVM repository, as it is more difficult, and after that file based which I found really valuable for development and testing/debugging as it is easily manageable and also they can be automatically scaled in run time.
LVM repository
To be able to manage LVM repository first think that is needed is some free unformatted disk space i.e. additional partition. Lets assume that we have additional partition sda5 that is designed for VMs repository:
After we will create disk for VM we can create VM, but before that there are two things needed: networking and installation media.
Typical use case for VM is deployment of Windows machine. To do that installation CD is more than needed, and way to interact with external world by using network.
As network configuration, we will use bridge type of interface which can be done on main system interface (excluding wireless type interfaces) xenbr0
(more information about configuration can be found here in section Network Configuration)
As can be easily seen, configuration file for windows is not as big and complex as for example configuration from beginning of article. Also reassuring the reader may be that the xen configurations for windows is one of the most complex in terms of size and options needed.
Analysis of configuration file can be divided into few parts:
- Hardware resources:
memory
andvcpu
numbers (no comment needed) - Xen specific commands:
kernel
andbuilder
options determine what kind of VM we are running. For windows, we don’t have choice we have to run HVM (hardware virtualization). This topic will be described later, right now we assume that hvm is the only choice for Windows and for other OSes there are available also other options.device_model_version
is also really important: as Windows always to run on real hardware, not as a VM, we have to provide it emulated hardware environment that will look from a guest perspective as real hardware. - Network/Storage: As a network interface we provide real existing interface that can be displayed using
ifconfig <if>
, in this time we connect the guest to the bridge. Storage has two records: first is disk that we created inside LVM repository in vg0 group,phy
suffix describe that this is real physical device,hda
is a name visible for guest, andw
describe permission to device (in this case obviously we need to write to this device). Second is CD-ROM file that we will use for installation,file
describe that we are dealing with file based medium,hdc:cdrom
guest will see it as CD-ROM, andr
mean that we don’t expect any write to file that emulate optical disk. - Other configuration:
acpi=1
is required by Windows from similar reason thatdevice_model_version
, othersdl
andserial
are recommended for Windows VM to emulate hardware as much as possible. The last thing is configuration of VNC, that we will use to get Windows desktop. For windows is the easiest way to install OS in way how we used to do on physical machines.
Now we have working configuration we have to create VM using xl
, and after that, we will have open VNC port on localhost:5900.
File based approach
In this approach rather than create full repository inside partition we will just going to create 10 Gb size VM image file:
After we have image that we will be installing OS
In this case, the configuration is even shorter, main difference is in disk
section: in this case we have tap:qcow2
which describe that we have a file with image type qcow2.
In previous examples, we saw how to deploy VM, and provide simple configurations. I especially used two different OS-es: Windows and FreeBSD to show how to provide configuration for specific systems. I didn’t bring Linux configuration because fortunately, Linux is able to boot with both of these configuration files.
Debugging Xen
Xen is separated from Dom0, and it make sense to don’t allow user to interact with hypervisor
After that, we got additional logging for Xen kernel and also for guests we got debug hypercalls that can be saw inside xl dmesg
Debugging with Xen!
Linux VM + Xen + gdb
Same thing that we made for FreeBSD we can apply for Linux. The fastest way to make kernel debug is just to run our qemu image nad then connect with gdb and point kernel image.
So again we need to build kernel sources with debug option. After make menuconfig
we can verify in .config
if CONFIG_DEBUG_INFO=y
.
Then we need to choose a right binary image for kernel usually it is vmlinux
in root kernel source folder. Using gdb we can verify if our image contains debug symbols.
Below simple configuration based on qcow2 image for Linux:
After that we need to connect remote debugging via gdb, as an argument, we need to pass vmlinux
image that is binary with debug symbols.