How to setup an OpenBSD virtual machine using qemu

Introduction

This tutorial covers the simplest minimal configuration that just worksTM. If you want anything more than that, read the manual. With that said, this configuration is what I use for all of my OpenBSD virtual machines without major problems.

Preparation

Installation

First we need to create the storage for the vm. The command bellow will create a file that expands as more space is required. The 30G specifies the maximum size and therefore the one the guest OS sees. The real size at the end will be around 1.5Gb.

qemu-img create -f qcow2 obsd.qcow2 30G

I have the headless version of qemu so to get a vnc session I use tigervnc.

qemu-system-x86_64 -hda obsd.qcow2 -cdrom install68.iso -monitor stdio
vncviewer localhost:5900

If you have the normal version of qemu, you can simply run

qemu-system-x86_64 -hda obsd.qcow2 -cdrom install68.iso -monitor stdio -sdl

After opening the vnc session you'll be greated by the boot process and after that, the installation prompt. Here's what you do:

  1. Enter i to install
  2. Press enter for default keyboard layout
  3. Enter a hostname, I use obsd
  4. Press enter until you reach the dns domain name
  5. Enter localhost
  6. Press enter for nameservers
  7. Enter password for root, for now root or similar should suffice
  8. Press enter to start sshd by default
  9. Enter no for running the X window system
  10. Press enter for changing the default console
  11. Press enter for setting up a new user
  12. Enter yes to allow ssh root login
  13. Press enter until you reach the part for selecting sets
  14. Enter -x* and -game*, then press enter
  15. Enter yes to continue without SHA256 verification
  16. Wait a bit
  17. Press enter for the location of sets
  18. Enter your timezone
  19. Wait a bit more
  20. Enter h to halt the vm

OpenBSD doesn't send the poweroff signal so once you see

The operating system has halted.
Please press any key to reboot.

don't press any key but exit the vnc session and send the poweroff signal from the qemu command line.

(qemu) system_powerdown

You'll need to do this every time you halt the vm but otherwise the installation is done!

Access with ssh

Let's setup ssh so we don't have to deal with vnc ever again. To start the vm you can use

qemu-system-x86_64 -hda obsd.qcow2 -nic user,hostfwd=tcp::10022-:22

then to setup ssh, assuming you already have a key, run

ssh-copy-id -p 10022 root@localhost

You can add an entry for the vm in ~/.ssh/config

host lvm
    HostName 0.0.0.0
    User root
    port 10022

so that you can connect to it by simply running

ssh lvm

Security

Now that you have access to the vm from ssh, it's a good time to change the root password. You could have set a secure password in the installation but pasting it through vnc is a major pain so I prefer to do it this way. If you don't have a firewall on the host machine, you should also be aware that any other computer on the local network can also try to login to the vm using port 10022 on your computer so you should either setup a firewall, set a good root password or disable ssh login as root with a password. It's even better if you do all of those things.

Ramblings

I like to leave an OpenBSD vm running in the background that I can quickly access. Their man pages are second to none and the focus of the project makes it very suitable for use on servers. The system is simpler than most so it's very enjoyable to look at its inner workings. The configuration files for their software are easy on the eyes and mind. I haven't tried running OpenBSD on my personal computer as I'm content with GNU/Linux bloat and I don't like pkg_*. The system is quite efficient. I can run this vm in the background on my x200 without any noticeable impact on system resources and that's without any acceleration! The vm uses around 1.3gb of ram. The vast majority of which is used by qemu itself. OpenBSD only really requires 100mb at most and idles at 25mb with the above setup. OpenBSD wants to relink the kernel for whatever reason after every boot. If you ssh in that period, it might be a little sluggish.