Introduction

This HOWTO is meant to be a manual for easy setup for kernel development/test environment which can be used by students during Google Summer of Code.

The most convenient way how to develop and test operating system is do it in virtual machine one of the most known/used emulators is a QEMU.

System Build/Installation

We always can setup our environment in two ways First I would like to describe automatic way where user can use wonderful application anita which can do unattended NetBSD installations. Later we will show how to do this installation manually.

Automatic Environment Setup

Prerequisites

You need the follwoing prerequisites from pkgsrc:

System build/installation

Build a full -current/i386 release with debug symbols using build.sh. Use something like "build.sh -V COPTS=-g release"; you will probably also need other options to set directories, architectures, etc, but those are outside the scope of this document. Do not specify "-V MKDEBUG=YES", because as of 2013-01-17, that puts the debug symbols in a separate debug.tgz file set which sysinst is currently unable to install.

Install the system, including the source sets:

 $ anita --workdir work --disk-size 4G --memory-size 256M \
     --sets kern-GENERIC,modules,base,etc,comp,games,man,misc,tests,text,syssrc,src,sharesrc,gnusrc \
     install /path/to/release/i386/

replacing /path/to/release/i386/ with the actual release/i386 directory of the release you just built.

Manual Environment Setup

If you did the installation using anita, skip to "Booting VMs" below. Otherwise...

Creating the raw disk image

To start our VM, we need some disk space to provide an emulated hard drive. For QEMU, by default, this is done through raw disk images. Therefore, the first step will be the creation of a disk image file. Here, we create a 2GB file, filled with zeros:

$ dd if=/dev/zero of=netbsd-guest.img bs=1m count=2000

/!\ if you want to mount the file image from within the host later through vnconfig(8) , it is recommended to use dd(1) and not the qemu-img tool, as vnd(4) does not support sparse disk image yet.

Now that the disk image file is ready, we will need to install our system inside.

Preparing the MBR, labels, and first stage boot loader

Mount the image file as a vnd(4) device. This will allow manipulating the image file just like a regular hard disk drive:

# vnconfig -c vnd0 netbsd-guest.img

Creating MBR

Setup the MBR; it musts contain the NetBSD partition. This will be done interactively via fdisk(8) :

# fdisk -u -a -0 /dev/rvnd0
Disk: /dev/rvnd0d
[...]
Do you want to change our idea of what BIOS thinks? [n] *n*

Partition 0:

The data for partition 0 is:

sysid: [0..255 default: 169] *press enter*
start: [0..255dcyl default: 63, 0dcyl, 0MB] *press enter*
size: [0..255dcyl default: 4095937, 255dcyl, 2000MB] *press enter*
bootmenu: [] *press enter*
Do you want to change the active partition? [n] *y*
Choosing 4 will make no partition active.
active partition: [0..4 default: 0] *press enter*
Are you happy with this choice? [n] *y*
We haven't written the MBR back to disk yet.  This is your last chance.
Partition table:
0: NetBSD (sysid 169)
    start 63, size 4095937 (2000 MB, Cyls 0-254/245/55), Active
        PBR is not bootable: All bytes are identical (0x00)
1: 
2: 
3: 
Bootselector disabled.
First active partition: 0
Should we write new partition table? [n] *y*

Editing labels

Edit the labels, with disklabel(8) . The example below will create:

# disklabel -e -I /dev/rvnd0
[...]
4 partitions:
#        size    offset     fstype [fsize bsize cpg/sgs]
 a:   3047361        63     4.2BSD      0     0     0  # (Cyl.      0*-   1487)
 b:   1048576   3047424       swap                     # (Cyl.   1488 -   1999)
 d:   4096000         0     unused      0     0        # (Cyl.      0 -   1999)

Copying first stage boot loader

Lastly, we have to install the first stage boot loader, the one that will be able to read the second stage boot loader, which will reside in partition a. Use installboot(8) :

# installboot /dev/rvnd0a /usr/mdec/bootxx_ffsv2

Format and mount the filesystem

With newfs(8) , format label a in FFSv2:

# newfs -O2 /dev/rvnd0a
/dev/rvnd0a: 1488.0MB (3047360 sectors) block size 16384, fragment size 2048
    using 9 cylinder groups of 165.34MB, 10582 blks, 20544 inodes.
super-block backups (for fsck_ffs -b #) at:
160, 338784, 677408, 1016032, 1354656, 1693280, 2031904, 2370528, 2709152,

then mount(8) it:

# mkdir /tmp/netbsd-guest
# mount /dev/vnd0a /tmp/netbsd-guest

Installing the system

Booting VMs

Next, start two qemu virtual machines, one to run the kernel being debugged (the "kgdb target") and another to run gdb (the "kgdb host"). They could be on different physical macines, but in this example, they are run on the same physical machine, and the "-snapshot" qemu option is used to avoid modifying the hard disk image so that it can be shared between the host and target. First start the kgdb target, enabling qemu's built-in GDB target stub on TCP port 1234:

 $ qemu -nographic -snapshot -hda work/wd0.img -gdb tcp::1234

If you don't want everyone on the Internet to be able to debug your target, make sure incoming connections on port 1234 are blocked in your firewall.

In a second terminal window, start the kgdb host:

 $ qemu -nographic -snapshot -hda work/wd0.img --net user --net nic,model=ne2k_pci   

Log in to the kgdb host as root and set up the network:

 login: root
 # dhclient ne2

If the sources you built using build.sh were in a location other than /usr/src, set up a symlink from the place where they resided on the build system to /usr/src (which is where they now reside on the kgdb host) so that gdb can find them:

 # mkdir -p /path/to/parent/dir/of/your/sources
 # ln -s /usr/src /path/to/parent/dir/of/your/sources/src

Start gdb on the kgdb host and connect to the target:

 # gdb /netbsd
 (gdb) target remote my.host.name:1234

where my.host.name is the domain name or IP address of the physical machine running the kgdb target qemu VM.

Now you should be able to get a stack trace and start digging:

 (gdb) where

If the stack trace prints very slowly (like 30 seconds per stack frame), it's likely because you are using a version of qemu where the user-mode networking code fails to disable the Nagle algorithm. This is fixed in qemu-0.15.1nb5 in pkgsrc.

Qemu usage

There are couple useful commands to know when you are developing kernel features under the qemu.

1) Ctr-a-b will send a break to a NetBSD VM which will startup ddb kernel debugger. 2) Ctr-a-c will switch to qemu monitor where user can use commands to save/restore vm from file.

Add a comment