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

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

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