Skip to content

Instantly share code, notes, and snippets.

@StefanoBelli
Last active April 11, 2021 05:43
Show Gist options
  • Save StefanoBelli/719105c97c7efe11907e3bfd1e1917ff to your computer and use it in GitHub Desktop.
Save StefanoBelli/719105c97c7efe11907e3bfd1e1917ff to your computer and use it in GitHub Desktop.
Fast OS X launch on QEMU with KVM support enabled. Credits to: https://github.com/kholia, original repository: https://github.com/kholia/OSX-KVM
#!/bin/sh
# qemu-img create -f qcow2 mac_hdd.img 64G
# echo 1 > /sys/module/kvm/parameters/ignore_msrs
#
# Type the following after boot,
# -v "KernelBooter_kexts"="Yes" "CsrActiveConfig"="103"
#
# printf 'DE:AD:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))
#
# no_floppy = 1 is required for OS X guests!
#
# Commit 473a49460db0a90bfda046b8f3662b49f94098eb (qemu) makes "no_floppy = 0"
# for pc-q35-2.3 hardware, and OS X doesn't like this (it hangs at "Waiting for
# DSMOS" message). Hence, we switch to pc-q35-2.4 hardware.
#
# Network device "-device e1000-82545em" can be replaced with "-device vmxnet3"
# for possibly better performance.
#
# Replace line 46 with this if networking is not working properly:
# -netdev user,id=net0 -device e1000-82545em,netdev=net0 \
# this is not using tun/tap interfacing and does not require you to be root
# on macOS, connect to ethernet using DHCP (auto IP assign) method, and you're done!
#
if ! which qemu-system-x86_64 2>/dev/null >>/dev/null ; then
echo "Cannot find qemu-system-x86_64... Aborting"
exit 1
fi
KVM="-enable-kvm"
DEDICATED_RAM="8192"
KERNEL_IMAGE="/home/$USER/.qemu/osx_sierra/enoch_rev2839_boot"
HDD_IMAGE="/home/$USER/.qemu/osx_sierra/sierra.img"
qemu-system-x86_64 $KVM -m $DEDICATED_RAM -cpu Penryn,kvm=off,vendor=GenuineIntel \
-machine pc-q35-2.4 \
-smp 4,cores=2 \
-usb -device usb-kbd -device usb-mouse \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-kernel $KERNEL_IMAGE \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=MacHDD \
-drive id=MacHDD,if=none,file=$HDD_IMAGE \
-netdev tap,id=net0,ifname=tap0,script=no,downscript=no -device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-monitor stdio
# -vnc 0.0.0.0:0 -k en-us \
# -redir tcp:5901::5900 \
# -netdev user,id=hub0port0 -device e1000-82545em,netdev=hub0port0,id=mac_vnet0 \
#!/bin/bash
# Thanks to kholia (https://github.com/kholia)
# https://github.com/kholia/OSX-KVM
# He did the hard work!
BOOT_MACOS_SH="boot-macOS.sh"
ENOCH_REV2839_BOOT="enoch_rev2839_boot"
BOOT_MACOS_SCRIPT="https://raw.githubusercontent.com/kholia/OSX-KVM/master/${BOOT_MACOS_SH}"
ENOCH_REV2839_BOOT_KERNEL="https://raw.githubusercontent.com/kholia/OSX-KVM/master/${ENOCH_REV2839_BOOT}"
IMAGE_HDD_NAME="${1}.img"
IMAGE_HDD_SIZE=$2
ISO="$(pwd)/$3"
INSTALL_DIR=$4
DL_BOOT_MACOS_RC="wget $BOOT_MACOS_SCRIPT"
DL_ENOCH_REV2839_BOOT_RC="wget $ENOCH_REV2839_BOOT_KERNEL"
PERM_RC="sudo sh boot-macOS.sh"
RAM_MB="4096"
echo
echo " ** THANKS TO ** "
echo " -> Dhiru Kholia : the man who did the hard work for us, not me"
echo " -> GitHub profile : https://github.com/kholia"
echo " -> Original OSX-KVM repository: https://github.com/kholia/OSX-KVM, be sure to star this!"
echo
if [[ $# < 4 ]]; then
echo "Usage: <$0> [target HDD name] [target HDD size(including unit: M,G,K,..)] [ISO file] [Install directory] (dedicated RAM memory) [in Megabytes]"
exit 1
fi
if [[ $# == 5 ]] || [[ $# > 5 ]]; then
RAM_MB=$5
echo "Dedicated RAM size: ${RAM_MB}MB"
fi
if [ ! -f $ISO ]; then
echo "Cannot find your iso file named: $ISO ... Aborting"
exit 5
fi
if [ ! -d $INSTALL_DIR ]; then
echo "Creating $INSTALL_DIR for you..."
mkdir -p $INSTALL_DIR || exit 1 && echo "Done"
fi
if ! which wget 2>/dev/null >>/dev/null; then
echo "wget not found, trying curl..."
if ! which curl 2>/dev/null >>/dev/null; then
echo "Cannot find curl... Aborting"
exit 1
else
DL_BOOT_MACOS_RC="curl -O $BOOT_MACOS_SCRIPT"
DL_ENOCH_REV2839_BOOT_RC="curl -O $ENOCH_REV2839_BOOT_SCRIPT"
fi
fi
if ! which sudo 2>/dev/null >>/dev/null; then
echo "Using su as privilege escalation method"
PERM_RC="su -c \"sh boot-macOS.sh\""
fi
if ! which qemu-system-x86_64 2>/dev/null >>/dev/null; then
echo "Cannot find QEMU (x86_64)... Aborting"
exit 2
fi
if ! which qemu-img 2>/dev/null >>/dev/null; then
echo "Cannot find QEMU IMG (needed to create image)... Aborting"
exit 3
fi
echo "Changing directory to: $INSTALL_DIR"
cd $INSTALL_DIR || exit 1
if [ -f $IMAGE_HDD_NAME ]; then
echo -n "$IMAGE_HDD_NAME already exists, continue[y/N] ? "
read CONTINUE
if [[ $CONTINUE == "y" ]] || [[ $CONTINUE == "Y" ]]; then
echo "Keep going..."
elif [[ $CONTINUE == "n" ]] || [[ $CONTINUE == "N" ]]; then
echo "Good choice..."
exit 1
fi
fi
qemu-img create -f qcow2 $IMAGE_HDD_NAME $IMAGE_HDD_SIZE
if [ -f $BOOT_MACOS_SH ]; then
echo "$BOOT_MACOS_SH already exists"
else
echo "Getting boot-macOS.sh..."
$DL_BOOT_MACOS_RC >>/dev/null || exit 1
sed -i "s:./mac_hdd.img:$IMAGE_HDD_NAME:" boot-macOS.sh
sed -i "s:./'Install_macOS_Sierra_(OS_X_10.12).iso':${ISO}:" boot-macOS.sh
sed -i "s:-m 8192:-m $RAM_MB:" boot-macOS.sh
fi
if [ -f $ENOCH_REV2839_BOOT ]; then
echo "$ENOCH_REV2839_BOOT already exists"
else
echo "Getting enoch_rev2839_boot..."
$DL_ENOCH_REV2839_BOOT_RC >>/dev/null || exit 1
fi
echo "(Waiting 3 secs) Launching QEMU... Starting OSX ($ISO) installation..."
sleep 3
echo "Here we go"
$PERM_RC
@NerijusKr
Copy link

Hi,

I am trying to boot up High Sierra on Ubuntu 16.04, qemu 2.11.91.

I managed to boot it to clover, but neither keyboard nor mouse are working.

Btw, I am connecting to host via ssh -X

Any suggestions?

@JazzMaster
Copy link

HiSierra changed the CPU requirements.
2.14+ TY, Im using 2.19 and QEMU 3 sources.

1- youre supposed to dump the apple smc- not blast the net w apples PROM dumped code.
2- try this.

qemu-system-x86_64 -enable-kvm -m 4096 -cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+aes,+xsave,+avx,+xsaveopt,avx2,+smep\

MoJave requires METAL/ 4GB+ UEFI AMD hardware and even more options. AMDFX still beats MoJaves CPU requirements.

clover disk image works just fine. So its a native install. Clone your mac HDD with dd and qcow convert it(if using qcow).
I forget who is hosting it- I pulled clover from another repo- but reworked it into something useful.

-JazzMaster-

@JazzMaster
Copy link

The AMD kext will have to be patched if using VFIO- which is even faster.

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