NetBSD/xen Howto
================
[![BSD
daemon](../../images/BSD-daemon.jpg)](../../about/disclaimer.html#bsd-daemon)
Table Of Contents
-----------------
- [Introduction](#introduction)
- [Installing NetBSD as privileged domain (Dom0)](#netbsd-dom0)
- [Creating an unprivileged NetBSD domain (DomU)](#netbsd-domU)
- [Creating an unprivileged Linux domain (DomU)](#linux-domU)
- [Creating an unprivileged Solaris domain (DomU)](#solaris-domU)
- [Using PCI devices in guest domains](#pci-pass-through)
- [Links and further information](#links-and-more)
* * * * *
### Introduction
[![[Xen
screenshot]](../../gallery/in-Action/hubertf-xens.png)](../../gallery/in-Action/hubertf-xen.png)
Xen is a virtual machine monitor for x86 hardware (requires i686-class
CPUs), which supports running multiple guest operating systems on a
single machine. Guest OSes (also called <80><9C>domains<80><9D>) require a modified
kernel which supports Xen hypercalls in replacement to access to the
physical hardware. At boot, the Xen kernel (also known as the Xen
hypervisor) is loaded (via the bootloader) along with the guest kernel
for the first domain (called *domain0*). The Xen kernel has to be loaded
using the multiboot protocol. You would use the NetBSD boot loader for
this, or alternatively the **grub** boot loader (**grub** has some
limitations, detailed below). *domain0* has special privileges to access
the physical hardware (PCI and ISA devices), administrate other domains
and provide virtual devices (disks and network) to other domains that
lack those privileges. For more details, see
[http://www.xen.org/](http://www.xen.org/).
NetBSD can be used for both *domain0 (Dom0)* and further, unprivileged
(DomU) domains. (Actually there can be multiple privileged domains
accessing different parts of the hardware, all providing virtual devices
to unprivileged domains. We will only talk about the case of a single
privileged domain, *domain0*). *domain0* will see physical devices much
like a regular i386 or amd64 kernel, and will own the physical console
(VGA or serial). Unprivileged domains will only see a character-only
virtual console, virtual disks (`xbd`{.code}) and virtual network
interfaces (`xennet`{.code}) provided by a privileged domain (usually
*domain0*). xbd devices are connected to a block device (i.e., a
partition of a disk, raid, ccd, ... device) in the privileged domain.
xennet devices are connected to virtual devices in the privileged
domain, named xvif\.\, e.g.,
xvif1.0. Both xennet and xvif devices are seen as regular Ethernet
devices (they can be seen as a crossover cable between 2 PCs) and can be
assigned addresses (and be routed or NATed, filtered using IPF, etc ...)
or be added as part of a bridge.
* * * * *
### Installing NetBSD as privileged domain (Dom0)
First do a NetBSD/i386 or NetBSD/amd64
[installation](../../docs/guide/en/chap-inst.html) of the 5.1 release
(or newer) as you usually do on x86 hardware. The binary releases are
available from
[ftp://ftp.NetBSD.org/pub/NetBSD/](ftp://ftp.NetBSD.org/pub/NetBSD/).
Binary snapshots for current and the stable branches are available on
[daily autobuilds](http://nyftp.NetBSD.org/pub/NetBSD-daily/). If you
plan to use the **grub** boot loader, when partitioning the disk you
have to make the root partition smaller than 512Mb, and formatted as
FFSv1 with 8k block/1k fragments. If the partition is larger than this,
uses FFSv2 or has different block/fragment sizes, grub may fail to load
some files. Also keep in mind that you'll probably want to provide
virtual disks to other domains, so reserve some partitions for these
virtual disks. Alternatively, you can create large files in the file
system, map them to vnd(4) devices and export theses vnd devices to
other domains.
Next step is to install the Xen packages via pkgsrc or from binary
packages. See [the pkgsrc
documentation](http://www.NetBSD.org/docs/pkgsrc/) if you are unfamiliar
with pkgsrc and/or handling of binary packages. Xen 3.1, 3.3, 4.1 and
4.2 are available. 3.1 supports PCI pass-through while other versions do
not. You'll need either
[`sysutils/xentools3`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools3/README.html)
and
[`sysutils/xenkernel3`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel3/README.html)
for Xen 3.1,
[`sysutils/xentools33`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools33/README.html)
and
[`sysutils/xenkernel33`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel33/README.html)
for Xen 3.3,
[`sysutils/xentools41`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools41/README.html)
and
[`sysutils/xenkernel41`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel41/README.html)
for Xen 4.1. or
[`sysutils/xentools42`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools42/README.html)
and
[`sysutils/xenkernel42`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel42/README.html)
for Xen 4.2. You'll also need
[`sysutils/grub`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/grub/README.html)
if you plan do use the grub boot loader. If using Xen 3.1, you may also
want to install
[`sysutils/xentools3-hvm`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools3-hvm/README.html)
which contains the utilities to run unmodified guests OSes using the
*HVM* support (for later versions this is included in
[`sysutils/xentools`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools/README.html)).
Note that your CPU needs to support this. Intel CPUs must have the 'VT'
instruction, AMD CPUs the 'SVM' instruction. You can easily find out if
your CPU support HVM by using NetBSD's cpuctl command:
~~~ {.programlisting}
# cpuctl identify 0
cpu0: Intel Core 2 (Merom) (686-class), id 0x6f6
cpu0: features 0xbfebfbff
cpu0: features 0xbfebfbff
cpu0: features 0xbfebfbff
cpu0: features2 0x4e33d
cpu0: features3 0x20100800
cpu0: "Intel(R) Xeon(R) CPU 5130 @ 2.00GHz"
cpu0: I-cache 32KB 64B/line 8-way, D-cache 32KB 64B/line 8-way
cpu0: L2 cache 4MB 64B/line 16-way
cpu0: ITLB 128 4KB entries 4-way
cpu0: DTLB 256 4KB entries 4-way, 32 4MB entries 4-way
cpu0: Initial APIC ID 0
cpu0: Cluster/Package ID 0
cpu0: Core ID 0
cpu0: family 06 model 0f extfamily 00 extmodel 00
~~~
Depending on your CPU, the feature you are looking for is called HVM,
SVM or VMX.
Next you need to copy the selected Xen kernel itself. pkgsrc installed
them under `/usr/pkg/xen*-kernel/`{.filename}. The file you're looking
for is `xen.gz`{.filename}. Copy it to your root file system.
`xen-debug.gz`{.filename} is a kernel with more consistency checks and
more details printed on the serial console. It is useful for debugging
crashing guests if you use a serial console. It is not useful with a VGA
console.
You'll then need a NetBSD/Xen kernel for *domain0* on your root file
system. The XEN3PAE\_DOM0 kernel or XEN3\_DOM0 provided as part of the
i386 or amd64 binaries is suitable for this, but you may want to
customize it. Keep your native kernel around, as it can be useful for
recovery. *Note:* the *domain0* kernel must support KERNFS and
`/kern`{.filename} must be mounted because *xend* needs access to
`/kern/xen/privcmd`{.filename}.
Next you need to get a bootloader to load the `xen.gz`{.filename}
kernel, and the NetBSD *domain0* kernel as a module. This can be
**grub** or NetBSD's boot loader. Below is a detailled example for grub,
see the boot.cfg(5) manual page for an example using the latter.
This is also where you'll specify the memory allocated to *domain0*, the
console to use, etc ...
Here is a commented `/grub/menu.lst`{.filename} file:
~~~ {.programlisting}
#Grub config file for NetBSD/xen. Copy as /grub/menu.lst and run
# grub-install /dev/rwd0d (assuming your boot device is wd0).
#
# The default entry to load will be the first one
default=0
# boot the default entry after 10s if the user didn't hit keyboard
timeout=10
# Configure serial port to use as console. Ignore if you'll use VGA only
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
# Let the user select which console to use (serial or VGA), default
# to serial after 10s
terminal --timeout=10 serial console
# An entry for NetBSD/xen, using /netbsd as the domain0 kernel, and serial
# console. Domain0 will have 64MB RAM allocated.
# Assume NetBSD is installed in the first MBR partition.
title Xen 3 / NetBSD (hda0, serial)
root(hd0,0)
kernel (hd0,a)/xen.gz dom0_mem=65536 com1=115200,8n1
module (hd0,a)/netbsd bootdev=wd0a ro console=ttyS0
# Same as above, but using VGA console
# We can use console=tty0 (Linux syntax) or console=pc (NetBSD syntax)
title Xen 3 / NetBSD (hda0, vga)
root(hd0,0)
kernel (hd0,a)/xen.gz dom0_mem=65536
module (hd0,a)/netbsd bootdev=wd0a ro console=tty0
# NetBSD/xen using a backup domain0 kernel (in case you installed a
# nonworking kernel as /netbsd
title Xen 3 / NetBSD (hda0, backup, serial)
root(hd0,0)
kernel (hd0,a)/xen.gz dom0_mem=65536 com1=115200,8n1
module (hd0,a)/netbsd.backup bootdev=wd0a ro console=ttyS0
title Xen 3 / NetBSD (hda0, backup, VGA)
root(hd0,0)
kernel (hd0,a)/xen.gz dom0_mem=65536
module (hd0,a)/netbsd.backup bootdev=wd0a ro console=tty0
#Load a regular NetBSD/i386 kernel. Can be useful if you end up with a
#nonworking /xen.gz
title NetBSD 5.1
root (hd0,a)
kernel --type=netbsd /netbsd-GENERIC
#Load the NetBSD bootloader, letting it load the NetBSD/i386 kernel.
#May be better than the above, as grub can't pass all required infos
#to the NetBSD/i386 kernel (e.g. console, root device, ...)
title NetBSD chain
root (hd0,0)
chainloader +1
## end of grub config file.
~~~
Install grub with the following command:
~~~ {.programlisting}
# grub --no-floppy
grub> root (hd0,a)
Filesystem type is ffs, partition type 0xa9
grub> setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/ffs_stage1_5" exists... yes
Running "embed /grub/ffs_stage1_5 (hd0)"... 14 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+14 p (hd0,0,a)/grub/stage2 /grub/menu.lst"...
succeeded
Done.
~~~
* * * * *
### Creating an unprivileged NetBSD domain (DomU)
Once you have *domain0* running, you need to start the xen tool daemon
(**/usr/pkg/share/examples/rc.d/xend start**) and the xen backend daemon
(**/usr/pkg/share/examples/rc.d/xenbackendd start** for Xen3\*,
**/usr/pkg/share/examples/rc.d/xencommons start** for Xen4.\*). Make
sure that `/dev/xencons`{.filename} and `/dev/xenevt`{.filename} exist
before starting **xend**. You can create them with this command:
~~~ {.programlisting}
# cd /dev && sh MAKEDEV xen
~~~
xend will write logs to `/var/log/xend.log`{.filename} and
`/var/log/xend-debug.log`{.filename}. You can then control xen with the
xm tool. 'xm list' will show something like:
~~~ {.programlisting}
# xm list
Name Id Mem(MB) CPU State Time(s) Console
Domain-0 0 64 0 r---- 58.1
~~~
'xm create' allows you to create a new domain. It uses a config file in
PKG\_SYSCONFDIR for its parameters. By default, this file will be in
`/usr/pkg/etc/xen/`{.filename}. On creation, a kernel has to be
specified, which will be executed in the new domain (this kernel is in
the *domain0* file system, not on the new domain virtual disk; but
please note, you should install the same kernel into *domainU* as
`/netbsd`{.filename} in order to make your system tools, like
[savecore(8)](http://netbsd.gw.com/cgi-bin/man-cgi?savecore+8+NetBSD-6.0+i386),
work). A suitable kernel is provided as part of the i386 and amd64
binary sets: XEN3\_DOMU.
Here is an /usr/pkg/etc/xen/nbsd example config file:
~~~ {.programlisting}
# -*- mode: python; -*-
#============================================================================
# Python defaults setup for 'xm create'.
# Edit this file to reflect the configuration of your system.
#============================================================================
#----------------------------------------------------------------------------
# Kernel image file. This kernel will be loaded in the new domain.
kernel = "/home/bouyer/netbsd-XEN3_DOMU"
#kernel = "/home/bouyer/netbsd-INSTALL_XEN3_DOMU"
# Memory allocation (in megabytes) for the new domain.
memory = 128
# A handy name for your new domain. This will appear in 'xm list',
# and you can use this as parameters for xm in place of the domain
# number. All domains must have different names.
#
name = "nbsd"
# The number of virtual CPUs this domain has.
#
vcpus = 1
#----------------------------------------------------------------------------
# Define network interfaces for the new domain.
# Number of network interfaces (must be at least 1). Default is 1.
nics = 1
# Define MAC and/or bridge for the network interfaces.
#
# The MAC address specified in ``mac'' is the one used for the interface
# in the new domain. The interface in domain0 will use this address XOR'd
# with 00:00:00:01:00:00 (i.e. aa:00:00:51:02:f0 in our example). Random
# MACs are assigned if not given.
#
# ``bridge'' is a required parameter, which will be passed to the
# vif-script called by xend(8) when a new domain is created to configure
# the new xvif interface in domain0.
#
# In this example, the xvif is added to bridge0, which should have been
# set up prior to the new domain being created -- either in the
# ``network'' script or using a /etc/ifconfig.bridge0 file.
#
vif = [ 'mac=aa:00:00:50:02:f0, bridge=bridge0' ]
#----------------------------------------------------------------------------
# Define the disk devices you want the domain to have access to, and
# what you want them accessible as.
#
# Each disk entry is of the form:
#
# phy:DEV,VDEV,MODE
#
# where DEV is the device, VDEV is the device name the domain will see,
# and MODE is r for read-only, w for read-write. You can also create
# file-backed domains using disk entries of the form:
#
# file:PATH,VDEV,MODE
#
# where PATH is the path to the file used as the virtual disk, and VDEV
# and MODE have the same meaning as for ``phy'' devices.
#
# VDEV doesn't really matter for a NetBSD guest OS (it's just used as an index),
# but it does for Linux.
# Worse, the device has to exist in /dev/ of domain0, because xm will
# try to stat() it. This means that in order to load a Linux guest OS
# from a NetBSD domain0, you'll have to create /dev/hda1, /dev/hda2, ...
# on domain0, with the major/minor from Linux :(
# Alternatively it's possible to specify the device number in hex,
# e.g. 0x301 for /dev/hda1, 0x302 for /dev/hda2, etc ...
disk = [ 'phy:/dev/wd0e,0x1,w' ]
#disk = [ 'file:/var/xen/nbsd-disk,0x01,w' ]
#disk = [ 'file:/var/xen/nbsd-disk,0x301,w' ]
#----------------------------------------------------------------------------
# Set the kernel command line for the new domain.
# Set root device. This one does matter for NetBSD
root = "xbd0"
# extra parameters passed to the kernel
# this is where you can set boot flags like -s, -a, etc ...
#extra = ""
#----------------------------------------------------------------------------
# Set according to whether you want the domain restarted when it exits.
# The default is False.
#autorestart = True
# end of nbsd config file ====================================================
~~~
When a new domain is created, xen calls the
`/usr/pkg/etc/xen/vif-bridge`{.filename} script for each virtual network
interface created in *domain0*. This can be used to automatically
configure the xvif?.? interfaces in *domain0*. In our example, these
will be bridged with the bridge0 device in *domain0*, but the bridge has
to exist first. To do this, create the file
`/etc/ifconfig.bridge0`{.filename} and make it look like this:
~~~ {.programlisting}
create
!brconfig $int add ex0 up
~~~
(replace `ex0`{.literal} with the name of your physical interface). Then
bridge0 will be created on boot. See the
[bridge(4)](http://netbsd.gw.com/cgi-bin/man-cgi?bridge+4+NetBSD-6.0+i386)
man page for details.
So, here is a suitable `/usr/pkg/etc/xen/vif-bridge`{.filename} for
xvif?.? (a working vif-bridge is also provided with xentools20)
configuring:
~~~ {.programlisting}
#!/bin/sh
#============================================================================
# $NetBSD: howto.mdwn,v 1.1 2013/10/31 12:20:57 mspo Exp $
#
# /usr/pkg/etc/xen/vif-bridge
#
# Script for configuring a vif in bridged mode with a dom0 interface.
# The xend(8) daemon calls a vif script when bringing a vif up or down.
# The script name to use is defined in /usr/pkg/etc/xen/xend-config.sxp
# in the ``vif-script'' field.
#
# Usage: vif-bridge up|down [var=value ...]
#
# Actions:
# up Adds the vif interface to the bridge.
# down Removes the vif interface from the bridge.
#
# Variables:
# domain name of the domain the interface is on (required).
# vifq vif interface name (required).
# mac vif MAC address (required).
# bridge bridge to add the vif to (required).
#
# Example invocation:
#
# vif-bridge up domain=VM1 vif=xvif1.0 mac="ee:14:01:d0:ec:af" bridge=bridge0
#
#============================================================================
# Exit if anything goes wrong
set -e
echo "vif-bridge $*"
# Operation name.
OP=$1; shift
# Pull variables in args into environment
for arg ; do export "${arg}" ; done
# Required parameters. Fail if not set.
domain=${domain:?}
vif=${vif:?}
mac=${mac:?}
bridge=${bridge:?}
# Optional parameters. Set defaults.
ip=${ip:-''} # default to null (do nothing)
# Are we going up or down?
case $OP in
up) brcmd='add' ;;
down) brcmd='delete' ;;
*)
echo 'Invalid command: ' $OP
echo 'Valid commands are: up, down'
exit 1
;;
esac
# Don't do anything if the bridge is "null".
if [ "${bridge}" = "null" ] ; then
exit
fi
# Don't do anything if the bridge doesn't exist.
if ! ifconfig -l | grep "${bridge}" >/dev/null; then
exit
fi
# Add/remove vif to/from bridge.
ifconfig x${vif} $OP
brconfig ${bridge} ${brcmd} x${vif}
~~~
Now, running
~~~ {.programlisting}
xm create -c /usr/pkg/etc/xen/nbsd
~~~
should create a domain and load a NetBSD kernel in it. (Note:
`-c`{.code} causes xm to connect to the domain's console once created.)
The kernel will try to find its root file system on xbd0 (i.e., wd0e)
which hasn't been created yet. wd0e will be seen as a disk device in the
new domain, so it will be 'sub-partitioned'. We could attach a ccd to
wd0e in *domain0* and partition it, newfs and extract the NetBSD/i386 or
amd64 tarballs there, but there's an easier way: load the
`netbsd-INSTALL_XEN3_DOMU`{.filename} kernel provided in the NetBSD
binary sets. Like other install kernels, it contains a ramdisk with
sysinst, so you can install NetBSD using sysinst on your new domain.
If you want to install NetBSD/Xen with a CDROM image, the following line
should be used in the `/usr/pkg/etc/xen/nbsd`{.filename} file:
~~~ {.programlisting}
disk = [ 'phy:/dev/wd0e,0x1,w', 'phy:/dev/cd0a,0x2,r' ]
~~~
After booting the domain, the option to install via CDROM may be
selected. The CDROM device should be changed to **xbd1d**.
Once done installing, **halt -p** the new domain (don't reboot or halt,
it would reload the INSTALL\_XEN3\_DOMU kernel even if you changed the
config file), switch the config file back to the XEN3\_DOMU kernel, and
start the new domain again. Now it should be able to use **root on
xbd0a** and you should have a second, functional NetBSD system on your
xen installation.
When the new domain is booting you'll see some warnings about *wscons*
and the pseudo-terminals. These can be fixed by editing the files
`/etc/ttys`{.filename} and `/etc/wscons.conf`{.filename}. You must
disable all terminals in `/etc/ttys`{.filename}, except *console*, like
this:
~~~ {.programlisting}
console "/usr/libexec/getty Pc" vt100 on secure
ttyE0 "/usr/libexec/getty Pc" vt220 off secure
ttyE1 "/usr/libexec/getty Pc" vt220 off secure
ttyE2 "/usr/libexec/getty Pc" vt220 off secure
ttyE3 "/usr/libexec/getty Pc" vt220 off secure
~~~
Finally, all screens must be commented out from
`/etc/wscons.conf`{.filename}.
It is also desirable to add
~~~ {.programlisting}
powerd=YES
~~~
in rc.conf. This way, the domain will be properly shut down if **xm
shutdown -R** or **xm shutdown -H** is used on the domain0.
Your domain should be now ready to work, enjoy.
* * * * *
### Creating an unprivileged Linux domain (DomU)
Creating unprivileged Linux domains isn't much different from
unprivileged NetBSD domains, but there are some details to know.
First, the second parameter passed to the disk declaration (the '0x1' in
the example below)
~~~ {.programlisting}
disk = [ 'phy:/dev/wd0e,0x1,w' ]
~~~
does matter to Linux. It wants a Linux device number here (e.g. 0x300
for hda). Linux builds device numbers as: (major \