This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
macos-guest-virtualbox.sh
executable file
·1745 lines (1607 loc) · 83.6 KB
/
macos-guest-virtualbox.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash -i
# Push-button installer of macOS on VirtualBox
# (c) myspaghetti, licensed under GPL2.0 or higher
# url: https://github.com/myspaghetti/macos-virtualbox
# version 0.99.2.4
# Dependencies: bash coreutils gzip unzip wget xxd dmg2img
# Optional features: tesseract-ocr tesseract-ocr-eng
# Supported versions:
# VirtualBox >= 6.1.6 dmg2img >= 1.6.5
# GNU bash >= 4.3 GNU coreutils >= 8.22
# GNU gzip >= 1.5 GNU wget >= 1.14
# Info-ZIP unzip >= 6.0 xxd with -e little endian support
# tesseract-ocr >= 4
function set_variables() {
# Customize the installation by setting these variables:
vm_name="macOS" # name of the VirtualBox virtual machine
macOS_release_name="Catalina" # install "HighSierra" "Mojave" or "Catalina"
storage_size=80000 # VM disk image size in MB, minimum 22000
storage_format="vdi" # VM disk image file format, "vdi" or "vmdk"
cpu_profile="host" # VM CPU profile, see "CPU profiles" in docs
cpu_count=2 # VM CPU cores, minimum 2
memory_size=4096 # VM RAM in MB, minimum 2048
gpu_vram=128 # VM video RAM in MB, minimum 34, maximum 128
resolution="1280x800" # VM display resolution
# Values for NVRAM and EFI parameters are required by iCloud, iMessage,
# and other connected Apple applications, but otherwise not required.
# Parameters taken from a genuine Mac may result in a "Call customer support"
# message if they do not match the genuine Mac exactly.
# Non-genuine yet genuine-like parameters usually work.
# Assigning the following parameters is not required when installing or using macOS.
DmiSystemFamily="MacBook Pro" # Model Name
DmiSystemProduct="MacBookPro11,2" # Model Identifier
DmiBIOSVersion="string:MBP7.89" # Boot ROM Version
DmiSystemSerial="NO_DEVICE_SN" # Serial Number (system)
DmiSystemUuid="CAFECAFE-CAFE-CAFE-CAFE-DECAFFDECAFF" # Hardware UUID
ROM='%aa*%bbg%cc%dd' # ROM identifier
MLB="NO_LOGIC_BOARD_SN" # MLB SN stored in NVRAM
DmiBoardSerial="${MLB}" # MLB SN stored in EFI
DmiBoardProduct="Mac-3CBD00234E554E41" # Product (board) identifier
SystemUUID="aabbccddeeff00112233445566778899" # System UUID
# If the script is running on macOS and "get_parameters_from_macOS_host" is
# set to "yes", the script will attempt to get the host's EFI and NVRAM
# parameters and override the above EFI and NVRAM parameters.
get_parameters_from_macOS_host="no"
if [[ "$(sw_vers 2>/dev/null)" && "${get_parameters_from_macOS_host}" =~ [Yy] ]]; then
# These values are taken from a genuine Mac...
hardware_overview="$(system_profiler SPHardwareDataType)"
model_name="${hardware_overview##*Model Name: }"; model_name="${model_name%%$'\n'*}"
model_identifier="${hardware_overview##*Model Identifier: }"; model_identifier="${model_identifier%%$'\n'*}"
boot_rom_ver="${hardware_overview##*Boot ROM Version: }"; boot_rom_ver="${boot_rom_ver%%$'\n'*}"
sn_system="${hardware_overview##*Serial Number (system): }"; sn_system="${sn_system%%$'\n'*}"
hardware_uuid="${hardware_overview##*Hardware UUID: }"; hardware_uuid="${hardware_uuid%%$'\n'*}"
nvram_rom="$(nvram 4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:ROM)"; nvram_rom="${nvram_rom##*$'\t'}"
nvram_mlb="$(nvram 4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:MLB)"; nvram_mlb="${nvram_mlb##*$'\t'}"
ioreg_board_id="$(ioreg -p "IODeviceTree" -r -n / -d 1)"; ioreg_board_id="${ioreg_board_id##*board-id\" = <\"}"; ioreg_board_id="${ioreg_board_id%%\">*}"
ioreg_system_id="$(ioreg -p "IODeviceTree" -n platform -r)"; ioreg_system_id="${ioreg_system_id##*system-id\" = <}"; ioreg_system_id="${ioreg_system_id%%>*}"
# ...and set in VirtualBox EFI and NVRAM...
DmiSystemFamily="${model_name}" # Model Name
DmiSystemProduct="${model_identifier}" # Model Identifier
DmiBIOSVersion="string:${boot_rom_ver}" # Boot ROM Version
DmiSystemSerial="${sn_system}" # Serial Number (system)
DmiSystemUuid="${hardware_uuid}" # Hardware UUID
ROM="${nvram_rom}" # ROM identifier, stored in NVRAM
MLB="${nvram_mlb}" # MLB SN, stored in NVRAM
DmiBoardSerial="${nvram_mlb}" # MLB SN, stored in EFI
DmiBoardProduct="${ioreg_board_id}" # Product (board) identifier
SystemUUID="${ioreg_system_id}" # System UUID, stored in NVRAM
fi
system_integrity_protection='10' # '10' - enabled, '77' - disabled
# Additional configurations may be saved in external files and loaded with the
# following command prior to executing the script:
# export macos_vm_vars_file=/path/to/variable_assignment_file
# "variable_assignment_file" is a plain text file that contains zero or more
# lines with a variable assignment for any variable specified above.
[[ -r "${macos_vm_vars_file}" ]] && source "${macos_vm_vars_file}"
}
# welcome message
function welcome() {
echo -ne "\n${highlight_color}Push-button installer of macOS on VirtualBox${default_color}
This script installs only open-source software and unmodified Apple binaries,
and requires about ${highlight_color}50GB${default_color} of available storage, of which 25GB are for temporary
installation files that may be deleted when the script is finished.
The script interacts with the virtual machine twice, ${highlight_color}please do not interact${default_color}
${highlight_color}with the virtual machine manually${default_color} before the script is finished.
Documentation about optional configuration, ${highlight_color}iCloud and iMessage connectivity${default_color},
resuming the script by stages, and other topics can be viewed with the
following command:
"
would_you_like_to_know_less
echo -ne "\n${highlight_color}Press enter to review the script configuration${default_color}"
clear_input_buffer_then_read
function pad_to_33_chars() {
local padded="${1} "
echo "${padded:0:33}${2}"
}
# custom settings prompt
echo -e "\nvm_name=\"${vm_name}\""
pad_to_33_chars "macOS_release_name=\"${macOS_release_name}\"" "# install \"HighSierra\" \"Mojave\" \"Catalina\""
pad_to_33_chars "storage_size=${storage_size}" "# VM disk image size in MB, minimum 22000"
pad_to_33_chars "storage_format=\"${storage_format}\"" "# VM disk image file format, \"vdi\" or \"vmdk\""
pad_to_33_chars "cpu_profile=\"${cpu_profile}\"" "# VM CPU profile, see \"CPU profiles\" in docs"
pad_to_33_chars "cpu_count=${cpu_count}" "# VM CPU cores, minimum 2"
pad_to_33_chars "memory_size=${memory_size}" "# VM RAM in MB, minimum 2048"
pad_to_33_chars "gpu_vram=${gpu_vram}" "# VM video RAM in MB, minimum 34, maximum 128"
pad_to_33_chars "resolution=\"${resolution}\"" "# VM display resolution"
echo -ne "\nThese values may be customized as described in the documentation.\n
${highlight_color}Press enter to continue, CTRL-C to exit${default_color}"
clear_input_buffer_then_read
}
# check dependencies
function check_shell() {
if [[ -n "${BASH_VERSION}" && -n "${ZSH_VERSION}" ]]; then
echo "The script cannot determine if it is executed on bash or zsh."
echo "Please explicitly execute the script on the same shell as the interactive shell,"
echo -e "for example, for zsh:\n"
echo " ${highlight_color}zsh -i macos-guest-virtualbox.sh${default_color}"
exit
elif [[ -n "${BASH_VERSION}" ]]; then
if [[ ! ( "${BASH_VERSION:0:1}" -ge 5
|| "${BASH_VERSION:0:3}" =~ 4\.[3-9]
|| "${BASH_VERSION:0:4}" =~ 4\.[12][0-9] ) ]]; then
echo "Please execute this script with Bash 4.3 or higher, or zsh 5.5 or higher."
if [[ -n "$(sw_vers 2>/dev/null)" ]]; then
echo "macOS detected. Make sure the script is not executed with the default /bin/bash"
echo "which is version 3. Explicitly type the executable path, for example for zsh:"
echo " ${highlight_color}/path/to/5.5/zsh macos-guest-virtualbox.sh${default_color}"
fi
exit
fi
elif [[ -n "${ZSH_VERSION}" ]]; then
if [[ ( "${ZSH_VERSION:0:1}" -ge 6
|| "${ZSH_VERSION:0:3}" =~ 5\.[5-9]
|| "${ZSH_VERSION:0:4}" =~ 5\.[1-4][0-9] ) ]]; then
# make zsh parse the script (almost) like bash
setopt extendedglob sh_word_split ksh_arrays posix_argzero nullglob bsd_echo
else
echo "Please execute this script with zsh version 5.5 or higher."
exit
fi
else
echo "The script appears to be executed on a shell other than bash or zsh. Exiting."
exit
fi
if [[ ! $- =~ i ]]; then # terminal is not interactive
echo "The shell appears to be executed non-interactively. If this is not the case,"
echo "please press CTRL-C and run the script under an interactive shell, for example"
echo -e "${highlight_color}bash -i macos-guest-virtualbox.sh${default_color} or ${highlight_color}zsh -i macos-guest-virtualbox.sh${default_color}\n"
echo "Otherwise, the script will continue running non-interactively."
animated_please_wait 5
echo ""
tesseract_ocr="$(tesseract --version 2>/dev/null)"
tesseract_lang="$(tesseract --list-langs 2>/dev/null)"
regex_ver='[Tt]esseract [45]' # for zsh quoted regex compatibility
if [[ ! ( "${tesseract_ocr}" =~ ${regex_ver} ) || -z "${tesseract_lang}" ]]; then
echo "Running the script on a non-interactive shell requires the following packages:"
echo -e " tesseract-ocr >= 4 tesseract-ocr-eng\n"
echo "Exiting."
animated_please_wait 5
echo ""
exit
fi
fi
}
function check_gnu_coreutils_prefix() {
if [[ -n "$(gcsplit --help 2>/dev/null)" ]]; then
function base64() {
gbase64 "$@"
}
function csplit() {
gcsplit "$@"
}
function expr() {
gexpr "$@"
}
function ls() {
gls "$@"
}
function split() {
gsplit "$@"
}
function tac() {
gtac "$@"
}
function seq() {
gseq "$@"
}
function sort() {
gsort "$@"
}
fi
}
function check_dependencies() {
# check environment for macOS and non-GNU coreutils
if [[ -n "$(sw_vers 2>/dev/null)" ]]; then
# Add Homebrew GNU coreutils to PATH if path exists
homebrew_gnubin="/usr/local/opt/coreutils/libexec/gnubin"
if [[ -d "${homebrew_gnubin}" ]]; then
PATH="${homebrew_gnubin}:${PATH}"
fi
# if csplit isn't GNU variant, exit
if [[ -z "$(csplit --help 2>/dev/null)" ]]; then
echo -e "\nmacOS detected.\nPlease use a package manager such as ${highlight_color}homebrew${default_color}, ${highlight_color}pkgsrc${default_color}, ${highlight_color}nix${default_color}, or ${highlight_color}MacPorts${default_color}"
echo "Please make sure the following packages are installed and that"
echo "their path is in the PATH variable:"
echo -e " ${highlight_color}bash coreutils dmg2img gzip unzip wget xxd${default_color}"
echo "Please make sure Bash and coreutils are the GNU variant."
exit
fi
fi
# check for gzip, unzip, coreutils, wget
if [[ -z "$(gzip --help 2>/dev/null)" ||
-z "$(unzip -hh 2>/dev/null)" ||
-z "$(csplit --help 2>/dev/null)" ||
-z "$(wget --version 2>/dev/null)" ]]; then
echo "Please make sure the following packages are installed"
echo -e "and that they are of the version specified or newer:\n"
echo " coreutils 8.22 wget 1.14 gzip 1.5 unzip 6.0"
echo -e "\nPlease make sure the coreutils and gzip packages are the GNU variant."
exit
fi
# check that xxd supports endianness -e flag
if [[ -z "$(xxd -e -p -l 16 /dev/urandom 2>/dev/null)" ]]; then
echo "Please make sure a version of xxd which supports the -e option is installed."
echo -e "The -e option should be listed when executing ${low_contrast_color}xxd --help${default_color}"
echo "The package vim-common-8 provides a compatible version on most modern distros."
exit
fi
# wget supports --show-progress from version 1.16
regex='1\.1[6-9]|1\.[2-9][0-9]' # for zsh quoted regex compatibility
if [[ "$(wget --version 2>/dev/null | head -n 1)" =~ ${regex} ]]; then
wgetargs="--quiet --continue --show-progress --timeout=60 --secure-protocol=PFS" # pretty
else
wgetargs="--continue" # ugly
fi
# VirtualBox in ${PATH}
# Cygwin
if [[ -n "$(cygcheck -V 2>/dev/null)" ]]; then
if [[ -n "$(cmd.exe /d/s/c call VBoxManage.exe -v 2>/dev/null)" ]]; then
function VBoxManage() {
cmd.exe /d/s/c call VBoxManage.exe "$@"
}
else
cmd_path_VBoxManage='C:\Program Files\Oracle\VirtualBox\VBoxManage.exe'
echo "Can't find VBoxManage in PATH variable,"
echo "checking ${cmd_path_VBoxManage}"
if [[ -n "$(cmd.exe /d/s/c call "${cmd_path_VBoxManage}" -v 2>/dev/null)" ]]; then
function VBoxManage() {
cmd.exe /d/s/c call "${cmd_path_VBoxManage}" "$@"
}
echo "Found VBoxManage"
else
echo "Please make sure VirtualBox version 5.2 or higher is installed, and that"
echo "the path to the VBoxManage.exe executable is in the PATH variable, or assign"
echo "in the script the full path including the name of the executable to"
echo -e "the variable ${highlight_color}cmd_path_VBoxManage${default_color}"
exit
fi
fi
# Windows Subsystem for Linux 2 (WSL2 or WSLg)
elif [[ "$(cat /proc/sys/kernel/osrelease 2>/dev/null)" =~ WSL[2Gg] ]]; then # WSL2 or WSLg
if [[ -n "$(VBoxManage -v 2>/dev/null)" ]]; then
if [[ ! "$(lsmod)" =~ vboxdrv ]]; then # if the vboxdrv kernel module is not present
echo "The script appears to be executed on WSL2 or WSLg."
echo "Mind that WSL2 and WSLg require kernel module compilation and"
echo "custom configuration that is not supported by the script."
echo "If the script does not detect the ${highlight_color}vboxdrv${default_color} kernel module, it exits."
exit
fi
else
echo "Please make sure VirtualBox version 5.2 or higher and its kernel module"
echo "are installed, and that the path to the VBoxManage executable"
echo "is in the PATH variable."
exit
fi
# Windows Subsystem for Linux (WSL "1")
elif [[ "$(cat /proc/sys/kernel/osrelease 2>/dev/null)" =~ [Mm]icrosoft ]]; then
if [[ -n "$(VBoxManage.exe -v 2>/dev/null)" ]]; then
function VBoxManage() {
VBoxManage.exe "$@"
}
else
wsl_path_VBoxManage='/mnt/c/Program Files/Oracle/VirtualBox/VBoxManage.exe'
echo "Can't find VBoxManage in PATH variable,"
echo "checking ${wsl_path_VBoxManage}"
if [[ -n "$("${wsl_path_VBoxManage}" -v 2>/dev/null)" ]]; then
PATH="${PATH}:${wsl_path_VBoxManage%/*}"
function VBoxManage() {
VBoxManage.exe "$@"
}
echo "Found VBoxManage"
else
echo "Please make sure VirtualBox is installed on Windows, and that the path to the"
echo "VBoxManage.exe executable is in the PATH variable, or assigned in the script"
echo -e "to the variable \"${highlight_color}wsl_path_VBoxManage${default_color}\" including the name of the executable."
exit
fi
fi
# everything else (not cygwin and not wsl)
elif [[ -z "$(VBoxManage -v 2>/dev/null)" ]]; then
echo "Please make sure VirtualBox version 5.2 or higher and its kernel module"
echo "are installed, and that the path to the VBoxManage executable"
echo "is in the PATH variable."
exit
fi
# VirtualBox version
vbox_version="$(VBoxManage -v 2>/dev/null)"
vbox_version="${vbox_version//[$'\r\n']/}"
if [[ -z "${vbox_version}" || -z "${vbox_version:2:1}" ]]; then
echo "Can't determine VirtualBox version. Exiting."
exit
elif [[ ! ( "${vbox_version:0:1}" -gt 5
|| "${vbox_version:0:3}" =~ 5\.2 ) ]]; then
echo -e "\nPlease make sure VirtualBox version 5.2 or higher is installed."
echo "Exiting."
exit
elif [[ "${vbox_version:0:1}" = 5 ]]; then
echo -e "\n${highlight_color}VirtualBox version ${vbox_version} detected.${default_color} Please see the following"
echo -ne "URL for issues with the VISO filesystem on VirtualBox 5.2 to 5.2.40:\n\n"
echo " https://github.com/myspaghetti/macos-virtualbox/issues/86"
echo -ne "\n${highlight_color}Press enter to continue, CTRL-C to exit${default_color}"
clear_input_buffer_then_read
fi
# Oracle VM VirtualBox Extension Pack
extpacks="$(VBoxManage list extpacks 2>/dev/null)"
if [[ "$(expr match "${extpacks}" '.*Oracle VM VirtualBox Extension Pack')" -le "0" ||
"$(expr match "${extpacks}" '.*Usable:[[:blank:]]*false')" -gt "0" ]];
then
echo -e "\nThe command \"VBoxManage list extpacks\" either does not list the Oracle VM"
echo -e "VirtualBox Extension Pack, or lists one or more extensions as unusable."
echo -e "The virtual machine will be configured without USB xHCI controllers."
extension_pack_usb3_support="--usbxhci off"
else
extension_pack_usb3_support="--usbxhci on"
fi
# dmg2img
if ! dmg2img >/dev/null 2>&1; then
if [[ -z "$(cygcheck -V 2>/dev/null)" ]]; then
echo -e "\nPlease install the package dmg2img."
exit
fi
if [[ -z "$("${PWD%%/}/dmg2img.exe" -d 2>/dev/null)" ]]; then
if [[ -z "${PWD}" ]]; then echo "PWD environment variable is not set. Exiting."; exit; fi
echo "Locally installing dmg2img"
wget "https://web.archive.org/web/20190322013244/http://vu1tur.eu.org/tools/dmg2img-1.6.6-win32.zip" \
${wgetargs} \
--output-document="dmg2img-1.6.6-win32.zip"
if [[ ! -s dmg2img-1.6.6-win32.zip ]]; then
echo "Error downloading dmg2img. Please provide the package manually."
exit
fi
unzip -oj "dmg2img-1.6.6-win32.zip" "dmg2img.exe"
command rm "dmg2img-1.6.6-win32.zip"
chmod +x "dmg2img.exe"
[[ ! -x "dmg2img.exe" ]] && echo "Error setting the executable permission for dmg2img.exe. Exiting." && exit
fi
function dmg2img() {
"${PWD%%/}/dmg2img.exe" "$@"
}
fi
# set Apple software update catalog URL according to macOS version
HighSierra_sucatalog='https://swscan.apple.com/content/catalogs/others/index-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog'
Mojave_sucatalog='https://swscan.apple.com/content/catalogs/others/index-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog'
Catalina_sucatalog='https://swscan.apple.com/content/catalogs/others/index-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog'
if [[ "${macOS_release_name:0:1}" =~ [Cc] ]]; then
if [[ ! ( "${vbox_version:0:1}" -gt 6 ||
"${vbox_version}" =~ ^6\.1\.[4-9] ||
"${vbox_version}" =~ ^6\.1\.[123][0-9] ||
"${vbox_version}" =~ ^6\.[2-9] ) ]]; then
echo -e "\nmacOS Catalina requires VirtualBox version 6.1.4 or higher."
echo "Exiting."
exit
fi
fi
if [[ "${macOS_release_name:0:1}" =~ [Cc] ]]; then
macOS_release_name="Catalina"
CFBundleShortVersionString="10.15"
sucatalog="${Catalina_sucatalog}"
elif [[ "${macOS_release_name:0:1}" =~ [Hh] ]]; then
macOS_release_name="HighSierra"
CFBundleShortVersionString="10.13"
sucatalog="${HighSierra_sucatalog}"
elif [[ "${macOS_release_name:0:1}" =~ [Mm] ]]; then
macOS_release_name="Mojave"
CFBundleShortVersionString="10.14"
sucatalog="${Mojave_sucatalog}"
else
echo "Can't parse macOS_release_name. Exiting."
exit
fi
print_dimly "${macOS_release_name} selected to be downloaded and installed"
}
# Done with dependencies
function prompt_delete_existing_vm() {
print_dimly "stage: prompt_delete_existing_vm"
if [[ -n "$(VBoxManage showvminfo "${vm_name}" 2>/dev/null)" ]]; then
echo -e "\nA virtual machine named \"${vm_name}\" already exists."
echo -ne "${warning_color}Delete existing virtual machine \"${vm_name}\"?${default_color}"
prompt_delete_y_n
if [[ "${delete}" == "y" ]]; then
echo "Deleting ${vm_name} virtual machine."
VBoxManage unregistervm "${vm_name}" --delete
else
echo -e "\n${highlight_color}Please assign a different VM name to variable \"vm_name\" by editing the script,${default_color}"
echo "or skip this check manually as described when executing the following command:"
would_you_like_to_know_less
exit
fi
fi
}
# Attempt to create new virtual machine named "${vm_name}"
function create_vm() {
print_dimly "stage: create_vm"
if [[ -n "$( VBoxManage createvm --name "${vm_name}" --ostype "MacOS1013_64" --register 2>&1 >/dev/null )" ]]; then
echo -e "\nError: Could not create virtual machine \"${vm_name}\"."
echo -e "${highlight_color}Please delete exising \"${vm_name}\" VirtualBox configuration files ${warning_color}manually${default_color}.\n"
echo -e "Error message:\n"
VBoxManage createvm --name "${vm_name}" --ostype "MacOS1013_64" --register 2>/dev/tty
exit
fi
}
function check_default_virtual_machine() {
print_dimly "stage: check_default_virtual_machine"
echo -e "\nChecking that VirtualBox starts the virtual machine without errors."
if [[ -n $(VBoxManage startvm "${vm_name}" 2>&1 1>/dev/null) ]]; then
echo -e "Error while starting the virtual machine.\nExiting."
exit
fi
VBoxManage controlvm "${vm_name}" poweroff 2>/dev/null
echo -e "\nChecking that VirtualBox uses hardware-supported virtualization."
vbox_log="$(VBoxManage showvminfo "${vm_name}" --log 0)"
regex='Attempting fall back to NEM' # for zsh quoted regex compatibility
if [[ "${vbox_log}" =~ ${regex} ]]; then
echo -e "\nVirtualbox is not using hardware-supported virtualization features."
if [[ -n "$(cygcheck -V 2>/dev/null)" ||
"$(cat /proc/sys/kernel/osrelease 2>/dev/null)" =~ [Mm]icrosoft ]]; then
echo "Check that software such as Hyper-V, Windows Sandbox, WSL2, memory integrity"
echo "protection, and other Windows features that lock virtualization are turned off."
fi
echo "Exiting."
exit
fi
}
function prepare_macos_installation_files() {
print_dimly "stage: prepare_macos_installation_files"
# Find the correct download URL in the Apple catalog
echo -e "\nDownloading Apple macOS ${macOS_release_name} software update catalog"
wget "${sucatalog}" \
${wgetargs} \
--output-document="${macOS_release_name}_sucatalog"
# if file was not downloaded correctly
if [[ ! -s "${macOS_release_name}_sucatalog" ]]; then
wget --debug --timeout=60 -O /dev/null -o "${macOS_release_name}_wget.log" "${sucatalog}"
echo -e "\nCouldn't download the Apple software update catalog."
if [[ "$(expr match "$(cat "${macOS_release_name}_wget.log")" '.*ERROR[[:print:]]*is not trusted')" -gt "0" ]]; then
echo -e "\nMake sure certificates from a certificate authority are installed."
echo "Certificates are often installed through the package manager with"
echo "a package named ${highlight_color}ca-certificates${default_color}"
fi
echo "Exiting."
exit
fi
echo "Trying to find macOS ${macOS_release_name} InstallAssistant download URL"
tac "${macOS_release_name}_sucatalog" | csplit - '/InstallAssistantAuto.smd/+1' '{*}' -f "${macOS_release_name}_sucatalog_" -s
for catalog in "${macOS_release_name}_sucatalog_"* "error"; do
if [[ "${catalog}" == error ]]; then
command rm "${macOS_release_name}_sucatalog"*
echo "Couldn't find the requested download URL in the Apple catalog. Exiting."
exit
fi
urlbase="$(tail -n 1 "${catalog}" 2>/dev/null)"
urlbase="$(expr match "${urlbase}" '.*\(http://[^<]*/\)')"
wget "${urlbase}InstallAssistantAuto.smd" \
${wgetargs} \
--output-document="${catalog}_InstallAssistantAuto.smd"
if [[ "$(cat "${catalog}_InstallAssistantAuto.smd" )" =~ Beta ]]; then
continue
fi
found_version="$(head -n 6 "${catalog}_InstallAssistantAuto.smd" | tail -n 1)"
if [[ "${found_version}" == *${CFBundleShortVersionString}* ]]; then
echo -e "Found download URL: ${urlbase}\n"
command rm "${macOS_release_name}_sucatalog"*
break
fi
done
echo "Downloading macOS installation files from swcdn.apple.com"
for filename in "BaseSystem.chunklist" \
"InstallInfo.plist" \
"AppleDiagnostics.dmg" \
"AppleDiagnostics.chunklist" \
"BaseSystem.dmg" \
"InstallESDDmg.pkg"; \
do wget "${urlbase}${filename}" \
${wgetargs} \
--output-document "${macOS_release_name}_${filename}"
done
echo -e "\nSplitting the several-GB InstallESDDmg.pkg into 1GB parts because"
echo "VirtualBox hasn't implemented UDF/HFS VISO support yet and macOS"
echo "doesn't support ISO 9660 Level 3 with files larger than 2GB."
split --verbose -a 2 -d -b 1000000000 "${macOS_release_name}_InstallESDDmg.pkg" "${macOS_release_name}_InstallESD.part"
if [[ ! -s "ApfsDriverLoader.efi" ]]; then
echo -e "\nDownloading open-source APFS EFI drivers used for VirtualBox 6.0 and 5.2"
[[ "${vbox_version:0:1}" -gt 6 || ( "${vbox_version:0:1}" = 6 && "${vbox_version:2:1}" -ge 1 ) ]] && echo "...even though VirtualBox version 6.1 or higher is detected."
wget 'https://github.com/acidanthera/AppleSupportPkg/releases/download/2.0.4/AppleSupport-v2.0.4-RELEASE.zip' \
${wgetargs} \
--output-document 'AppleSupport-v2.0.4-RELEASE.zip'
unzip -oj 'AppleSupport-v2.0.4-RELEASE.zip'
fi
}
function create_nvram_files() {
print_dimly "stage: create_nvram_files"
# Each NVRAM file may contain multiple entries.
# Each entry contains a namesize, datasize, name, guid, attributes, and data.
# Each entry is immediately followed by a crc32 of the entry.
# The script creates each file with only one entry for easier editing.
#
# The hex strings are stripped by xxd, so they can
# look like "0xAB 0xCD" or "hAB hCD" or "AB CD" or "ABCD" or a mix of formats
# and have extraneous characters like spaces or minus signs.
# Load the binary files into VirtualBox VM NVRAM with the builtin command dmpstore
# in the VM EFI Internal Shell, for example:
# dmpstore -all -l fs0:\system-id.bin
#
# DmpStore code is available at this URL:
# https://github.com/mdaniel/virtualbox-org-svn-vbox-trunk/blob/master/src/VBox/Devices/EFI/Firmware/ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
function generate_nvram_bin_file() {
# input: name data guid (three positional arguments, all required)
# output: function outputs nothing to stdout
# but writes a binary file to working directory
local namestring="${1}" # string of chars
local filename="${namestring}"
# represent string as string-of-hex-bytes, add null byte after every byte,
# terminate string with two null bytes
local name="$( for (( i = 0 ; i < ${#namestring} ; i++ )); do printf -- "${namestring:${i}:1}" | xxd -p | tr -d '\n'; printf '00'; done; printf '0000' )"
# size of string in bytes, represented by eight hex digits, big-endian
local namesize="$(printf "%08x" $(( ${#name} / 2 )) )"
# flip four big-endian bytes byte-order to little-endian
local namesize="$(printf "${namesize}" | xxd -r -p | xxd -e -g 4 | xxd -r | xxd -p)"
# strip string-of-hex-bytes representation of data of spaces, "x", "h", etc
local data="$(printf -- "${2}" | xxd -r -p | xxd -p)"
# size of data in bytes, represented by eight hex digits, big-endian
local datasize="$(printf "%08x" $(( ${#data} / 2 )) )"
# flip four big-endian bytes byte-order to little-endian
local datasize="$(printf "${datasize}" | xxd -r -p | xxd -e -g 4 | xxd -r | xxd -p)"
# guid string-of-hex-bytes is five fields, 8+4+4+4+12 nibbles long
# first three are little-endian, last two big-endian
# for example, 0F1A2B3C-4D5E-6A7B-8C9D-A1B2C3D4E5F6
# is stored as 3C2B1A0F-5E4D-7B6A-8C9D-A1B2C3D4E5F6
local g="$( printf -- "${3}" | xxd -r -p | xxd -p )" # strip spaces etc
local guid="${g:6:2} ${g:4:2} ${g:2:2} ${g:0:2} ${g:10:2} ${g:8:2} ${g:14:2} ${g:12:2} ${g:16:16}"
# attributes in four bytes little-endian
local attributes="07 00 00 00"
# the data structure
local entry="${namesize} ${datasize} ${name} ${guid} ${attributes} ${data}"
# calculate crc32 using gzip, flip crc32 bytes into big-endian
local crc32="$(printf "${entry}" | xxd -r -p | gzip -c | tail -c8 | xxd -p -l 4)"
# save binary data
printf -- "${entry} ${crc32}" | xxd -r -p - "${vm_name}_${filename}.bin"
}
# MLB
MLB_b16="$(printf -- "${MLB}" | xxd -p)"
generate_nvram_bin_file "MLB" "${MLB_b16}" "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14"
# ROM
# Convert the mixed-ASCII-and-base16 ROM value
# into an ASCII string that represents a base16 number.
ROM_b16="$(for (( i=0; i<${#ROM}; )); do
if [[ "${ROM:${i}:1}" == "%" ]]; then
let j=i+1
echo -n "${ROM:${j}:2}"
let i=i+3
else
x="$(echo -n "${ROM:${i}:1}" | xxd -p | tr -d ' ')"
echo -n "${x}"
let i=i+1
fi
done)"
generate_nvram_bin_file "ROM" "${ROM_b16}" "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14"
# system-id
generate_nvram_bin_file "system-id" "${SystemUUID}" "7C436110-AB2A-4BBB-A880-FE41995C9F82"
# SIP / csr-active-config
generate_nvram_bin_file "csr-active-config" "${system_integrity_protection}" "7C436110-AB2A-4BBB-A880-FE41995C9F82"
}
function create_macos_installation_files_viso() {
print_dimly "stage: create_macos_installation_files_viso"
echo "Creating EFI startup script"
echo 'echo -off' > "${vm_name}_startup.nsh"
if [[ ( "${vbox_version:0:1}" -lt 6 ) || ( "${vbox_version:0:1}" = 6 && "${vbox_version:2:1}" = 0 ) ]]; then
echo 'load fs0:\EFI\OC\Drivers\AppleImageLoader.efi
load fs0:\EFI\OC\Drivers\AppleUiSupport.efi
load fs0:\EFI\OC\Drivers\ApfsDriverLoader.efi
map -r' >> "${vm_name}_startup.nsh"
fi
# EFI Internal Shell 2.1 (VBox 6.0) doesn't support for-loops that start with 0
echo 'if exist "fs0:\EFI\NVRAM\MLB.bin" then
dmpstore -all -l fs0:\EFI\NVRAM\MLB.bin
dmpstore -all -l fs0:\EFI\NVRAM\ROM.bin
dmpstore -all -l fs0:\EFI\NVRAM\csr-active-config.bin
dmpstore -all -l fs0:\EFI\NVRAM\system-id.bin
endif
for %a run (1 5)
if exist "fs%a:\EFI\NVRAM\MLB.bin" then
dmpstore -all -l fs%a:\EFI\NVRAM\MLB.bin
dmpstore -all -l fs%a:\EFI\NVRAM\ROM.bin
dmpstore -all -l fs%a:\EFI\NVRAM\csr-active-config.bin
dmpstore -all -l fs%a:\EFI\NVRAM\system-id.bin
endif
endfor
for %a run (1 5)
if exist "fs%a:\macOS Install Data\Locked Files\Boot Files\boot.efi" then
"fs%a:\macOS Install Data\Locked Files\Boot Files\boot.efi"
endif
endfor
for %a run (1 5)
if exist "fs%a:\System\Library\CoreServices\boot.efi" then
"fs%a:\System\Library\CoreServices\boot.efi"
endif
endfor' >> "${vm_name}_startup.nsh"
echo -e "\nCreating VirtualBox 6 virtual ISO containing the"
echo -e "installation files from swcdn.apple.com\n"
create_viso_header "${macOS_release_name}_installation_files.viso" "${macOS_release_name:0:5}-files"
# add files to viso
# Apple macOS installation files
for filename in "BaseSystem.chunklist" \
"InstallInfo.plist" \
"AppleDiagnostics.dmg" \
"AppleDiagnostics.chunklist" \
"BaseSystem.dmg" ; do
if [[ -s "${macOS_release_name}_${filename}" ]]; then
echo "/${filename}=\"${macOS_release_name}_${filename}\"" >> "${macOS_release_name}_installation_files.viso"
fi
done
if [[ -s "${macOS_release_name}_InstallESD.part00" ]]; then
for part in "${macOS_release_name}_InstallESD.part"*; do
echo "/InstallESD${part##*InstallESD}=\"${part}\"" >> "${macOS_release_name}_installation_files.viso"
done
fi
# NVRAM binary files
for filename in "MLB.bin" "ROM.bin" "csr-active-config.bin" "system-id.bin"; do
if [[ -s "${vm_name}_${filename}" ]]; then
echo "/ESP/EFI/NVRAM/${filename}=\"${vm_name}_${filename}\"" >> "${macOS_release_name}_installation_files.viso"
fi
done
# EFI drivers for VirtualBox 6.0 and 5.2
for filename in "ApfsDriverLoader.efi" "AppleImageLoader.efi" "AppleUiSupport.efi"; do
if [[ -s "${filename}" ]]; then
echo "/ESP/EFI/OC/Drivers/${filename}=\"${filename}\"" >> "${macOS_release_name}_installation_files.viso"
fi
done
# EFI startup script
echo "/ESP/startup.nsh=\"${vm_name}_startup.nsh\"" >> "${macOS_release_name}_installation_files.viso"
}
function configure_vm() {
print_dimly "stage: configure_vm"
if [[ -n "$(
VBoxManage modifyvm "${vm_name}" --cpu-profile "${cpu_profile}" --cpus "${cpu_count}" \
--memory "${memory_size}" --vram "${gpu_vram}" --pae on \
--boot1 none --boot2 none --boot3 none --boot4 none \
--firmware efi --rtcuseutc on --chipset ich9 ${extension_pack_usb3_support} \
--mouse usbtablet --keyboard usb 2>&1 >/dev/null
)" ]]; then
echo -e "\nError: Could not configure virtual machine \"${vm_name}\"."
echo -e "If the VM is powered on, power off the virtual machine and resume the script or"
echo -e "execute the stage ${low_contrast_color}configure_vm${default_color}\n"
echo "Exiting."
exit
fi
VBoxManage setextradata "${vm_name}" \
"VBoxInternal2/EfiGraphicsResolution" "${resolution}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemFamily" "${DmiSystemFamily}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemProduct" "${DmiSystemProduct}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemSerial" "${DmiSystemSerial}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemUuid" "${DmiSystemUuid}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiOEMVBoxVer" "${DmiOEMVBoxVer}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiOEMVBoxRev" "${DmiOEMVBoxRev}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiBIOSVersion" "${DmiBIOSVersion}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiBoardProduct" "${DmiBoardProduct}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiBoardSerial" "${DmiBoardSerial}"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemVendor" "Apple Inc."
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/efi/0/Config/DmiSystemVersion" "1.0"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/smc/0/Config/DeviceKey" \
"ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/Devices/smc/0/Config/GetKeyFromRealSMC" 0
VBoxManage setextradata "${vm_name}" \
"VBoxInternal/TM/TSCMode" "RealTSCOffset" # avoid boot loop when upgrading to Monterey
}
# Create the macOS base system virtual disk image
function populate_basesystem_virtual_disk() {
print_dimly "stage: populate_basesystem_virtual_disk"
[[ -s "${macOS_release_name}_BaseSystem.${storage_format}" ]] && echo "${macOS_release_name}_BaseSystem.${storage_format} bootstrap virtual disk image exists." && return
[[ ! -s "${macOS_release_name}_BaseSystem.dmg" ]] && echo -e "\nCould not find ${macOS_release_name}_BaseSystem.dmg; exiting." && exit
echo "Converting BaseSystem.dmg to BaseSystem.img"
dmg2img "${macOS_release_name}_BaseSystem.dmg" "${macOS_release_name}_BaseSystem.img"
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
VBoxManage closemedium "${macOS_release_name}_BaseSystem.${storage_format}" >/dev/null 2>&1
local success=''
VBoxManage convertfromraw --format "${storage_format}" "${macOS_release_name}_BaseSystem.img" "${macOS_release_name}_BaseSystem.${storage_format}" && local success="True"
if [[ "${success}" = "True" ]]; then
command rm "${macOS_release_name}_BaseSystem.img" 2>/dev/null
return
fi
echo "Failed to create \"${macOS_release_name}_BaseSystem.${storage_format}\"."
if [[ "$(cat /proc/sys/kernel/osrelease 2>/dev/null)" =~ [Mm]icrosoft ]]; then
echo -e "\nSome versions of WSL require the script to execute on a Windows filesystem path,"
echo -e "for example ${highlight_color}/mnt/c/Users/Public/Documents${default_color}"
echo -e "Switch to a path on the Windows filesystem if VBoxManage.exe fails to"
echo -e "create or open a file.\n"
fi
echo "Exiting."
exit
}
# Create the installation media virtual disk image
function create_bootable_installer_virtual_disk() {
print_dimly "stage: create_bootable_installer_virtual_disk"
if [[ -w "${macOS_release_name}_bootable_installer.${storage_format}" ]]; then
echo "\"${macOS_release_name}_bootable_installer.${storage_format}\" virtual disk image exists."
echo -ne "${warning_color}Delete \"${macOS_release_name}_bootable_installer.${storage_format}\"?${default_color}"
prompt_delete_y_n
if [[ "${delete}" == "y" ]]; then
if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]]
then
echo "\"${macOS_release_name}_bootable_installer.${storage_format}\" may be deleted"
echo "only when the virtual machine is powered off."
echo "Exiting."
exit
else
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
VBoxManage closemedium "${macOS_release_name}_bootable_installer.${storage_format}" >/dev/null 2>&1
command rm "${macOS_release_name}_bootable_installer.${storage_format}"
fi
else
echo "Exiting."
exit
fi
fi
if [[ ! -e "${macOS_release_name}_bootable_installer.${storage_format}" ]]; then
echo "Creating ${macOS_release_name} installation media virtual disk image."
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
VBoxManage closemedium "${macOS_release_name}_bootable_installer.${storage_format}" >/dev/null 2>&1
VBoxManage createmedium --size=12000 \
--format "${storage_format}" \
--filename "${macOS_release_name}_bootable_installer.${storage_format}" \
--variant standard 2>/dev/tty
fi
}
function populate_bootable_installer_virtual_disk() {
print_dimly "stage: populate_bootable_installer_virtual_disk"
# Attach virtual disk images of the base system, installation, and target
# to the virtual machine
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
if [[ -n $(
2>&1 VBoxManage storagectl "${vm_name}" --add sata --name SATA --hostiocache on >/dev/null
) ]]; then echo "Could not configure virtual machine storage controller. Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 1 --hotpluggable on \
--type hdd --nonrotational on --medium "${macOS_release_name}_bootable_installer.${storage_format}" >/dev/null
) ]]; then echo "Could not attach \"${macOS_release_name}_bootable_installer.${storage_format}\". Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 2 \
--type dvddrive --medium "${macOS_release_name}_installation_files.viso" >/dev/null
) ]]; then echo "Could not attach \"${macOS_release_name}_installation_files.viso\". Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 3 --hotpluggable on \
--type hdd --nonrotational on --medium "${macOS_release_name}_BaseSystem.${storage_format}" >/dev/null
) ]]; then echo "Could not attach \"${macOS_release_name}_BaseSystem.${storage_format}\". Exiting."; exit; fi
echo -e "\nCreating VirtualBox 6 virtual ISO containing macOS Terminal script"
echo -e "for partitioning and populating the bootable installer virtual disk.\n"
create_viso_header "${vm_name}_populate_bootable_installer_virtual_disk.viso" "bootinst-sh"
echo "/bootinst.sh=\"${vm_name}_bootinst.txt\"" >> "${vm_name}_populate_bootable_installer_virtual_disk.viso"
# Assigning "physical" disks from largest to smallest to "${disks[]}" array
# Partitioning largest disk as APFS
# Partition second-largest disk as JHFS+
echo '# this script is executed on the macOS virtual machine' > "${vm_name}_bootinst.txt"
echo 'disks="$(diskutil list | grep -o "\*[0-9][^ ]* [GTP]B *disk[0-9]$" | grep -o "[0-9].*")" && \
disks="$(echo "${disks}" | sed -e "s/\.[0-9] T/000.0 G/" -e "s/\.[0-9] P/000000.0 G/" | sort -gr | grep -o disk[0-9] )" && \
disks=(${disks[@]}) && \
if [ -z "${disks}" ]; then echo "Could not find disks"; fi && \
[ -n "${disks[0]}" ] && \
diskutil partitionDisk "/dev/${disks[0]}" 1 GPT JHFS+ "Install" R && \' >> "${vm_name}_bootinst.txt"
# Create secondary base system on the Install disk
# and copy macOS install app files to the app directory
echo 'asr restore --source "/Volumes/'"${macOS_release_name:0:5}-files"'/BaseSystem.dmg" --target /Volumes/Install --erase --noprompt && \
app_path="$(ls -d "/Volumes/"*"Base System 1/Install"*.app)" && \
install_path="${app_path}/Contents/SharedSupport/" && \
mkdir -p "${install_path}" && cd "/Volumes/'"${macOS_release_name:0:5}-files/"'" && \
cp *.chunklist *.plist *.dmg "${install_path}" && \
echo "" && echo "Copying the several-GB InstallESD.dmg to the installer app directory" && echo "Please wait" && \
if [ -s "${install_path}/InstallESD.dmg" ]; then \
rm -f "${install_path}/InstallESD.dmg" ; fi && \
for part in InstallESD.part*; do echo "Concatenating ${part}"; cat "${part}" >> "${install_path}/InstallESD.dmg"; done && \
sed -i.bak -e "s/InstallESDDmg\.pkg/InstallESD.dmg/" -e "s/pkg\.InstallESDDmg/dmg.InstallESD/" "${install_path}InstallInfo.plist" && \
sed -i.bak2 -e "/InstallESD\.dmg/{n;N;N;N;d;}" "${install_path}InstallInfo.plist" && \' >> "${vm_name}_bootinst.txt"
# shut down the virtual machine
echo 'shutdown -h now' >> "${vm_name}_bootinst.txt"
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 4 \
--type dvddrive --medium "${vm_name}_populate_bootable_installer_virtual_disk.viso" >/dev/null
) ]]; then echo "Could not attach \"${vm_name}_populate_bootable_installer_virtual_disk.viso\". Exiting."; exit; fi
echo -e "\nStarting virtual machine \"${vm_name}\".
This should take a couple of minutes. If booting fails, exit the script by
pressing CTRL-C then see the documentation for information about applying
different CPU profiles in the section ${highlight_color}CPU profiles and CPUID settings${default_color}."
( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 )
echo -e "\nUntil the script completes, please do not manually interact with\nthe virtual machine."
[[ -z "${kscd}" ]] && declare_scancode_dict
prompt_lang_utils_terminal
kbstring='/Volumes/bootinst-sh/bootinst.sh'
send_keys
send_enter
echo -e "\nPartitioning the bootable installer virtual disk; loading base system onto the
installer virtual disk; moving installation files to installer virtual disk;
updating the InstallInfo.plist file; and rebooting the virtual machine.
The virtual machine may report that disk space is critically low; this is fine.
When the bootable installer virtual disk is finished being populated, the script
will shut down the virtual machine. After shutdown, the initial base system will
be detached from the VM and released from VirtualBox."
print_dimly "If the partitioning fails, exit the script by pressing CTRL-C"
print_dimly "Otherwise, please wait."
while [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]]; do sleep 2 >/dev/null 2>&1; done
echo "Waiting for the VirtualBox GUI to shut off."
animated_please_wait 10
# Detach the original 2GB BaseSystem virtual disk image
# and release basesystem VDI from VirtualBox configuration
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 3 --medium none >/dev/null
2>&1 VBoxManage closemedium "${macOS_release_name}_BaseSystem.${storage_format}" >/dev/null
) ]]; then
echo "Could not detach ${macOS_release_name}_BaseSystem.${storage_format}"
echo "The script will try to detach the virtual disk image. If this takes more than"
echo "a few seconds, terminate the script with CTRL-C, manually shut down VirtualBox,"
echo "and resume with the following stages as described in the documentation:"
echo " ${highlight_color}create_target_virtual_disk populate_macos_target_disk${default_color}"
while [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 3 --medium none >/dev/null
2>&1 VBoxManage closemedium "${macOS_release_name}_BaseSystem.${storage_format}" >/dev/null
) ]]; do
animated_please_wait 10
done
fi
echo -e "\n${macOS_release_name}_BaseSystem.${storage_format} successfully detached from"
echo "the virtual machine and released from VirtualBox Manager."
}
function create_target_virtual_disk() {
print_dimly "stage: create_target_virtual_disk"
if [[ -w "${vm_name}.${storage_format}" ]]; then
echo "${vm_name}.${storage_format} target system virtual disk image exists."
echo -ne "${warning_color}Delete \"${vm_name}.${storage_format}\"?${default_color}"
prompt_delete_y_n
if [[ "${delete}" == "y" ]]; then
if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]]
then
echo "\"${vm_name}.${storage_format}\" may be deleted"
echo "only when the virtual machine is powered off."
echo "Exiting."
exit
else
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
VBoxManage closemedium "${vm_name}.${storage_format}" >/dev/null 2>&1
command rm "${vm_name}.${storage_format}"
fi
else
echo "Exiting."
exit
fi
fi
if [[ "${macOS_release_name}" = "Catalina" && "${storage_size}" -lt 25000 ]]; then
echo "Attempting to install macOS Catalina on a disk smaller than 25000MB will fail."
echo "Please assign a larger virtual disk image size. Exiting."
exit
elif [[ "${storage_size}" -lt 22000 ]]; then
echo "Attempting to install macOS on a disk smaller than 22000MB will fail."
echo "Please assign a larger virtual disk image size. Exiting."
exit
fi
if [[ ! -e "${vm_name}.${storage_format}" ]]; then
echo "Creating target system virtual disk image for \"${vm_name}\""
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
VBoxManage closemedium "${vm_name}.${storage_format}" >/dev/null 2>&1
VBoxManage createmedium --size="${storage_size}" \
--format "${storage_format}" \
--filename "${vm_name}.${storage_format}" \
--variant standard 2>/dev/tty
fi
}
function populate_macos_target_disk() {
print_dimly "stage: populate_macos_target_disk"
if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]]; then
echo -e "${highlight_color}Please ${warning_color}manually${highlight_color} shut down the virtual machine and press enter to continue.${default_color}"
clear_input_buffer_then_read
fi
VBoxManage storagectl "${vm_name}" --remove --name SATA >/dev/null 2>&1
if [[ -n $(
2>&1 VBoxManage storagectl "${vm_name}" --add sata --name SATA --hostiocache on >/dev/null
) ]]; then echo "Could not configure virtual machine storage controller. Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 0 \
--type hdd --nonrotational on --medium "${vm_name}.${storage_format}" >/dev/null
) ]]; then echo "Could not attach \"${vm_name}.${storage_format}\". Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 1 --hotpluggable on \
--type hdd --nonrotational on --medium "${macOS_release_name}_bootable_installer.${storage_format}" >/dev/null
) ]]; then echo "Could not attach \"${macOS_release_name}_bootable_installer.${storage_format}\". Exiting."; exit; fi
if [[ -n $(
2>&1 VBoxManage storageattach "${vm_name}" --storagectl SATA --port 2 \
--type dvddrive --medium "${macOS_release_name}_installation_files.viso" >/dev/null
) ]]; then echo "Could not attach \"${macOS_release_name}_installation_files.viso\". Exiting."; exit; fi
echo -e "\nCreating VirtualBox 6 virtual ISO containing macOS Terminal scripts"
echo -e "for partitioning and populating the target virtual disk.\n"
create_viso_header "${vm_name}_populate_macos_target_disk.viso" "target-sh"
echo "/nvram.sh=\"${vm_name}_configure_nvram.txt\"" >> "${vm_name}_populate_macos_target_disk.viso"
echo "/startosinstall.sh=\"${vm_name}_startosinstall.txt\"" >> "${vm_name}_populate_macos_target_disk.viso"
# execute script concurrently, catch SIGUSR1 when installer finishes preparing
echo '# this script is executed on the macOS virtual machine' > "${vm_name}_configure_nvram.txt"
echo 'printf '"'"'trap "exit 0" SIGUSR1; while true; do sleep 10; done;'"'"' | sh && \
disks="$(diskutil list | grep -o "[0-9][^ ]* [GTP]B *disk[0-9]$")" && \
disks="$(echo "${disks}" | sed -e "s/\.[0-9] T/000.0 G/" -e "s/\.[0-9] P/000000.0 G/" | sort -gr | grep -o disk[0-9])" && \
disks=(${disks[@]}) && \
mkdir -p "/Volumes/'"${vm_name}"'/tmp/mount_efi" && \
mount_msdos /dev/${disks[0]}s1 "/Volumes/'"${vm_name}"'/tmp/mount_efi" && \