Annotation of wikisrc/ports/xen/howto.mdwn, revision 1.2

1.1       mspo        1: NetBSD/xen Howto
                      2: ================
                      3: 
                      4: [![BSD
                      5: daemon](../../images/BSD-daemon.jpg)](../../about/disclaimer.html#bsd-daemon)
                      6: 
                      7: Table Of Contents
                      8: -----------------
                      9: 
                     10: -   [Introduction](#introduction)
                     11: -   [Installing NetBSD as privileged domain (Dom0)](#netbsd-dom0)
                     12: -   [Creating an unprivileged NetBSD domain (DomU)](#netbsd-domU)
                     13: -   [Creating an unprivileged Linux domain (DomU)](#linux-domU)
                     14: -   [Creating an unprivileged Solaris domain (DomU)](#solaris-domU)
                     15: -   [Using PCI devices in guest domains](#pci-pass-through)
                     16: -   [Links and further information](#links-and-more)
                     17: 
                     18: * * * * *
                     19: 
                     20: ### Introduction
                     21: 
                     22: [![[Xen
                     23: screenshot]](../../gallery/in-Action/hubertf-xens.png)](../../gallery/in-Action/hubertf-xen.png)
                     24: 
                     25: Xen is a virtual machine monitor for x86 hardware (requires i686-class
                     26: CPUs), which supports running multiple guest operating systems on a
                     27: single machine. Guest OSes (also called <E2><80><9C>domains<E2><80><9D>) require a modified
                     28: kernel which supports Xen hypercalls in replacement to access to the
                     29: physical hardware. At boot, the Xen kernel (also known as the Xen
                     30: hypervisor) is loaded (via the bootloader) along with the guest kernel
                     31: for the first domain (called *domain0*). The Xen kernel has to be loaded
                     32: using the multiboot protocol. You would use the NetBSD boot loader for
                     33: this, or alternatively the **grub** boot loader (**grub** has some
                     34: limitations, detailed below). *domain0* has special privileges to access
                     35: the physical hardware (PCI and ISA devices), administrate other domains
                     36: and provide virtual devices (disks and network) to other domains that
                     37: lack those privileges. For more details, see
                     38: [http://www.xen.org/](http://www.xen.org/).
                     39: 
                     40: NetBSD can be used for both *domain0 (Dom0)* and further, unprivileged
                     41: (DomU) domains. (Actually there can be multiple privileged domains
                     42: accessing different parts of the hardware, all providing virtual devices
                     43: to unprivileged domains. We will only talk about the case of a single
                     44: privileged domain, *domain0*). *domain0* will see physical devices much
                     45: like a regular i386 or amd64 kernel, and will own the physical console
                     46: (VGA or serial). Unprivileged domains will only see a character-only
                     47: virtual console, virtual disks (`xbd`{.code}) and virtual network
                     48: interfaces (`xennet`{.code}) provided by a privileged domain (usually
                     49: *domain0*). xbd devices are connected to a block device (i.e., a
                     50: partition of a disk, raid, ccd, ... device) in the privileged domain.
                     51: xennet devices are connected to virtual devices in the privileged
                     52: domain, named xvif\<domain number\>.\<if number for this domain\>, e.g.,
                     53: xvif1.0. Both xennet and xvif devices are seen as regular Ethernet
                     54: devices (they can be seen as a crossover cable between 2 PCs) and can be
                     55: assigned addresses (and be routed or NATed, filtered using IPF, etc ...)
                     56: or be added as part of a bridge.
                     57: 
                     58: * * * * *
                     59: ### Installing NetBSD as privileged domain (Dom0)
                     60: 
                     61: First do a NetBSD/i386 or NetBSD/amd64
                     62: [installation](../../docs/guide/en/chap-inst.html) of the 5.1 release
                     63: (or newer) as you usually do on x86 hardware. The binary releases are
                     64: available from
                     65: [ftp://ftp.NetBSD.org/pub/NetBSD/](ftp://ftp.NetBSD.org/pub/NetBSD/).
                     66: Binary snapshots for current and the stable branches are available on
                     67: [daily autobuilds](http://nyftp.NetBSD.org/pub/NetBSD-daily/). If you
                     68: plan to use the **grub** boot loader, when partitioning the disk you
                     69: have to make the root partition smaller than 512Mb, and formatted as
                     70: FFSv1 with 8k block/1k fragments. If the partition is larger than this,
                     71: uses FFSv2 or has different block/fragment sizes, grub may fail to load
                     72: some files. Also keep in mind that you'll probably want to provide
                     73: virtual disks to other domains, so reserve some partitions for these
                     74: virtual disks. Alternatively, you can create large files in the file
                     75: system, map them to vnd(4) devices and export theses vnd devices to
                     76: other domains.
                     77: 
                     78: Next step is to install the Xen packages via pkgsrc or from binary
                     79: packages. See [the pkgsrc
                     80: documentation](http://www.NetBSD.org/docs/pkgsrc/) if you are unfamiliar
                     81: with pkgsrc and/or handling of binary packages. Xen 3.1, 3.3, 4.1 and
                     82: 4.2 are available. 3.1 supports PCI pass-through while other versions do
                     83: not. You'll need either
                     84: [`sysutils/xentools3`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools3/README.html)
                     85: and
                     86: [`sysutils/xenkernel3`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel3/README.html)
                     87: for Xen 3.1,
                     88: [`sysutils/xentools33`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools33/README.html)
                     89: and
                     90: [`sysutils/xenkernel33`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel33/README.html)
                     91: for Xen 3.3,
                     92: [`sysutils/xentools41`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools41/README.html)
                     93: and
                     94: [`sysutils/xenkernel41`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel41/README.html)
                     95: for Xen 4.1. or
                     96: [`sysutils/xentools42`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools42/README.html)
                     97: and
                     98: [`sysutils/xenkernel42`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xenkernel42/README.html)
                     99: for Xen 4.2. You'll also need
                    100: [`sysutils/grub`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/grub/README.html)
                    101: if you plan do use the grub boot loader. If using Xen 3.1, you may also
                    102: want to install
                    103: [`sysutils/xentools3-hvm`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools3-hvm/README.html)
                    104: which contains the utilities to run unmodified guests OSes using the
                    105: *HVM* support (for later versions this is included in
                    106: [`sysutils/xentools`{.filename}](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/xentools/README.html)).
                    107: Note that your CPU needs to support this. Intel CPUs must have the 'VT'
                    108: instruction, AMD CPUs the 'SVM' instruction. You can easily find out if
                    109: your CPU support HVM by using NetBSD's cpuctl command:
                    110: 
1.2     ! mspo      111: ```
1.1       mspo      112: # cpuctl identify 0
                    113: cpu0: Intel Core 2 (Merom) (686-class), id 0x6f6
                    114: cpu0: features 0xbfebfbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR>
                    115: cpu0: features 0xbfebfbff<PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX>
                    116: cpu0: features 0xbfebfbff<FXSR,SSE,SSE2,SS,HTT,TM,SBF>
                    117: cpu0: features2 0x4e33d<SSE3,DTES64,MONITOR,DS-CPL,VMX,TM2,SSSE3,CX16,xTPR,PDCM,DCA>
                    118: cpu0: features3 0x20100800<SYSCALL/SYSRET,XD,EM64T>
                    119: cpu0: "Intel(R) Xeon(R) CPU            5130  @ 2.00GHz"
                    120: cpu0: I-cache 32KB 64B/line 8-way, D-cache 32KB 64B/line 8-way
                    121: cpu0: L2 cache 4MB 64B/line 16-way
                    122: cpu0: ITLB 128 4KB entries 4-way
                    123: cpu0: DTLB 256 4KB entries 4-way, 32 4MB entries 4-way
                    124: cpu0: Initial APIC ID 0
                    125: cpu0: Cluster/Package ID 0
                    126: cpu0: Core ID 0
                    127: cpu0: family 06 model 0f extfamily 00 extmodel 00
1.2     ! mspo      128: ```
1.1       mspo      129: 
                    130: Depending on your CPU, the feature you are looking for is called HVM,
                    131: SVM or VMX.
                    132: 
                    133: Next you need to copy the selected Xen kernel itself. pkgsrc installed
                    134: them under `/usr/pkg/xen*-kernel/`{.filename}. The file you're looking
                    135: for is `xen.gz`{.filename}. Copy it to your root file system.
                    136: `xen-debug.gz`{.filename} is a kernel with more consistency checks and
                    137: more details printed on the serial console. It is useful for debugging
                    138: crashing guests if you use a serial console. It is not useful with a VGA
                    139: console.
                    140: 
                    141: You'll then need a NetBSD/Xen kernel for *domain0* on your root file
                    142: system. The XEN3PAE\_DOM0 kernel or XEN3\_DOM0 provided as part of the
                    143: i386 or amd64 binaries is suitable for this, but you may want to
                    144: customize it. Keep your native kernel around, as it can be useful for
                    145: recovery. *Note:* the *domain0* kernel must support KERNFS and
                    146: `/kern`{.filename} must be mounted because *xend* needs access to
                    147: `/kern/xen/privcmd`{.filename}.
                    148: 
                    149: Next you need to get a bootloader to load the `xen.gz`{.filename}
                    150: kernel, and the NetBSD *domain0* kernel as a module. This can be
                    151: **grub** or NetBSD's boot loader. Below is a detailled example for grub,
                    152: see the boot.cfg(5) manual page for an example using the latter.
                    153: 
                    154: This is also where you'll specify the memory allocated to *domain0*, the
                    155: console to use, etc ...
                    156: 
                    157: Here is a commented `/grub/menu.lst`{.filename} file:
                    158: 
                    159: ~~~ {.programlisting}
                    160: #Grub config file for NetBSD/xen. Copy as /grub/menu.lst and run
                    161: # grub-install /dev/rwd0d (assuming your boot device is wd0).
                    162: #
                    163: # The default entry to load will be the first one
                    164: default=0
                    165: 
                    166: # boot the default entry after 10s if the user didn't hit keyboard
                    167: timeout=10
                    168: 
                    169: # Configure serial port to use as console. Ignore if you'll use VGA only
                    170: serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
                    171: 
                    172: # Let the user select which console to use (serial or VGA), default
                    173: # to serial after 10s
                    174: terminal --timeout=10 serial console
                    175: 
                    176: # An entry for NetBSD/xen, using /netbsd as the domain0 kernel, and serial
                    177: # console. Domain0 will have 64MB RAM allocated.
                    178: # Assume NetBSD is installed in the first MBR partition.
                    179: title Xen 3 / NetBSD (hda0, serial)
                    180:   root(hd0,0)
                    181:   kernel (hd0,a)/xen.gz dom0_mem=65536 com1=115200,8n1
                    182:   module (hd0,a)/netbsd bootdev=wd0a ro console=ttyS0
                    183: 
                    184: # Same as above, but using VGA console
                    185: # We can use console=tty0 (Linux syntax) or console=pc (NetBSD syntax)
                    186: title Xen 3 / NetBSD (hda0, vga)
                    187:   root(hd0,0)
                    188:   kernel (hd0,a)/xen.gz dom0_mem=65536
                    189:   module (hd0,a)/netbsd bootdev=wd0a ro console=tty0
                    190: 
                    191: # NetBSD/xen using a backup domain0 kernel (in case you installed a
                    192: # nonworking kernel as /netbsd
                    193: title Xen 3 / NetBSD (hda0, backup, serial)
                    194:   root(hd0,0)
                    195:   kernel (hd0,a)/xen.gz dom0_mem=65536 com1=115200,8n1
                    196:   module (hd0,a)/netbsd.backup bootdev=wd0a ro console=ttyS0
                    197: title Xen 3 / NetBSD (hda0, backup, VGA)
                    198:   root(hd0,0)
                    199:   kernel (hd0,a)/xen.gz dom0_mem=65536
                    200:   module (hd0,a)/netbsd.backup bootdev=wd0a ro console=tty0
                    201: 
                    202: #Load a regular NetBSD/i386 kernel. Can be useful if you end up with a
                    203: #nonworking /xen.gz
                    204: title NetBSD 5.1
                    205:   root (hd0,a)
                    206:   kernel --type=netbsd /netbsd-GENERIC
                    207: 
                    208: #Load the NetBSD bootloader, letting it load the NetBSD/i386 kernel.
                    209: #May be better than the above, as grub can't pass all required infos
                    210: #to the NetBSD/i386 kernel (e.g. console, root device, ...)
                    211: title NetBSD chain
                    212:   root        (hd0,0)
                    213:   chainloader +1
                    214: 
                    215: ## end of grub config file.
                    216:       
                    217: ~~~
                    218: 
                    219: Install grub with the following command:
                    220: 
                    221: ~~~ {.programlisting}
                    222: # grub --no-floppy
                    223: 
                    224: grub> root (hd0,a)
                    225:  Filesystem type is ffs, partition type 0xa9
                    226: 
                    227: grub> setup (hd0)
                    228:  Checking if "/boot/grub/stage1" exists... no
                    229:  Checking if "/grub/stage1" exists... yes
                    230:  Checking if "/grub/stage2" exists... yes
                    231:  Checking if "/grub/ffs_stage1_5" exists... yes
                    232:  Running "embed /grub/ffs_stage1_5 (hd0)"...  14 sectors are embedded.
                    233: succeeded
                    234:  Running "install /grub/stage1 (hd0) (hd0)1+14 p (hd0,0,a)/grub/stage2 /grub/menu.lst"...
                    235:  succeeded
                    236: Done.
                    237:       
                    238: ~~~
                    239: 
                    240: * * * * *
                    241: 
                    242: ### Creating an unprivileged NetBSD domain (DomU)
                    243: 
                    244: Once you have *domain0* running, you need to start the xen tool daemon
                    245: (**/usr/pkg/share/examples/rc.d/xend start**) and the xen backend daemon
                    246: (**/usr/pkg/share/examples/rc.d/xenbackendd start** for Xen3\*,
                    247: **/usr/pkg/share/examples/rc.d/xencommons start** for Xen4.\*). Make
                    248: sure that `/dev/xencons`{.filename} and `/dev/xenevt`{.filename} exist
                    249: before starting **xend**. You can create them with this command:
                    250: 
                    251: ~~~ {.programlisting}
                    252: # cd /dev && sh MAKEDEV xen
                    253: ~~~
                    254: 
                    255: xend will write logs to `/var/log/xend.log`{.filename} and
                    256: `/var/log/xend-debug.log`{.filename}. You can then control xen with the
                    257: xm tool. 'xm list' will show something like:
                    258: 
                    259: ~~~ {.programlisting}
                    260: # xm list
                    261: Name              Id  Mem(MB)  CPU  State  Time(s)  Console
                    262: Domain-0           0       64    0  r----     58.1
                    263: ~~~
                    264: 
                    265: 'xm create' allows you to create a new domain. It uses a config file in
                    266: PKG\_SYSCONFDIR for its parameters. By default, this file will be in
                    267: `/usr/pkg/etc/xen/`{.filename}. On creation, a kernel has to be
                    268: specified, which will be executed in the new domain (this kernel is in
                    269: the *domain0* file system, not on the new domain virtual disk; but
                    270: please note, you should install the same kernel into *domainU* as
                    271: `/netbsd`{.filename} in order to make your system tools, like
                    272: [savecore(8)](http://netbsd.gw.com/cgi-bin/man-cgi?savecore+8+NetBSD-6.0+i386),
                    273: work). A suitable kernel is provided as part of the i386 and amd64
                    274: binary sets: XEN3\_DOMU.
                    275: 
                    276: Here is an /usr/pkg/etc/xen/nbsd example config file:
                    277: 
                    278: ~~~ {.programlisting}
                    279: #  -*- mode: python; -*-
                    280: #============================================================================
                    281: # Python defaults setup for 'xm create'.
                    282: # Edit this file to reflect the configuration of your system.
                    283: #============================================================================
                    284: 
                    285: #----------------------------------------------------------------------------
                    286: # Kernel image file. This kernel will be loaded in the new domain.
                    287: kernel = "/home/bouyer/netbsd-XEN3_DOMU"
                    288: #kernel = "/home/bouyer/netbsd-INSTALL_XEN3_DOMU"
                    289: 
                    290: # Memory allocation (in megabytes) for the new domain.
                    291: memory = 128
                    292: 
                    293: # A handy name for your new domain. This will appear in 'xm list',
                    294: # and you can use this as parameters for xm in place of the domain
                    295: # number. All domains must have different names.
                    296: #
                    297: name = "nbsd"
                    298: 
                    299: # The number of virtual CPUs this domain has.
                    300: #
                    301: vcpus = 1
                    302: 
                    303: #----------------------------------------------------------------------------
                    304: # Define network interfaces for the new domain.
                    305: 
                    306: # Number of network interfaces (must be at least 1). Default is 1.
                    307: nics = 1
                    308: 
                    309: # Define MAC and/or bridge for the network interfaces.
                    310: #
                    311: # The MAC address specified in ``mac'' is the one used for the interface
                    312: # in the new domain. The interface in domain0 will use this address XOR'd
                    313: # with 00:00:00:01:00:00 (i.e. aa:00:00:51:02:f0 in our example). Random
                    314: # MACs are assigned if not given.
                    315: #
                    316: # ``bridge'' is a required parameter, which will be passed to the
                    317: # vif-script called by xend(8) when a new domain is created to configure
                    318: # the new xvif interface in domain0.
                    319: #
                    320: # In this example, the xvif is added to bridge0, which should have been
                    321: # set up prior to the new domain being created -- either in the
                    322: # ``network'' script or using a /etc/ifconfig.bridge0 file.
                    323: #
                    324: vif = [ 'mac=aa:00:00:50:02:f0, bridge=bridge0' ]
                    325: 
                    326: #----------------------------------------------------------------------------
                    327: # Define the disk devices you want the domain to have access to, and
                    328: # what you want them accessible as.
                    329: #
                    330: # Each disk entry is of the form:
                    331: #
                    332: #       phy:DEV,VDEV,MODE
                    333: #
                    334: # where DEV is the device, VDEV is the device name the domain will see,
                    335: # and MODE is r for read-only, w for read-write.  You can also create
                    336: # file-backed domains using disk entries of the form:
                    337: #
                    338: #       file:PATH,VDEV,MODE
                    339: #
                    340: # where PATH is the path to the file used as the virtual disk, and VDEV
                    341: # and MODE have the same meaning as for ``phy'' devices.
                    342: #
                    343: # VDEV doesn't really matter for a NetBSD guest OS (it's just used as an index),
                    344: # but it does for Linux.
                    345: # Worse, the device has to exist in /dev/ of domain0, because xm will
                    346: # try to stat() it. This means that in order to load a Linux guest OS
                    347: # from a NetBSD domain0, you'll have to create /dev/hda1, /dev/hda2, ...
                    348: # on domain0, with the major/minor from Linux :(
                    349: # Alternatively it's possible to specify the device number in hex,
                    350: # e.g. 0x301 for /dev/hda1, 0x302 for /dev/hda2, etc ...
                    351: 
                    352: disk = [ 'phy:/dev/wd0e,0x1,w' ]
                    353: #disk = [ 'file:/var/xen/nbsd-disk,0x01,w' ]
                    354: #disk = [ 'file:/var/xen/nbsd-disk,0x301,w' ]
                    355: 
                    356: #----------------------------------------------------------------------------
                    357: # Set the kernel command line for the new domain.
                    358: 
                    359: # Set root device. This one does matter for NetBSD
                    360: root = "xbd0"
                    361: # extra parameters passed to the kernel
                    362: # this is where you can set boot flags like -s, -a, etc ...
                    363: #extra = ""
                    364: 
                    365: #----------------------------------------------------------------------------
                    366: # Set according to whether you want the domain restarted when it exits.
                    367: # The default is False.
                    368: #autorestart = True
                    369: 
                    370: # end of nbsd config file ====================================================
                    371: ~~~
                    372: 
                    373: When a new domain is created, xen calls the
                    374: `/usr/pkg/etc/xen/vif-bridge`{.filename} script for each virtual network
                    375: interface created in *domain0*. This can be used to automatically
                    376: configure the xvif?.? interfaces in *domain0*. In our example, these
                    377: will be bridged with the bridge0 device in *domain0*, but the bridge has
                    378: to exist first. To do this, create the file
                    379: `/etc/ifconfig.bridge0`{.filename} and make it look like this:
                    380: 
                    381: ~~~ {.programlisting}
                    382: create
                    383: !brconfig $int add ex0 up
                    384: ~~~
                    385: 
                    386: (replace `ex0`{.literal} with the name of your physical interface). Then
                    387: bridge0 will be created on boot. See the
                    388: [bridge(4)](http://netbsd.gw.com/cgi-bin/man-cgi?bridge+4+NetBSD-6.0+i386)
                    389: man page for details.
                    390: 
                    391: So, here is a suitable `/usr/pkg/etc/xen/vif-bridge`{.filename} for
                    392: xvif?.? (a working vif-bridge is also provided with xentools20)
                    393: configuring:
                    394: 
                    395: 
                    396: ~~~ {.programlisting}
                    397: #!/bin/sh
                    398: #============================================================================
1.2     ! mspo      399: # $NetBSD: howto.mdwn,v 1.1 2013/10/31 12:20:57 mspo Exp $
1.1       mspo      400: #
                    401: # /usr/pkg/etc/xen/vif-bridge
                    402: #
                    403: # Script for configuring a vif in bridged mode with a dom0 interface.
                    404: # The xend(8) daemon calls a vif script when bringing a vif up or down.
                    405: # The script name to use is defined in /usr/pkg/etc/xen/xend-config.sxp
                    406: # in the ``vif-script'' field.
                    407: #
                    408: # Usage: vif-bridge up|down [var=value ...]
                    409: #
                    410: # Actions:
                    411: #    up         Adds the vif interface to the bridge.
                    412: #    down       Removes the vif interface from the bridge.
                    413: #
                    414: # Variables:
                    415: #    domain     name of the domain the interface is on (required).
                    416: #    vifq       vif interface name (required).
                    417: #    mac        vif MAC address (required).
                    418: #    bridge     bridge to add the vif to (required).
                    419: #
                    420: # Example invocation:
                    421: #
                    422: # vif-bridge up domain=VM1 vif=xvif1.0 mac="ee:14:01:d0:ec:af" bridge=bridge0
                    423: #
                    424: #============================================================================
                    425: 
                    426: # Exit if anything goes wrong
                    427: set -e
                    428: 
                    429: echo "vif-bridge $*"
                    430: 
                    431: # Operation name.
                    432: OP=$1; shift
                    433: 
                    434: # Pull variables in args into environment
                    435: for arg ; do export "${arg}" ; done
                    436: 
                    437: # Required parameters. Fail if not set.
                    438: domain=${domain:?}
                    439: vif=${vif:?}
                    440: mac=${mac:?}
                    441: bridge=${bridge:?}
                    442: 
                    443: # Optional parameters. Set defaults.
                    444: ip=${ip:-''}   # default to null (do nothing)
                    445: 
                    446: # Are we going up or down?
                    447: case $OP in
                    448: up)     brcmd='add' ;;
                    449: down)   brcmd='delete' ;;
                    450: *)
                    451:         echo 'Invalid command: ' $OP
                    452:         echo 'Valid commands are: up, down'
                    453:         exit 1
                    454:         ;;
                    455: esac
                    456: 
                    457: # Don't do anything if the bridge is "null".
                    458: if [ "${bridge}" = "null" ] ; then
                    459:         exit
                    460: fi
                    461: 
                    462: # Don't do anything if the bridge doesn't exist.
                    463: if ! ifconfig -l | grep "${bridge}" >/dev/null; then
                    464:         exit
                    465: fi
                    466: 
                    467: # Add/remove vif to/from bridge.
                    468: ifconfig x${vif} $OP
                    469: brconfig ${bridge} ${brcmd} x${vif}
                    470: ~~~
                    471: 
                    472: Now, running
                    473: 
                    474: ~~~ {.programlisting}
                    475: xm create -c /usr/pkg/etc/xen/nbsd
                    476: ~~~
                    477: 
                    478: should create a domain and load a NetBSD kernel in it. (Note:
                    479: `-c`{.code} causes xm to connect to the domain's console once created.)
                    480: The kernel will try to find its root file system on xbd0 (i.e., wd0e)
                    481: which hasn't been created yet. wd0e will be seen as a disk device in the
                    482: new domain, so it will be 'sub-partitioned'. We could attach a ccd to
                    483: wd0e in *domain0* and partition it, newfs and extract the NetBSD/i386 or
                    484: amd64 tarballs there, but there's an easier way: load the
                    485: `netbsd-INSTALL_XEN3_DOMU`{.filename} kernel provided in the NetBSD
                    486: binary sets. Like other install kernels, it contains a ramdisk with
                    487: sysinst, so you can install NetBSD using sysinst on your new domain.
                    488: 
                    489: If you want to install NetBSD/Xen with a CDROM image, the following line
                    490: should be used in the `/usr/pkg/etc/xen/nbsd`{.filename} file:
                    491: 
                    492: ~~~ {.programlisting}
                    493: disk = [ 'phy:/dev/wd0e,0x1,w', 'phy:/dev/cd0a,0x2,r' ]
                    494: ~~~
                    495: 
                    496: After booting the domain, the option to install via CDROM may be
                    497: selected. The CDROM device should be changed to **xbd1d**.
                    498: 
                    499: Once done installing, **halt -p** the new domain (don't reboot or halt,
                    500: it would reload the INSTALL\_XEN3\_DOMU kernel even if you changed the
                    501: config file), switch the config file back to the XEN3\_DOMU kernel, and
                    502: start the new domain again. Now it should be able to use **root on
                    503: xbd0a** and you should have a second, functional NetBSD system on your
                    504: xen installation.
                    505: 
                    506: When the new domain is booting you'll see some warnings about *wscons*
                    507: and the pseudo-terminals. These can be fixed by editing the files
                    508: `/etc/ttys`{.filename} and `/etc/wscons.conf`{.filename}. You must
                    509: disable all terminals in `/etc/ttys`{.filename}, except *console*, like
                    510: this:
                    511: 
                    512: ~~~ {.programlisting}
                    513: console "/usr/libexec/getty Pc"         vt100   on secure
                    514: ttyE0   "/usr/libexec/getty Pc"         vt220   off secure
                    515: ttyE1   "/usr/libexec/getty Pc"         vt220   off secure
                    516: ttyE2   "/usr/libexec/getty Pc"         vt220   off secure
                    517: ttyE3   "/usr/libexec/getty Pc"         vt220   off secure
                    518: ~~~
                    519: 
                    520: Finally, all screens must be commented out from
                    521: `/etc/wscons.conf`{.filename}.
                    522: 
                    523: It is also desirable to add
                    524: 
                    525: ~~~ {.programlisting}
                    526: powerd=YES
                    527: ~~~
                    528: 
                    529: in rc.conf. This way, the domain will be properly shut down if **xm
                    530: shutdown -R** or **xm shutdown -H** is used on the domain0.
                    531: 
                    532: Your domain should be now ready to work, enjoy.
                    533: 
                    534: * * * * *
                    535: 
                    536: ### Creating an unprivileged Linux domain (DomU)
                    537: 
                    538: Creating unprivileged Linux domains isn't much different from
                    539: unprivileged NetBSD domains, but there are some details to know.
                    540: 
                    541: First, the second parameter passed to the disk declaration (the '0x1' in
                    542: the example below)
                    543: 
                    544: ~~~ {.programlisting}
                    545: disk = [ 'phy:/dev/wd0e,0x1,w' ]
                    546: ~~~
                    547: 
                    548: does matter to Linux. It wants a Linux device number here (e.g. 0x300
                    549: for hda). Linux builds device numbers as: (major \<\< 8 + minor). So,
                    550: hda1 which has major 3 and minor 1 on a Linux system will have device
                    551: number 0x301. Alternatively, devices names can be used (hda, hdb, ...)
                    552: as xentools has a table to map these names to devices numbers. To export
                    553: a partition to a Linux guest we can use:
                    554: 
                    555: ~~~ {.programlisting}
                    556: disk = [ 'phy:/dev/wd0e,0x300,w' ]
                    557: root = "/dev/hda1 ro"
                    558: ~~~
                    559: 
                    560: and it will appear as /dev/hda on the Linux system, and be used as root
                    561: partition.
                    562: 
                    563: To install the Linux system on the partition to be exported to the guest
                    564: domain, the following method can be used: install sysutils/e2fsprogs
                    565: from pkgsrc. Use mke2fs to format the partition that will be the root
                    566: partition of your Linux domain, and mount it. Then copy the files from a
                    567: working Linux system, make adjustments in `/etc`{.filename} (fstab,
                    568: network config). It should also be possible to extract binary packages
                    569: such as .rpm or .deb directly to the mounted partition using the
                    570: appropriate tool, possibly running under NetBSD's Linux emulation. Once
                    571: the filesystem has been populated, umount it. If desirable, the
                    572: filesystem can be converted to ext3 using tune2fs -j. It should now be
                    573: possible to boot the Linux guest domain, using one of the
                    574: vmlinuz-\*-xenU kernels available in the Xen binary distribution.
                    575: 
                    576: To get the linux console right, you need to add:
                    577: 
                    578: ~~~ {.programlisting}
                    579: extra = "xencons=tty1"
                    580: ~~~
                    581: 
                    582: to your configuration since not all linux distributions auto-attach a
                    583: tty to the xen console.
                    584: 
                    585: * * * * *
                    586: 
                    587: ### Creating an unprivileged Solaris domain (DomU)
                    588: 
                    589: Download an Opensolaris [release](http://opensolaris.org/os/downloads/)
                    590: or [development snapshot](http://genunix.org/) DVD image. Attach the DVD
                    591: image to a
                    592: [vnd(4)](http://netbsd.gw.com/cgi-bin/man-cgi?vnd+4+NetBSD-6.0+i386)
                    593: device. Copy the kernel and ramdisk filesystem image to your dom0
                    594: filesystem.
                    595: 
                    596: ~~~ {.programlisting}
                    597: dom0# mkdir /root/solaris
                    598: dom0# vnconfig vnd0 osol-1002-124-x86.iso
                    599: dom0# mount /dev/vnd0a /mnt
                    600: 
                    601: ## for a 64-bit guest
                    602: dom0# cp /mnt/boot/amd64/x86.microroot /root/solaris
                    603: dom0# cp /mnt/platform/i86xpv/kernel/amd64/unix /root/solaris
                    604: 
                    605: ## for a 32-bit guest
                    606: dom0# cp /mnt/boot/x86.microroot /root/solaris
                    607: dom0# cp /mnt/platform/i86xpv/kernel/unix /root/solaris
                    608: 
                    609: dom0# umount /mnt
                    610:       
                    611: ~~~
                    612: 
                    613: Keep the
                    614: [vnd(4)](http://netbsd.gw.com/cgi-bin/man-cgi?vnd+4+NetBSD-6.0+i386)
                    615: configured. For some reason the boot process stalls unless the DVD image
                    616: is attached to the guest as a "phy" device. Create an initial
                    617: configuration file with the following contents. Substitute */dev/wd0k*
                    618: with an empty partition at least 8 GB large.
                    619: 
                    620: ~~~ {.programlisting}
                    621: memory = 640
                    622: name = 'solaris'
                    623: disk = [ 'phy:/dev/wd0k,0,w' ]
                    624: disk += [ 'phy:/dev/vnd0d,6:cdrom,r' ]
                    625: vif = [ 'bridge=bridge0' ]
                    626: kernel = '/root/solaris/unix'
                    627: ramdisk = '/root/solaris/x86.microroot'
                    628: # for a 64-bit guest
                    629: extra = '/platform/i86xpv/kernel/amd64/unix - nowin -B install_media=cdrom'
                    630: # for a 32-bit guest
                    631: #extra = '/platform/i86xpv/kernel/unix - nowin -B install_media=cdrom'
                    632:       
                    633: ~~~
                    634: 
                    635: Start the guest.
                    636: 
                    637: ~~~ {.programlisting}
                    638: dom0# xm create -c solaris.cfg
                    639: Started domain solaris
                    640:                       v3.3.2 chgset 'unavailable'
                    641: SunOS Release 5.11 Version snv_124 64-bit
                    642: Copyright 1983-2009 Sun Microsystems, Inc.  All rights reserved.
                    643: Use is subject to license terms.
                    644: Hostname: opensolaris
                    645: Remounting root read/write
                    646: Probing for device nodes ...
                    647: WARNING: emlxs: ddi_modopen drv/fct failed: err 2
                    648: Preparing live image for use
                    649: Done mounting Live image
                    650:       
                    651: ~~~
                    652: 
                    653: Make sure the network is configured. Note that it can take a minute for
                    654: the xnf0 interface to appear.
                    655: 
                    656: ~~~ {.programlisting}
                    657: opensolaris console login: jack
                    658: Password: jack
                    659: Sun Microsystems Inc.   SunOS 5.11      snv_124 November 2008
                    660: jack@opensolaris:~$ pfexec sh
                    661: sh-3.2# ifconfig -a
                    662: sh-3.2# exit
                    663:       
                    664: ~~~
                    665: 
                    666: Set a password for VNC and start the VNC server which provides the X11
                    667: display where the installation program runs.
                    668: 
                    669: ~~~ {.programlisting}
                    670: jack@opensolaris:~$ vncpasswd
                    671: Password: solaris
                    672: Verify: solaris
                    673: jack@opensolaris:~$ cp .Xclients .vnc/xstartup
                    674: jack@opensolaris:~$ vncserver :1
                    675:       
                    676: ~~~
                    677: 
                    678: From a remote machine connect to the VNC server. Use **ifconfig xnf0**
                    679: on the guest to find the correct IP address to use.
                    680: 
                    681: ~~~ {.programlisting}
                    682: remote$ vncviewer 172.18.2.99:1
                    683:       
                    684: ~~~
                    685: 
                    686: It is also possible to launch the installation on a remote X11 display.
                    687: 
                    688: ~~~ {.programlisting}
                    689: jack@opensolaris:~$ export DISPLAY=172.18.1.1:0
                    690: jack@opensolaris:~$ pfexec gui-install
                    691:        
                    692: ~~~
                    693: 
                    694: After the GUI installation is complete you will be asked to reboot.
                    695: Before that you need to determine the ZFS ID for the new boot filesystem
                    696: and update the configuration file accordingly. Return to the guest
                    697: console.
                    698: 
                    699: ~~~ {.programlisting}
                    700: jack@opensolaris:~$ pfexec zdb -vvv rpool | grep bootfs
                    701:                 bootfs = 43
                    702: ^C
                    703: jack@opensolaris:~$
                    704:        
                    705: ~~~
                    706: 
                    707: The final configuration file should look like this. Note in particular
                    708: the last line.
                    709: 
                    710: 
                    711: ~~~ {.programlisting}
                    712: memory = 640
                    713: name = 'solaris'
                    714: disk = [ 'phy:/dev/wd0k,0,w' ]
                    715: vif = [ 'bridge=bridge0' ]
                    716: kernel = '/root/solaris/unix'
                    717: ramdisk = '/root/solaris/x86.microroot'
                    718: extra = '/platform/i86xpv/kernel/amd64/unix -B zfs-bootfs=rpool/43,bootpath="/xpvd/xdf@0:a"'
                    719:        
                    720: ~~~
                    721: 
                    722: Restart the guest to verify it works correctly.
                    723: 
                    724: ~~~ {.programlisting}
                    725: dom0# xm destroy solaris
                    726: dom0# xm create -c solaris.cfg
                    727: Using config file "./solaris.cfg".
                    728: v3.3.2 chgset 'unavailable'
                    729: Started domain solaris
                    730: SunOS Release 5.11 Version snv_124 64-bit
                    731: Copyright 1983-2009 Sun Microsystems, Inc.  All rights reserved.
                    732: Use is subject to license terms.
                    733: WARNING: emlxs: ddi_modopen drv/fct failed: err 2
                    734: Hostname: osol
                    735: Configuring devices.
                    736: Loading smf(5) service descriptions: 160/160
                    737: svccfg import warnings. See /var/svc/log/system-manifest-import:default.log .
                    738: Reading ZFS config: done.
                    739: Mounting ZFS filesystems: (6/6)
                    740: Creating new rsa public/private host key pair
                    741: Creating new dsa public/private host key pair
                    742: 
                    743: osol console login:
                    744:        
                    745: ~~~
                    746: 
                    747: Using PCI devices in guest domains
                    748: ----------------------------------
                    749: 
                    750: The domain0 can give other domains access to selected PCI devices. This
                    751: can allow, for example, a non-privileged domain to have access to a
                    752: physical network interface or disk controller. However, keep in mind
                    753: that giving a domain access to a PCI device most likely will give the
                    754: domain read/write access to the whole physical memory, as PCs don't have
                    755: an IOMMU to restrict memory access to DMA-capable device. Also, it's not
                    756: possible to export ISA devices to non-domain0 domains (which means that
                    757: the primary VGA adapter can't be exported. A guest domain trying to
                    758: access the VGA registers will panic).
                    759: 
                    760: This functionality is only available in NetBSD-5.1 (and later) domain0
                    761: and domU. If the domain0 is NetBSD, it has to be running Xen 3.1, as
                    762: support has not been ported to later versions at this time.
                    763: 
                    764: For a PCI device to be exported to a domU, is has to be attached to the
                    765: `pciback`{.literal} driver in domain0. Devices passed to the domain0 via
                    766: the pciback.hide boot parameter will attach to `pciback`{.literal}
                    767: instead of the usual driver. The list of devices is specified as
                    768: `(bus:dev.func)`{.literal}, where bus and dev are 2-digit hexadecimal
                    769: numbers, and func a single-digit number:
                    770: 
                    771: ~~~ {.programlisting}
                    772: pciback.hide=(00:0a.0)(00:06.0)
                    773: ~~~
                    774: 
                    775: pciback devices should show up in the domain0's boot messages, and the
                    776: devices should be listed in the `/kern/xen/pci`{.filename} directory.
                    777: 
                    778: PCI devices to be exported to a domU are listed in the `pci`{.literal}
                    779: array of the domU's config file, with the format
                    780: `'0000:bus:dev.func'`{.literal}
                    781: 
                    782: ~~~ {.programlisting}
                    783: pci = [ '0000:00:06.0', '0000:00:0a.0' ]
                    784: ~~~
                    785: 
                    786: In the domU an `xpci`{.literal} device will show up, to which one or
                    787: more pci busses will attach. Then the PCI drivers will attach to PCI
                    788: busses as usual. Note that the default NetBSD DOMU kernels do not have
                    789: `xpci`{.literal} or any PCI drivers built in by default; you have to
                    790: build your own kernel to use PCI devices in a domU. Here's a kernel
                    791: config example:
                    792: 
                    793: ~~~ {.programlisting}
                    794: include         "arch/i386/conf/XEN3_DOMU"
                    795: #include         "arch/i386/conf/XENU"           # in NetBSD 3.0
                    796: 
                    797: # Add support for PCI busses to the XEN3_DOMU kernel
                    798: xpci* at xenbus ?
                    799: pci* at xpci ?
                    800: 
                    801: # Now add PCI and related devices to be used by this domain
                    802: # USB Controller and Devices
                    803: 
                    804: # PCI USB controllers
                    805: uhci*   at pci? dev ? function ?        # Universal Host Controller (Intel)
                    806: 
                    807: # USB bus support
                    808: usb*    at uhci?
                    809: 
                    810: # USB Hubs
                    811: uhub*   at usb?
                    812: uhub*   at uhub? port ? configuration ? interface ?
                    813: 
                    814: # USB Mass Storage
                    815: umass*  at uhub? port ? configuration ? interface ?
                    816: wd*     at umass?
                    817: # SCSI controllers
                    818: ahc*    at pci? dev ? function ?        # Adaptec [23]94x, aic78x0 SCSI
                    819: 
                    820: # SCSI bus support (for both ahc and umass)
                    821: scsibus* at scsi?
                    822: 
                    823: # SCSI devices
                    824: sd*     at scsibus? target ? lun ?      # SCSI disk drives
                    825: cd*     at scsibus? target ? lun ?      # SCSI CD-ROM drives
                    826: ~~~
                    827: 
                    828: Links and further information
                    829: -----------------------------
                    830: 
                    831: -   The HowTo on [Installing into
                    832:     RAID-1](http://mail-index.NetBSD.org/port-xen/2006/03/01/0010.html)
                    833:     gives some hints on using Xen (grub) with NetBSD's RAIDframe
                    834: -   Harold Gutch wrote documentation on [setting up a Linux DomU with a
                    835:     NetBSD Dom0](http://www.gutch.de/NetBSD/docs/xen.html)
                    836: -   An example of how to use NetBSD's native bootloader to load
                    837:     NetBSD/Xen instead of Grub can be found in the i386/amd64
                    838:     [boot(8)](http://netbsd.gw.com/cgi-bin/man-cgi?boot+8+NetBSD-6.0+i386)
                    839:     and
                    840:     [boot.cfg(5)](http://netbsd.gw.com/cgi-bin/man-cgi?boot.cfg+5+NetBSD-6.0+i386)
                    841:     manpages.
                    842: 
                    843: 

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb