Xen Project Beginners Guide: Difference between revisions

From Xen
Jump to navigationJump to search
Line 275: Line 275:


We will cover some more commands during the creation of our guest domains.
We will cover some more commands during the creation of our guest domains.

'''See also'''
* [[Xen Man Pages]]


===Creating a Debian PV Guest===
===Creating a Debian PV Guest===

Revision as of 21:44, 6 December 2011

Xen Beginners Guide

Preface

Welcome!

This guide was written to introduce beginners to basic Xen concepts and allow you to get started with Xen with no prior knowledge. Some prior Linux experience is required however, knowledge of networking, lvm and grub will go a long way!

By completing this guide you will have installed a fully functional Xen hypervisor and started your first guest operating systems, connected them to your network and have been introduced to fundamental concepts such as virtual machine storage and virtual networking. To make this process easy we will be using a Linux distribution called Debian. Debian’s current stable release “Squeeze” ships with support for Xen 4 and everything you need to get started!

Though this guide looks long at first don’t be daunted, it is very indepth and comprehensive but doesn’t expect you to know all that much beforehand . The goal instead is to teach you all the things you need to know to build a functioning Xen hypervisor. :)

What is Xen all about?

Xen is a Virtual Machine Monitor (VMM) also known as a hypervisor, this is a software system that allows the execution of multiple virtual guest operating systems simultaneously on a single physical machine. Xen is known as a Type 1 or “bare-metal” hypervisor, meaning that it runs directly ontop of the physical machine as opposed to within an operating system.

Guest virtual machines running on Xen are known as “domains” and a special domain known as dom0 is responsible for controlling the hypervisor and starting other guest operating systems. These other guest operating systems are called domUs, this is because these domains are “unprivileged” in the sense they cannot control Xen or start/stop other domains.

Xen supports 2 primary types of virtualization, para-virtualization and hardware virtual machine (HVM) also known as “full virtualization”. Para-virtualization uses modified guest operating systems that we refer to as enlightened guests. These operating systems are aware that they are being virtualized and as such don’t require virtual “hardware” devices, instead they make special calls to Xen that allow them to access CPUs, storage and network resources.

In contrast HVM guests need not be modified as Xen will create a fully virtual set of hardware devices for this machine that resemble a physical x86 computer. This emulation requires much more overhead than the paravirtualisation approach but allows unmodified guest operating systems like Microsoft Windows to run ontop of Xen. HVM support requires special CPU extensions - VT-x for Intel processors and AMD-V for AMD based machines. This technology is now prevalent and all recent servers and desktop systems should be equipped with them.

A third type of virtualization though not discussed in this guide is called PVHVM or “Para-virtualisation on HVM” which is a HVM domain with paravirtualized storage, network and other devices. This provides the best of both worlds by reducing expensive emulation but providing hardware accelerated CPU and memory access. A brief look at Xen architecture To understand how storage, networking and other resources are delivered to guest systems we need to quickly delve into how the different bits of Xen interact.

<todo diagram>

<todo explain dom0 role and datapath for guest IO/network>

The dom0 forms the interface to the hypervisor, through special instructions the dom0 communicates to Xen and changes the configuration of the hypervisor. This includes instantiating new domains and related tasks.

Another crucial part of the dom0’s role is that it is the primary interface to the hardware. Xen doesn’t contain device drivers, instead the devices are attached to dom0 and you can use standard Linux drivers. Dom0 then shares these resources with guest operating systems through a number of “backend” deamons.

For each para-virtualized subsystem in Xen consists of 2 parts, the aforementioned “backend” that lives in dom0 and the “frontend” driver within the guest domain. The backend is effectively a deamon which uses special buffer based interfaces called “eventchannel” to transfer data to guests, be it to provide a virtual hard-disk, Ethernet adapter of even a generic SCSI device. The frontend driver then takes this stream of data and converts it back into a device within the guest operating system.

The 2 important paravirtualisation systems are known as net-back/net-front and blk-back/blk-front which are the paravirtualized networking and storage systems respectively.

You can read more about how Xen is architected, paravirtualization and the benefits of such here:

<todo find xen article on wiki with deep explaination of PV>

Preparation

This guide requires a number of items, this checklist is what you will need:

  • 64bit x86 computer with atleast 1GB of RAM (this can be a server,desktop or laptop!)
  • (Optional) VT-d or AMD-V support
  • Sufficient storage space for your dom0 and whatever guests you want to install
  • A CD burner + blank CD (you can use a USB but this is not covered here)
  • Internet access for downloading and installing Debian
  • (Optional) Windows Server 2008R2 installation ISO, a trial copy is sufficient
  • (Optional) VNC client for installing HVM domain

