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