File:  [NetBSD Developer Wiki] / wikisrc / kernel_debugging_with_qemu.mdwn
Revision 1.4: download - view: text, annotated - select for diffs
Thu Mar 31 09:00:49 2011 UTC (11 years, 8 months ago) by plunky
Branches: MAIN
CVS tags: HEAD
/dev/zero is the source of zeros

# Introduction

Virtual machines are a convenient way to test, debug or even audit different systems on one single host. This is particularly helpful when you need to set up a machine for which you do not necessarily have the hardware, or the access, in a very cheap way, without risking breaking your day-to-day system.

This tutorial show the different steps required to set up a raw disk image like the one used by QEMU. It deals with two different point of views:

* the host, which is the machine and OS hosting the different VMs.
* the guest(s), representing the different systems emulated/hosted on the host, through QEMU.

# Setting up the environment

## Creating the raw disk image

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:

[[!template  id=programlisting text="""
$ dd if=/dev/zero of=netbsd-guest.img bs=1m count=2000

/!\ 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.

Now that the disk image file is ready, we will need to install our system inside.

## Preparing the MBR, labels, and first stage boot loader

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:

[[!template  id=programlisting text="""
# vnconfig -c vnd0 netbsd-guest.img

### Creating MBR

Setup the MBR; it musts contain the NetBSD partition. This will be done interactively via [[!template id=man name="fdisk" section="8"]]:

[[!template  id=programlisting text="""
# fdisk -u -a -0 /dev/rvnd0
Disk: /dev/rvnd0d
Do you want to change our idea of what BIOS thinks? [n] *n*

Partition 0:
The data for partition 0 is:
sysid: [0..255 default: 169] *press enter*
start: [0..255dcyl default: 63, 0dcyl, 0MB] *press enter*
size: [0..255dcyl default: 4095937, 255dcyl, 2000MB] *press enter*
bootmenu: [] *press enter*
Do you want to change the active partition? [n] *y*
Choosing 4 will make no partition active.
active partition: [0..4 default: 0] *press enter*
Are you happy with this choice? [n] *y*
We haven't written the MBR back to disk yet.  This is your last chance.
Partition table:
0: NetBSD (sysid 169)
    start 63, size 4095937 (2000 MB, Cyls 0-254/245/55), Active
        PBR is not bootable: All bytes are identical (0x00)
Bootselector disabled.
First active partition: 0
Should we write new partition table? [n] *y*

### Editing labels

Edit the labels, with [[!template id=man name="disklabel" section="8"]]. The example below will create:

* label **a**, approximately 1.5GiB long -- will contain the future FFS / partition
* label **b**, 512MiB swap.

[[!template  id=programlisting text="""
# disklabel -e -I /dev/rvnd0
4 partitions:
#        size    offset     fstype [fsize bsize cpg/sgs]
 a:   3047361        63     4.2BSD      0     0     0  # (Cyl.      0*-   1487)
 b:   1048576   3047424       swap                     # (Cyl.   1488 -   1999)
 d:   4096000         0     unused      0     0        # (Cyl.      0 -   1999)

### Copying first stage boot loader

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"]]:

[[!template  id=programlisting text="""
# installboot /dev/rvnd0a /usr/mdec/bootxx_ffsv2

## Format and mount the filesystem

With [[!template id=man name="newfs" section="8"]], format label **a** in FFSv2:

[[!template  id=programlisting text="""
# newfs -O2 /dev/rvnd0a
/dev/rvnd0a: 1488.0MB (3047360 sectors) block size 16384, fragment size 2048
	using 9 cylinder groups of 165.34MB, 10582 blks, 20544 inodes.
super-block backups (for fsck_ffs -b #) at:
160, 338784, 677408, 1016032, 1354656, 1693280, 2031904, 2370528, 2709152,

then [[!template id=man name="mount" section="8"]] it:

[[!template  id=programlisting text="""
# mkdir /tmp/netbsd-guest
# mount /dev/vnd0a /tmp/netbsd-guest

# Installing the system

## Quick and easy way

## Through

# Configuring the system

# Starting-up the VM

# Debugging

# Convenient scripts

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb