File:  [NetBSD Developer Wiki] / wikisrc / netbsd_kernel_development_setup.mdwn
Revision 1.6: download - view: text, annotated - select for diffs
Mon Jan 21 18:12:19 2013 UTC (4 years, 10 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by gson: Omit needless words

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