debian: macOS guest on VirtualBox - create installation media short note.

SYSTEM
OS: Debian Linux 4.18.0-3-amd64
CPU: AMD Ryzen 7 1700 Eight-Core Processor
VirtualBox: virtualbox-5.2 : 5.2.20-125813~Ubuntu~bionic

STEP
1. CREATE VM
2. MODIFY VM
3. BOOT INSTALLER
4. BOOT NEW OS
5. CREATE NEXT VERSION CDR INSTALLER
6. FIX VirtualBox NOT SUPPORT apfs IN High Sierra/Mojave

1. CREATE VM
General
Machine -> New
Name="NAME OF VM"
Type=Mac OS X
Version=Mac OS X (64-bit)

Storage
HDD=NEW VDI DISK IMAGE (20-60G)
CD=CDR INSTALLER

2. MODIFY VM
RUN COMMANDS:

VM_RES="1280x960"	#EXAMPLE
NAME="NAME OF VM"	#EXAMPLE

VBoxManage modifyvm "$NAME" --usbxhci on --firmware efi --chipset ich9 --mouse usbtablet --keyboard usb --vram 128 --cpus 2 --memory 4096 --boot1 dvd --boot2 disk --boot3 none --boot4 none --cpu-profile "Intel Core i7-6700K" --cpuidset 00000001 000106e5 00100800 0098e3fd bfebfbff
VBoxManage setextradata "$NAME" "CustomVideoMode1" "${VM_RES}x32"
VBoxManage setextradata "$NAME" VBoxInternal2/EfiGraphicsResolution "$VM_RES"
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

#FOR Snow Leopard: NUMBER OF CPU=1
VBoxManage modifyvm "$NAME" --usbxhci on --firmware efi --chipset ich9 --mouse usbtablet --keyboard usb --vram 128 --cpus 1 --memory 4096 --boot1 dvd --boot2 disk --boot3 none --boot4 none --cpu-profile "Intel Core i7-6700K" --cpuidset 00000001 000106e5 00100800 0098e3fd bfebfbff

#FOR Sierra: SOLVE PARAVIRTUALIZATION BUG
#https://gist.github.com/arobb/447a962af4f07ef81e79987d686275e5
#https://www.insanelymac.com/forum/topic/292170-how-to-spoof-real-mac-in-vmware/
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
#REVERT BACK WHEN FINISH INSTALLATION
VBoxManage modifyvm "$NAME" --paravirtprovider default

##SOME USEFUL SETTINGS, DO NOT RUN
##BOOT SINGLE USER
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs "-s"
##REVERT
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs ""
##TURN OFF
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs " "
##VERBOSE
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs "-v"
##NO AMD, NO NVIDIA
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs "-s agc=0 -amd_no_dgpu_accel nv_disable=1"
##DEBUG
#VBoxManage setextradata "$NAME" VBoxInternal2/EfiBootArgs "-v debug=0x144"

#IF MOUSE OR KEYBOARD DO NOT WORK, TRY TO ADD NEW USB FILTERS

3. BOOT INSTALLER
#STEP
#- ERASE DISK
#- INSTALL
#FOR Mavericks NETWORK MAY NOT WORK, JUST INSTALL LOCALLY, WILL WORK AFTER REBOOT.

4. BOOT NEW OS

5. CREATE NEXT VERSION CDR INSTALLER
#GET PACKAGES FROM SAFARI, DO NOT INSTALL:

#LION
https://itunes.apple.com/app/id444303913?mt=12

#MOUNTAIN LION
https://itunes.apple.com/app/id537386512?mt=12

#MAVERICKS
https://itunes.apple.com/app/id675248567?mt=12

#YOSEMITE
https://itunes.apple.com/app/id444303913?mt=12

#EL CAPITAN
https://itunes.apple.com/app/id1147835434?mt=12

#SIERRA
https://itunes.apple.com/app/id1127487414?mt=12

#HIGH SIERRA
https://itunes.apple.com/app/id1246284741?mt=12

#MOJAVE
https://itunes.apple.com/app/id1398502828?mt=12
#IF FAIL, DOWNLOAD FROM HERE
#http://osxdaily.com/2018/09/29/download-full-macos-mojave-installer/
#http://dosdude1.com/mojave/
#AFTER FINISH INSTALLATION, RE-DOWNLOAD FROM APPLE AGAIN, THEN RE-CREATE INSTALLER FROM MOJAVE

#SOME LINK ARE DEAD, PLEASE DOWNLOAD FROM "iTunes -> Purchased"

#START BUILD
FILE="ElCapitan"	#EXAMPLE
APPDIR="/Applications/Install OS X El Capitan.app"	#EXAMPLE

SIZE=8000

#CREATE IMAGE AND MOUNT
hdiutil create -o /tmp/$FILE -size ${SIZE}m -layout SPUD -fs HFS+J  # -> /tmp/$FILE.dmg
hdiutil attach /tmp/$FILE.dmg -noverify -nobrowse 	# -> /Volumes/untitled

#***** asr METHOD ***** (<=Yosemite, El Capitan(CDR), Sierra)
#MOUNT InstallESD
hdiutil attach "$APPDIR/Contents/SharedSupport/InstallESD.dmg"  # -> /Volumes/OS X Install ESD
ESD="/Volumes/OS X Install ESD"

#FOR <= Yosemite, Sierra:
#RESTORE BaseSystem TO IMAGE
asr restore -source "$ESD/BaseSystem.dmg" -target /Volumes/untitled -noprompt -noverify -erase    #/Volumes/untitled -> /Volumes/OS X Base System

