File:  [NetBSD Developer Wiki] / wikisrc / netbsd_kernel_development_setup.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Sat May 19 19:50:33 2012 UTC (8 years, 2 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by reed: typo

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