Hosts/BSD

From QEMU

QEMU on BSD hosts

This documentation is not written by a BSD expert - corrections welcome!

QEMU can be built on BSD hosts. At the moment most QEMU developers are Linux users, though, so BSD is not very well supported.

This page includes documentation of how to get the various BSD flavours running in a VM inside QEMU, so that Linux-based developers can do build tests on them. If you're running natively on BSD you can ignore the "VM setup" instructions.

FreeBSD

VM setup

Download and uncompress the official FreeBSD qcow2 image from

https://download.freebsd.org/ftp/releases/VM-IMAGES/11.0-RELEASE/amd64/Latest/

Run the image with:

qemu-system-x86_64 -m 2048 \
 -hda FreeBSD-11.0-RELEASE-amd64.qcow2 -enable-kvm \
 -netdev user,id=mynet0,hostfwd=tcp:127.0.0.1:7722-:22 \
 -device e1000,netdev=mynet0

(TODO: check virtio works and recommend that instead.)

Enable networking and ssh by adding these lines to /etc/rc.conf:

sshd_enable="YES"
ifconfig_em0="DHCP"

Edit /etc/ssh/sshd_config to add

PermitRootLogin prohibit-password

and then reboot the VM or run

service netif restart
service sshd start

Copy your ssh public key into the VM's /root/.ssh/authorized_keys

You should now be able to ssh into the VM from outside with

ssh -p 7722 root@localhost

Required packages

Install enough packages to do builds:

 pkg update
 pkg install git
 pkg install gcc
 pkg install gmake
 pkg install python
 pkg install pkgconf
 pkg install pixman
 pkg install bison
 pkg install glib

TODO: suggest some not-required-but-recommended packages?

Building

You can configure and build QEMU as you would on Linux:

 mkdir build
 cd build
 ../configure
 gmake
 gmake check

Note that you need to use 'gmake', not plain 'make'.

