Xen/Creating DomUs

From Gentoo Linux Wiki

< Xen
Jump to: navigation, search


Virtualization TOC


Virtual pc.png

DomUs are guest operating systems. This guide explains to you several ways to make these for Xen.

Contents

[edit] Using pre-built OS images

One of the easiest methods of creating domUs is to use a pre-built image - a single file that contains a filesystem and an already installed operating system.

With an existing image, you only need to create the Xen domU configuration file and supply a domU kernel (which can be re-used between several domUs).

http://stacklet.com/ provides pre-built Xen images for a range of common Linux distributions, including CentOS, Debian, Fedora Core, Slackware, and Gentoo.

[edit] Using app-emulation/domi

app-emulation/domi is a set of shell scripts from Gerd Knorr that can be used to build Suse, Fedora, Debian, Ubuntu and Gentoo domUs. domi creates a virtual disk on either a regular file or new LVM2 logical volume.

domi creates Fedora and CentOS domUs using sys-apps/yum, Debian and Ubuntu domUs using debootstrap, and Gentoo domUs using the regular chroot-style stage3 Gentoo install.

Unmask domi and its dependencies:

File: /etc/portage/package.keywords

app-emulation/domi sys-fs/multipath-tools sys-apps/yum app-arch/rpm

emerge app-emulation/domi
Note: DomUs have a default password of "secret" for the root user.

[edit] Example: File-backed Debian Sarge domU

Settings can be passed to domi as environment variables (ie. NAME=value domi) or through a file passed as the first argument to domi (ie. domi config-file). We'll use a config file:

File: debian-test.cfg
DOMI_DISK_FILE="/var/xen/debian-domU.img"
DOMI_DISK_SPARSE="yes"
DOMI_XEN_CONF="/etc/xen/debian-test"
DOMI_XEN_KERNEL="/boot/xen-domU"
DOMI_DISTRO="debian"
DOMI_NAME="debian-test"
DOMI_VDISK="hda"
domi debian-test.cfg
###
### debian-test: initialization (i386)
###

###
### debian-test: setup disk (sparse file /var/xen/debian-domU.img)
###
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.003593 seconds, 292 MB/s
Disk geometry for /dev/loop/1: 0kB - 4295MB
Disk label type: msdos
Number  Start   End     Size    Type      File system  Flags
1       1kB     4026MB  4026MB  primary                boot
2       4026MB  4294MB  268MB   primary
add map 1-part1 : 0 7863281 linear /dev/loop/1 1
add map 1-part2 : 0 522648 linear /dev/loop/1 7863282

###
### debian-test: setup root fs and swap
###
Label was truncated.
Setting up swapspace version 1, size = 267591 kB
LABEL=debian-test-swa, UUID=0328303e-2634-48b9-ace5-1cff6ff95cc2

###
### debian-test: copy cached debs [/var/cache/domi/debian-sarge]
###

###
### debian-test: fetching debootstrap from ftp://ftp.monash.edu.au/pub/linux/debian
###
15:23:05 URL: ftp://ftp.monash.edu.au/pub/linux/debian/pool/main/d/debootstrap/ [2107] -> ".listing" [1]
15:23:05 URL: ftp://ftp.monash.edu.au/pub/linux/debian/pool/main/d/debootstrap/debootstrap_0.1.17.7woody1_i386.deb [51554] -> "debootstrap_0.1.17.7woody1_i386.deb" [1]
15:23:05 URL: ftp://ftp.monash.edu.au/pub/linux/debian/pool/main/d/debootstrap/debootstrap_0.2.45-0.2_i386.deb [72236] -> "debootstrap_0.2.45-0.2_i386.deb" [1]

FINISHED --15:23:05--
Downloaded: 125,897 bytes in 3 files

###
### debian-test: unpack /var/cache/domi/debian-sarge/debootstrap_0.2.45-0.2_i386.deb
###

###
### debian-test: bootstrap debian sarge from ftp://ftp.monash.edu.au/pub/linux/debian
###
I: Retrieving debootstrap.invalid_dists_sarge_Release
I: Validating debootstrap.invalid_dists_sarge_Release
I: Retrieving debootstrap.invalid_dists_sarge_main_binary-i386_Packages
I: Validating debootstrap.invalid_dists_sarge_main_binary-i386_Packages
I: Checking adduser...
I: Checking apt...
I: Checking apt-utils...

.
.
.

###
### debian-test: save downloaded debs [/var/cache/domi/debian-sarge]
###
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/1-part1    3869896    147060   3526256   5% /tmp/domi-6942/mnt

###
### debian-test: cleanup: virtual disk
###
/dev/mapper/1-part1 umounted
del devmap : 1-part1
del devmap : 1-part2

###
### debian-test: cleanup: remove tmp files
###

Now start the domU and attach to its console:

