forked from grahampugh/erase-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erase-install.sh
executable file
·2185 lines (1975 loc) · 97.5 KB
/
erase-install.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
# shellcheck disable=SC2001
# this is to use sed in the case statements
# shellcheck disable=SC2034
# this is due to the dynamic variable assignments used in the localization strings
:<<DOC
erase-install.sh
by Graham Pugh
WARNING. This is a self-destruct script. Do not try it out on your own device!
See README.md and the GitHub repo's Wiki for details on use.
It is recommended to use the package installer of this script. It contains the bundled
installinstallmacos.py fork, plus a relocatable python with which to run it.
This script can, however, also be run standalone.
It will download and install the MacAdmins Python Framework if not found.
It will also download the installinstallmacos.py fork if it is not found.
Suppress the downloads with the --no-curl option.
Requirements:
- macOS 12.4+
- macOS 10.13.4+ (for --erase option)
- macOS 10.15+ (for --fetch-full-installer option)
- Device file system is APFS
Original version of installinstallmacos.py - Greg Neagle; GitHub munki/macadmins-scripts
DOC
###############
## VARIABLES ##
###############
# script name
script_name="erase-install"
# Version of this script
version="26.0"
# URL for downloading installinstallmacos.py
installinstallmacos_url="https://raw.githubusercontent.com/grahampugh/macadmin-scripts/v${version}/installinstallmacos.py"
installinstallmacos_checksum="ae7fa803f1c05fd43a8c7c3167aed585a85ffe18c803ef484a8c248bc0350366"
# Directory in which to place the macOS installer. Overridden with --path
installer_directory="/Applications"
# Default working directory (may be overridden by the --workdir parameter)
workdir="/Library/Management/erase-install"
# URL for downloading macadmins python (with tag version) for standalone script running
macadmins_python_version="v.3.9.5.09222021234106"
macadmins_python_url="https://api.github.com/repos/macadmins/python/releases/tags/$macadmins_python_version"
macadmins_python_path="/Library/ManagedFrameworks/Python/Python3.framework/Versions/Current/bin/python3"
# Dialog helper apps
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
depnotify_app="/Applications/Utilities/DEPNotify.app"
depnotify_log="/var/tmp/depnotify.log"
depnotify_confirmation_file="/var/tmp/com.depnotify.provisioning.done"
depnotify_download_url="https://files.nomad.menu/DEPNotify.pkg"
###################
## LOCALIZATIONS ##
###################
# Grab currently logged in user to set the language for Dialogue messages
current_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk -F': ' '/[[:space:]]+Name[[:space:]]:/ { if ( $2 != "loginwindow" ) { print $2 }}')
current_uid=$(/usr/bin/id -u "$current_user")
# Get proper home directory. Output of scutil might not reflect the canonical RecordName or the HomeDirectory at all, which might prevent us from detecting the language
current_user_homedir=$(/usr/libexec/PlistBuddy -c 'Print :dsAttrTypeStandard\:NFSHomeDirectory:0' /dev/stdin <<< "$(/usr/bin/dscl -plist /Search -read "/Users/${current_user}" NFSHomeDirectory)")
language=$(/usr/libexec/PlistBuddy -c 'print AppleLanguages:0' "/${current_user_homedir}/Library/Preferences/.GlobalPreferences.plist")
if [[ $language = de* ]]; then
user_language="de"
elif [[ $language = nl* ]]; then
user_language="nl"
elif [[ $language = fr* ]]; then
user_language="fr"
else
user_language="en"
fi
# Dialogue localizations - download window
dialog_dl_title_en="Downloading macOS"
dialog_dl_title_de="macOS wird heruntergeladen"
dialog_dl_title_nl="macOS downloaden"
dialog_dl_title_fr="Téléchargement de macOS"
dialog_dl_desc_en="We need to download the macOS installer to your computer; this will take several minutes."
dialog_dl_desc_de="Der macOS Installer wird heruntergeladen, dies dauert mehrere Minuten."
dialog_dl_desc_nl="We moeten het macOS besturingssysteem downloaden, dit duurt enkele minuten."
dialog_dl_desc_fr="Nous devons télécharger le programme d'installation de macOS sur votre ordinateur, cela prendra plusieurs minutes."
# Dialogue localizations - erase lockscreen
dialog_erase_title_en="Erasing macOS"
dialog_erase_title_de="macOS wiederherstellen"
dialog_erase_title_nl="macOS herinstalleren"
dialog_erase_title_fr="Effacement de macOS"
dialog_erase_desc_en="Preparing the installer may take up to 30 minutes. Once completed your computer will reboot and continue the reinstallation."
dialog_erase_desc_de="Das Vorbereiten des Installationsprogramms kann bis zu 30 Minuten dauern. Nach Abschluss wird Ihr Computer neu gestartet und die Neuinstallation fortgesetzt."
dialog_erase_desc_nl="Het voorbereiden van het installatieprogramma kan tot 30 minuten duren. Zodra het proces is voltooid, wordt uw computer opnieuw opgestart en wordt de herinstallatie voortgezet."
dialog_erase_desc_fr="La préparation de l'installation peut prendre jusqu'à 30 minutes. Une fois terminée, votre ordinateur redémarrera et poursuivra la réinstallation."
# Dialogue localizations - reinstall lockscreen
dialog_reinstall_title_en="Upgrading macOS"
dialog_reinstall_title_de="Upgrading macOS"
dialog_reinstall_title_nl="macOS upgraden"
dialog_reinstall_title_fr="Mise à niveau de macOS"
dialog_reinstall_heading_en="Please wait as we prepare your computer for upgrading macOS."
dialog_reinstall_heading_de="Bitte warten, das Upgrade macOS wird ausgeführt."
dialog_reinstall_heading_nl="Even geduld terwijl we uw computer voorbereiden voor de upgrade van macOS."
dialog_reinstall_heading_fr="Veuillez patienter pendant que nous préparons votre ordinateur pour la mise à niveau de macOS."
dialog_reinstall_desc_en="This process may take up to 30 minutes. Once completed your computer will reboot and begin the upgrade."
dialog_reinstall_desc_de="Dieser Prozess benötigt bis zu 30 Minuten. Der Mac startet anschliessend neu und beginnt mit dem Update."
dialog_reinstall_desc_nl="Dit proces duurt ongeveer 30 minuten. Zodra dit is voltooid, wordt uw computer opnieuw opgestart en begint de upgrade."
dialog_reinstall_desc_fr="Ce processus peut prendre jusqu'à 30 minutes. Une fois terminé, votre ordinateur redémarrera et commencera la mise à niveau."
dialog_reinstall_status_en="Preparing macOS for installation"
dialog_reinstall_status_de="Vorbereiten von macOS für die Installation"
dialog_reinstall_status_nl="MacOS voorbereiden voor installatie"
dialog_reinstall_status_fr="Préparation de macOS pour l'installation"
dialog_rebooting_heading_en="The upgrade is now ready for installation. Please save your work!"
dialog_rebooting_heading_de="Das Upgrade ist nun bereit für die Installation. Bitte speichern Sie Ihre Arbeit!"
dialog_rebooting_heading_nl="De upgrade is nu klaar voor installatie. Sla uw werk op!"
dialog_rebooting_heading_fr="La mise à niveau est maintenant prête à être installée. Veuillez sauvegarder votre travail!"
dialog_rebooting_status_en="Preparation complete - restarting in"
dialog_rebooting_status_de="Vorbereitung abgeschlossen - Neustart in "
dialog_rebooting_status_nl="Voorbereiding compleet - herstart over"
dialog_rebooting_status_fr="Préparation terminée - redémarrage dans"
# Dialogue localizations - confirmation window (erase)
dialog_erase_confirmation_desc_en="Please confirm that you want to ERASE ALL DATA FROM THIS DEVICE and reinstall macOS"
dialog_erase_confirmation_desc_de="Bitte bestätigen, dass Sie ALLE DATEN VON DIESEM GERÄT LÖSCHEN und macOS neu installieren wollen"
dialog_erase_confirmation_desc_nl="Weet je zeker dat je ALLE GEGEVENS VAN DIT APPARAAT WILT WISSEN en macOS opnieuw installeert?"
dialog_erase_confirmation_desc_fr="Veuillez confirmer que vous souhaitez EFFACER TOUTES LES DONNÉES DE CET APPAREIL et réinstaller macOS"
# Dialogue localizations - confirmation window (reinstall)
dialog_reinstall_confirmation_desc_en="Please confirm that you want to upgrade macOS on this system now"
dialog_reinstall_confirmation_desc_de="Bitte bestätigen Sie, dass Sie macOS auf diesem System jetzt aktualisieren möchten"
dialog_reinstall_confirmation_desc_nl="Bevestig dat u macOS op dit systeem nu wilt updaten"
dialog_reinstall_confirmation_desc_fr="Veuillez confirmer que vous voulez mettre à jour macOS sur ce système maintenant."
# Dialogue localizations - confirmation window status
dialog_confirmation_status_en="Press Cmd + Ctrl + C to Cancel"
dialog_confirmation_status_de="Drücken Sie Cmd + Ctrl + C zum Abbrechen"
dialog_confirmation_status_nl="Druk op Cmd + Ctrl + C om te Annuleren"
dialog_confirmation_status_fr="Appuyez sur Cmd + Ctrl + C pour annuler"
# Dialogue buttons
dialog_confirmation_button_en="Confirm"
dialog_confirmation_button_de="Bestätigen"
dialog_confirmation_button_nl="Bevestig"
dialog_confirmation_button_fr="Confirmer"
dialog_cancel_button_en="Stop"
dialog_cancel_button_de="Abbrechen"
dialog_cancel_button_nl="Annuleren"
dialog_cancel_button_fr="Annuler"
dialog_enter_button_en="Enter"
dialog_enter_button_de="Eingeben"
dialog_enter_button_nl="Enter"
dialog_enter_button_fr="Entrer"
# Dialogue localizations - free space check
dialog_check_desc_en="The macOS upgrade cannot be installed as there is not enough space left on the drive."
dialog_check_desc_de="Das Upgrade von macOS kann nicht installiert werden, da nicht genügend Speicherplatz auf dem Laufwerk vorhanden ist."
dialog_check_desc_nl="De upgrade van macOS kan niet worden geïnstalleerd omdat er niet genoeg ruimte is op de schijf."
dialog_check_desc_fr="La mise à niveau de macOS ne peut pas être installée car il n'y a pas assez d'espace disponible sur ce volume."
# Dialogue localizations - power check
dialog_power_title_en="Waiting for AC Power Connection"
dialog_power_title_de="Warten auf AC-Netzteil"
dialog_power_title_nl="Wachten op stroomadapter"
dialog_power_title_fr="En attente de l'alimentation secteur"
dialog_power_desc_en="Please connect your computer to power using an AC power adapter. This process will continue if AC power is detected within the next:"
dialog_power_desc_de="Bitte schließen Sie Ihren Computer mit einem AC-Netzteil an das Stromnetz an. Dieser Prozess wird fortgesetzt, sobald die AC-Stromversorgung innerhalb der folgende Zeitdauer erkannt wird:"
dialog_power_desc_nl="Sluit uw computer aan met de stroomadapter. Zodra deze is gedetecteerd gaat het proces verder binnen de volgende:"
dialog_power_desc_fr="Veuillez connecter votre ordinateur à un adaptateur secteur. Ce processus se poursuivra une fois que l'alimentation secteur sera détectée dans la suivante:"
dialog_nopower_desc_en="Exiting. AC power was not connected after waiting for:"
dialog_nopower_desc_de="Beenden. Die Stromversorgung wurde nach einer Wartezeit nicht hergestellt:"
dialog_nopower_desc_nl="Afsluiten. De wisselstroom was niet aangesloten na het wachten op:"
dialog_nopower_desc_fr="Sortie. Le courant alternatif n'a pas été connecté après avoir attendu:"
# Dialogue localizations - ask for short name
dialog_short_name_en="Please enter an account name to start the reinstallation process"
dialog_short_name_de="Bitte geben Sie einen Kontonamen ein, um die Neuinstallation zu starten"
dialog_short_name_nl="Voer een accountnaam in om het installatieproces te starten"
dialog_short_name_fr="Veuillez entrer un nom de compte pour démarrer le processus de réinstallation"
# Dialogue localizations - ask for password
dialog_not_volume_owner_en="Account is not a Volume Owner! Please login using one of the following accounts and try again"
dialog_not_volume_owner_de="Konto ist kein Volume-Besitzer! Bitte melden Sie sich mit einem der folgenden Konten an und versuchen Sie es erneut"
dialog_not_volume_owner_nl="Account is geen volume-eigenaar! Log in met een van de volgende accounts en probeer het opnieuw"
dialog_not_volume_owner_fr="Le compte n'est pas propriétaire du volume! Veuillez vous connecter en utilisant l'un des comptes suivants et réessayer"
# Dialogue localizations - invalid user
dialog_user_invalid_en="This account cannot be used to to perform the reinstall"
dialog_user_invalid_de="Dieses Konto kann nicht zur Durchführung der Neuinstallation verwendet werden"
dialog_user_invalid_nl="Dit account kan niet worden gebruikt om de herinstallatie uit te voeren"
dialog_user_invalid_fr="Ce compte ne peut pas être utilisé pour effectuer la réinstallation"
# Dialogue localizations - invalid password
dialog_invalid_password_en="ERROR: The password entered is NOT the login password for"
dialog_invalid_password_de="ERROR: Das eingegebene Kennwort ist NICHT das Anmeldekennwort für"
dialog_invalid_password_nl="FOUT: Het ingevoerde wachtwoord is NIET het inlogwachtwoord voor"
dialog_invalid_password_fr="ERREUR : Le mot de passe entré n'est PAS le mot de passe de connexion pour"
# Dialogue localizations - not a volume owner
dialog_get_password_en="Please enter the password for the account"
dialog_get_password_de="Bitte geben Sie das Passwort für das Konto ein"
dialog_get_password_nl="Voer het wachtwoord voor het account in"
dialog_get_password_fr="Veuillez saisir le mot de passe du compte"
# icon for download window
dialog_dl_icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/SidebarDownloadsFolder.icns"
# icon for confirmation dialog
dialog_confirmation_icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"
# set localisation variables
dialog_dl_title=dialog_dl_title_${user_language}
dialog_dl_desc=dialog_dl_desc_${user_language}
dialog_erase_title=dialog_erase_title_${user_language}
dialog_erase_desc=dialog_erase_desc_${user_language}
dialog_reinstall_title=dialog_reinstall_title_${user_language}
dialog_reinstall_heading=dialog_reinstall_heading_${user_language}
dialog_reinstall_desc=dialog_reinstall_desc_${user_language}
dialog_reinstall_status=dialog_reinstall_status_${user_language}
dialog_rebooting_title=dialog_rebooting_title_${user_language}
dialog_rebooting_heading=dialog_rebooting_heading_${user_language}
dialog_rebooting_status=dialog_rebooting_status_${user_language}
dialog_erase_confirmation_title=dialog_erase_confirmation_title_${user_language}
dialog_erase_confirmation_desc=dialog_erase_confirmation_desc_${user_language}
dialog_confirmation_status=dialog_confirmation_status_${user_language}
dialog_confirmation_button=dialog_confirmation_button_${user_language}
dialog_reinstall_confirmation_title=dialog_reinstall_confirmation_title_${user_language}
dialog_reinstall_confirmation_desc=dialog_reinstall_confirmation_desc_${user_language}
dialog_cancel_button=dialog_cancel_button_${user_language}
dialog_enter_button=dialog_enter_button_${user_language}
dialog_check_desc=dialog_check_desc_${user_language}
dialog_power_desc=dialog_power_desc_${user_language}
dialog_nopower_desc=dialog_nopower_desc_${user_language}
dialog_power_title=dialog_power_title_${user_language}
dialog_short_name=dialog_short_name_${user_language}
dialog_user_invalid=dialog_user_invalid_${user_language}
dialog_get_password=dialog_get_password_${user_language}
dialog_invalid_password=dialog_invalid_password_${user_language}
dialog_not_volume_owner=dialog_not_volume_owner_${user_language}
###############
## FUNCTIONS ##
###############
ask_for_password() {
# required for Silicon Macs
/bin/launchctl asuser "$current_uid" /usr/bin/osascript <<END
set nameentry to text returned of (display dialog "${!dialog_get_password} ($account_shortname)" default answer "" with hidden answer buttons {"${!dialog_enter_button}", "${!dialog_cancel_button}"} default button 1 with icon 2)
END
}
ask_for_shortname() {
# required for Silicon Macs
/bin/launchctl asuser "$current_uid" /usr/bin/osascript <<END
set nameentry to text returned of (display dialog "${!dialog_short_name}" default answer "" buttons {"${!dialog_enter_button}", "${!dialog_cancel_button}"} default button 1 with icon 2)
END
}
check_free_space() {
# determine if the amount of free and purgable drive space is sufficient for the upgrade to take place.
free_disk_space=$(osascript -l 'JavaScript' -e "ObjC.import('Foundation'); var freeSpaceBytesRef=Ref(); $.NSURL.fileURLWithPath('/').getResourceValueForKeyError(freeSpaceBytesRef, 'NSURLVolumeAvailableCapacityForImportantUsageKey', null); Math.round(ObjC.unwrap(freeSpaceBytesRef[0]) / 1000000000)") # with thanks to Pico
if [[ -z "$current_user" ]]; then
# fall back to df -h if the above fails
free_disk_space=$(df -Pk . | column -t | sed 1d | awk '{print $4}')
fi
if [[ $free_disk_space -ge $min_drive_space ]]; then
echo " [check_free_space] OK - $free_disk_space GB free/purgeable disk space detected"
else
echo " [check_free_space] ERROR - $free_disk_space GB free/purgeable disk space detected"
if [[ -f "$jamfHelper" ]]; then
"$jamfHelper" -windowType "utility" -description "${!dialog_check_desc}" -alignDescription "left" -icon "$dialog_confirmation_icon" -button1 "OK" -defaultButton "0" -cancelButton "1"
else
# open_osascript_dialog syntax: title, message, button1, icon
open_osascript_dialog "${!dialog_check_desc}" "" "OK" stop &
fi
exit 1
fi
}
check_installer_pkg_is_valid() {
# check InstallAssistant pkg validity
# packages generated by installinstallmacos.py have the format InstallAssistant-version-build.pkg
# Extracting an actual version from the package is slow as the entire package must be unpackaged
# to read the PackageInfo file.
# We are here YOLOing the filename instead. Of course it could be spoofed, but that would not be
# in anyone's interest to attempt as it will just make the script eventually fail.
echo " [check_installer_pkg_is_valid] Checking validity of $existing_installer_pkg."
installer_pkg_build=$( basename "$existing_installer_pkg" | sed 's|.pkg||' | cut -d'-' -f 3 )
system_build=$( /usr/bin/sw_vers -buildVersion )
compare_build_versions "$system_build" "$installer_pkg_build"
if [[ $first_build_newer == "yes" ]]; then
echo " [check_installer_pkg_is_valid] Installer: $installer_pkg_build < System: $system_build : invalid build."
working_installer_pkg="$existing_installer_pkg"
invalid_installer_found="yes"
else
echo " [check_installer_pkg_is_valid] Installer: $installer_pkg_build >= System: $system_build : valid build."
working_installer_pkg="$existing_installer_pkg"
invalid_installer_found="no"
fi
working_macos_app="$existing_installer_app"
}
check_installer_is_valid() {
# check installer validity:
# The Build version in the app Info.plist is often older than the advertised build,
# so it's not a great check for validity
# check if running --erase, where we might be using the same build.
# The actual build number is found in the SharedSupport.dmg in com_apple_MobileAsset_MacSoftwareUpdate.xml (Big Sur and greater).
# This is new from Big Sur, so we include a fallback to the Info.plist file just in case.
echo " [check_installer_is_valid] Checking validity of $existing_installer_app."
# first ensure that some earlier instance is not still mounted as it might interfere with the check
[[ -d "/Volumes/Shared Support" ]] && diskutil unmount force "/Volumes/Shared Support"
# now attempt to mount
if [[ -f "$existing_installer_app/Contents/SharedSupport/SharedSupport.dmg" ]]; then
if hdiutil attach -quiet -noverify "$existing_installer_app/Contents/SharedSupport/SharedSupport.dmg" ; then
echo " [check_installer_is_valid] Mounting $existing_installer_app/Contents/SharedSupport/SharedSupport.dmg"
sleep 1
build_xml="/Volumes/Shared Support/com_apple_MobileAsset_MacSoftwareUpdate/com_apple_MobileAsset_MacSoftwareUpdate.xml"
if [[ -f "$build_xml" ]]; then
echo " [check_installer_is_valid] Using Build value from com_apple_MobileAsset_MacSoftwareUpdate.xml"
installer_build=$(/usr/libexec/PlistBuddy -c "Print :Assets:0:Build" "$build_xml")
sleep 1
diskutil unmount force "/Volumes/Shared Support"
else
echo " [check_installer_is_valid] ERROR: com_apple_MobileAsset_MacSoftwareUpdate.xml not found. Check the mount point at /Volumes/Shared Support"
fi
else
echo " [check_installer_is_valid] Mounting SharedSupport.dmg failed"
fi
else
# if that fails, fallback to the method for 10.15 or less, which is less accurate
echo " [check_installer_is_valid] Using DTSDKBuild value from Info.plist"
if [[ -f "$existing_installer_app/Contents/Info.plist" ]]; then
installer_build=$( /usr/bin/defaults read "$existing_installer_app/Contents/Info.plist" DTSDKBuild )
else
echo " [check_installer_is_valid] Installer Info.plist could not be found!"
fi
fi
if [[ ! $installer_build ]]; then
echo " [check_installer_is_valid] Build of existing installer could not be found!"
exit 1
fi
system_build=$( /usr/bin/sw_vers -buildVersion )
compare_build_versions "$system_build" "$installer_build"
if [[ $first_build_major_newer == "yes" || $first_build_minor_newer == "yes" ]]; then
echo " [check_installer_is_valid] Installer: $installer_build < System: $system_build : invalid build."
invalid_installer_found="yes"
elif [[ $first_build_patch_newer == "yes" ]]; then
echo " [check_installer_is_valid] Installer: $installer_build < System: $system_build : build might work but if it fails, please obtain a newer installer."
warning_issued="yes"
invalid_installer_found="no"
else
echo " [check_installer_is_valid] Installer: $installer_build >= System: $system_build : valid build."
invalid_installer_found="no"
fi
working_macos_app="$existing_installer_app"
}
check_newer_available() {
# Download installinstallmacos.py and MacAdmins python
get_installinstallmacos
get_relocatable_python
if [[ ! -f "$python_path" ]]; then
# fall back to python2
python_path=$(which python)
fi
# build arguments for installinstallmacos
installinstallmacos_args=()
installinstallmacos_args+=("--workdir")
installinstallmacos_args+=("$workdir")
installinstallmacos_args+=("--list")
if [[ $catalogurl ]]; then
echo " [check_newer_available] Non-standard catalog URL selected"
installinstallmacos_args+=("--catalogurl")
installinstallmacos_args+=("$catalogurl")
elif [[ $seedprogram ]]; then
echo " [check_newer_available] Non-standard seedprogram selected"
installinstallmacos_args+=("--seed")
installinstallmacos_args+=("$seedprogram")
elif [[ $catalog ]]; then
darwin_version=$(get_darwin_from_os_version "$catalog")
echo " [run_installinstallmacos] Non-default catalog selected (darwin version $darwin_version)"
installinstallmacos_args+=("--catalog")
installinstallmacos_args+=("$darwin_version")
fi
if [[ $beta == "yes" ]]; then
echo " [check_newer_available] Beta versions included"
installinstallmacos_args+=("--beta")
fi
if [[ $pkg_installer ]]; then
echo " [check_newer_available] checking against package installers"
installinstallmacos_args+=("--pkg")
fi
# run installinstallmacos.py with list and then interrogate the plist
# TEST
echo
echo " [check_newer_available] This command is now being run:"
echo
echo " installinstallmacos.py ${installinstallmacos_args[*]}"
if "$python_path" "$workdir/installinstallmacos.py" "${installinstallmacos_args[@]}" > /dev/null; then
i=0
newer_build_found="no"
if [[ -f "$workdir/softwareupdate.plist" ]]; then
while available_build=$( /usr/libexec/PlistBuddy -c "Print :result:$i:build" "$workdir/softwareupdate.plist" 2>/dev/null); do
compare_build_versions "$available_build" "$installer_build"
if [[ "$first_build_newer" == "yes" ]]; then
newer_build_found="yes"
fi
i=$((i+1))
done
else
echo " [check_newer_available] ERROR reading output from installinstallmacos.py, cannot continue"
exit 1
fi
[[ $newer_build_found != "yes" ]] && echo " [check_newer_available] No newer builds found"
else
echo " [check_newer_available] ERROR running installinstallmacos.py, cannot continue"
exit 1
fi
}
check_password() {
# Check that the password entered matches actual password
# required for Silicon Macs
# thanks to Dan Snelson for the idea
user="$1"
password="$2"
password_matches=$( /usr/bin/dscl /Search -authonly "$user" "$password" )
if [[ -z "$password_matches" ]]; then
echo " [check_password] Success: the password entered is the correct login password for $user."
password_check="pass"
else
echo " [check_password] ERROR: The password entered is NOT the login password for $user."
password_check="fail"
# open_osascript_dialog syntax: title, message, button1, icon
open_osascript_dialog "${!dialog_user_invalid}: $user" "" "OK" 2 &
exit 1
fi
}
check_power_status() {
# Check if device is on battery or AC power
# If not, and our power_wait_timer is above 1, allow user to connect to power for specified time period
# Acknowledgements: https://github.com/kc9wwh/macOSUpgrade/blob/master/macOSUpgrade.sh
# default power_wait_timer to 60 seconds
[[ ! $power_wait_timer ]] && power_wait_timer=60
power_wait_timer_friendly=$( printf '%02dh:%02dm:%02ds\n' $((power_wait_timer/3600)) $((power_wait_timer%3600/60)) $((power_wait_timer%60)) )
if /usr/bin/pmset -g ps | /usr/bin/grep "AC Power" > /dev/null ; then
echo " [check_power_status] OK - AC power detected"
else
echo " [check_power_status] WARNING - No AC power detected"
if [[ "$power_wait_timer" -gt 0 ]]; then
if [[ -f "$jamfHelper" ]]; then
# use jamfHelper if possible
"$jamfHelper" -windowType "utility" -title "${!dialog_power_title}" -description "${!dialog_power_desc} ${power_wait_timer_friendly}" -alignDescription "left" -icon "$dialog_confirmation_icon" &
wait_for_power "jamfHelper"
else
# open_osascript_dialog syntax: title, message, button1, icon
open_osascript_dialog "${!dialog_power_desc} ${power_wait_timer_friendly}" "" "OK" stop &
wait_for_power "osascript"
fi
else
echo " [check_power_status] ERROR - No AC power detected after ${power_wait_timer_friendly}, cannot continue."
exit 1
fi
fi
}
compare_build_versions() {
first_build="$1"
second_build="$2"
first_build_darwin=${first_build:0:2}
second_build_darwin=${second_build:0:2}
first_build_letter=${first_build:2:1}
second_build_letter=${second_build:2:1}
first_build_minor=${first_build:3}
second_build_minor=${second_build:3}
first_build_minor_no=${first_build_minor//[!0-9]/}
second_build_minor_no=${second_build_minor//[!0-9]/}
first_build_minor_beta=${first_build_minor//[0-9]/}
second_build_minor_beta=${second_build_minor//[0-9]/}
echo " [compare_build_versions] Comparing (1) $first_build with (2) $second_build"
if [[ "$first_build" == "$second_build" ]]; then
echo " [compare_build_versions] $first_build = $second_build"
builds_match="yes"
return
elif [[ $first_build_darwin -gt $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build"
first_build_newer="yes"
first_build_major_newer="yes"
return
elif [[ $first_build_letter > $second_build_letter && $first_build_darwin -eq $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build"
first_build_newer="yes"
first_build_minor_newer="yes"
return
elif [[ ! $first_build_minor_beta && $second_build_minor_beta && $first_build_letter == "$second_build_letter" && $first_build_darwin -eq $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build (production > beta)"
first_build_newer="yes"
first_build_patch_newer="yes"
return
elif [[ ! $first_build_minor_beta && ! $second_build_minor_beta && $first_build_minor_no -lt 1000 && $second_build_minor_no -lt 1000 && $first_build_minor_no -gt $second_build_minor_no && $first_build_letter == "$second_build_letter" && $first_build_darwin -eq $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build"
first_build_newer="yes"
first_build_patch_newer="yes"
return
elif [[ ! $first_build_minor_beta && ! $second_build_minor_beta && $first_build_minor_no -ge 1000 && $second_build_minor_no -ge 1000 && $first_build_minor_no -gt $second_build_minor_no && $first_build_letter == "$second_build_letter" && $first_build_darwin -eq $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build (both betas)"
first_build_newer="yes"
first_build_patch_newer="yes"
return
elif [[ $first_build_minor_beta && $second_build_minor_beta && $first_build_minor_no -ge 1000 && $second_build_minor_no -ge 1000 && $first_build_minor_no -gt $second_build_minor_no && $first_build_letter == "$second_build_letter" && $first_build_darwin -eq $second_build_darwin ]]; then
echo " [compare_build_versions] $first_build > $second_build (both betas)"
first_build_patch_newer="yes"
first_build_newer="yes"
return
fi
}
confirm() {
if [[ $use_depnotify == "yes" ]]; then
# DEPNotify dialog option
echo " [$script_name] Opening DEPNotify confirmation message (language=$user_language)"
if [[ $fs == "yes" && ! $rebootdelay -gt 10 ]]; then
window_type="fs"
else
window_type="utility"
fi
if [[ $erase == "yes" ]]; then
dn_title="${!dialog_erase_title}"
dn_desc="${!dialog_erase_confirmation_desc}"
else
dn_title="${!dialog_reinstall_title}"
dn_desc="${!dialog_reinstall_confirmation_desc}"
fi
dn_status="${!dialog_confirmation_status}"
dn_icon="$dialog_confirmation_icon"
dn_button="${!dialog_confirmation_button}"
dn_quit_key="c"
dep_notify
dn_pid=$(pgrep -l "DEPNotify" | cut -d " " -f1)
# wait for the confirmation button to be pressed or for the user to cancel
until [[ "$dn_pid" = "" ]]; do
sleep 1
dn_pid=$(pgrep -l "DEPNotify" | cut -d " " -f1)
done
# DEPNotify creates a bom file if the user presses the confirmation button
# but not if they cancel
if [[ -f "$depnotify_confirmation_file" ]]; then
confirmation=2
else
confirmation=0
fi
# now clear the button, quit key and dialog
dep_notify_quit
elif [[ -f "$jamfHelper" ]]; then
# jamfHelper dialog option
echo " [$script_name] Opening jamfHelper confirmation message (language=$user_language)"
if [[ $erase == "yes" ]]; then
jh_title="${!dialog_erase_title}"
jh_desc="${!dialog_erase_confirmation_desc}"
else
jh_title="${!dialog_reinstall_title}"
jh_desc="${!dialog_reinstall_confirmation_desc}"
fi
"$jamfHelper" -windowType utility -title "$jh_title" -alignHeading center -alignDescription natural -description "$jh_desc" -lockHUD -icon "$dialog_confirmation_icon" -button1 "${!dialog_cancel_button}" -button2 "${!dialog_confirmation_button}" -defaultButton 1 -cancelButton 1 2> /dev/null
confirmation=$?
else
# osascript dialog option
echo " [$script_name] Opening osascript dialog for confirmation (language=$user_language)"
if [[ $erase == "yes" ]]; then
osa_desc="${!dialog_erase_confirmation_desc}"
else
osa_desc="${!dialog_reinstall_confirmation_desc}"
fi
answer=$(
/bin/launchctl asuser "$current_uid" /usr/bin/osascript <<-END
set nameentry to button returned of (display dialog "$osa_desc" buttons {"${!dialog_confirmation_button}", "${!dialog_cancel_button}"} default button "${!dialog_cancel_button}" with icon 2)
END
)
if [[ "$answer" == "${!dialog_confirmation_button}" ]]; then
confirmation=2
else
confirmation=0
fi
fi
if [[ "$confirmation" == "0"* ]]; then
echo " [$script_name] User DECLINED erase-install or reinstall"
exit 0
elif [[ "$confirmation" == "2"* ]]; then
echo " [$script_name] User CONFIRMED erase-install or reinstall"
else
echo " [$script_name] User FAILED to confirm erase-install or reinstall"
exit 1
fi
}
create_launchdaemon_to_remove_workdir () {
# Name of LaunchDaemon
plist_label="com.github.grahampugh.erase-install.remove"
launch_daemon="/Library/LaunchDaemons/$plist_label.plist"
# Create the plist
/usr/bin/defaults write "$launch_daemon" Label -string "$plist_label"
/usr/bin/defaults write "$launch_daemon" ProgramArguments -array \
-string /bin/rm \
-string -Rf \
-string "$workdir" \
-string "$launch_daemon"
/usr/bin/defaults write "$launch_daemon" RunAtLoad -boolean yes
/usr/bin/defaults write "$launch_daemon" LaunchOnlyOnce -boolean yes
/usr/sbin/chown root:wheel "$launch_daemon"
/bin/chmod 644 "$launch_daemon"
}
dep_notify() {
# configuration taken from https://github.com/jamf/DEPNotify-Starter
DEP_NOTIFY_CONFIG_PLIST="/Users/$current_user/Library/Preferences/menu.nomad.DEPNotify.plist"
# /usr/bin/defaults write "$DEP_NOTIFY_CONFIG_PLIST" pathToPlistFile "$DEP_NOTIFY_USER_INPUT_PLIST"
STATUS_TEXT_ALIGN="center"
/usr/bin/defaults write "$DEP_NOTIFY_CONFIG_PLIST" statusTextAlignment "$STATUS_TEXT_ALIGN"
chown "$current_user":staff "$DEP_NOTIFY_CONFIG_PLIST"
# Configure the window's look
{
echo "Command: Image: $dn_icon"
echo "Command: MainTitle: $dn_title"
echo "Command: MainText: $dn_desc"
} >> "$depnotify_log"
if [[ "$dn_button" ]]; then
echo "Adding DEPNotify button $dn_button" ## TEMP
echo "Command: ContinueButton: $dn_button" >> "$depnotify_log"
fi
if ! pgrep DEPNotify ; then
# Opening the app after initial configuration
if [[ "$window_type" == "fs" && ! "$rebootdelay" -gt 10 ]]; then
sudo -u "$current_user" open -a "$depnotify_app" --args -path "$depnotify_log" -fullScreen
else
sudo -u "$current_user" open -a "$depnotify_app" --args -path "$depnotify_log"
fi
fi
# set message below progress bar
echo "Status: $dn_status" >> "$depnotify_log"
# set alternaitve quit key (default is X)
if [[ $dn_quit_key ]]; then
echo "Command: QuitKey: $dn_quit_key" >> "$depnotify_log"
fi
}
dep_notify_progress() {
# function for DEPNotify to show progress while the installer is being downloaded or prepared
last_progress_value=0
current_progress_value=0
if [[ "$1" == "startosinstall" ]]; then
# Wait for the preparing process to start and set the progress bar to 100 steps
until grep -q "Preparing: \d" "$LOG_FILE" ; do
sleep 2
done
echo "Status: $dn_status - 0%" >> $depnotify_log
echo "Command: DeterminateManual: 100" >> $depnotify_log
# Until at least 100% is reached, calculate the preparing progress and move the bar accordingly
until [[ $current_progress_value -ge 100 ]]; do
until [[ $current_progress_value -gt $last_progress_value ]]; do
current_progress_value=$(tail -1 "$LOG_FILE" | awk 'END{print substr($NF, 1, length($NF)-3)}')
sleep 2
done
echo "Command: DeterminateManualStep: $((current_progress_value-last_progress_value))" >> $depnotify_log
echo "Status: $dn_status - $current_progress_value%" >> $depnotify_log
last_progress_value=$current_progress_value
done
elif [[ "$1" == "installinstallmacos" ]]; then
# Wait for the download to start and set the progress bar to 100 steps
until grep -q "Total" "$LOG_FILE" ; do
sleep 2
done
echo "Status: $dn_status - 0%" >> $depnotify_log
echo "Command: DeterminateManual: 100" >> $depnotify_log
sleep 2
until [[ $current_progress_value -gt 0 && $current_progress_value -lt 100 ]]; do
current_progress_value=$(tail -1 "$LOG_FILE" | awk '{print substr($(NF-9), 1, length($NF))}')
sleep 2
done
# Until at least 100% is reached, calculate the downloading progress and move the bar accordingly
until [[ $current_progress_value -ge 100 ]]; do
until [[ $current_progress_value -gt $last_progress_value ]]; do
current_progress_value=$(tail -1 "$LOG_FILE" | awk '{print substr($(NF-9), 1, length($NF))}')
sleep 2
done
echo "Command: DeterminateManualStep: $((current_progress_value-last_progress_value))" >> $depnotify_log
echo "Status: $dn_status - $current_progress_value%" >> $depnotify_log
last_progress_value=$current_progress_value
done
elif [[ "$1" == "fetch-full-installer" ]]; then
# Wait for the download to start and set the progress bar to 100 steps
until grep -q "Installing:" "$LOG_FILE" ; do
sleep 2
done
echo "Status: $dn_status - 0%" >> $depnotify_log
echo "Command: DeterminateManual: 100" >> $depnotify_log
# Until at least 100% is reached, calculate the downloading progress and move the bar accordingly
until [[ "$current_progress_value" -ge 100 ]]; do
until [ "$current_progress_value" -gt "$last_progress_value" ]; do
current_progress_value=$(tail -1 "$LOG_FILE" | awk 'END{print substr($NF, 1, length($NF)-3)}')
sleep 2
done
echo "Command: DeterminateManualStep: $((current_progress_value-last_progress_value))" >> $depnotify_log
echo "Status: $dn_status - $current_progress_value%" >> $depnotify_log
last_progress_value=$current_progress_value
done
elif [[ "$1" == "reboot-delay" ]]; then
# Countdown seconds to reboot (a bit shorter than rebootdelay)
countdown=$((rebootdelay-5))
echo "Status: $dn_status - ${countdown}s" >> $depnotify_log
echo "Command: DeterminateManual: $rebootdelay" >> $depnotify_log
until [ "$countdown" -eq 0 ]; do
sleep 1
countdown=$((countdown-1))
current_progress_value=$countdown
echo "Command: DeterminateManualStep: $((current_progress_value-last_progress_value))" >> $depnotify_log
echo "Status: $dn_status - ${countdown}s" >> $depnotify_log
last_progress_value=$current_progress_value
done
fi
}
dep_notify_quit() {
# quit DEP Notify
echo "Command: Quit" >> "$depnotify_log"
# reset all the settings that might be used again
/bin/rm "$depnotify_log" "$depnotify_confirmation_file" 2>/dev/null
dn_button=""
dn_quit_key=""
dn_cancel=""
# kill dep_notify_progress background job if it's already running
if [ -f "/tmp/depnotify_progress_pid" ]; then
while read -r i; do
kill -9 "${i}"
done < /tmp/depnotify_progress_pid
/bin/rm /tmp/depnotify_progress_pid
fi
}
find_existing_installer() {
# Search for an existing download
# First let's see if this script has been run before and left an installer
existing_macos_dmg=$( find $workdir/*.dmg -maxdepth 1 -type f -print -quit 2>/dev/null )
existing_sparseimage=$( find "$workdir/"*.sparseimage -maxdepth 1 -type f -print -quit 2>/dev/null )
existing_installer_app=$( find "$installer_directory/Install macOS"*.app -maxdepth 1 -type d -print -quit 2>/dev/null )
existing_installer_pkg=$( find "$workdir/InstallAssistant"*.pkg -maxdepth 1 -type f -print -quit 2>/dev/null )
if [[ -f "$existing_macos_dmg" ]]; then
echo " [find_existing_installer] Installer image found at $existing_macos_dmg."
hdiutil attach "$existing_macos_dmg"
existing_installer_app=$( find '/Volumes/'*macOS*/*.app -maxdepth 1 -type d -print -quit 2>/dev/null )
check_installer_is_valid
elif [[ -f "$existing_sparseimage" ]]; then
echo " [find_existing_installer] Installer sparse image found at $existing_sparseimage."
hdiutil attach "$existing_sparseimage"
existing_installer_app=$( find '/Volumes/'*macOS*/Applications/*.app -maxdepth 1 -type d -print -quit 2>/dev/null )
check_installer_is_valid
elif [[ -d "$existing_installer_app" ]]; then
echo " [find_existing_installer] Installer found at $existing_installer_app."
app_is_in_applications_folder="yes"
check_installer_is_valid
elif [[ -f "$existing_installer_pkg" ]]; then
echo " [find_existing_installer] InstallAssistant package found at $existing_installer_pkg."
check_installer_pkg_is_valid
else
echo " [find_existing_installer] No valid installer found."
if [[ $clear_cache == "yes" ]]; then
exit
fi
fi
}
find_extra_packages() {
# set install_package_list to blank.
install_package_list=()
for file in "$extras_directory"/*.pkg; do
if [[ $file != *"/*.pkg" ]]; then
echo " [find_extra_installers] Additional package to install: $file"
install_package_list+=("--installpackage")
install_package_list+=("$file")
fi
done
}
get_darwin_from_os_version() {
# convert a macOS major version to a darwin version
os_version="$1"
if [[ "${os_version:0:2}" == "10" ]]; then
darwin_version=${os_version:3:2}
darwin_version=$((darwin_version+4))
else
darwin_version=${os_version:0:2}
darwin_version=$((darwin_version+9))
fi
echo "$darwin_version"
}
get_depnotify() {
# grab installinstallmacos.py if not already there
# note this does a SHA256 checksum check and will delete the file and exit if this fails
if [[ -d "$depnotify_app" ]]; then
echo " [get_depnotify] DEPNotify is installed ($depnotify_app)"
else
if [[ ! $no_curl ]]; then
echo " [get_depnotify] Downloading DEPNotify.app..."
if /usr/bin/curl -L "$depnotify_download_url" -o "$workdir/DEPNotify.pkg" ; then
if ! installer -pkg "$workdir/DEPNotify.pkg" -target / ; then
echo " [get_depnotify] DEPNotify installation failed"
fi
else
echo " [get_depnotify] DEPNotify download failed"
fi
fi
# check it did actually get downloaded
if [[ -d "$depnotify_app" ]]; then
echo " [get_depnotify] DEPNotify is installed"
use_depnotify="yes"
dep_notify_quit
else
echo " [get_depnotify] Could not download DEPNotify.app."
fi
fi
}
get_installinstallmacos() {
# grab installinstallmacos.py if not already there
# note this does a SHA256 checksum check and will delete the file and exit if this fails
if [[ ! -f "$workdir/installinstallmacos.py" || $force_installinstallmacos == "yes" ]]; then
if [[ ! $no_curl ]]; then
echo " [get_installinstallmacos] Downloading installinstallmacos.py..."
# delete existing version so curl can create new file
if [[ -f "$workdir/installinstallmacos.py" ]]; then
/bin/rm "$workdir/installinstallmacos.py"
fi
# use curl -o instead of > redirect, which causes permission error when run with sudo
/usr/bin/curl -H 'Cache-Control: no-cache' -s "$installinstallmacos_url" -o "$workdir/installinstallmacos.py"
if echo "$installinstallmacos_checksum $workdir/installinstallmacos.py" | shasum -c; then
echo " [get_installinstallmacos] downloaded new installinstallmacos.py successfully."
else
echo " [get_installinstallmacos] ERROR: downloaded installinstallmacos.py does not match checksum. Possible corrupted file. Deleting file."
/bin/rm "$workdir/installinstallmacos.py"
fi
fi
fi
# check it did actually get downloaded
if [[ ! -f "$workdir/installinstallmacos.py" ]]; then
echo "Could not download installinstallmacos.py so cannot continue."
exit 1
else
echo " [get_installinstallmacos] installinstallmacos.py is in $workdir"
iim_downloaded=1
fi
}
get_relocatable_python() {
# grab macadmins python and install it if not already there - used when running this script as a standalone
if [[ -L "$relocatable_python_path" && -e "$relocatable_python_path" ]]; then
echo " [get_relocatable_python] Relocatable Python is installed in $workdir"
python_path="$relocatable_python_path"
elif [[ -L "$macadmins_python_path" && -e "$macadmins_python_path" ]]; then
echo " [get_relocatable_python] MacAdmins Python is installed"
python_path="$macadmins_python_path"
else
if [[ ! $no_curl ]]; then
echo " [get_relocatable_python] Downloading MacAdmins Python package..."
macadmins_python_pkg=$( /usr/bin/curl -sl -H "Accept: application/vnd.github.v3+json" "$macadmins_python_url" | grep signed | grep url | sed 's|^.*"browser_download_url": ||' | sed 's|\"||g' )
/usr/bin/curl -L "$macadmins_python_pkg" -o "$workdir/macadmins_python-$macadmins_python_version.pkg"
installer -pkg "$workdir/macadmins_python-$macadmins_python_version.pkg" -target /
fi
# check it did actually get downloaded
if [[ -L "$macadmins_python_path" && -e "$macadmins_python_path" ]]; then
echo " [get_relocatable_python] MacAdmins Python is installed"
python_path="$macadmins_python_path"
else
echo " [get_relocatable_python] Could not download MacAdmins Python."
fi
fi
}
get_user_details() {
# Apple Silicon devices require a username and password to run startosinstall
# get account name (short name)
if [[ $use_current_user == "yes" ]]; then
account_shortname="$current_user"
fi
if [[ $account_shortname == "" ]]; then
if ! account_shortname=$(ask_for_shortname) ; then
echo " [get_user_details] User cancelled."
exit 1
fi
fi
# check that this user exists
if ! /usr/sbin/dseditgroup -o checkmember -m "$account_shortname" everyone ; then
echo " [get_user_details] $account_shortname account cannot be found!"
user_invalid
exit 1
fi
# check that the user is a Volume Owner
user_is_volume_owner=0
users=$(/usr/sbin/diskutil apfs listUsers /)
enabled_users=""
while read -r line ; do
user=$(/usr/bin/cut -d, -f1 <<< "$line")
guid=$(/usr/bin/cut -d, -f2 <<< "$line")
# passwords are case sensitive, account names are not
shopt -s nocasematch
if [[ $(/usr/bin/grep -A2 "$guid" <<< "$users" | /usr/bin/tail -n1 | /usr/bin/awk '{print $NF}') == "Yes" ]]; then
enabled_users+="$user "
# The entered username might not match the output of fdesetup, so we compare
# all RecordNames for the canonical name given by fdesetup against the entered
# username, and then use the canonical version. The entered username might
# even be the RealName, and we still would end up here.
# Example:
# RecordNames for user are "John.Doe@pretendco.com" and "John.Doe", fdesetup
# says "John.Doe@pretendco.com", and account_shortname is "john.doe" or "Doe, John"
user_record_names_xml=$(/usr/bin/dscl -plist /Search -read "Users/$user" RecordName dsAttrTypeStandard:RecordName)
# loop through recordName array until error (we do not know the size of the array)
record_name_index=0
while true; do
if ! user_record_name=$(/usr/libexec/PlistBuddy -c "print :dsAttrTypeStandard\:RecordName:${record_name_index}" /dev/stdin 2>/dev/null <<< "$user_record_names_xml") ; then
break
fi
if [[ "$account_shortname" == "$user_record_name" ]]; then
account_shortname=$user
echo " [get_user_details] $account_shortname is a Volume Owner"
user_is_volume_owner=1
break
fi
record_name_index=$((record_name_index+1))
done
# if needed, compare the RealName (which might contain spaces)
if [[ $user_is_volume_owner = 0 ]]; then
user_real_name=$(/usr/libexec/PlistBuddy -c "print :dsAttrTypeStandard\:RealName:0" /dev/stdin <<< "$(/usr/bin/dscl -plist /Search -read "Users/$user" RealName)")
if [[ "$account_shortname" == "$user_real_name" ]]; then
account_shortname=$user
echo " [get_user_details] $account_shortname is a Volume Owner"
user_is_volume_owner=1
fi
fi
fi
shopt -u nocasematch