#COPY PACKAGES
rm "/Volumes/OS X Base System/System/Installation/Packages"
cp -av "$ESD/Packages" "/Volumes/OS X Base System/System/Installation/"


#FOR >= Yosemite:
cp -av "$ESD/BaseSystem.dmg" "/Volumes/OS X Base System/"
cp -av "$ESD/BaseSystem.chunklist" "/Volumes/OS X Base System/"

#FOR >= El Capitan(CDR):
cp -av "$ESD/AppleDiagnostics.dmg" "/Volumes/OS X Base System/"
cp -av "$ESD/AppleDiagnostics.chunklist" "/Volumes/OS X Base System/"

#UNMOUNT ALL
hdiutil detach "/Volumes/OS X Base System"
hdiutil detach "$ESD"

#***** createinstallmedia METHOD ***** (El Capitan(VDI), High Sierra, Mojave)
#CREATE INSTALLER TO IMAGE
#FOR El Capitan(VDI) (USE VDI INSTEAD OF CDR - NEED WRITABLE MEDIA WHEN INSTALL), >= High Sierra:
sudo "$APPDIR/Contents/Resources/createinstallmedia" --volume /Volumes/untitled/ --applicationpath "$APPDIR" --nointeraction    #/Volumes/untitles -> /Volumes/Install OS X XXX

#UNMOUNT BASE
hdiutil detach "/Volumes/Install XXX"

#***** END 2 METHOD *****

#RESIZE IMAGE FOR BEST FIT
hdiutil resize -size `hdiutil resize -limits /tmp/$FILE.dmg | tail -n 1 | awk '{ print $1 }'`b /tmp/$FILE.dmg

#CONVERT IMAGE TO CDR
hdiutil convert /tmp/$FILE.dmg -format UDTO -o /tmp/$FILE  # -> /tmp/$FILE.cdr

#COPY TO HOST
scp /tmp/$FILE.cdr USER1@HOST1:/WHERE/TO/STORE/IMAGE

6. FIX VirtualBox NOT SUPPORT apfs IN High Sierra/Mojave

#FOR Mojave FIRST BOOT 
fs1:
cd macOS\ Install\ Data
cd Locked\ Files
cd Boot\ Files
boot.efi

#PREPARE apfs DRIVER FOR NEXT BOOT
#https://github.com/acidanthera/AppleSupportPkg
#AT Debian HOST
mkdir /tmp/tmp
cd /tmp/tmp
wget https://github.com/acidanthera/AppleSupportPkg/releases/download/2.0.5/AppleSupport-v2.0.5-RELEASE.zip
unzip AppleSupport-v2.0.5-RELEASE.zip
cd Drivers/
genisoimage -o ../driver.iso .

#ATTACH /tmp/tmp/driver.iso TO Mojave GUEST OPTICAL DRIVE STORAGE
#BOOT Mojave GUEST
fs1:
load APFSDRIV.EFI
load USBKBDXE.EFI
load APPLEUIS.EFI
map -r
fs1:
cd System\Library\CoreServices
boot.efi
#IF FAILED, TRY TO LOAD JUST APFSDRIV.EFI

#AFTER FINISH INSTALLATION, CREATE startup.nsh FOR NEXT BOOT
#https://forums.virtualbox.org/viewtopic.php?f=22&t=88258&sid=20c97a3fb77e1578f58f3b1fd74460eb&start=15
#https://github.com/img2tab/okiomov/blob/master/macos_okiomov.sh
#AT Mojave GUEST
diskutil list
sudo mkdir /Volumes/efi
sudo mount -t msdos /dev/disk1s1 /Volumes/efi
mkdir -p /Volumes/efi/EFI/DRIVERS
cp -av /Volumes/CDROM/* /Volumes/efi/EFI/DRIVERS/
ls /Volumes/efi/EFI/DRIVERS/ >> /Volumes/efi/startup.nsh
vi /Volumes/efi/startup.nsh
load fs0:\EFI\DRIVERS\APFSDRIV.EFI
load fs0:\EFI\DRIVERS\USBKBDXE.EFI
load fs0:\EFI\DRIVERS\APPLEUIS.EFI
map -r
for %a run (1 5)
  fs%a:
  cd "macOS Install Data\Locked Files\Boot Files"
  boot.efi
  cd "System\Library\CoreServices"
  boot.efi
endfor
#SHUTDOWN GUEST AND DETACH driver.iso
#IF FAILED, TRY TO LOAD JUST APFSDRIV.EFI

##### SOME USEFUL LINK/COMMANDS #####

#IF BOOT FAILED, TRY TO BOOT...
#FROM: fs1:>macOS Install Data\Locked Files\Boot Files\boot.efi
#OR: fs1:>System\Library\CoreServices\boot.efi

#CREATE BOOTABLE INSTALLER
#https://support.apple.com/th-th/HT201372

#SOLVE "Install macOS High Sierra.app does not appear to be a valid OS installer application."
#https://www.reddit.com/r/MacOS/comments/7470rb/install_macos_high_sierraapp_does_not_appear_to/

sudo cp -av /macOS Install Data /Applications/Install macOS High Sierra.app/Contents/SharedSupport

#CREATE High Sierra ISO INSTALLER
#https://www.howtogeek.com/289594/how-to-install-macos-sierra-in-virtualbox-on-windows-10/

#REF
#https://github.com/rtrouton/create_macos_vm_install_dmg

#Mojave
#https://astr0baby.wordpress.com/2018/09/25/running-macos-mojave-10-14-on-virtualbox-5-2-18-on-linux-x86_64/
MOJAVE