xm create debian-test -c
Linux version 2.6.16.18-xen (root@oak) (gcc version 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)) #12 Wed Jun 7 12:28:59 EST 2006
BIOS-provided physical RAM map:
 Xen: 0000000000000000 - 0000000008000000 (usable)
136MB LOWMEM available.
IRQ lockup detection disabled
Built 1 zonelists
Kernel command line:  ip=:1.2.3.4::::eth0:dhcp root=/dev/xvda1 ro
Enabling fast FPU save and restore... done.
Enabling unmasked SIMD FPU exception support... done.
Initializing CPU#0
PID hash table entries: 1024 (order: 10, 16384 bytes)
Xen reported: 1665.426 MHz processor.
Dentry cache hash table entries: 32768 (order: 5, 131072 bytes)
Inode-cache hash table entries: 16384 (order: 4, 65536 bytes)
Software IO TLB disabled
vmalloc area: c9000000-fbefa000, maxmem 33ffe000
Memory: 125364k/139264k available (2744k kernel code, 5556k reserved, 816k data, 164k init, 0k highmem)
Checking if this processor honours the WP bit even in supervisor mode... Ok.
Calibrating delay using timer specific routine.. 3330.63 BogoMIPS (lpj=16653169)
Mount-cache hash table entries: 512
CPU: L1 I Cache: 64K (64 bytes/line), D cache 64K (64 bytes/line)
CPU: L2 Cache: 256K (64 bytes/line)
CPU: AMD Athlon(tm)  stepping 01
Checking 'hlt' instruction... OK.
Grant table initialized
NET: Registered protocol family 16
PCI: setting up Xen PCI frontend stub
Generic PHY: Registered new driver
xen_mem: Initialising balloon driver.
PCI: System does not support PCI
PCI: System does not support PCI
Initializing Cryptographic API

testing md5
test 1:
d41d8cd98f00b204e9800998ecf8427e
pass
test 2:
0cc175b9c0f1b6a831c399e269772661
pass

.
.
.

Setting the System Clock using the Hardware Clock as reference...
System Clock set. Local time: Wed Jun 14 06:52:44 UTC 2006

Initializing random number generator...done.
Recovering nvi editor sessions... done.
INIT: Entering runlevel: 2
Starting system log daemon: syslogd.
Starting kernel log daemon: klogd.
Starting MTA: exim4.
Starting internet superserver: inetd.
Starting deferred execution scheduler: atd.
Starting periodic command scheduler: cron.

Debian GNU/Linux 3.1 (none) tty1

(none) login:

[edit] Gentoo domU using quickpkg

The dom0 is a Gentoo system compiled specifically for Xen (through CFLAGS="-mno-tls-direct-seg-refs" in /etc/make.conf), and may even be built with your preferred CFLAGS and USE flags.

quickpkg allows you to create binary packages from an existing Gentoo system (such as your dom0), which can be combined with portage's support for alternate ROOTs to quickly create a Gentoo domU with the same CFLAGs as your dom0, without needing to recompile anything or perform a stage1 install.

If you don't already have it, install app-portage/gentoolkit, as we'll be using equery to list all packages currently installed on the system:

emerge app-portage/gentoolkit

If you're not already using the buildpkg feature in portage, you'll need to create binary packages from your dom0 install using the following script:

Code: Alternative
for PKG in $(equery -q list | cut -d ' ' -f 3)
do
  quickpkg --include-config=y =$PKG
done

There is an [TIP_Filtering_Packages example] in the tips section of filtering out packages, using USE flags, categories and arbitrary name components.

Create storage for your domU, using your preferred method (eg. loopback file-based image, LVM2 logical volume, physical partition, or EVMS volume). Create a filesystem on this storage and mount it at /mnt/gentoo.

Example: 4GB sparse loopback file-based image with reiserfs filesystem.

# Create sparse file
dd if=/dev/zero of=/var/xen/domU-gentoo bs=1M count=4095

mkreiserfs -f /var/xen/domU-gentoo

mkdir -p /mnt/gentoo
mount -o loop /var/xen/domU-gentoo /mnt/gentoo

Next you need to create and initialize a swap space for use by domU. These instructions use a swap file, but you may want to consider using a seperate partition as a normal system would. For a discussion on the differences between the two, see The Linuxk-Kernel Mailing List: Swap partition vs swap file.

dd if=/dev/zero of=/mnt/gentoo/swap bs=1M count=256
mkswap /mnt/gentoo/swap

Download a stage3 tarball and unpack it into /mnt/gentoo/:

tar -xvjpf stage3-i686-2008.0.tar.bz2 -C /mnt/gentoo/

Mount the proc and dev filesystems so they are available from within the chroot environment:

mount -t proc none /mnt/gentoo/proc mount -o bind /dev /mnt/gentoo/dev

Copy the existing portage tree from dom0. This will also copy the binary packages built earlier, as they are stored in /usr/portage/packages:

