Xen Setup

Xen Hypervisor 4.11 setup on Ubuntu 20.04 LTS

Posted on July 20, 2020

Many websites describe Xen Setup in different ways. However, one needs to search for different places before finally setting up the system.

I am trying to include many aspects of the Xen setup in this blog. This setup document includes:

Host Setup

Host system is Intel Xeon and installed with Ubuntu 20.04 LTS, Linux Kernel v5.4.y.

Xen Installation

Install Xen-4.11 along with Xen-tools, Xen-utils and bridge-utils (for network bridge).

$ sudo apt install qemu-system-x86-xen xen-hypervisor-4.11-amd64 xen-utils-4.11 xen-tools bridge-utils

Tweak the host

Modify the /etc/default/grub to tweak memory and vCPUs.

dom0_vcpus_pin will pin the vCPUs to the physical CPU, this will make sure physical CPUs allocated to dom0 will not be shared with guest domains. cpuinfo will display CPU information during boot time. dom0 is going to use a maximum of 4 vCPUs and 4096M memory.

GRUB_CMDLINE_XEN="dom0_mem=4096M,max:4096M dom0_max_vcpus=4 dom0_vcpus_pin cpuinfo"

To stop host memory from ballooning down, set autoballoon to “0” in /etc/xen/xl.conf.

autoballoon="0"

To shutdown guest domains safely during host shutdown, you can set below parameters /etc/default/xendomains.

XENDOMAINS_RESTORE=false
XENDOMAINS_SAVE=""

If you want to pass any boot parameters to the Xen, then you can use below definitions in /etc/default/grub.

GRUB_CMDLINE_XEN --> for all Xen boots
GRUB_CMDLINE_XEN_DEFAULT --> only for non-recovery boots

If you are planning to use xen-tools to create guest images and want to change the default path to store guest images, then modify /etc/xen-tools/xen-tools.conf.

dir=/home/xen/
passwd=1

Tweak Network Configuration

For obvious reasons, one needs to have network access to guest domains. For this, you can use a software bridge. We already installed bridge-utils during the installation step. In Ubuntu 20.04 LTS, the network is managed by Netplan.

$ sudo cp /etc/netplan/*.yaml /etc/netplan/*.yaml
network:
  ethernets:
    eno2:
      dhcp4: false

  bridges:
    xenbr0:
      interfaces: [eno2]
      dhcp4: true

  version: 2
$ sudo netplan generate
$ sudo netplan apply
$ sudo systemctl restart network-manager

Reboot Host

After these changes, you may want to restart the system for changes to take effect.

$ sudo update-grub
$ sudo reboot

Guest Setup

There are three types of guest systems possible in Xen.

  1. PV
  2. HVM or PVHVM
  3. PVH

PV Guest Domain Setup

PV guest domains can be created by using Xen-tools. Here, I will create a guest domain for Debian Buster.

$ sudo xen-create-image --hostname domU --dhcp --vcpus 2 --pygrub --dist buster

Follow the instructions while installing. You can find guest domain images in /home/xen, as set in previous steps. You can edit the guest domain configuration file, /etc/xen/domU.cfg, before starting the created domain.

To delete the guest domain,

$ sudo xen-delete-image domU

Start the created guest domain.

$ sudo xl create /etc/xen/domU.cfg

Connect to guest domain.

$ sudo xl console domU

In Debian, SSH to root is prohibited. Either you need to create new-user while creating a domain or you need to enable SSH to root. For enabling SSH to root, modify /etc/ssh/sshd_config.

PermitRootLogin yes

Then,

$ sudo /etc/init.d/ssh start

If you enable a network bridge, you will have an IP address assigned to the guest system. HAPPY SSH :)

HVM Guest Domain Setup

HVM is Hardware assisted Virtual Machine, you need to enable Virtualization Extension in BIOS (or bootloader).

Host setup for HVM guest

Additionl host setup is required to install HVM guest.

Modify the /etc/default/grub to enable Virtualization Extensions during boot time.

GRUB_CMDLINE_LINUX="add_efi_memmap intel_iommu=on“
GRUB_CMDLINE_XEN="dom0_mem=4096M,max:4096M dom0_max_vcpus=4 dom0_vcpus_pin iommu=verbose,workaround_bios_bug,debug iommu_inclusive_mapping=true cpuinfo”

LVM disk is used by HVM guest domain.

Create Physical Volume.

$ sudo pvcreate /dev/sdb

Create Volume Group

$ sudo vgcreate vg0 /dev/sdb

Create Logical Volume

$ sudo lvcreate --name hvm --size 20G vg0

Reboot the host for changes to take effect.

HVM Guest installation and tweaks

Download ISO image from any desired distribution. Here, I am using Ubuntu 20.04 Server.

Before installation, create guest config file as /etc/xen/hvm.cfg.

type = "hvm"
name = "hvm"
memory = 4096
vcpus = 2
dhcp = 'dhcp'
vif = [ 'mac=00:16:3E:3D:7F:89' ]
disk = [
         'phy:/dev/vg0/hvm,hda,w',
         'file:/path/to/ubuntu-20.04-live-server-amd64.iso,hdc:cdrom,r'
       ]
sdl = 0
vnc = 1
boot="d"

Then,

$ sudo xl create /etc/xen/hvm.cfg
$ vncviewer localhost:0

Follow the instruction to install the Ubuntu 20.04 as HVM guest domain.

After installation complete, modify the guest config file /etc/xen/hvm.cfg to boot from installed OS.

type = "hvm"
name = "hvm"
memory = 4096
vcpus = 2
dhcp = 'dhcp'
vif = [ 'mac=00:16:3E:3D:7F:89' ]
disk = [
         'phy:/dev/vg0/hvm,hda,w',
         'file:/path/to/ubuntu-20.04-live-server-amd64.iso,hdc:cdrom,r'
       ]
sdl = 0
vnc = 1
boot="c"

PVH Guest Domain Setup

PVH is lightweight HVM. Setup is same as in “HVM Guest Domain Setup”, except configuration file.

Create guest configuration /etc/xen/domU.cfg, while installing guest:

type = "pvh"
name = "domU"
memory = 4096
vcpus = 2
dhcp = 'dhcp'
vif = [ 'mac=00:16:3E:3D:7F:89' ]
disk = [
         'phy:/dev/vg0/pvh,hda,w',
         'file:/path/to/ubuntu-20.04-live-server-amd64.iso,hdc:cdrom,r'
       ]
sdl = 0
vnc = 1
boot="d"

Modify guest configuration /etc/xen/domU.cfg, after installing guest:

type = "pvh"
name = "domU"
memory = 4096
vcpus = 2
dhcp = 'dhcp'
vif = [ 'mac=00:16:3E:3D:7F:89' ]
disk = [
         'phy:/dev/vg0/pvh,hda,w',
         'file:/path/to/ubuntu-20.04-live-server-amd64.iso,hdc:cdrom,r'
       ]
sdl = 0
vnc = 1
boot="c"

Credits:

  1. https://wiki.debian.org/Xen
  2. https://help.ubuntu.com/community/Xen
  3. https://vidigest.com/2020/01/14/creating-hvm-domu-virtual-machines-in-xen-project-hypervisor/