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:

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:

$ 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 vnconfig(8) , it is recommended to use dd(1) and not the qemu-img tool, as vnd(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 vnd(4) device. This will allow manipulating the image file just like a regular hard disk drive:

# vnconfig -c vnd0 netbsd-guest.img

Creating MBR

Setup the MBR; it musts contain the NetBSD partition. This will be done interactively via fdisk(8) :

# 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 disklabel(8) . The example below will create:

# 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 installboot(8) :

# installboot /dev/rvnd0a /usr/mdec/bootxx_ffsv2

Format and mount the filesystem

With newfs(8) , format label a in FFSv2:

# 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 mount(8) it:

# mkdir /tmp/netbsd-guest
# mount /dev/vnd0a /tmp/netbsd-guest

Installing the system

Quick and easy way


Configuring the system

Starting-up the VM


Convenient scripts

Add a comment