User:Pepoluan/Paravirtualized Gentoo VMs on XenServer

Citrix XenServer is one of the leading baremetal hypervisors in use in the Enterprise environment. It is based on the FOSS Xen hypervisor technology.

Because XenServer is based on Xen, it is capable of running Linux VMs in paravirtualization (PV) mode. In PV mode, because Xen is freed from the necessity of emulating complex hardware (e.g., NICs and storage controllers), the performance of the VM will be significantly faster. Plus, Linux kernel (mainline since version 2.6.27) has provided pv_ops support to even increase the performance of the system when running as a PV VM on top of XenServer.

Unfortunately, despite the very significant benefits of running Linux VMs in PV mode, out-of-the-box XenServer only provide support for a limited amount of distros, namely: CentOS, Debian, Oracle Enterprise, Red Hat Enterprise, SUSE Enterprise, and Ubuntu.

This article will provide you a guide in creating a Paravirtualized Gentoo Linux Virtual Machine. Instead of describing the complete steps required for installation, this article will only highlight some important actions to do while following the Gentoo Handbook.

(You are assumed to have understood fully the steps in the Gentoo Handbook, and thus understand well where exactly the actions described here are applied).

Prerequisites

 * XenServer 5.0 or newer
 * XenCenter or OpenXenCenter
 * Internet connection

Screen
Nothing reduces a grown-up man to tears faster than leaving your Gentoo in the midst of  and returning just to find out that the SSH session has disconnected.

Safe yourself some hardship! Use !

Configure
You are *not* required to configure  to use it, but configuring it will help you remember where (i.e., which host) you are.

(The last line is Ctrl-D)

(Optional) Find fastest server
You’ll want to do this only once every now and then (like, once a month), because this will take a loooong time.

-s 5	choose 5 fastest servers -D	go ‘deep’, e.g., actually measure the throughput of downloading a 100 KB file, instead of just relying on ping’s and hopcounts

( You might also want to use the -o switch and redirect the output to a file )

Preparing the hard disk
When partitioning your hard disk, it would be wise to follow these rules of thumb:
 * Give at least 128 MB for  -- you’ll need the space if you want to experiment with kernel   settings after you’ve gotten your first PV-VM
 * Give at least 128 MB for  (unless you want to create the swap as a swapfile)
 * Give at least 3 GB for  (root)

When creating the filesystems, you must follow the following rules:
 * must be either   or   ( If you’re using XenServer 5.5 or newer, you can also use ext4. But usually you *don’t* want to use ext4 as /boot filesystem, unless you’re doing a single-partition-for-everything scheme )

Configuring your make.conf
For performance reasons, is strongly recommended to use the following as the value for CFLAGS:
 * x86 DomU, Intel host:  
 * amd64 DomU, Intel host:  
 * x86 DomU, AMD host:  
 * amd64 DomU, AMD host:  

Don’t forget to set your USE flags, MAKEOPTS, GENTOO_MIRRORS, and other stuffs.

Here’s an example: http://pastebin.com/wA8mLEJe

IMPORTANT !! Create Xen devices
Just after you mounted your root partition to, before you mount the other partitions, you must create some Xen device nodes that will used during boot-time.

If the above example is correct to you, you can copy it hre: http://pastebin.com/qiqsAWSf

Configuring & Installing the kernel
This is the trickiest action. Please read this section carefully.

Choosing the right kernel sources
You don’t need ! and  can support Xen DomU *perfectly*, thanks to the bits and pieces of Xen pv_ops integrated into the mainline.

So, emerge your preferred sources:

or

IMPORTANT Kernel Configuration for
If you want to use GrSecurity, there’s a procedure you must follow. First, configure it this way:

Exit and save. Then run   again, and configure it like this:

Kernel configuration for all sources
[1] If you can be sure that all your XenServer hosts have processors newer than the minimal XenServer requirement, feel free to choose the next-gen processor.

[2] Depends on whether your XenServer hosts are Intel-based or AMD-based. VM migration between different processor manufacturers are likely to fail, so you can't choose a 'generic' processor type.

[3] This option not visible in

[4] This option forced to "y" if you use  and set

Installation
Installing GRUB involves 3 (three) commands:

Since at this point the drive(s) are still detected as  and not yet as , you must not edit the.

Editing the
The entries in  are nearly identical to the entries when not running as a DomU. The only difference would be the  parameter to the kernel: Instead of   (or whatever the device name for the partition containing your root filesystem), you'd use   (i.e., replace the first letter with " ").

For example:

fstab
Just edit   as you would normally, but replace all drive names to its " "-equivalent.

For example:

inittab
If you don't edit  , the system can boot, but you won't see any output to the XenCenter console. So, edit the " " section of the file like this:

securetty
You must edit   to allow  login from the XenCenter console. Just do:

mdev.conf
You must also edit   to allow  login from the XenCenter console. Just do:

rc.conf
Finally, tell Gentoo that it's running as a Xen DomU:

Switching from HVM-mode to PV-mode
When you have finished installing your Gentoo system, do not reboot!

Instead, you must shutdown:

The reason: To switch modes, a VM must be restarted by XenServer itself, not because of a reboot-request from within the VM (which will result in the same mode).

After the VM has shut down completely, go to the XenServer console (via XenCenter or (recommended) via SSH).

Preparation for switching to PV-mode
In general, in this step you will:
 * "Activate" the virtual hard disk for booting
 * "Deactivate" the CD/DVD virtual drive

First, we must get the UUID of the VM:

(If you end up with more than one VM, extend the partial_vm_name until it's unique)

Next, we list the virtual hard disks of your VM:

Choose the hard disk where  is located, and mark it as bootable:

Next, we list the CD/DVD virtual drives of your VM:

And mark them as bootable (repeat the following command for all VBD_uuid you got in the previous command):

You can double-check the settings by entering these commands:

Make sure that there's exactly one and only one VBD marked as  .

Configure Boot Policy & Bootloader
After you ensure that there's one (and only one) VBD marked as, you can now change the VM's mode to PV. Do the following two commands (using the VM_uuid you got from the "Preparation" step):

Restart XenCenter!
It's a known bug: After switching from HVM-mode to PV-mode, XenCenter's Console will no longer send your keypresses to your VM.

Simple solution: close XenCenter, and open it again.

You can now type into the Console again :-)

Switching from HVM-mode to PV-mode
There might be times when you need to boot in HVM-mode, e.g., when your VM is not bootable and you need to boot from a SystemRescueCD.

To switch back to HVM-mode, enter the following pair of commands in the XenServer console:

No need to change the " " designation.

Installation of Citrix XS-Tools
After you've successfully booted your Gentoo VM in PV-mode, the next order of the day would be to install Citrix XS-Tools. The XS-Tools is a set of scripts (and one binary) that allows XenServer to 'deeply manage' your VM.

As of the writing of this article, there are two ways to install XS-Tools:
 * 1) Christopher Wojno's method : http://christopher.wojno.com/2009/08/11/gentoo-domu-in-xenserver
 * 2) Pandu Poluan's method : https://bitbucket.org/pepoluan/install-xe-guest-utilities-for-gentoo

Pandu's method is binary-based, but it's easier and also results in an initscript that is 100% compatible with OpenRC/baselayout-2.