Virtual MacOS

This post is about running MacOS in VirtualBox on Linux (i.e you've installed Linux on your Mac but would like to run macOS in a virtual machine).

This explanation is complete with links to download a Virtualbox image. However, its provenance is uncertain. It is best to obtain MacOS from Apple.

There are many GitHub Gists with useful information but I followed this one because it is up to date for the latest MacOS (Sierra) and has been updated in the last few days (20170317).

Apple explains how to get macOS: go to the Sierra Download Page on a Mac. This will download to /Applications/Install\ macOS\ Sierra.app, beneath which the install image file is Contents/SharedSupport/InstallESD.dmg.

Bear in mind that image is just under 5GiB so it will take a while to download (perhaps an overnight task?). It turned out to be 9692280 blocks which is 4.62GiB.

Take the time to verify the SHA1 Digest of the image file. Here is an unofficial list, caveat emptor!. For example:

$ shasum /Applications/Install\ macOS\ Sierra.app/Contents/SharedSupport/InstallESD.dmg
77d354ec06df0d0acc37c105ae524ba96948142b  /Applications/Install macOS Sierra.app/Contents/SharedSupport/InstallESD.dmg

(which is corroborated by the hash on the abovementioned list).

Now run the prepare-iso script on the mac (it uses macOS-specific tools including hdiutil (used to manipulate disk images) and asr (Apple Software Restore), neither of which are available on Linux). The script prepares a new Sierra.iso image file from the InstallESD.dmg file and, by default, saves it on the desktop.

Copy the Sierra.iso, which is 13519560 blocks (6.44GiB, some 40% larger!), to the virtualbox host. SHA1SUM = 9a563d69a57f425f3864cbf291e84fa81224ab46.

Prepare a virtual machine

Use the VirtualBox GUI to create a new virtual machine with the following attributes:

  • name whatever suits (e.g. macOS Sierra)
  • type OS X
  • version Mac OS X 10.11 El Capitan (64-bit)
  • video memory 128MiB
  • number of CPUs 2

Then run this script:

#!/bin/sh
vm='macOS Sierra'
s=4 # 0=640x480, 1=800x600, 2=1024x768, 3=1280x1024, 4=1440x900, 5=1920x1200
VBoxManage modifyvm "$vm" --cpuidset 00000001 000306a9 00020800 80000201 178bfbff
VBoxManage setextradata "$vm" "VBoxInternal/Devices/efi/0/Config/DmiSystemProduct" "iMac11,3"
VBoxManage setextradata "$vm" "VBoxInternal/Devices/efi/0/Config/DmiSystemVersion" "1.0"
VBoxManage setextradata "$vm" "VBoxInternal/Devices/efi/0/Config/DmiBoardProduct" "Iloveapple"
VBoxManage setextradata "$vm" "VBoxInternal/Devices/smc/0/Config/DeviceKey" "ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
VBoxManage setextradata "$vm" "VBoxInternal/Devices/smc/0/Config/GetKeyFromRealSMC" 1
VBoxManage setextradata "$vm" VBoxInternal2/EfiGopMode $s

(the s value is the screen resolution of the virtual machine; this cannot be changed whilst the machine is running and must be one of the provided options.)

Then boot the VM, selecting the ISO when prompted.

Note the limitations of macOS guests: there are no guest additions for macOS. Connect to a bridged network if you want multicast IP to work.

Also useful is the Apple Support download page.

https://7labs.heypub.com/tips-tricks/macos-sierra-direct-download.html

http://www.chrisrjones.com/articles/How-to-get-the-broadcom-pci-1570-web-cam-working-in-Linux-using-qemu-with-OS-X-VM
http://osxdaily.com/2010/03/19/anti-piracy-message-in-mac-os-x-kernel-extension
https://gist.github.com/TheMengzor/6cc7caa68256aa4871cf

https://www.howtogeek.com/289594/how-to-install-macos-sierra-in-virtualbox-on-windows-10/

Developer tools

To install the command-line deveoper tools, open a terminal window and do

$ xcode-select --install

To install Git, see https://git-scm.com/download/mac

Keyboard

If you have a non-Apple keyboard then you may want to use a more appropriate keyboard mapping. You can select keyboard mappings through the keyboard tab of the System Preferences dialog. Select Input Sources and use the + button to select a mapping appropriate for your keyboard. (You may also need to use the - button to delete the mapping that you do not wish to use before your chosen mapping becomes fully effective.)

If you find that the mappings provided by Apple to be unsatisfactory (Apple's British - PC keyboard does not map the pipe to the correct key (adjacent to the left-hand shift key) then you may install third-party layouts or create your own.

Here is a ZIP file containing two alternative layouts files for UK 105 key keyboards. You should unzip the files into /Library/Keyboard Layouts (use sudo).

The new layouts won't be available in the abovementioned Input Sources until you log out and back in, After that, you can select them like any other layout (they may be listed in the others category).

You can edit your chosen layout if it is still unsatisfactory (I chose the British (PC 105 alt) layout but found that Alt+3 emitted cent (¢) instead of the expected Euro (€) symbol). Layout files are simple XML text files. I identified the relevant key code and replaced:

<key code="21" output="&#xA2;" />

with

<key code="21" output="&#x20AC;" />

(The change was applied in two places: keymap index 3, for anyOption key (Alt to PC keyboard users), and keymap index 5 for the same with CapsLock on.)

(Once again, a log out/in cycle is required for the change to take effect).

The key code for the 4 key (which, when used with Alt should produce ) is 21. Should you wish to discover the key code for a physical key then you could use the Key Codes app, available free of charge from the Apple App Store.

Should you desire a more GUI way of customising your keyboard then you could look at Ukelele [sic] or Karabiner, formerly known as KeyRemap4MacBook (although the website states that Karabiner doesn't currently work on macOS Sierra at the moment.)

Update

I applied the OS update to version 10.12.4: