Skip to content

Instantly share code, notes, and snippets.

@astreknet
Last active April 11, 2024 08:39
Show Gist options
  • Save astreknet/4860e3362ad98e1116f0a970b99e2afc to your computer and use it in GitHub Desktop.
Save astreknet/4860e3362ad98e1116f0a970b99e2afc to your computer and use it in GitHub Desktop.
OpenBSD on Apple M2 with QEMU

OpenBSD on Apple M2 with QEMU

Minimalist installation of OpenBSD on the Apple M2 using QEMU

Ingredients

Installation

  1. install QEMU with Homebrew brew install qemu, also possible with MacPorts
  2. Write a script with execute permissions chmod +x qemu_aarch64_install_openbsd.sh for installing the image.
    • Download the OpenBSD installation image
    • Create an image of the OpenBSD drive
    • Getting the edk2-aarch64-code
    • Installing
#!/bin/sh -
  
curl https://ftp.eu.openbsd.org/pub/OpenBSD/7.5/arm64/miniroot75.img -o miniroot75.img 
curl https://ftp.openbsd.org/pub/OpenBSD/7.5/arm64/SHA256 -o SHA256
shasum -c --ignore-missing SHA256

qemu-img create -f qcow2 obsd_aarch64.qcow2 32G

qemu-system-aarch64 \
    -M virt \
    -cpu host \
    -accel hvf \
    -smp 4 \
    -m 4096 \
    -nographic \
    -serial mon:stdio \
    -bios /opt/homebrew/share/qemu/edk2-aarch64-code.fd \
    -drive file=miniroot75.img,format=raw,if=virtio \
    -drive file=obsd_aarch64.qcow2,format=qcow2,if=virtio \
    -netdev user,id=mynet0,hostfwd=tcp::7922-:22 -device virtio-net,netdev=mynet0

rm miniroot75.img SHA256 
  1. Follow the OpenBSD installation program, please notice the remarks at the end of this document:

  2. When the installation is done, pkill qemu-system-aarch64

Running OpenBSD with QEMU

like this:

qemu-system-aarch64 \
    -M virt \
    -cpu host \
    -accel hvf \
    -smp 4 \
    -m 4096 \
    -nographic \
    -bios /opt/homebrew/share/qemu/edk2-aarch64-code.fd \
    -drive file=obsd_aarch64.qcow2,format=qcow2,if=virtio \
    -netdev user,id=mynet0,hostfwd=tcp::7922-:22 -device virtio-net,netdev=mynet0 

Remarks

  • there is a problem with the DNS if the network interface (vio0) is on autoconf so is better configure it manually, have a look at QEMU's User Networking, for instance IPv4 would work on 10.0.2.15/24 and 10.0.2.2 gateway.

  • alternatively a full image installXX.img instead of minirootXX.img would work.

  • for the lazy asses, like me, it is possible to do autoinstall(8) with an install.conf on your localhost like this one:

    System hostname = myHost
    IPv4 address for vio0 = 10.0.2.15    
    Default IPv4 route = 10.0.2.2
    DNS nameservers = 1.1.1.1 8.8.8.8
    Password for root = RootsPassword
    Setup a user = myName
    Password for user = myPassword
    Public ssh key for user = ssh-ed25519 AAA...318C myName@myHost.local
    What timezone are you in = YOUR/TIMEZONE
    Which disk is the root disk = sd1
    Location of sets = http 
    HTTP Server = ftp.eu.openbsd.org
    URL to autopartitioning template for disklabel = http://10.0.2.2/openbsd-pgdb.disklabel 
    

    also possible to define the disk partition adding openbsd-pgdb.disklabel on your localhost like this one:

    /           0.3G
    swap        2M        10%
    /altroot    0.3G
    /tmp        120M-4G   8%
    /var        3G        13%
    /usr        0.9G-3G   5%
    /usr/local  3G-7.5G   10%
    /home       9G-*      45%
    

Sources

@solus-hq
Copy link

@ast3k Thank you for you help!

  1. I created a smaller disk image
  2. I was choosing the wrong disk device (sd0 insteand of sd1) which led to insufficient disk space when moving kernels or something
  3. I was using miniroot71.img and now I'm using install71.img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment