I wanted to install OpenBSD as a guest VM on my home server so that I could have a sandbox to test some potentially destructive commands for live copying my primary hard drive. My home server is running headless- I don’t have a display connected, nor do I have X11 installed. I also wanted to avoid using VNC for the install and do everything from the command line.

Create a Disk Image

> qemu-img create -f qcow2 openbsd.qcow2 16G Formatting 'openbsd.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=17179869184 lazy_refcounts=off refcount_bits=16 >

The Install Command

Here’s the command to run virt-install to set up the guest VM.

virt-install \ --name=openbsd \ --virt-type=kvm \ --memory=1024,maxmemory=1024 \ --vcpus=1,maxvcpus=1 \ --cpu host \ --os-variant=openbsd7.5 \ --import \ --disk '/home/pharkas/vm/openbsd/miniroot76.img' \ --disk '/home/pharkas/vm/openbsd/openbsd.qcow2',size=15,bus=virtio,format=qcow2 \ --boot bootmenu.enable=on,bios.useserial=on \ --network bridge:br0,model=virtio \ --serial pty \ --console pty,target_type=virtio \ --graphics=none

I’m using the miniroot image file and mounting it as the first disk for the guest. In combination with the --import flag it means the VM will start and attempt to boot off the first drive.

You could also download a .iso image instead and replace that line with --cdrom '/home/pharkas/vm/openbsd/cd76.iso'. If you use an emulated cdrom you won’t need to do any shenanigans with installing to a different drive or removing the drive from your VM settings after the installer completes. I’m using a .img installer in these instructions because I couldn’t find many good examples of doing that on the internet.

Booting the Guest

Once the guest VM boots you’ll want to enable console output from the OpenBSD boot menu with set tty com0. You only have a couple seconds to do this before the installer boots automatically, and once it does that you won’t see any more output unless you enabled the console. I also bumped up the speed to 115200 baud, but that not might really matter.

>> OpenBSD/amd64 BOOT 3.67 boot> set tty com0 switching console to com>> OpenBSD/amd64 BOOT 3.67 boot> stty com0 115200 com0: changing speed to 115200 baud in 5 seconds, change your terminal to match! 0 com0: 115200 baud boot>

Press the enter key at the boot> menu after setting the console and you should see the OpenBSD installer.

Installing

Install as usual, but when you’re asked which disk is the root disk make sure to pick sd1 (sd0 is the installer image).

Which disk is the root disk? ('?' for details) [sd0] sd1

After the installer completes you’ll need to halt the VM, shut it down (virsh destroy), edit the drives, and restart the VM.

CONGRATULATIONS! Your OpenBSD install has been successfully completed! When you login to your new system the first time, please read your mail using the 'mail' command. Exit to (S)hell, (H)alt or (R)eboot? [reboot] h syncing disks... done The operating system has halted. Please press any key to reboot. ^c > virsh destroy openbsd Domain 'openbsd' destroyed

Edit the VM settings to remove the install disk

> virsh edit openbsd ... <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/home/pharkas/vm/openbsd/miniroot76.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/home/pharkas/vm/openbsd/openbsd.qcow2'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk>

You’ll need to remove the first <disk> ... </disk> section entirely so that the VM doesn’t try to boot off the installer image any more.

While you’re in there you can change the target dev on the remaining disk from vdb to vda since it’s now the primary drive, but the machine will run fine even if you don’t make this change.

Save the file and exit, then boot your guest VM and you should be in business!

> virsh start openbsd --console