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
@StefanoBelli
Copy link
Author

StefanoBelli commented Dec 26, 2016

Example

./osx-qemu-kvm_install.sh sierra 150G Documents/sierra-kvm.iso  ~/.qemu 8192

Script parameters

  • sierra : is the img qemu-generated file ("hdd") name (do not inculde extension, as the script does this)
  • 150G : is the size of the image file
  • Documents/sierra-kvm.iso : is the iso used to install OS X
  • ~/.qemu : is the location where the img and other needed files are placed
  • 8192 : is the size in Megabyte of dedicated RAM to this VM. (not needed, default is 4096MB)

Enjoy

@StefanoBelli
Copy link
Author

StefanoBelli commented Dec 26, 2016

Problems: macOS can't see my installation drive!!

if macOS can't see your drive for installation,

press Win+L, then CTRL+F2 to go to Utilities at the top of the screen (use the keyboard, mouse doesn't work)

then go to "Disk Utilities" or something like that and format your target HDD, then exit the utility pressing the 'x'.

Thanks to telegram user: @Hackdem (Federico)

@StefanoBelli
Copy link
Author

StefanoBelli commented Dec 26, 2016

Problems: Networking is not working properly

I workarounded this not using the tun/tap interfacing:

replace line 46 with this:

-netdev user,id=net0 -device e1000-82545em,netdev=net0 \

Then connect to the internet using DHCP (Automatic IP Address Assignment)

Its not like having a tun/tap device, but it is good for most cases.

@StefanoBelli
Copy link
Author

I will update this gist frequently

Trying to find out the best configuration! :)

And solving as many problems as possible

@khrak
Copy link

khrak commented Feb 27, 2017

When I run ./osx-qemu-kvm_install.sh yosemite 15G Yosemite.iso . 4096 it gives me the response:

  -> Dhiru Kholia : the man who did the hard work for us, not me
  -> GitHub profile : https://github.com/kholia
  -> Original OSX-KVM repository: https://github.com/kholia/OSX-KVM, be sure to star this!

./osx-qemu-kvm_install.sh: 28: ./osx-qemu-kvm_install.sh: cannot open 4: No such file
./osx-qemu-kvm_install.sh: 28: ./osx-qemu-kvm_install.sh: [[: not found
./osx-qemu-kvm_install.sh: 33: ./osx-qemu-kvm_install.sh: [[: not found
./osx-qemu-kvm_install.sh: 33: ./osx-qemu-kvm_install.sh: [[: not found
Changing directory to: . 

I downloaded osx-qemu-kvm_install.sh file and changed mode by +x.
I'm on Ubuntu 14.04 and kernel 4.9.0-040900-generic version

@StefanoBelli
Copy link
Author

Yup, my own fault, I used /bin/sh instead of /bin/bash (strange situation since /bin/sh is a symlink to /bin/bash...)
Anyway,try now :P

@besthyhy
Copy link

besthyhy commented May 9, 2017

Hi. How to boot ?

@besthyhy
Copy link

besthyhy commented May 9, 2017

I do exactly what you do. But the black window that pops up QEMU

@besthyhy
Copy link

besthyhy commented May 9, 2017

2017-05-10 00-12-54

@StefanoBelli
Copy link
Author

The bootloader clearly says you to hit enter to boot without customizing kernel command line parameters.

@astronom66
Copy link

Hi Developer's,
I run the script osx-qemu-kvm_install.sh
and get this error message

Getting enoch_rev2839_boot...
--2017-07-28 13:40:41-- https://raw.githubusercontent.com/kholia/OSX-KVM/master/enoch_rev2839_boot
Auflösen des Hostnamens »raw.githubusercontent.com (raw.githubusercontent.com)« … 151.101.192.133, 151.101.128.133, 151.101.64.133, ...
Verbindungsaufbau zu raw.githubusercontent.com (raw.githubusercontent.com)|151.101.192.133|:443 … verbunden.
HTTP-Anforderung gesendet, auf Antwort wird gewartet … 404 Not Found
2017-07-28 13:40:44 FEHLER 404: Not Found.

Is there something changed at https://raw.githubusercontent.com?

best regards
Markus

@StefanoBelli
Copy link
Author

yes, kholia replaced the kernel image with a new one, I will replace it ASAP

@fergrorke
Copy link

Hi Stefano

I changed your script to reflect the fact that Kohila is now using enoch_rev2898_boot. Now, however, the script aborts, saying it cannot find Install_macOS_10.12.6_Sierra.iso, yet it it sitting the same directory as the script. Any idea what I am doing wrong?

Regards
Fergus

@bhaveekdesai
Copy link

bhaveekdesai commented Oct 28, 2017

I copied the iso file (renamed to Install_macOS_10.12.6_Sierra.iso) to the ~/.qemu/ folder where boot-macOS.sh file is. And then I reran the kvm_install script. I got rid of the iso not found error but then I got another error: "Cannot find boot file: '/System/Library/Kernals/kernel' ".
Any thoughts?

@brianwoo
Copy link

I have the same problem as @bhaveekdesai. "Cannot find boot file: '/System/Library/Kernals/kernel' ". I tried doing it with an ISO as suggested in the guide as well as building my own UniBeast installer on USB. Same results.

@StefanoBelli
Copy link
Author

Guys this is not a help desk, sorry, check if Kholia is available for support,

Cannot find boot file: '/System/Library/Kernals/kernel'

This problem is not related with my script, @brianwoo is having the same problem using UniBeast.

Let me know

@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