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 system is Intel Xeon and installed with Ubuntu 20.04 LTS, Linux Kernel v5.4.y.
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
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.
To shutdown guest domains safely during host shutdown, you can set below parameters /etc/default/xendomains.
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.
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
After these changes, you may want to restart the system for changes to take effect.
$ sudo update-grub $ sudo reboot
There are three types of guest systems possible in Xen.
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.
$ 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 is Hardware assisted Virtual Machine, you need to enable Virtualization Extension in BIOS (or bootloader).
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.
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"
$ 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 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"