It is very helpful to use an emulator with disk images to test code. We use bochs. It is also convenient to use a bootloader. We use grub. The osdever tutorial is useful, but doesn't really lay out how to do this. At first, I used two floppied: one with grub, then other with my kernel image.

I first downloaded, built and installed bochs. I leave that as an exercise for the reader. Next, I used bximage (from bochs) to build two floppy images: one to contain grub, and one to contain my kernel binary. I had a problem getting GRUB to recognize a harddrive image configuration for the purpose of loading the kernel, but it worked when GRUB needed to look at a floppy, hence the set up. To build the floppy images:

wells@local:~$ bximage ======================================================================== bximage Disk Image Creation Tool for Bochs $Id: bximage.c,v 1.32 2006/06/16 07:29:33 vruppert Exp $ ======================================================================== Do you want to create a floppy disk image or a hard disk image? Please type hd or fd. [hd] hd Choose the size of floppy disk image to create, in megabytes. Please type 0.16, 0.18, 0.32, 0.36, 0.72, 1.2, 1.44, 1.68, 1.72, or 2.88. [1.44] 1.44 I will create a floppy image with cyl=80 heads=2 sectors per track=18 total sectors=2880 total bytes=1474560 What should I name the image? [a.img] grub-floppy.img Writing: [] Done. I wrote 1474560 bytes to grub-floppy.img. The following line should appear in your bochsrc: floppya: image="grub-floppy.img", status=inserted wells@local:~$

I repeated the same sequence, but called the second image kernel-floppy.img. As the output suggests, you need to manipulate the elusive .bochsrc. First, you'll have to find a sample. Because I built bochs, the sample was in /usr/local/share/doc/bochs/bochsrc-sample.txt:

wells@local:~$ cp /usr/local/share/doc/bochs/bochsrc-sample.txt ~/.bochsrc

I didn't really need to change anything, except I commented out the line referring to "floppya", inserting in its place the line from the bximage run above:

floppya: image="grub-floppy.img", status=inserted

I also inserted the line from the second run of bximage, but made it a "floppyb".

I next needed to "format" both images, putting a filesystem on each. In the case of the GRUB disk ("floppya"), I would end up trashing the filesystem, but needed it so I could dd the grub loaders onto the disk image. For the kernel image disk ("floppya"), the filesystem allows GRUB to find the image. I figured the simplest way to do what I needed was to use the Linux loopback device, as follows:

wells@local:~$ mkfs -t ext2 grub-floppy.img grub-floppy.img is not a block special device. Proceed anyway? (y,n) y [...] wells@local:~$ mkfs -t ext2 kernel-floppy.img kernel-floppy.img is not a block special device. Proceed anyway? (y,n) y [...] wells@local:~$ mkdir mnt wells@local:~$ sudo mount -o loop=/dev/loop0 grub-floppy.img mnt wells@local:~$ sudo dd if=/boot/grub/stage1 of=/dev/loop0 bs=512 count=1 2 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.0271384 seconds, 18.9 kB/s wells@local:~$ sudo dd if=/boot/grub/stage2 of=/dev/loop0 bs=512 seek=1 2 seek=1 206+1 records in 206+1 records out 105652 bytes (106 kB) copied, 0.0323499 seconds, 3.3 MB/s wells@local:~$ sudo umount mnt wells@local:~$ sudo mount -o loop=/dev/loop0 kernel-floppy.img mnt wells@local:~$ cp kernel.bin mnt wells@local:~$ sudo umount mnt

Notice that I simply swiped the first and second stage boot loaders from my system. If you're not using GRUB, you'll probably have to compile. kernel.bin is my compiled and linked kernel image.

With this configuration, I fire up bochs, select option "6" to "Begin Simulation". This gets me the GRUB prompt:

grub> root (fd1) grub> kernel kernel.bin , <0x100000:0x2000:0x3860>, shtab=-x106168 grub> boot

And viola! My kernel loads. After more digging, I got more clever and use a single harddisk image with both grub and my kernel installed.