(NB as of 2017-03-20 you'll also need --disable-user to work around a QEMU bug.)

OpenBSD

VM setup

Get installer, create disk:

qemu-img create -f qcow2 disk.qcow2 15G
wget https://www.mirrorservice.org/pub/OpenBSD/6.0/amd64/cd60.iso

Run installer:

qemu-system-x86_64 -m 2048 -hda disk.qcow2 \
  -cdrom cd60.iso -enable-kvm  \
  -netdev user,id=mynet0,hostfwd=tcp:127.0.0.1:7922-:22 \
  -device e1000,netdev=mynet0 -smp 2

Follow the straightforward install prompts (accepting defaults generally OK). You'll want to enable sshd and allow logins with prohibit-password.

NB: it's important to run the installer with more than one CPU, or it will not install the SMP kernel. To fix this and add the SMP kernel after the initial install enter 'boot hd0a:bsd.rd' at the bootloader prompt to re-run the installer and add the 'bsd.mp' layer.

Reboot when it asks, and when it's rebooted kill the QEMU VM.

Now run the VM on the installed image:

 qemu-system-x86_64 -m 2048 \
  -drive if=virtio,file=disk.qcow2,format=qcow2 \
  -enable-kvm  \
  -netdev user,id=mynet0,hostfwd=tcp:127.0.0.1:7922-:22 \
  -device e1000,netdev=mynet0

Copy your ssh public key into root's .ssh/authorized_keys

ssh-copy-id root@localhost

You should now be able to get in to the VM with

ssh root@localhost

Make sure that the filesystem you're building and running QEMU from has the "wxallowed" option set in /etc/fstab, like:

 d3651b0622794af6.k /home ffs rw,wxallowed,nodev,nosuid 1 2

then either reboot or use "mount -uo wxallowed /wherever" to set it for this session. (This is necessary because QEMU assumes it can map memory RWX for TCG and 'make check' will fail if it can't.)

Required packages

Install enough packages to build QEMU

pkg_add git
pkg_add gmake
pkg_add python
 (select a python 2.7, and run the ln -sf runes to make it default)
pkg_add glib2
pkg_add bison
pkg_add sdl

TODO: recommended-but-not-required packages?

As of OpenBSD release 6.2, the default system compiler is a modern CLang. Prior to this release the default compiler was an ancient (circa 2007 vintage) GCC version which cannot compile QEMU. So for OpenBSD < 6.2, install the optional newer GCC package too:

pkg_add gcc
pkg_add g++

Building

For OpenBSD 6.2 or later, configure and make as usual:

mkdir build
cd build
../configure
gmake

For OpenBSD versions before 6.2, you must specify the compiler to use to avoid the ancient system GCC compilers:

mkdir build
cd build
../configure --cc=egcc --cxx=eg++
gmake

NetBSD

Create a disk image, and grab the install cd image:

qemu-img create -f qcow2 disk.qcow2 15G
wget http://cdn.netbsd.org/pub/NetBSD/NetBSD-7.1/images/NetBSD-7.1-amd64.iso

Run the installer:

qemu-system-x86_64 -m 2048 -enable-kvm \
  -drive if=virtio,file=disk.qcow2,format=qcow2 \
  -netdev user,id=mynet0,hostfwd=tcp::7722-:22 \
  -device e1000,netdev=mynet0 \
  -cdrom NetBSD-7.1-amd64.iso

The defaults will generally do. When it gets to the big menu of things to configure at the end of the install: configure networking (select defaults); enable NTP and ntpdate; enable ssh; set a root password.

Reboot into the installed system. Edit /etc/ssh/sshd_config to set 'PermitRootLogin: without-password', and set up ssh authorized keys.

Now you can run the installed image:

 qemu-system-x86_64 -m 2048 \
  -drive if=virtio,file=disk.qcow2,format=qcow2 \
  -enable-kvm  \
  -netdev user,id=mynet0,hostfwd=tcp:127.0.0.1:7722-:22 \
  -device e1000,netdev=mynet0

You should now be able to get in to the VM with

ssh -p 7722 root@localhost

Required packages

Run this to make pkg_add actually work:

export PKG_PATH="http://ftp.NetBSD.org/pub/pkgsrc/packages/NetBSD/<PORT>/<RELEASE-NUMBER>/All"

Then install dependencies:

pkg_add git gmake python27 glib2 bison pkgconf

TODO: optional dependencies?

Building

You can configure and build QEMU as you would on Linux:

 mkdir build
 cd build
 ../configure --python=python2.7
 gmake
 gmake check

Note that you need to use 'gmake', not plain 'make'.

DragonFly BSD

Resources for the installation

There are two options here. One is to use the USB image to boot from it directly, as a raw image, the other one is to use the ISO and install dfly on the qemu host.

Fetch either daily snapshot or the release ISO/image files, links below. At the moment of this writing latest stable version is 4.8.1.

USB image

Stable release: IMG.bz2

Daily snapshot: IMG.bz2

ISO image

Stable release: ISO.bz2

Daily snapshot: ISO.bz2

VM Setup

Option 1. Using the USB IMG file

No need to create a disk image as the USB image itself will act as the raw image disk file for qemu:

 qemu-system-x86_64 -m 2048 \
   -drive if=virtio,file=DragonFly-x86_64-LATEST-IMG.img,format=raw \
   -netdev user,id=mynet0,hostfwd=tcp::7722-:22 \
   -device e1000,netdev=mynet0
 

Note: You will likely find this error while booting. There is a way to make this permanent which is explained below.

 Manual root filesystem specification:
   <fstype>:<device>  Specify root (e.g. ufs:da0s1a)
   ?                  List valid disk boot devices
   panic              Just panic
   abort              Abort manual input
 
 mountroot> ?
 Possibly valid devices for root FS:
  "vn0"  "vn1"  "vn2"  "vn3"  "md0"  "md0s0"  "acd0"  "vbd0"  "vbd0s1"  "vbd0s2"
  "vbd0s2a"  "cd0"  "sg0"
 mountroot> ufs:vbd0s2a
 Mounting root from ufs:vbd0s2a
 DMA space used: 1192k, remaining available: 127912k
 Mounting devfs
 [...]

To set a device for booting permanently on the USB image:

 login: root
 Last login: Tue Sep 12 17:44:26 2017 on ttyv0
 Copyright (c) 2003-2017 The DragonFly Project.
 Copyright (c) 1992-2003 The FreeBSD Project.
 Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
            The Regents of the University of California. All rights reserved.           
 DragonFly v4.9.0.653.g8e456-DEVELOPMENT (X86_64_GENERIC) #0: Tue Sep 12 10:48:26        UTC 2017
 Welcome to DragonFly!
 # echo 'vfs.root.mountfrom=ufs:vbd0s2a' >> /boot/loader.conf
 # reboot

Option 2. Installing from the ISO image

Depending on whether you're going to use UFS or HAMMER you are encouraged to use a specific HDD size since HAMMER works better with +50GB disks. Of course this is not mandatory and you can use whatever size you prefer as long as it's bigger than 10GB.

Create a virtual disk:

 qemu-img create -f qcow2 disk.qcow2 15G

Run qemu and boot from CD

 qemu-system-x86_64 -m 2048 -boot d \
   -cdrom DragonFly-x86_64-LATEST-ISO.iso \
   -drive if=virtio,file=disk.qcow2,format=qcow2 \
   -netdev user,id=mynet0,hostfwd=tcp::7722-:22 \
   -device e1000,netdev=mynet0

For installation instructions you can follow this guide, section "Long Walkthrough of DragonFly BSD Installation and Configuration", although it might not be completely accurate it is a good one: https://www.dragonflybsd.org/varialus/

Configuration

Enable networking and ssh by adding these lines to /etc/rc.conf:

 sshd_enable="YES"
 ifconfig_em0="DHCP"

Copy your ssh public key into the VM's /root/.ssh/authorized_keys

You should now be able to ssh into the VM from outside with

 ssh -p 7722 root@localhost

Required packages

Install enough packages to do builds:

 pkg install git python pkgconf glib

gcc and binutils are in the base distribution so no need to install them. pixmap is now of of the submodules, so no need to install it either.

Building

Instructions from https://www.qemu.org/download/#source:

 git clone git://git.qemu.org/qemu.git
 cd qemu
 git submodule init
 git submodule update --recursive
 ./configure --prefix=/opt/local
 gmake
 gmake check

NOTE: You need to use 'gmake', not plain 'make'.

NOTE-2: Do not use default /usr/local as it is the place where the package system installs the software.