Enable virtualization support in BIOS

NOTE: This is optional and not required for PV guests

In order to support HVM guests we need to ensure that virtualization extensions are enabled in the BIOS. If you don’t wish to start a HVM guest you can skip this step but it is still highly recommended. If your system doesn’t support these extensions you cannot use Xen to virtualize unmodified operating systems, however para-virtualization will work fine.

The virtualization option appears differently in different BIOS builds but generally it is refered to as “Enable Virtualisation Technology” or “Enable Intel VT” for Intel chipsets, however in some cases it can be listed as “Vanderpool Technology”. Oftentimes this option can be found under the “Advanced Chipset Features” menu in the BIOS. Similar also for AMD.

Consult your motherboard documentation for more assistance in enabling virtualization extensions on your system.

Download and burn Debian Squeeze Installer CD

Download the ISO image from this URL:

   http://cdimage.debian.org/debian-cd/6.0.3/amd64/iso-cd/debian-6.0.3-amd64-netinst.iso

Burn the ISO to disk using your computers standard utilities. I recommend wodim on Linux or the built in ISO burning feature in Windows.

Quick intro to Debian

Debian is a simple, stable and well supported Linux distribution. It has included Xen support since Debian 3.1 “Sarge” released in 2005. The current stable release Debian 6.0 “Squeeze” ships with Xen 4.0.1 and a Xen enabled Linux 2.6.32 kernel.

Debian uses the simple Apt package management system which is both powerful and simple to use. Installing a package is as simple as the following example:

   aptitude install htop

Where htop was the application desired to install.

Simple tasks such as configuring startup scripts, setting up the network etc are covered by this tutorial so don’t worry if you haven’t used Debian before!

Many popular distributions are based off of Debian and also use the Apt package manager, if you have used Ubuntu, Linux Mint or Damn Small Linux you will feel right at home.

Installing Debian Squeeze

Boot the Debian Squeeze Installer CD Insert the Debian CD and configure the CDROM drive as your default boot device in the BIOS or use the system bootmenu if your BIOS supports it (usually F12).

You should see a menu, choose the default “Install” option to begin the installation process. Install the system The Debian installer is very straight forward. Follow the prompts until you reach the disk partitioning section.

Choose advanced/custom, we are going to configure a few partitions here, one for /boot another for /, one more for swap and a final partition to setup as an LVM volume group for our guest machines.

First create the /boot partition by choosing the disk and hitting enter, make the partition 300MB and format it as ext2, choose /boot as the mountpoint.

Repeat the process for / but ofcourse changing the mountpoint to / and making it 15GB or so large. Format it as ext3.

Create another partition approximately 1.5x the amount of RAM you have in size and elect to have it used as a swap volume.

Finally create a partition that consumes the rest of the diskspace but don’t format it or assign a mount point.

We should now have a layout that looks like this assuming your disk device is /dev/sda :

   sda1 - /boot 200MB
   sda2 - / 15GB
   sda3 - swap
   sda4 - reserved for LVM

When you reach the package selection stage only install the base system, we won’t require any GUI or other packages for this guide.

<todo: extend to cover the rest of the installation process>

Continue through the installer then reboot and login at the prompt as root.

Setup LVM storage for guests

LVM is the Linux Logical Volume manager. It is a technology that allows Linux to manage block devices in a more abstract manner.

LVM introduces the concept of a “logical volume”, effectively a virtualized block device composed of blocks written to 1 or more physical devices. These blocks don’t need to be contiguous unlike proper disk partitions.

Because of this abstraction logical volumes can be created, deleted, resized and even snapshotted without affecting other logical volumes.

LVM creates logical volumes within what is called a volume group, which is simply a set of logical volumes that share the same physical storage, known as physical volumes.

The process of setting up LVM can be sumarized as allocating a physical volume, creating a volume group ontop of this then creating logical volumes to store data.

Because of these features and superior performance over file backed virtual machines I recommend the use of LVM if you are going to store VM data locally.

Now lets install LVM and get started!

Install LVM with aptitude by running this command:

   aptitude install lvm2

Now that we have LVM installed lets configure it to use /dev/sda4 as it’s physical volume

   pvcreate /dev/sda4

Ok, now LVM has somewhere to store it’s blocks (known as extents for future reference). Lets create a volume group called ‘vg0’ using this physical volume:

   vgcreate vg0 /dev/sda4

