Saturday, June 23, 2012

Xen - host Arch Linux - guest Arch Linux

This is a small guide that tries to show you how I installed Xen and run Arch Linux as a guest on my host Arch Linux.


You are expected to install Arch Linux. The wiki is your resource. And for Xen this wiki is your main resource.

But the Xen package seems to be out of date, luckely one of the comment gives a link for a more up to date source. So I download the source:

cd ~/builds
hg clone https://bitbucket.org/encbladexp/aur

Then I followed the AUR wiki installing packages to install Xen.

When it is installed, you need to make some adjustments to both Grub boot list in /boot/grub/menu.lst (I asume your using Grub) and /etc/rc.conf. The changes needed is found in the file ~/builds/aur/xen/xen.install.

But these changes didnt work for me, I needed to modify menu.lst:

# (2) Xen
title Xen with Arch Linux
root (hd0,0)
kernel /boot/xen.gz dom0_mem=512M dom0_max_vcpus=4 dom0_vcpus_pin
module /boot/vmlinuz-linux root=/dev/sdaXX ro
module /boot/initramfs-linux.img

As you can compare I changed the "root=" option (Note /dev/sdaXX is just an example. This is the parition that your HOST OS uses and not the GUEST OS see below where we format a new empty partition). Now after reboot choose "Xen with Arch Linux" from your Grub menu.
Next I tried to setup a guest Arch Linux in Xen, this is called domU (U for User). I struggeled with this step, things seems to be scattered all over the wiki page and some is out dated. So this is my attempt to organize this process.

## /dev/sdbXX is an example of a block device
$ mkfs.ext4 /dev/sdbXX    ## format partition
$ mkdir /tmp/install
$ mount /dev/sdbXX /tmp/install
$ mkdir -p /tmp/install/{dev,proc,sys} /tmp/install/var/lib/pacman /tmp/install/var/cache/pacman/pkg
$ mount -o bind /dev /tmp/install/dev
$ mount -t proc none /tmp/install/proc
$ mount -o bind /sys /tmp/install/sys
$ pacman -Sy -r /tmp/install --cachedir /tmp/install/var/cache/pacman/pkg -b /tmp/install/var/lib/pacman base
$ cp -r /etc/pacman* /tmp/install/etc
$ chroot /tmp/install /bin/bash
$ vi /etc/fstab
    /dev/xvda               /           ext4    defaults                0       1
# Find and uncomment all 6 console and add one as follows:
$ vi /etc/inittab
    c1:2345:respawn:/sbin/agetty -8 38400 hvc0 linux
    #c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux
    #c2:2345:respawn:/sbin/agetty -8 38400 tty2 linux
    #c3:2345:respawn:/sbin/agetty -8 38400 tty3 linux
    #c4:2345:respawn:/sbin/agetty -8 38400 tty4 linux
    #c5:2345:respawn:/sbin/agetty -8 38400 tty5 linux
    #c6:2345:respawn:/sbin/agetty -8 38400 tty6 linux 
$ exit  ## exit chroot
$ umount /tmp/install/dev
$ umount /tmp/install/proc
$ umount /tmp/install/sys
$ umount /tmp/install
$ vi /etc/xen/dom01     ## create config file
    #  -*- mode: python; -*-
    kernel = "/boot/vmlinuz-linux"
    ramdisk = "/boot/xen-initramfs-linux.img"
    memory = 256
    name = "dom01"
    vif = [ 'mac=00:16:3e:00:01:01' ]
    disk = [ 'phy:/dev/sdaXX,xvda,w' ]
    dhcp="dhcp"
    hostname = "ooga"
    root = "/dev/xvda ro"
Now we have a working filesystem for the guest OS, usuable kernel from the host system (you can compile your own custom kernel if you like). But because we use the same kernel we need to load some Xen specific modules early on for the guest OS to boot. This is done by a tool called mkinitcpio. This tool needs some configuration files. You can modify the ones that comes with Arch Linux but I copied the files and modified them. These are the files I copied and edited:
$ cat /etc/mkinitcpio.d/xen-linux.preset
# mkinitcpio preset file for the 'linux' package
    
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
    