cp -av /usr/portage/ /mnt/gentoo/usr/

Copy dom0's make.conf and /etc/portage/ to domU, so we're using the correct CFLAGS, CHOST, USE flags etc.

cp /etc/make.conf /mnt/gentoo/etc/ cp -R /etc/portage /mnt/gentoo/etc/

Copy resolv.conf into the chroot environment to ensure we can use internet access:

cp /etc/resolv.conf /mnt/gentoo/etc/

Make sure dom0 and domU are using the same profile, which is probably default-linux/x86/2008.0 (/desktop or /server)

rm /mnt/gentoo/etc/make.profile ln -s ../usr/portage/profiles/default/linux/x86/2008.0/server /mnt/gentoo/etc/make.profile

Use binary packages to create a new Gentoo system in /mnt/gentoo/, overwriting config files from the stage3 tarball:

ROOT=/mnt/gentoo/ CONFIG_PROTECT=-/etc FEATURES=-collision-protect emerge --usepkg --emptytree system
Warning: You may be tempted to install additional packages at this stage, such as net-misc/dhcp, but you MUST wait until inside the chroot environment! Portage's pkg_setup function does not work as expected when combined with ROOT, and certain packages (such as those using enewuser or enewgroup ) will not install correctly.


Chroot into the Gentoo domU and run the following. Don't worry about the ominous /usr/bin/python: error while loading shared libraries:
libstdc++.so.6: cannot open shared object file: No such file or directory
when running gcc-config, as that's what we're fixing here. It seems sys-devel/gcc doesn't play too well with portage's ROOT option, so after the above commands we have an invalid gcc profile selected.
chroot /mnt/gentoo

gcc-config -1 env-update

source /etc/profile

Using the same method as you do for a normal install, set the domU's timezone and hostname.

Install app-portage/gentoolkit, then use revdep-rebuild to remerge (from our binary packages) anything that still links against a package from the stage tarball.

emerge --usepkg app-portage/gentoolkit revdep-rebuild -- --usepkg


Tip: You can use the --usepkg flag to emerge any package that was also installed on the dom0 system that the binary packages were created on as long as you want exactly the same USE flags.


Take care of any rebuilds or cleanups required by python and perl updates (using our binary packages, of course).

perl-cleaner all --usepkg python-updater

Install any non-system packages that will be required to boot the domU (eg. net-misc/dhcpd sys-fs/reiserfsprogs, net-misc/dhcpd):

emerge --usepkg net-misc/dhcp sys-fs/reiserfsprogs
Set a password for the domU's root user using:
passwd
Finally, exit the chroot environment with:
exit

Create a Xen configuration file for our new domU. In this example, the loopback file /var/xen/domU-gentoo will be exposed to the domU as /dev/xvda.

File: /etc/xen/gentoo
# general
name    = "gentoo";
memory  = 256;

# booting
kernel  = "/boot/xen-domU";

# virtual harddisk
disk = [ "file:/var/xen/domU-gentoo,xvda,w" ];
root = "/dev/xvda ro";

# virtual network
vif = [ "" ];
dhcp = "dhcp";

# If you can't see anything from init in your "xm console" command try uncommenting this line.
#extra = 'xencons=tty'

Note: Note that the disk and root entries must both refer to this same virtual block device otherwise the boot attempt will fail. The domU cannot see the physical hardware attached to the dom0 unless it is defined on the disk line in this configuration file.

Edit domU's /etc/fstab to use the swapfile and Xen block device (/dev/xvda). domUs don't need a boot partition and don't have a CD-ROM or floppy drive, so remove those lines.

File: /mnt/gentoo/etc/fstab
# <fs>                  <mountpoint>    <type>          <opts>          <dump/pass>

/dev/xvda               /               reiserfs        noatime         0 1
/swap                   none            swap            sw              0 0

proc                    /proc           proc            defaults        0 0
shm                     /dev/shm        tmpfs           nodev,nosuid,noexec     0 0

Umount the domU filesystem:

umount /mnt/gentoo/{dev,proc}

Start the domU and attach to its console: xm create gentoo -c

Once the domU running, check that all necessary users for various services as ssh are present in /etc/passwd and /etc/group.


Tip: You can now create a stage4 tarball to use for future Gentoo domUs on the same dom0. Autostart domU

If you want your domU to be started on system boot you have to create symlinks in /etc/xen/auto to your domU configuration file.

cd /etc/xen/auto ln -s /etc/xen/gentoo

Add the xendomains daemon to the default runlevel with:

rc-update add xendomains default

[edit] By Hand

It is possible to create domUs by hand, you can do so as you would normally create any other linux install. Once finished, load it the same way as ttylinux or when it's on disk look at /etc/xen/xmexample1 to see how to configure xen to use partitions instead of files.

Personal tools