Setting up macOS on virtualbox

## VirtualBox

6.1 (or later) should be used as it has built-in APFS boot support.

## Making ISO

### Sierra

- https://qiita.com/sho7650/items/c24aa487938ac177de01
- This method actually works for Sierra/ElCapitan/Yosemite/Mavericks.
    - (MountainLion/Lion just require to convert InstallESD.dmg.)
- https://support.apple.com/en-us/HT201372 's Sierra InstallOS.dmg has issues around createmedia, but this script is not affected.
- It is possible to extract InstallESD.dmg from InstallOS.dmg or InstallMacOSX.dmg using xar command.

```sh:sierra.bash
hdiutil attach ./InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/esd
hdiutil create -o Sierra.cdr -size 7316m -layout SPUD -fs HFS+J
hdiutil attach Sierra.cdr.dmg -noverify -nobrowse -mountpoint /Volumes/iso
asr restore -source /Volumes/esd/BaseSystem.dmg -target /Volumes/iso -noprompt -noverify -erase
rm /Volumes/OS\ X\ Base\ System/System/Installation/Packages
cp -rp /Volumes/esd/Packages /Volumes/OS\ X\ Base\ System/System/Installation
cp -rp /Volumes/esd/BaseSystem.chunklist /Volumes/OS\ X\ Base\ System/
cp -rp /Volumes/esd/BaseSystem.dmg /Volumes/OS\ X\ Base\ System/
hdiutil detach /Volumes/esd
hdiutil detach /Volumes/OS\ X\ Base\ System
hdiutil convert Sierra.cdr.dmg -format UDTO -o Sierra.iso
mv Sierra.iso.cdr Sierra.iso 
rm Sierra.cdr.dmg
```

### HighSierra/Mojave/Catalina

- I recommend cloning https://github.com/kholia/OSX-KVM/ and use `create_iso_*.sh`. Note that git is preinstalled on macOS.
- By the way, note that 10.15.2+ are not supported yet.
    - https://forums.virtualbox.org/viewtopic.php?f=22&t=96078

```sh
git clone https://github.com/kholia/OSX-KVM/
```

## Basic setup

```sh
NAME=macOS
VBoxManage createvm --name "$NAME"
VBoxManage registervm "$HOME/VirtualBox VMs/$NAME/$NAME.vbox"
VBoxManage modifyvm "$NAME" --ostype MacOS1013_64
VBoxManage modifyvm "$NAME" --memory 4096
VBoxManage modifyvm "$NAME" --acpi on
VBoxManage modifyvm "$NAME" --ioapic on
VBoxManage modifyvm "$NAME" --cpus 2
VBoxManage modifyvm "$NAME" --nic1 nat --nictype1 82540EM
VBoxManage modifyvm "$NAME" --chipset piix3
VBoxManage modifyvm "$NAME" --vram 64
VBoxManage modifyvm "$NAME" --firmware efi64
VBoxManage modifyvm "$NAME" --usb on --usbxhci on
VBoxManage modifyvm "$NAME" --mouse usbtablet
VBoxManage modifyvm "$NAME" --cpuidset 00000001 000106e5 00100800 0098e3fd bfebfbff
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/efi/0/Config/DmiSystemProduct" "iMac11,3"
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/efi/0/Config/DmiSystemVersion" "1.0"
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/efi/0/Config/DmiBoardProduct" "Iloveapple"
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/smc/0/Config/DeviceKey" "ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/smc/0/Config/GetKeyFromRealSMC" 1
VBoxManage storagectl "$NAME" --name SATA --add sata --bootable on
```

## Storage setup

```sh
VBoxManage createhd --size 50000 --variant standard --filename "$HOME/VirtualBox VMs/$NAME/$NAME.vdi"
VBoxManage storageattach "$NAME" --storagectl SATA --port 1 --type hdd --medium "$HOME/VirtualBox VMs/$NAME/$NAME.vdi"
VBoxManage storageattach "$NAME" --storagectl SATA --port 2 --type dvddrive --medium OSX.iso
```

## Sierra hack

- If the Sierra is 10.12.4 (or 5/6), virtualbox cannot startup the ISO.
    - https://www.virtualbox.org/ticket/16644
    - Thanks to widhaya3 (https://www.virtualbox.org/ticket/16644#comment:33), we could implement hackintosh approach.

- Maybe nvram cannot be shared with/without this hack. Be careful.

```sh
VBoxManage modifyvm "$NAME" --ostype MacOS_64
VBoxManage modifyvm "$NAME" --chipset ich9
VBoxManage modifyvm "$NAME" --system-uuid-le off
VBoxManage modifyvm "$NAME" --usbxhci on
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/efi/0/Config/DmiBoardProduct" "Mac-F22589C8"
VBoxManage setextradata "$NAME" "VBoxInternal/Devices/efi/0/Config/DmiSystemSerial" "CK1156I6DB6"
VBoxManage modifyvm "$NAME" --paravirtprovider none
# VBoxManage modifyvm "$NAME" --paravirtprovider default # apply after installation
```