Now LVM is setup and initialized so that we can later create logical volumes for our virtual machines.

<todo: write advanced lvm storage guide and link to it here>

Setup Linux Bridge for guest networking

Next we need to setup our system so that we can attach virtual machines to the external network. This is done by creating a virtual switch within dom0 that takes packets from the virtual machines and forwards them onto the physical network so they can see the internet and other machines on your network.

The piece of software we use to do this is called the Linux bridge and it’s core components reside inside the Linux kernel. In this case the “bridge” is effectively our virtual switch. Our Debian kernels are compiled with the Linux bridging module so all we need to do is install the control utilities.

   aptitude install bridge-utils

With bridge-utils installed we now have a utility called “brctl” this utility talks to the Linux bridging module to setup new bridges and attach physical or virtual interfaces to these bridges.

brctl can be used to create a bridge as such, where <bridgename> is the name of the bridge:

   brctl addbr <bridgename>

And interfaces can be added to that bridge by running the following:

   brctl addif <bridgename> <interface>

Instead of calling brctl directly we are instead going to configure our bridge through Debian’s networking infrastructure which can be configured through

   /etc/network/interfaces

Open this file with the editor of your choice (more editors can be installed with aptitude) Nano is installed by default if you selected the minimal install

   nano /etc/network/interfaces

Depending on your hardware you probably see a file pretty similar to this:

    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp

This file is very simple. Each stanza represents a single interface. Breaking it down “auto eth0” means that eth0 will be configured when ifup -a is run (which is run a boot time) what this means is that the interface with automatically be started/stopped for you. “iface eth0” then describes the interface itself, in this case it merely specifies that it should be configured by DHCP - we are going to assume that you have DHCP running on your network for this guide. If you are using static addressing you probably know how to set that up. We are going to edit this file so it resembles such:

    auto lo
    iface lo inet loopback

    iface eth0 inet manual
    auto xenbr0

    iface xenbr0 inet dhcp
        bridge_ports eth0

This will setup the bridge xenbr0 and add eth0 to the bridge for us. The equivalent commands would be:

   brctl addbr xenbr0
   brctl addif xenbr0 eth0
   dhclient xenbr0

Except this will now be done automatically on boot and completely managed by Debian.

This method of network configuration is best practice and the automated scripts called by xend are now deprecated, we will discuss this later once we have Xen installed.

Installing Xen

The Debian Xen packages consists primarily of a Xen enabled Linux kernel, the Xen hypervisor itself, a modified version of QEMU that support Xen’s HVM mode and a set of userland tools.

All of this except QEMU can be installed via an Apt meta-package callled xen-linux-system. A meta-package is basically a way of installing a group of packages automatically. Apt will ofcourse resolve all dependencies and bring in all the extra libraries we need.

Lets install the xen-linux-system metapackage:

   aptitude -P install xen-linux-system

Next we will install the Xen QEMU package so that we can boot HVM guests later (this is optional but highly recommended)

   aptitude install xen-qemu-dm

Now have Xen, a Xen kernel and the userland tools installed, almost ready to go.

Configure GRUB to start Xen

Because Xen starts before your operating system we need to change how your systems boot process is setup. The bootloader installed during installation called GRUB is what tells your computer which operating system to start and how.