PRESETS=('default' 'fallback')

default_config="/etc/xen-mkinitcpio.conf"
default_image="/boot/xen-initramfs-linux.img"
#default_options=""
    
fallback_config="/etc/xen-mkinitcpio.conf"
fallback_image="/boot/xen-initramfs-linux-fallback.img"
fallback_options="-S autodetect"
$ cat /etc/xen-mkinitcpio.conf
# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES="piix ide_disk reiserfs"
MODULES="xen-blkfront xen-fbfront xenfs xen-netfront xen-kbdfront"

# BINARIES
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
BINARIES=""

# FILES
# This setting is similar to BINARIES above, however, files are added
# as-is and are not parsed in any way.  This is useful for config files.
# Some users may wish to include modprobe.conf for custom module options
# like so:
#    FILES="/etc/modprobe.d/modprobe.conf"
FILES=""

# HOOKS
# This is the most important setting in this file.  The HOOKS control the
# modules and scripts added to the image, and what happens at boot time.
# Order is important, and it is recommended that you do not change the
# order in which HOOKS are added.  Run 'mkinitcpio -H ' for
# help on a given hook.

# 'base' is _required_ unless you know precisely what you are doing.
# 'udev' is _required_ in order to automatically load modules
# 'filesystems' is _required_ unless you specify your fs modules in MODULES
# Examples:
##   This setup specifies all modules in the MODULES setting above.
##   No raid, lvm2, or encrypted root is needed.
#    HOOKS="base"
#
##   This setup will autodetect all modules for your system and should
##   work as a sane default
#    HOOKS="base udev autodetect pata scsi sata filesystems"
#
##   This is identical to the above, except the old ide subsystem is
##   used for IDE devices instead of the new pata subsystem.
#    HOOKS="base udev autodetect ide scsi sata filesystems"
#
##   This setup will generate a 'full' image which supports most systems.
##   No autodetection is done.
#    HOOKS="base udev pata scsi sata usb filesystems"
#

##   This setup assembles a pata mdadm array with an encrypted root FS.
##   Note: See 'mkinitcpio -H mdadm' for more information on raid devices.
#    HOOKS="base udev pata mdadm encrypt filesystems"
#
##   This setup loads an lvm2 volume group on a usb device.
#    HOOKS="base udev usb lvm2 filesystems"
#
##   NOTE: If you have /usr on a separate partition, you MUST include the
#    usr, fsck and shutdown hooks.
HOOKS="base udev autodetect pata scsi sata filesystems usbinput fsck"

# COMPRESSION
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use 'cat' to create an uncompressed image.
#COMPRESSION="gzip"
#COMPRESSION="bzip2"
#COMPRESSION="lzma"
#COMPRESSION="xz"
#COMPRESSION="lzop"

# COMPRESSION_OPTIONS
# Additional options for the compressor
#COMPRESSION_OPTIONS=""
Then run
$ mkinitcpio -p xen-linux

This will create an initial filesystem on ram-memory that is loaded before your filesystem and makes it possible for the guest OS to boot. Now you can boot the guest OS with
$ xm create -c dom01
And you should see it boot with:
Using config file "/etc/xen/dom01".
Started domain dom01 (id=24)
                            [    0.076618] i8042: No controller found
[    0.137323] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
:: running early hook [udev]
:: running hook [udev]
:: Triggering uevents...
:: performing fsck on '/dev/xvda'
/dev/xvda: clean, 27237/2564096 files, 349439/10240000 blocks
:: mounting '/dev/xvda' on real root
:: running cleanup hook [udev]
INIT: version 2.88 booting
 
 > Arch Linux
 
 > http://www.archlinux.org


Arch Linux 3.4.3-1-ARCH (hvc0)

(none) login:
Have fun!