Xen

Xen is a native, or bare-metal, hypervisor that allows multiple distinct virtual machines (referred to as domains) to share a single physical machine. As the highest privilege process on the system, Xen is responsible for the distribution of processor and memory resources between guest domains on the host. Other hardware resources such as network interfaces, disks, or direct PCI/USB devices are controlled by a privileged domain known as domain-0 (dom0).

From it's inception Xen has focused on the para-virtualization approach to hypervisor design. As a result, Xen guests or unprivileged domains (domU) are typically aware of the hypervisor and their status. The base system, Domain-0, must have inherent Xen support, however, unmodified domU guests are supported on hardware which implements Intel (VT-x) or AMD (SVM) virtualization technology.

Introduction
Domain0 is the primary domain under Xen, hosting the host operating system which governs all other domains. In this chapter we will prepare an existing Gentoo installation to become the host operating system in this domain and build the Xen-powered kernel so that Gentoo is ready to host other Xen domains.

Rebuilding the Gentoo Installation?
A dramatic change that might be necessary on 32-bit systems is to rebuild the entire Gentoo installation with a different  setting. Guest operating systems running under Xen might otherwise see major performance degradation. If you, however, are planning on checking out Xen rather than installing it for production use and are not terribly fond of rebuilding all programs, you can skip this step. In this case you will notice performance degradation but you will still be able to use Xen.

Add -mno-tls-direct-seg-refs ONLY if you have a 32-bit dom0. You don't need this flag if you have a 64-bit dom0.

If you boot your system using an initial ramdisk (initrd) you need to rebuild the initrd as well (which is best done by running all steps you would do when you rebuild your kernel).

Installing Xen
Xen actually contains many components, so you'll need to install a few packages.

Building the Kernel
Next we'll build the Linux kernel with Xen support. This kernel, whose sources are available at, will be our main running kernel (i.e. the one running domain 0). In the  section you'll find drivers for all kinds of input/output, each driver having a backend and frontend implementation available. For the domain 0 kernel you need to select the backend implementation: these are used by the other domains (who use the frontend drivers) to communicate directly with the hardware. However, you should be able to configure the kernel to provide support for both frontend (guest) and backend (host) drivers.

If you're wondering about networking: each interface in a domain has a point-to-point link to an interface on domain 0 (called where X is the domain number and Y the Yth interface of that domain), so you can configure your network the way you want (bridging, NAT, etc.)

Enable general Xen support:

Add support for paravirtualized console connections:

Facilitates guest access to block and network devices via dom0:

In some configurations it can be desirable to provide a guest with direct access to a PCI device. This is known as Xen PCI Passthrough:

Keyboard, mouse, and display support via dom0 backend:

Xen dom0 support depends on APCI; without it dom0 related options will be hidden:

Typical network configurations depend on linux bridge functionality:

The remaining drivers flesh out memory management, domain-to-domain communication, and communication to Xen via sysfs interfaces:

With all of the above configuration enabled, this kernel image should be able to boot as the dom0 host or as another domU guest. Note that the domU kernel can be slimmed down significantly if desired.

Bootloader
Once the kernel is built you'll find the kernel image immediately in the build directory (not inside or any other directory) called. Copy it to and then configure your bootloader to use the Xen hypervisor (one of the components installed previously) which is stored as. In the bootloader configuration, add your newly built kernel as the kernel that Xen should boot. For instance, for GRUB:

If you are using grub2, which provides auto-configuration scripts through grub2-mkconfig, you can also copy your kernel .config as config- eg. config-3.5.x.y-xen0 in the above example. The scripts will automatically look for the Xen Dom0 options in kernel config and append Xen hypervisor boot lines to the grub menu. Note that for this to function correctly the kernel config file must be located in one of the following directories with a suffix matching the desired kernel:

The example column above assumes a kernel named /boot/kernel-3.18.11-gentoo.

Alternatively, the following command should do the trick:

Now reboot your system into Xen and check if you can do whatever you normally do on your system. If this is the case, you can edit your bootloader configuration to always boot into Xen.

Building the Kernel
Go to the Xen-powered Linux kernel source and, if necessary, update the configuration. It is wise to keep as many topics as possible similar to the main kernel. Then build the kernel and place the resulting file where you want (we assume this is  ):

If you'd like to trim down the domU kernel the following flags are necessary.

Enable general Xen support:

Facilitates guest access to block and network devices via dom0:

In some configurations it can be desirable to provide a guest with direct access to a PCI device. This is known as Xen PCI Passthrough:

Keyboard, mouse, and display support via dom0 backend:

The remaining drivers flesh out memory management, domain-to-domain communication, and communication to Xen via sysfs interfaces:

Creating the Domain Disks
For best performance, it is best to dedicate a partition (or logical volume) to a domain rather than a file based filesystem. However, if you are going to use Xen primarily for tests using a file based filesystem does have its advantages (especially regarding maintenance).

You can create a file based filesystem using  and   (or any other file system creation tool). For instance, to create a 4 Gbyte ext4 filesystem:

Configuring a Domain
Next we create a Xen configuration file for a domain. You can store these configuration files where you want, for instance at. As an example, we create a configuration file for a small Gentoo environment which uses the disk image we created previously:

If you are using a block device (such as an lvm volume or partition) for the disk use 'phy:' instead of 'file:' and leave off /dev. For example:

You can find example configuration files in, which is also the default location for domU config files.

Launching the New Domain
Now we're all set and we can launch the new domain. If the disk image contained an operating system, we could just create and attach the domain using the  command:

The domain would be booted inside the terminal in which you executed the command. However, in our case, the disk image is empty so the domain won't boot up in anything useful. To fix this, you can loop-mount the image and install Gentoo as you're used to.

If you want to disconnect from the domain, press. You can always reconnect to the domains' console using. However, there is only one console per domain, so only use it when you can't access the domain otherwise (for instance, through SSH).

If you are missing login prompt on the console, make sure you have entries like this in your inittab files on dom0 and domU pointing to :

To apply the changes in without a reboot issue the following command:

If it still does not work, check the kernel config for the entries: and. They should be set.

Introduction
Xen works best when using a bridged mode network configuration. This means that your default network interface on the administrative domain becomes a bridge which accepts connections to the virtual domains as well as to the IP address your administrative domain has.

Bridged Interfaces
Create a bridge interface by creating a new link to the networking init script as provided by Gentoo:

Next, edit and setup the bridge:

Finally, install the package, and make sure the  init script is loaded at boot.

If you use bridged networks with real internet IP's in hosted environments, it may be necessary to add one or all of the following lines (depending on your environment) in your file to prevent redirects, that can cause intermittent network interruptions:

To get the changes in to work, use:

if you encounter a poor network performance or if your domU network permanently stops working under heavy load (backup jobs, etc) (from outside it looks, like the instance would crash, but deactivating and activating the interface e.g. form the  with , restores normal operation) , use   to improve/prevent it on all interfaces connected to the bridge (don't forget the bridge himself):

Xen Documentation

 * Official Xen documentation


 * Xen Wiki

Xen Tools

 * app-emulation/virt-manager is a graphical tool for administering virtual machines

Xen Tuning

 * Xen network tuning