GRUB2 configuration is stored in the file /boot/grub/grub.cfg However we aren’t going to edit this file directly, as it changes every time we update our kernel. Debian configures GRUB for us using a number of automated scripts that handle upgrades etc, these scripts are stored in /etc/grub.d/* and are configured via

   /etc/default/grub

We are going to change the order of the operating systems so that our Xen system is the default option. By executing the below command we are moving Xen to a higher priority than default Linux so that it gets the first position in the boot menu.

   mv -i /etc/grub.d/20_linux_xen /etc/grub.d/09_linux_xen

We then generate the /boot/grub/grub.cfg file by running the command below:

   update-grub

We can now reboot and the default boot option will be our Xen dom0 running ontop of Xen!

See also

Basic Xen commands

Before we dive into creating some guest domains we will quickly cover some basic Xen commands using the “xm” utiltiy. Going forward it is likely that xm will be deprecated as previously mentioned but once xl is stable and part of a supported release this guide will be rewritten to reflect that.

So lets start with simple stuff!

   xm info

This returns the information about the Xen hypervisor and dom0 including version, free memory etc.

   xm list

Lists running domains, their IDs. memory, state and CPU time consumed

   xm top

Shows running domains in real time and is similar to the “top” command under Linux. This can be used to visualize CPU, memory usage and block device access.

We will cover some more commands during the creation of our guest domains.

See also

Creating a Debian PV Guest

PV guests are notoriously “different” to install. Due to the nature of enlightened systems they don’t have the usual concepts of a CD-ROM drive installer analagous to their physical counterparts. However, luckly enough there does exist some tools that help us prepare “images” or effectively snapshots of the operating systems that are able to run inside of Xen domains.

Debian contains a number of tools for creating Xen guests. The easiest of which is known as xen-tools. This software suite manages the downloading and installing of guest operating systems including both Debian and RHEL based DomUs. In this guide we are going to use xen-tools to prepare a Debian Squeeze paravirtualized domU.

Xen-tools can use LVM storage for storing the guest operating systems, in this guide we created the volume group “vg0” in the Setting up LVM Storage section.

When guests are paravirtualized there is no “BIOS” or bootloader resident within the guest filesystem and for a long time guests were provided with kernels external to the guest image. This however is bad for maintainability (guests cannot upgrade their kernels without access to the dom0) and is not as flexible in terms of boot options as they must be passed via the config file.

The Xen community wrote a utility known as pygrub which is a python application for PV guests that enables the dom0 to parse the GRUB configuration of the domU and extract it’s kernel, initrd and boot parameters. This allows for kernel upgrades etc inside of our guest machines along with a GRUB menu. Using pygrub or the stub-dom implementation known as pv-grub is best practice for starting PV guests. In some cases pv-grub is arguably more secure but as it is not included with Debian we won’t use it here though it is recommended in production environments where guests cannot be trusted.

Apart from this PV guests are very similar to their HVM and physical OS counterparts. Configuring xen-tools and building our guest First lets install the xen-tools package:

   aptitude install xen-tools

Wwe can now create a guest operating system with this tool. It effectly automates the process of setting up a PV guest from scratch right to the point of creating config files and starting the guest. The process can be summarized as follows:

  • Create logical volume for rootfs
  • Create logical volume for swap
  • Create filesystem for rootfs
  • Mount rootfs
  • Install operating system using debootstrap (or rinse etc, only debootstrap covered here)
  • Run a series of scripts to generate guest config files like fstab/inittab/menu.lst
  • Create a Xen config file for the guest
  • Generate a root password for the guest system
  • Unmount the guest filesystem

These 9 steps can be carried out manually but the manual process is outside the scope of this guide. We instead will execute the below command:

  xen-create-image --hostname=tutorial-pv-guest \
  --memory=512mb \
  --vcpus=2 \
  --lvm=vg0 \
  --dhcp \
  --pygrub \
  --dist=squeeze

This command instructs xen-create-image (the primary binary of the xen-tools toolkit) to create a guest domain with 512MB of memory, 2 vcpus, using storage from the vg0 volume group we created, use DHCP for networking, pygrub to extract the kernel from the image when booted and lastly we specify that we want to deploy a Debian Squeeze operating system.

This process will take a few minutes after which you can start the guest with:

  xm create -c /etc/xen/tutorial-pv-guest.cfg

The -c in this command tells xm that we wish to connect to the guest virtual console. Which is a paravirtualised serial port within the domain that xen-create-image configured to listen with a getty. This is analgous to running:

  xm create /etc/xen/tutorial-pv-guest.cfg && xm console tutorial-pv-guest

You can leave the guest virtual console by pressing ctrl+] and re-enter it by running the “xm console <domain>” command.

You can later shutdown this guest either from within the domain or from dom0 with the following:

  xm shutdown tutorial-pv-guest

That completes our section on setting up your first paravirtualized domain! If you don’t have any interest in setting up a HVM domain then no need to read any further but it is highly recommended!

Creating a Windows HVM Guest

HVM guests are quite abit different to their PV counterparts. Because they require the emulation of hardware there is more moving pieces that need to be configured etc.

The main point worth mentioning here is that HVM requires the emulation of ATA, Ethernet and other devices, while virtualized CPU and Memory access is performed in hardware to achieve good performance. Because of this the default emulated devices are very slow and we generally try to use PV drivers within HVM domains. We will be installing a set of Windows PV drivers that greatly increase performance once we have our Windows guest running.

This extra emulation is provided by a Xen modified version of QEMU we should have installed this earlier but in case you skipped that step install the Xen QEMU package now:

   aptitude install xen-qemu-dm