Annotation of wikisrc/netbsd_kernel_development_setup.mdwn, revision 1.2

1.1       wiki        1: # Introduction
                      2: This HOWTO is meant to be a manual for easy setup for kernel development/test environment which can be used by students 
                      3: during Google Summer of Code.
                      4: 
                      5: 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.
                      6: 
                      7: 
                      8: ## System Build/Installation
                      9: 
                     10: 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.
                     11: 
                     12: ### Automatic Environemnt Setup
                     13: 
                     14: #### Prerequsities
                     15: 
                     16: You need the follwoing prerequisites from pkgsrc:
                     17: 
                     18: * emulators/qemu >= 0.15.1nb5
                     19: * misc/py-anita
                     20: 
                     21: #### System build/instalaltion
                     22: 
                     23: Build a full -current/i386 release with debug symbols using build.sh.
1.2     ! wiki       24: Use something like "build.sh -V COPTS=-g release"; you
1.1       wiki       25: will probably also need other options to set directories,
                     26: architectures, etc, but those are outside the scope of this document.
1.2     ! wiki       27: Do not specify "-V MKDEBUG=YES", because as of 2013-01-17,
        !            28: that puts the debug symbols in a separate debug.tgz file set which
        !            29: sysinst is currently unable to install.
1.1       wiki       30: 
                     31: Debug builds sometimes fail with error messages indicating that
                     32: shared libraries are missing or corrupted.  This is PR 44046.
                     33: If this happens to you, retry to build, or better yet, fix the bug :)
                     34: 
                     35: Install the system, including the source sets:
                     36: 
                     37: [[!template  id=programlisting text="""
                     38:  $ anita --workdir work --disk-size 4G --memory-size 256M \
                     39:      --sets kern-GENERIC,modules,base,etc,comp,games,man,misc,tests,text,syssrc,src,sharesrc,gnusrc \
                     40:      install /path/to/release/i386/
                     41: """]]
                     42: 
                     43: replacing /path/to/release/i386/ with the actual release/i386
                     44: directory of the release you just built.
                     45: 
                     46: ### Manual Environment Setup
                     47: 
                     48: #### Creating the raw disk image
                     49: 
                     50: 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:
                     51: 
                     52: [[!template  id=programlisting text="""
                     53: $ dd if=/dev/zero of=netbsd-guest.img bs=1m count=2000
                     54: """]]
                     55: 
                     56: /!\ if you want to mount the file image from within the host later through [[!template id=man name="vnconfig" section="8"]], it is recommended to use [[!template id=man name="dd" section="1"]] and not the *qemu-img* tool, as [[!template id=man name="vnd" section="4"]] does not support sparse disk image yet.
                     57: 
                     58: Now that the disk image file is ready, we will need to install our system inside.
                     59: 
                     60: #### Preparing the MBR, labels, and first stage boot loader
                     61: 
                     62: Mount the image file as a [[!template id=man name="vnd" section="4"]] device. This will allow manipulating the image file just like a regular hard disk drive:
                     63: 
                     64: [[!template  id=programlisting text="""
                     65: # vnconfig -c vnd0 netbsd-guest.img
                     66: """]]
                     67: 
                     68: #### Creating MBR
                     69: 
                     70: Setup the MBR; it musts contain the NetBSD partition. This will be done interactively via [[!template id=man name="fdisk" section="8"]]:
                     71: 
                     72: [[!template  id=programlisting text="""
                     73: # fdisk -u -a -0 /dev/rvnd0
                     74: Disk: /dev/rvnd0d
                     75: [...]
                     76: Do you want to change our idea of what BIOS thinks? [n] *n*
                     77: 
                     78: Partition 0:
                     79: <UNUSED>
                     80: The data for partition 0 is:
                     81: <UNUSED>
                     82: sysid: [0..255 default: 169] *press enter*
                     83: start: [0..255dcyl default: 63, 0dcyl, 0MB] *press enter*
                     84: size: [0..255dcyl default: 4095937, 255dcyl, 2000MB] *press enter*
                     85: bootmenu: [] *press enter*
                     86: Do you want to change the active partition? [n] *y*
                     87: Choosing 4 will make no partition active.
                     88: active partition: [0..4 default: 0] *press enter*
                     89: Are you happy with this choice? [n] *y*
                     90: We haven't written the MBR back to disk yet.  This is your last chance.
                     91: Partition table:
                     92: 0: NetBSD (sysid 169)
                     93:     start 63, size 4095937 (2000 MB, Cyls 0-254/245/55), Active
                     94:         PBR is not bootable: All bytes are identical (0x00)
                     95: 1: <UNUSED>
                     96: 2: <UNUSED>
                     97: 3: <UNUSED>
                     98: Bootselector disabled.
                     99: First active partition: 0
                    100: Should we write new partition table? [n] *y*
                    101: """]]
                    102: 
                    103: #### Editing labels
                    104: 
                    105: Edit the labels, with [[!template id=man name="disklabel" section="8"]]. The example below will create:
                    106: 
                    107: * label **a**, approximately 1.5GiB long -- will contain the future FFS / partition
                    108: * label **b**, 512MiB swap.
                    109: 
                    110: [[!template  id=programlisting text="""
                    111: # disklabel -e -I /dev/rvnd0
                    112: [...]
                    113: 4 partitions:
                    114: #        size    offset     fstype [fsize bsize cpg/sgs]
                    115:  a:   3047361        63     4.2BSD      0     0     0  # (Cyl.      0*-   1487)
                    116:  b:   1048576   3047424       swap                     # (Cyl.   1488 -   1999)
                    117:  d:   4096000         0     unused      0     0        # (Cyl.      0 -   1999)
                    118: """]]
                    119: 
                    120: #### Copying first stage boot loader
                    121: 
                    122: 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 [[!template id=man name="installboot" section="8"]]:
                    123: 
                    124: [[!template  id=programlisting text="""
                    125: # installboot /dev/rvnd0a /usr/mdec/bootxx_ffsv2
                    126: """]]
                    127: 
                    128: ### Format and mount the filesystem
                    129: 
                    130: With [[!template id=man name="newfs" section="8"]], format label **a** in FFSv2:
                    131: 
                    132: [[!template  id=programlisting text="""
                    133: # newfs -O2 /dev/rvnd0a
                    134: /dev/rvnd0a: 1488.0MB (3047360 sectors) block size 16384, fragment size 2048
                    135:        using 9 cylinder groups of 165.34MB, 10582 blks, 20544 inodes.
                    136: super-block backups (for fsck_ffs -b #) at:
                    137: 160, 338784, 677408, 1016032, 1354656, 1693280, 2031904, 2370528, 2709152,
                    138: """]]
                    139: 
                    140: then [[!template id=man name="mount" section="8"]] it:
                    141: 
                    142: [[!template  id=programlisting text="""
                    143: # mkdir /tmp/netbsd-guest
                    144: # mount /dev/vnd0a /tmp/netbsd-guest
                    145: """]]
                    146: 
                    147: ### Installing the system
                    148: 
                    149: 
                    150: ## Booting VMs
                    151: 
                    152: Next, start two qemu virtual machines, one to run the kernel being
                    153: debugged (the "kgdb target") and another to run gdb (the "kgdb host").
                    154: They could be on different physical macines, but in this example, they
                    155: are run on the same physical machine, and the "-snapshot" qemu option
                    156: is used to avoid modifying the hard disk image so that it can be
                    157: shared between the host and target.  First start the kgdb target,
                    158: enabling qemu's built-in GDB target stub on TCP port 1234:
                    159: 
                    160: [[!template  id=programlisting text="""
                    161:  $ qemu -nographic -snapshot -hda work/wd0.img -gdb tcp::1234
                    162: """]]
                    163: 
                    164: If you don't want everyone on the Internet to be able to debug your
                    165: target, make sure incoming connections on port 1234 are blocked in
                    166: your firewall.
                    167: 
                    168: In a second terminal window, start the kgdb host:
                    169: 
                    170: [[!template  id=programlisting text="""
                    171:  $ qemu -nographic -snapshot -hda work/wd0.img --net user --net nic,model=ne2k_pci   
                    172: """]]
                    173: 
                    174: Log in to the kgdb host as root and set up the network:
                    175: 
                    176: [[!template  id=programlisting text="""
                    177:  login: root
                    178:  # dhclient ne2
                    179: """]]
                    180: 
                    181: If the sources you built using build.sh were in a location other than
                    182: /usr/src, set up a symlink from the place where they resided on the build
                    183: system to /usr/src (which is where they now reside on the kgdb host)
                    184: so that gdb can find them:
                    185: 
                    186: [[!template  id=programlisting text="""
                    187:  # mkdir -p /path/to/parent/dir/of/your/sources
                    188:  # ln -s /usr/src /path/to/parent/dir/of/your/sources/src
                    189: """]]
                    190: 
                    191: Start gdb on the kgdb host and connect to the target:
                    192: 
                    193: [[!template  id=programlisting text="""
                    194:  # gdb /netbsd
                    195:  (gdb) target remote my.host.name:1234
                    196: """]]
                    197: 
                    198: where my.host.name is the domain name or IP address of the
                    199: physical machine running the kgdb target qemu VM.
                    200: 
                    201: Now you should be able to get a stack trace and start digging:
                    202: 
                    203: [[!template  id=programlisting text="""
                    204:  (gdb) where
                    205: """]]
                    206: 
                    207: If the stack trace prints very slowly (like 30 seconds per stack
                    208: frame), it's likely because you are using a version of qemu where
                    209: the user-mode networking code fails to disable the Nagle algorithm.
                    210: This is fixed in qemu-0.15.1nb5 in pkgsrc.
                    211: 
                    212: ## Qemu usage
                    213: 
                    214: There are couple useful commands to know when you are developing kernel features under the qemu.
                    215: 
                    216: 1) Ctr-a-b will send a break to a NetBSD VM which will startup ddb kernel debugger.
                    217: 2) Ctr-a-c will switch to qemu monitor where user can use commands to save/restore vm from file.

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