Skip to content

Instantly share code, notes, and snippets.

@phips
Forked from ctsrc/README.md
Last active November 26, 2023 21:54
Show Gist options
  • Save phips/98d415b5383d77816aa6965b6c825b67 to your computer and use it in GitHub Desktop.
Save phips/98d415b5383d77816aa6965b6c825b67 to your computer and use it in GitHub Desktop.
Guide: Run FreeBSD 13.2-RELEASE for ARM64 in QEMU on Apple Silicon Mac (MacBook Pro M1, etc) with HVF acceleration (Hypervisor.framework)

Guide: Run FreeBSD 13.2-RELEASE for ARM64 in QEMU on Apple Silicon Mac (MacBook Pro M1, etc) with HVF acceleration (Hypervisor.framework)

FreeBSD 13.2-RELEASE for ARM64 boot in QEMU on Apple Silicon Mac screenshot

This guide was adapted from https://gist.github.com/ctsrc/a1f57933a2cde9abc0f07be12889f97f

Running FreeBSD 13.2-RELEASE for ARM64

  1. Install Xcode from App Store or install Command Line Tools on your Mac running on Apple Silicon.

    xcode-select --install
    
  2. Install Homebrew and QEMU.

    https://brew.sh/

    brew install qemu
    rehash
    qemu-system-aarch64 --version
    QEMU emulator version 8.1.3
    Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
    
  3. Download pre-build EDK II OVMF EFI image for QEMU.

    This EFI image is built from stable202011 tag with additional resolutions in QemuRamfb.c.

    https://gist.github.com/niw/4f1f9bb572f40d406866f23b3127919b/raw/f546faea68f4149c06cca88fa67ace07a3758268/QEMU_EFI-cb438b9-edk2-stable202011-with-extra-resolutions.tar.gz

    To build it from the source code for adding more resolutions, see the following section.

  4. Prepare pflash for non-volatile variable store, such as screen resolution.

    mkdir ~/qemu-vm/
    cd ~/qemu-vm/
    tar xvf ~/Downloads/QEMU_EFI-cb438b9-edk2-stable202011-with-extra-resolutions.tar.gz
    dd if=/dev/zero of=pflash0.img bs=1m count=64
    dd if=/dev/zero of=pflash1.img bs=1m count=64
    dd if=QEMU_EFI.fd of=pflash0.img conv=notrunc
    dd if=QEMU_VARS.fd of=pflash1.img conv=notrunc
    
    • This step is optional, you can use -bios QEMU_EFI.fd instead of -drive ...if=pflash lines in the next step, but in that case, any changes in EFI will not be persistent.
  5. Download FreeBSD 13.2-RELEASE for ARM64 raw VM image xz-compressed file

    https://ftp2.uk.freebsd.org/pub/FreeBSD/releases/VM-IMAGES/13.2-RELEASE/aarch64/Latest/FreeBSD-13.2-RELEASE-arm64-aarch64.raw.xz

    (See https://docs.freebsd.org/en/books/handbook/mirrors/ for a list of mirrors to choose from.)

  6. Decompress xz-compressed file, keeping a copy of the original compressed file

    Keeping a copy of the original file is convenient because then you can use it if you want to create additional VMs later. Just be careful not to overwrite the image of your first VM when you want to make a second VM though :P

    mv ~/Downloads/FreeBSD-13.2-RELEASE-arm64-aarch64.raw.xz .
    unxz -k FreeBSD-13.2-RELEASE-arm64-aarch64.raw.xz
    
  7. Grow the disk image

    After you've decompressed the disk image, it'll be about 5 GiB in size. Depending on what you plan to do the amount of available space may be a bit on the low side. Let's grow the disk image by another 30 GiB.

    qemu-img resize -f raw FreeBSD-13.2-RELEASE-arm64-aarch64.raw +30G
    

    By performing this resize before you boot the VM for the first time, FreeBSD will automatically adjust the partition size during first boot.

  8. Run your FreeBSD 13.2-RELEASE for ARM64 VM

    qemu-system-aarch64 \
      -M virt \
      -accel hvf \
      -cpu host \
      -smp 4 \
      -m 4096 \
      -drive file=pflash0.img,format=raw,if=pflash,readonly=on \
      -drive file=pflash1.img,format=raw,if=pflash \
      -device virtio-gpu-pci \
      -display default,show-cursor=on \
      -device qemu-xhci \
      -device usb-kbd \
      -device usb-tablet \
      -device intel-hda \
      -device hda-duplex \
      -drive file=FreeBSD-13.2-RELEASE-arm64-aarch64.raw,format=raw,if=virtio,cache=writethrough \
      -nographic \
      -serial mon:stdio
    

A screenshot of FreeBSD 13.2-RELEASE for ARM64 console in QEMU on Apple Silicon Mac, showing the output of freebsd-version

Another screenshot of FreeBSD 13.2-RELEASE for ARM64 console in QEMU on Apple Silicon Mac, this time showing that networking is working and is able to connect over HTTP to the website example.com

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