Skip to content

Instantly share code, notes, and snippets.

@frazei
Last active March 19, 2024 17:44
Show Gist options
  • Save frazei/0c9d71ff248a60095ce5542053a974ef to your computer and use it in GitHub Desktop.
Save frazei/0c9d71ff248a60095ce5542053a974ef to your computer and use it in GitHub Desktop.
Apple Silicon M1 QEMU #osx #virtualization #arm

Apple Silicon M1 QEMU

How to run Windows ARM and Linux ARM on Apple Silicon M1

09/03/2021 Update: I've found this app UTM that is a GUI for qemu and includes a precompiled version of it in various formats (x86, x86_64, arm, risc..). It works very well!

Prerequisites

  • You need a clean brew install using the arm version (not the x86 on rosetta2). So, first of all, I've removed the x86/rosetta2 version:
% brew remove --force $(brew list)
% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"
  • Then be sure to have the right version of CommandLineTools installed (useful if you have restored from Time Machine made on a Intel Mac):
% sudo rm -rf /Library/Developer/CommandLineTools
% sudo xcode-select --install
  • And then install the arm version:
% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
% brew analytics off
  • Install some useful stuff:
% brew install wget -g
% brew install inetutils -g

Install QEMU

  • Now compile quemu with the patch needed to run on Apple Silicon CPU:
% git clone https://gitlab.com/qemu-project/qemu.git
% cd qemu
% git submodule init
% git submodule update --recursive
% brew install libffi gettext pkg-config autoconf automake pixman libjpeg libpng libusb ninja glib
% git checkout 56a11a9b7580b576a9db930667be07f1dd1564d5
% curl https://patchwork.kernel.org/series/418581/mbox/ | git am
% mkdir build
% cd build
% ../configure --target-list=aarch64-softmmu --enable-hvf --disable-gnutls
% make -j8
% sudo make install
% sudo codesign --entitlements ../accel/hvf/entitlements.plist --force -s - `which qemu-system-aarch64`
  • For the last command I got this error:
/usr/local/bin/qemu-system-aarch64: replacing existing signature
/usr/local/bin/qemu-system-aarch64: the codesign_allocate helper tool cannot be found or used

The strange thing is that after a few tries everything went ok:

% which codesign_allocate
/usr/bin/codesign_allocate
% echo $PATH | tr : '\n'
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/opt/libxml2/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
% codesign_allocate 
Usage: /Library/Developer/CommandLineTools/usr/bin/codesign_allocate -i input [[-a <arch> <size>]... [-A <cputype> <cpusubtype> <size>]... | -r] [-p] -o output
% sudo codesign --entitlements ../accel/hvf/entitlements.plist --force -s - `which qemu-system-aarch64`
/usr/local/bin/qemu-system-aarch64: replacing existing signature
  • Check that everything is ready:
% qemu-system-aarch64 --version
QEMU emulator version 5.2.50 (v5.2.0-932-g5ceede7e6d)
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers

Run Ubuntu Server for ARM

% cd ~/Downloads
% wget https://cdimage.ubuntu.com/releases/20.04/release/ubuntu-20.04.2-live-server-arm64.iso
  • Create a hard disk and an empty file for persisting UEFI variables::
% mkdir ~/qemu
% cd ~/qemu
% qemu-img create -f qcow2 disk.qcow2 10G
% dd if=/dev/zero conv=sync bs=1m count=64 of=ovmf_vars.fd
  • Run qemu with the following command-line arguments:
qemu-system-aarch64 \
    -accel hvf \
    -m 2048 \
    -cpu cortex-a57 -M virt,highmem=off  \
    -drive file=/usr/local/share/qemu/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on \
    -drive file=ovmf_vars.fd,if=pflash,format=raw \
    -serial telnet::4444,server,nowait \
    -drive if=none,file=disk.qcow2,format=qcow2,id=hd0 \
    -device virtio-blk-device,drive=hd0,serial="dummyserial" \
    -device virtio-net-device,netdev=net0 \
    -netdev user,id=net0 \
    -vga none -device ramfb \
    -cdrom ~/Downloads/ubuntu-20.04.2-live-server-arm64.iso \
    -device usb-ehci -device usb-kbd -device usb-mouse -usb \
    -monitor stdio
  • After Ubuntu is installed you can remove -cdrom ~/Downloads/ubuntu-20.04.2-live-server-arm64.iso \ from the command

Run Windows for ARM

  • Register for Windows Insider Program form here
  • Download Windows for ARM from here
  • Create an empty file for persisting UEFI variables (you must create a new UEFI as you can't reuse the one created for Ubuntu) and move the downloaded VHDX here as Windows will run directly inside this image (it does not require and hard disk):
% mkdir ~/qemu
% cd ~/qemu
% dd if=/dev/zero conv=sync bs=1m count=64 of=ovmf_vars1.fd
% mv ~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_21286.VHDX .
  • Run qemu with the following command-line arguments (my VHDX version is 21286):
qemu-system-aarch64 \
    -accel hvf \
    -m 2048 -smp 2 \
    -cpu cortex-a72 -M virt,highmem=off  \
    -drive file=/usr/local/share/qemu/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on \
    -drive file=ovmf_vars1.fd,if=pflash,format=raw \
    -serial telnet::4444,server,nowait \
    -drive if=none,file=Windows10_InsiderPreview_Client_ARM64_en-us_21286.VHDX,format=vhdx,id=hd0,cache=writethrough \
    -device nvme,drive=hd0,serial="dummyserial" \
    -nic user,model=virtio \
    -vga none -device ramfb \
    -device usb-ehci -device usb-kbd -device usb-mouse -usb \
    -monitor stdio

NOTE: -smp 2 stands for 2 cores

Credits

https://gist.github.com/citruz/9896cd6fb63288ac95f81716756cb9aa

https://gist.github.com/niw/e4313b9c14e968764a52375da41b4278

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