OpenBSD Journal

Publicly Accessible Server Inside qemu

Contributed by jcr on from the virtually-witty dept.

Undeadly reader Ted Walther writes in with a story about how he set up qemu for live testing:

I have a server at a colocation site that hosts various company software. A new hire came in and I wanted him to get some practice at configuring Apache, DNS, and various things like that. Instead of buying a new computer, I decided to use qemu to emulate a computer on the colocated server. I wanted the emulated computer to be accessible to the internet at large, just as a regular server with static IP is. Here is how I did it.

We assume you already have OpenBSD installed and running on the raw disk image that you will be booting with qemu.

The main network interface (NIC) on the colocated box is re0. The colocated server is a vanilla OpenBSD 4.7 install; if the following files already exist, pick new names as appropriate.

Fill in these two files:

$ cat /etc/hostname.bridge0
add re0
add tun0
up
$ cat /etc/hostname.tun0
link0 up

Substitute your real interface for re0 in the bridge file.

Add the following to /etc/rc.local

$ cat /etc/rc.local
if [ -x /usr/local/bin/qemu ]; then
    echo -n 'Qemu: vmi386'
    sh -c "sudo -C 4 -u qemuuser \
        /usr/local/bin/qemu \
        -daemonize \
        -nographic \
        -net nic,vlan=0,model=rtl8139,macaddr=aa:aa:aa:aa:aa:aa \
        -net tap,vlan=0,fd=3,script=no \
        -m 512 \
        -hda /path/to/raw/disk.img \
        -serial null \
        -monitor null \
        -no-fd-bootchk 3<>/dev/tun0"
    echo "."
fi

The above is almost identical to the README. It starts qemu and then drops root privileges. There was a recent bug fix in sudo on March 11, 2010 and the -C 4 option to sudo works around the bug. Without it, the tun0 device is never opened, and qemu has no network access.

Change qemuuser to the user you want the qemu instance to run as. Make sure that user has read/write access to the raw disk image.

Change the aa:aa:aa:aa:aa:aa to some random MAC address; this is to prevent network sniffers from guessing you are a qemu instance based on your MAC address.

With the -serial and -monitor options set to null, you can only ssh in. You will need completely different options when doing your fresh OpenBSD install on the raw disk image. I eventually broke down and installed a vnc client on another local box; then I was able to connect to the qemu instance on OpenBSD and install OpenBSD on the raw disk image.

There is a kernel module and package called kqemu which accelerates qemu performance. It is apparently very unstable on multi-cpu and on non-i386 architectures. Without kqemu, qemu runs at 1/4 of the speed of the host processor. A KVM specific to OpenBSD for accelerating qemu would be nice. I wonder what it would cost in developer time.

Why did I feel the need to write this document? After several days of struggling and not knowing about the sudo bug, I was questioning everything, and wondering what exactly the network model of qemu is. But it is simple; qemu connects to the tun0 device. tun0 is bridged to the bridge0 device. From there, tun0 sees all packets that come into your real interface (re0 in my case). re0 also forwards the packets coming out from tun0. Magic. There is some stuff in the README about a trunk device; it isn't necessary in this simple configuration.

No modifications to /etc/pf.conf need to be made. You do not need have to have net.inet.ip.forwarding enabled in /etc/sysctl.conf.

Thank you Todd Fries (todd@) for your effective and reasonably priced consulting services. After I'd gotten as far as I could, Todd just Made It Work.

The /usr/local/share/doc/qemu/README.OpenBSD file and the qemu manpage are still essential reading; this little writeup is just to add clarification.

(Comments are closed)


Comments
  1. By Bryan Brake (brakeb) brakeb@gmail.com on

    The "sudo" issue has always plagued me. It was getting to the point where I was just running qemu as root.

    Also, if someone has a good tutorial on how to use PF to do the networking for multiple qemu instances, I would be eternally grateful...

    Thanks for explaining this, it is much appreciated.

  2. By Ted Walther (TedWalther) ted@reactor-core.org on http://reactor-core.org/

    The issue of requiring VNC goes away if you boot with serial enabled as the console.

    Right now, to set that, you need to use vnconfig and modify a floppy disk image. But I like to do installs from the installation cdrom.

    Would it be practical to have the kernel send output to serial and vga simultaneously, at least through bootup stage, or at least up to the stage where you can tell the kernel which type of console you want to use? Then remote installs of OpenBSD onto qemu instances could be done, heck, I could automate it with tcl/expect.

    Comments
    1. By Anonymous Coward (anon) on

      > The issue of requiring VNC goes away if you boot with serial enabled as the console.
      >
      > Right now, to set that, you need to use vnconfig and modify a floppy disk image. But I like to do installs from the installation cdrom.

      The other way would be to use PXE - read pxeboot(8), this is *really* easy on OpenBSD. You can provide an etc/boot.conf in the TFTP directory.

      > Would it be practical to have the kernel send output to serial and vga simultaneously, at least through bootup stage, or at least up to the stage where you can tell the kernel which type of console you want to use?

      That would be useful. I don't think it's something that could be enabled by default though. As well as blocking at spltty for much longer periods of time during console writes (as they have to be sent to the relatively slow serial port) it's hard to say how various machines would react to it. The in-depth device probes aren't done until autoconf(4) when the kernel boots, so at the point you want it, we don't even know if the machine has a serial port.

  3. By jirib (jirib) jirib@mailinator.com on

    "The upstream qemu-0.11 release series (which was in Ubuntu 9.10) disabled kqemu in the build. And the upstream qemu-0.12 release series (which is in Ubuntu 10.04 LTS) actually removed all remnants of the kqemu hooks from the source."

    looks like kqemu is history and qemu is now in higher speed only Linux tools (with KVM) :(

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]