-
Notifications
You must be signed in to change notification settings - Fork 4
/
SIC_1.5.sh
executable file
·6924 lines (6719 loc) · 344 KB
/
SIC_1.5.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/sh
# Default Base Folder
BaseFolder="/Users/Shared/SIC"
# Default Configurations Folder
ConfigurationFolder="${BaseFolder}/Configurations"
# Default Library Folder
LibraryFolder="${BaseFolder}/Library"
# Default Packages Folder
PackageFolder="${BaseFolder}/Packages"
# Default Masters Folder
MasterFolder="${BaseFolder}/Masters"
# Default Image Size (GB)
ImageSize="56.5"
# Minimum Image Size
minImageSize="5"
# Maximum Image Size
maxImageSize="2048"
# Default Volume Name
VolumeName="Macintosh HD"
# Default Export Option for Recovery Partition (Include in Master)
ExportType=1
# Default for ASR Scan on Masters
ScanImage=1
# FirstBoot Script Path
FirstBootPath="/usr/libexec/FirstBoot"
# Default System Configuration
Language="English"
SACountryCode="US"
InputSourceID="US"
NTPServerName="Apple Americas/U.S. (time.apple.com)"
NTPServer="time.apple.com"
NTPEnabled=1
GeonameID=5341145
TZAuto=0
RemoteLogin=0
RemoteManagement=0
# Additional System Settings, menus not yet implemented
# Display host info at login window
AdminHostInfo=0 # 0 (Off) | 1 (On)
# Display login window as Name and password
ShowFullName=0 # 0 (Off) | 1 (On)
# Allow guests to connect to shared folders
AllowGuestAccess=1 # 1 (On) | 0 (Off)
# Allow managed users to add or delete printers
ManagedUserPrinters=0 # 0 (Off) | 1 (On)
# Automatically check for updates
SoftwareUpdateCheck=1 # 1 (On) | 0 (Off)
# Additional User Settings, menus not yet implemented
UserSettings=1 # 0 (Off) | 1 (On)
# General: Show scroll bars
AppleShowScrollBars="Always" # Automatic | WhenScrolling | Always
# Finder: Show Status Bar
ShowStatusBar=TRUE
# Finder: New Finder windows show: Computer; Volume; Home; Desktop; Documents; All My Files
NewWindowTarget="PfHm" # PfCm | PfVo | PfHm | PfDe | PfDo | PfAF
# Setup Assistant: Gesture Movie
GestureMovieSeen="trackpad" # trackpad |
# iCloud Setup
SuppressCloudSetup=1 # 1 (On) | 0 (Off)
# Version
SICVersion="1.5b9"
# ${0}: Path to this script
ScriptName=`basename "${0}"`
# Resize Terminal Window
printf "\e[8;36;80;t"
# Section: Common
spin="/-\|"
function display_Title {
clear
printf "\033[1mSystem Image Creator (SIC) ${SICVersion}\033[m\n"
printf "Copyright (c) 2011-2012 Mondada Pty Ltd. All rights reserved.\n"
}
function display_Subtitle {
# ${1}: Subtitle
display_Title
printf "\n\033[1m${1}\033[m\n\n"
}
function privelege_Check {
if [ `id -u` -ne 0 ] ; then
display_Title
printf "\n${ScriptName} must be run with root privileges, exiting.\n\n"
exit 1
fi
}
function display_Options {
# ${1}: Text above selection list
# ${2}: Text for prompt
printf "${1}\n\n"
PS3=`printf "\n${2}"`
}
function press_anyKey {
# ${1}: Message to display above prompt
if [ -n "${1}" ] ; then echo "${1}" ; echo ; fi
read -sn 1 -p "Press any key to continue..." anyKey < /dev/tty
echo
}
function get_SystemOSVersion {
SystemOSMajor=`sw_vers -productVersion | awk -F "." '{print $1}'`
SystemOSMinor=`sw_vers -productVersion | awk -F "." '{print $2}'`
SystemOSPoint=`sw_vers -productVersion | awk -F "." '{print $3}'`
if [ -z "${SystemOSPoint}" ] ; then SystemOSPoint=0 ; fi
SystemOSBuild=`sw_vers -buildVersion`
}
function get_LocalUsers {
unset LocalRecordNames[@]
unset LocalRealNames[@]
unset LocalUniqueIDs[@]
unset LocalNFSHomeDirectories[@]
LocalUsers=( `dscl -f "/var/db/dslocal/nodes/Default" localonly -list /Local/Target/Users` )
i=0 ; for Element in "${LocalUsers[@]}" ; do
AuthenticationAuthority=`dscl -f "/var/db/dslocal/nodes/Default" localonly -read /Local/Target/Users/${Element} "AuthenticationAuthority" 2>/dev/null`
if [ "${Element}" != "root" ] && [ -n "${AuthenticationAuthority}" ] ; then
LocalRecordNames[i]="${Element}"
LocalRealNames[i]=`dscl -f "/var/db/dslocal/nodes/Default" localonly -read /Local/Target/Users/${Element} "RealName" 2>/dev/null | grep -v "RealName:" | sed "s/^ *//g"`
LocalUniqueIDs[i]=`dscl -f "/var/db/dslocal/nodes/Default" localonly -read /Local/Target/Users/${Element} "UniqueID" 2>/dev/null | awk -F "UniqueID: " '{print $NF}'`
LocalNFSHomeDirectories[i]=`dscl -f "/var/db/dslocal/nodes/Default" localonly -read /Local/Target/Users/${Element} "NFSHomeDirectory" 2>/dev/null | awk -F "NFSHomeDirectory: " '{print $NF}'`
let i++
fi
done
}
function set_Target {
# ${1}: Volume
if [ -n "${1}" ] ; then
Target="/Volumes/${1}"
else
unset Target
TargetType=0
fi
}
# Section: License & Copyright
function get_LicenseStatus {
LicenseStatus=`defaults read ~/Library/Preferences/au.com.mondada.SIC "LicenseStatus" 2>/dev/null`
}
function display_License {
display_Title
echo
echo "This program is free software: you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation, either version 3 of the License, or"
echo "(at your option) any later version."
echo
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo
echo "You should have received a copy of the GNU General Public License"
echo "along with this program. If not, see <http://www.gnu.org/licenses/>."
echo
echo "Report bugs to <duncan.mccracken@mondada.com.au>"
echo
}
function display_Copyright {
display_Title
echo
echo "This program comes with ABSOLUTELY NO WARRANTY."
echo "This is free software, and you are welcome to redistribute it"
echo "under certain conditions; See the GNU General Public License for"
echo "details."
echo
echo "Report bugs to <duncan.mccracken@mondada.com.au>"
echo
sleep 1
}
function menu_License {
if [ "${LicenseStatus}" != "1" ] ; then
display_License
LicenseOptions=( "Agree" "Disagree" )
display_Options "To continue you must agree to the terms of the license agreement" "Select an option: "
select LicenseOption in "${LicenseOptions[@]}" ; do
if [ -n "${LicenseOption}" ] ; then break ; fi
done
if [ "${LicenseOption}" == "Disagree" ] ; then echo ; exit 1 ; fi
defaults write ~/Library/Preferences/au.com.mondada.SIC "LicenseStatus" -bool TRUE
else
display_Copyright
fi
}
# Section: Help
function help_Preferences {
display_Subtitle "Preferences"
echo "Default Folders: The locations in which SIC stores things."
echo
echo "Configurations: Plists that contain all of your settings for an image."
echo
echo "Library: 'Vanilla' system images, configurations are applied to these to create"
echo "your masters."
echo
echo "Packages: If you want to add software to your image, place OS X installer"
echo "packages in this folder."
echo
echo "Masters: Where SIC exports the restore-ready images."
echo
echo "Image Settings: Set the default image size & volume name."
echo
echo "Export Settings: Allows you to select the default export behaviours for"
echo "masters. Splitting the recovery partition away from the image is the default"
echo "behaviour for DeployStudio."
echo
press_anyKey
}
function help_Creating {
display_Subtitle "Creating Images"
echo "SIC will create images from two sources:"
echo " - An un-booted, un-configured system volume attached to your computer."
echo " - An OS X install ESD or DVD."
echo
echo "These images will be saved to SIC's Library. These images are un-configured"
echo "and used to create masters by applying configuration(s) to them."
echo
echo "Once a version of OS X is in the library, you don't need to capture/install"
echo "that version again."
echo
echo "Note: When performing an installation, you will only be able to select an"
echo " installer of the same version of OS X your are running."
echo " eg. If you are running Mountain Lion, you can only perform Mountain Lion"
echo " installations."
echo
press_anyKey
}
function help_Configuring {
display_Subtitle "Configuring Images"
echo "The System Setup section will allow you to configure the system settings you"
echo "wish to apply to your image(s). If you have an un-configured system attached to"
echo "your computer, you may also apply your system settings to this volume allowing"
echo "you to use SIC for a 'No-Imaging' workflow."
echo
echo "Select Target: Allows you to choose an image from the Library, or an attached"
echo "volume to configure. You don't need to select a target to create a"
echo "configuration, but you must have a target selected to apply a configuration."
echo
echo "Configurations: Files which contain the settings which you wish to apply to"
echo "un-configured images/volumes. You may apply a configuration to multiple OS X"
echo "versions, but remember to check your settings are being displayed correctly in"
echo "each section, particularly System Preferences."
echo
echo "System Preferences: This simply creates the settings achieved by running the"
echo "Setup Assistant at 1st boot. There are a few additional options, which are"
echo "helpful when creating images for mass-deployment"
echo
echo "Users: Create and edit User Accounts on your image."
echo
echo "Packages: Allows you to select packages to install software on your image."
echo "If the package is unable to be installed, it will be copied to the image and"
echo "installation attempted on first boot."
echo
echo "Remove Software: To reduce your image size, particularly when creating an image"
echo "of a new (factory) system, you may want to remove some of the pre-installed"
echo "software."
echo
press_anyKey
display_Subtitle "Configuring Images"
echo "Apply Configuration: This will export a master image or configure an attached"
echo "volume, with the settings displayed in the previous sections."
echo
echo "Note: Applying a configuration is non-destructive to images in your Library"
echo " the configured image will be exported to the Masters folder, ready for"
echo " restore."
echo
echo "Note: Applying a configuration to a volume will cause SIC to ignore it once"
echo " the configuration is complete, the volume is seen as a configured"
echo " system which SIC can do nothing with."
echo
press_anyKey
}
function menu_Help {
HelpOptions=( "Main Menu" "Preferences" "Creating Images" "Configuring Images" )
while [ "${Option}" != "Main Menu" ] ; do
display_Subtitle "Help"
echo "SIC is a tool to assist with the creation of OS X images for mass deployment."
echo
echo "The workflow is designed to prevent the need to create everything from scratch"
echo "when a new version of OS X is released. Additionally, once a build of OS X is"
echo "captured it can be re-used to create multiple images, without the need to"
echo "re-install the base operating system."
echo
echo "The simplest possible workflow for SIC is:"
echo " - Mount your OS X installer (or insert disc)"
echo " - Select Create Image"
echo " - Follow the prompts (Once this is captured, you may use it as many times as"
echo " you wish)"
echo " - Return to the Main Menu, select System Setup"
echo " - Select your Target (the image you just created)"
echo " - Set your System Preferences"
echo " - Create your User(s)"
echo " - Apply your Configuration"
echo
echo "For detailed information, select from the options below."
echo
display_Options "Options" "Select an option: "
select Option in "${HelpOptions[@]}" ; do
case "${Option}" in
"Main Menu" ) break ;;
"Preferences" ) help_Preferences ; unset Option ; break ;;
"Creating Images" ) help_Creating ; unset Option ; break ;;
"Configuring Images" ) help_Configuring ; unset Option ; break ;;
esac
done
done
unset Option
}
# Section: Preferences
function check_Path {
# ${1}: Path to validate
validPath=0
# Check for invalid characters
escapedPath="${1//[\$\(\)\[\]\`\~\?\*\#\\\!\|\'\"]/_}"
if [ "${1}" != "${escapedPath}" ] ; then
printf "\nThe specified path cannot contain the following characters: \033[1m\$()[]\`~?*#\!|'\"\033[m\n" ; validPath=1 ; return 1
fi
# Check that it's absolute
relativePath=`echo "${1}" | awk -F "/" '{print $1}'`
if [ -n "${relativePath}" ] ; then
printf "\nThe specified path must be absolute, please enter an absolute path\n" ; validPath=1 ; return 1
fi
# Check the volume exists
if echo "${1}" | grep -q "/Volumes/" ; then
volumeName=`echo "${1}" | awk -F "/Volumes/" '{print $NF}' | awk -F "/" '{print $1}'`
if [ ! -d "/Volumes/${volumeName}" ] ; then
printf "\nThe specified volume cannot be found, please enter a valid path\n" ; validPath=1 ; return 1
fi
fi
# Check that the path is not a file
if [ -f "${1}" ] ; then
printf "\nThe specified path is to a file, please enter a path to a folder\n" ; validPath=1 ; return 1
fi
# Check that the path is unique
if [ "${1}" == "${ConfigurationFolder}" ] || [ "${1}" == "${LibraryFolder}" ] || [ "${1}" == "${PackageFolder}" ] || [ "${1}" == "${MasterFolder}" ] ; then
printf "\nThe folders must be unique, please enter a different path\n" ; validPath=1 ; return 1
fi
return 0
}
function get_ConfigurationFolder {
prefConfigurationFolder=`defaults read ~/Library/Preferences/au.com.mondada.SIC "ConfigurationFolder" 2>/dev/null`
if [ -n "${prefConfigurationFolder}" ] ; then
if [ -d "${prefConfigurationFolder}" ] ; then
ConfigurationFolder="${prefConfigurationFolder}"
else
printf "Warning: Configurations Folder missing, reverting to default\n"
fi
fi
}
function set_ConfigurationFolder {
display_Subtitle "Default Folders"
printf "Configurations Folder (${ConfigurationFolder}): " ; read newConfigurationFolder
if [ -z "${newConfigurationFolder}" ] || [ "${newConfigurationFolder}" == "${ConfigurationFolder}" ] ; then return 0 ; fi
check_Path "${newConfigurationFolder}"
while [ ${validPath} -ne 0 ] ; do
printf "\nConfiguration Folder (${ConfigurationFolder}): " ; read newConfigurationFolder
if [ -z "${newConfigurationFolder}" ] || [ "${newConfigurationFolder}" == "${ConfigurationFolder}" ] ; then return 0 ; fi
check_Path "${newConfigurationFolder}"
done
ConfigurationFolder="${newConfigurationFolder}"
}
function get_LibraryFolder {
prefLibraryFolder=`defaults read ~/Library/Preferences/au.com.mondada.SIC "LibraryFolder" 2>/dev/null`
if [ -n "${prefLibraryFolder}" ] ; then
if [ -d "${prefLibraryFolder}" ] ; then
LibraryFolder="${prefLibraryFolder}"
else
printf "Warning: Library Folder missing, reverting to default\n"
fi
fi
}
function set_LibraryFolder {
display_Subtitle "Default Folders"
printf "Library Folder (${LibraryFolder}): " ; read newLibraryFolder
if [ -z "${newLibraryFolder}" ] || [ "${newLibraryFolder}" == "${LibraryFolder}" ] ; then return 0 ; fi
check_Path "${newLibraryFolder}"
while [ ${validPath} -ne 0 ] ; do
printf "\nLibrary Folder (${LibraryFolder}): " ; read newLibraryFolder
if [ -z "${newLibraryFolder}" ] || [ "${newLibraryFolder}" == "${LibraryFolder}" ] ; then return 0 ; fi
check_Path "${newLibraryFolder}"
done
LibraryFolder="${newLibraryFolder}"
}
function get_PackageFolder {
prefPackageFolder=`defaults read ~/Library/Preferences/au.com.mondada.SIC "PackageFolder" 2>/dev/null`
if [ -n "${prefPackageFolder}" ] ; then
if [ -d "${prefPackageFolder}" ] ; then
PackageFolder="${prefPackageFolder}"
else
printf "Warning: Packages Folder missing, reverting to default\n"
fi
fi
}
function set_PackageFolder {
display_Subtitle "Default Folders"
printf "Packages Folder (${PackageFolder}): " ; read newPackageFolder
if [ -z "${newPackageFolder}" ] || [ "${newPackageFolder}" == "${PackageFolder}" ] ; then return 0 ; fi
check_Path "${newPackageFolder}"
while [ ${validPath} -ne 0 ] ; do
printf "\nPackage Folder (${PackageFolder}): " ; read newPackageFolder
if [ -z "${newPackageFolder}" ] || [ "${newPackageFolder}" == "${PackageFolder}" ] ; then return 0 ; fi
check_Path "${newPackageFolder}"
done
PackageFolder="${newPackageFolder}"
}
function get_MasterFolder {
prefMasterFolder=`defaults read ~/Library/Preferences/au.com.mondada.SIC "MasterFolder" 2>/dev/null`
if [ -n "${prefMasterFolder}" ] ; then
if [ -d "${prefMasterFolder}" ] ; then
MasterFolder="${prefMasterFolder}"
else
printf "Warning: Masters Folder missing, reverting to default\n"
fi
fi
}
function set_MasterFolder {
display_Subtitle "Default Folders"
printf "Masters Folder (${MasterFolder}): " ; read newMasterFolder
if [ -z "${newMasterFolder}" ] || [ "${newMasterFolder}" == "${MasterFolder}" ] ; then return 0 ; fi
check_Path "${newMasterFolder}"
while [ ${validPath} -ne 0 ] ; do
printf "\nMaster Folder (${MasterFolder}): " ; read newMasterFolder
if [ -z "${newMasterFolder}" ] || [ "${newMasterFolder}" == "${MasterFolder}" ] ; then return 0 ; fi
check_Path "${newMasterFolder}"
done
MasterFolder="${newMasterFolder}"
}
function display_DefaultFolders {
printf "Configurations: ${ConfigurationFolder}\n"
printf "Library: ${LibraryFolder}\n"
printf "Packages: ${PackageFolder}\n"
printf "Masters: ${MasterFolder}\n"
printf "\n"
}
function save_DefaultFolders {
defaults write ~/Library/Preferences/au.com.mondada.SIC "ConfigurationFolder" -string "${ConfigurationFolder}"
if [ ! -d "${ConfigurationFolder}" ] ; then mkdir -p "${ConfigurationFolder}" ; fi
defaults write ~/Library/Preferences/au.com.mondada.SIC "LibraryFolder" -string "${LibraryFolder}"
if [ ! -d "${LibraryFolder}" ] ; then mkdir -p "${LibraryFolder}" ; fi
defaults write ~/Library/Preferences/au.com.mondada.SIC "PackageFolder" -string "${PackageFolder}"
if [ ! -d "${PackageFolder}" ] ; then mkdir -p "${PackageFolder}" ; fi
defaults write ~/Library/Preferences/au.com.mondada.SIC "MasterFolder" -string "${MasterFolder}"
if [ ! -d "${MasterFolder}" ] ; then mkdir -p "${MasterFolder}" ; fi
}
function menu_DefaultFolders {
FolderOptions=( "Preferences Menu" "Configurations" "Library" "Packages" "Masters" )
while [ "${Option}" != "Preferences Menu" ] ; do
display_Subtitle "Default Folders"
display_DefaultFolders
display_Options "Options" "Select an option: "
select Option in "${FolderOptions[@]}" ; do
case "${Option}" in
"Preferences Menu" ) break ;;
"Configurations" ) set_ConfigurationFolder ; unset Option ; break ;;
"Library" ) set_LibraryFolder ; unset Option ; break ;;
"Packages" ) set_PackageFolder ; unset Option ; break ;;
"Masters" ) set_MasterFolder ; unset Option ; break ;;
esac
done
done
unset Option
unset SaveDefaults
while [ -z "${SaveDefaults}" ] ; do
echo
read -sn 1 -p "Save as default settings (Y/n)? " SaveDefaults < /dev/tty
echo
if [ -z "${SaveDefaults}" ] ; then SaveDefaults="y" ; fi
case "${SaveDefaults}" in
"Y" | "y" ) save_DefaultFolders ; echo ;;
"N" | "n" ) echo ;;
* ) echo ; unset SaveDefaults ;;
esac
done
unset SaveDefaults
}
function get_ImageSize {
prefImageSize=`defaults read ~/Library/Preferences/au.com.mondada.SIC "ImageSize" 2>/dev/null`
if [ -n "${prefImageSize}" ] ; then ImageSize="${prefImageSize}" ; fi
}
function check_Size {
# ${1}: Size to validate
validSize=0
# Remove invalid characters
newImageSize="${1//[^0-9.]/}"
# Validate the clean string to ensure its numeric
isValid=$( echo "scale=0; ${newImageSize}/${newImageSize} + 1" | bc -l 2>/dev/null )
if [ ${isValid} -gt 1 ] ; then
# Check that it's larger than 5 GB
if [ $( echo "${newImageSize} < ${minImageSize}" | bc 2>/dev/null ) -ne 0 ] ; then printf "\nMac OS X requires at least \033[1m${minImageSize}\033[m GB of free space to install.\n" ; ImageSize="${minImageSize}" ; validSize=1 ; return 1 ; fi
# Check that it's smaller than 2 TB
if [ $( echo "${newImageSize} > ${maxImageSize}" | bc 2>/dev/null ) -ne 0 ] ; then printf "\nThe maximum image size is \033[1m${maxImageSize}\033[m GB.\n" ; ImageSize="${maxImageSize}" ; validSize=1 ; return 1 ; fi
return 0
else
printf "\n\033[1m${1}\033[m is not a valid value, please enter a numeric value.\n" ; validSize=1 ; return 1
fi
}
function set_ImageSize {
display_Subtitle "Image Settings"
printf "Image Size (${ImageSize}): " ; read newImageSize
if [ -z "${newImageSize}" ] || [ "${newImageSize}" == "${ImageSize}" ] ; then return 0 ; fi
check_Size "${newImageSize}"
while [ ${validSize} -ne 0 ] ; do
printf "\nImage Size (${ImageSize} GB): " ; read newImageSize
if [ -z "${newImageSize}" ] || [ "${newImageSize}" == "${ImageSize}" ] ; then return 0 ; fi
check_Size "${newImageSize}"
done
ImageSize="${newImageSize}"
}
function get_VolumeName {
prefVolumeName=`defaults read ~/Library/Preferences/au.com.mondada.SIC "VolumeName" 2>/dev/null`
if [ -n "${prefVolumeName}" ] ; then VolumeName="${prefVolumeName}" ; fi
}
function check_VolumeName {
# ${1}: Volume Name to validate
validName=0
# Check for invalid characters
escapedName="${1//[\$\(\)\[\]\`\~\?\*\#\\\!\|\'\"]/_}"
if [ "${1}" != "${escapedName}" ] ; then printf "\nThe volume name cannot contain the following characters: \033[1m\$()[]\`~?*#\!|'\"\033[m\n" ; validName=1 ; return 1 ; fi
return 0
}
function set_VolumeName {
display_Subtitle "Image Settings"
printf "Volume Name (${VolumeName}): " ; read newVolumeName
if [ -z "${newVolumeName}" ] || [ "${newVolumeName}" == "${VolumeName}" ] ; then return 0 ; fi
newVolumeName="${newVolumeName//\//:}"
check_VolumeName "${newVolumeName}"
while [ ${validName} -ne 0 ] ; do
printf "Volume Name (${VolumeName}): " ; read newVolumeName
if [ -z "${newVolumeName}" ] || [ "${newVolumeName}" == "${VolumeName}" ] ; then return 0 ; fi
newVolumeName="${newVolumeName//\//:}"
check_VolumeName "${newVolumeName}"
done
VolumeName="${newVolumeName}"
}
function display_ImageSettings {
printf "Image Size: ${ImageSize} GB\n"
printf "Volume Name: ${VolumeName}\n"
printf "\n"
}
function save_ImageSettings {
defaults write ~/Library/Preferences/au.com.mondada.SIC "ImageSize" -float "${ImageSize}"
defaults write ~/Library/Preferences/au.com.mondada.SIC "VolumeName" -string "${VolumeName}"
}
function menu_ImageSettings {
ImageOptions=( "Preferences Menu" "Image Size" "Volume Name" )
while [ "${Option}" != "Preferences Menu" ] ; do
display_Subtitle "Image Settings"
display_ImageSettings
display_Options "Options" "Select an option: "
select Option in "${ImageOptions[@]}" ; do
case "${Option}" in
"Preferences Menu" ) break ;;
"Image Size" ) set_ImageSize ; unset Option ; break ;;
"Volume Name" ) set_VolumeName ; unset Option ; break ;;
esac
done
done
unset Option
unset SaveDefaults
while [ -z "${SaveDefaults}" ] ; do
echo
read -sn 1 -p "Save as default settings (Y/n)? " SaveDefaults < /dev/tty
echo
if [ -z "${SaveDefaults}" ] ; then SaveDefaults="y" ; fi
case "${SaveDefaults}" in
"Y" | "y" ) save_ImageSettings ; echo ;;
"N" | "n" ) echo ;;
* ) echo ; unset SaveDefaults ;;
esac
done
unset SaveDefaults
}
function get_ExportType {
prefExportType=`defaults read ~/Library/Preferences/au.com.mondada.SIC "ExportType" 2>/dev/null`
if [ -n "${prefExportType}" ] ; then ExportType="${prefExportType}" ; fi
}
function display_RecoveryPartition {
printf " ["
if [ ${ExportType} -eq 1 ] ; then printf "*" ; else printf " " ; fi
printf "] Include Recovery Partition in Master\n"
printf " ["
if [ ${ExportType} -eq 2 ] ; then printf "*" ; else printf " " ; fi
printf "] Remove Recovery Partition from Master\n"
printf " ["
if [ ${ExportType} -eq 3 ] ; then printf "*" ; else printf " " ; fi
printf "] Create separate image for Recovery Partition\n"
printf "\n"
}
function set_RecoveryPartition {
RecoveryOptions=( "Include Recovery Partition in Master" "Remove Recovery Partition from Master" "Create separate image for Recovery Partition" )
display_Subtitle "Export Settings"
display_RecoveryPartition
display_Options "Options" "Select an option: "
select Option in "${RecoveryOptions[@]}" ; do
case "${Option}" in
"Include Recovery Partition in Master" ) ExportType=1 ; break ;;
"Remove Recovery Partition from Master" ) ExportType=2 ; break ;;
"Create separate image for Recovery Partition" ) ExportType=3 ; break ;;
esac
done
unset Option
}
function get_ScanImage {
prefScanImage=`defaults read ~/Library/Preferences/au.com.mondada.SIC "ScanImage" 2>/dev/null`
if [ -n "${prefScanImage}" ] ; then ScanImage="${prefScanImage}" ; fi
}
function display_ScanImage {
printf " ["
if [ ${ScanImage} -eq 1 ] ; then printf "*" ; else printf " " ; fi
printf "] Scan masters for restore\n"
printf "\n"
}
function set_ScanImage {
display_Subtitle "Export Settings"
display_ScanImage
while [ -z "${ASR}" ] ; do
read -sn 1 -p "Scan masters for restore (Y/n)? " ASR < /dev/tty
if [ -z "${ASR}" ] ; then ASR="y" ; fi
case "${ASR}" in
"Y" | "y" ) echo ; ScanImage=1 ;;
"N" | "n" ) echo ; ScanImage=0 ;;
* ) echo ; unset ASR ;;
esac
done
unset ASR
}
function display_ExportSettings {
display_RecoveryPartition
display_ScanImage
}
function save_ExportSettings {
defaults write ~/Library/Preferences/au.com.mondada.SIC "ExportType" -int ${ExportType}
defaults write ~/Library/Preferences/au.com.mondada.SIC "ScanImage" -int ${ScanImage}
}
function menu_ExportSettings {
ExportOptions=( "Preferences Menu" "Recovery Partition" "Scan for Restore" )
while [ "${Option}" != "Preferences Menu" ] ; do
display_Subtitle "Export Settings"
display_ExportSettings
display_Options "Options" "Select an option: "
select Option in "${ExportOptions[@]}" ; do
case "${Option}" in
"Preferences Menu" ) break ;;
"Recovery Partition" ) set_RecoveryPartition ; unset Option ; break ;;
"Scan for Restore" ) set_ScanImage ; unset Option ; break ;;
esac
done
done
unset Option
unset SaveDefaults
while [ -z "${SaveDefaults}" ] ; do
echo
read -sn 1 -p "Save as default settings (Y/n)? " SaveDefaults < /dev/tty
echo
if [ -z "${SaveDefaults}" ] ; then SaveDefaults="y" ; fi
case "${SaveDefaults}" in
"Y" | "y" ) save_ExportSettings ; echo ;;
"N" | "n" ) echo ;;
* ) echo ; unset SaveDefaults ;;
esac
done
unset SaveDefaults
}
function get_Preferences {
get_ConfigurationFolder
get_LibraryFolder
get_PackageFolder
get_MasterFolder
get_VolumeName
get_ImageSize
get_ExportType
get_ScanImage
}
function menu_Preferences {
PrefOptions=( "Main Menu" "Default Folders" "Image Settings" "Export Settings" )
while [ "${Option}" != "Main Menu" ] ; do
display_Subtitle "Preferences"
display_Options "Options" "Select an option: "
select Option in "${PrefOptions[@]}" ; do
case "${Option}" in
"Main Menu" ) break ;;
"Default Folders" ) menu_DefaultFolders ; unset Option ; break ;;
"Image Settings" ) menu_ImageSettings ; unset Option ; break ;;
"Export Settings" ) menu_ExportSettings ; unset Option ; break ;;
esac
done
done
unset Option
}
# Section: Create Image
function detect_Sources {
unset SourceVersions[@]
unset SourceBuilds[@]
unset SourceVolumes[@]
unset ImageNames[@]
IFS=$'\n'
Volumes=( `df | grep "/Volumes/" | awk -F "/Volumes/" '{print $NF}'` )
unset IFS
for Volume in "${Volumes[@]}" ; do
if [ ! -e "/Volumes/${Volume}/var/db/.AppleSetupDone" ] && [ -e "/Volumes/${Volume}/System/Library/CoreServices/SystemVersion.plist" ] ; then
SourceOSMinor=`defaults read "/Volumes/${Volume}/System/Library/CoreServices/SystemVersion" ProductVersion | awk -F "." '{print $2}'`
SourceOSPoint=`defaults read "/Volumes/${Volume}/System/Library/CoreServices/SystemVersion" ProductVersion | awk -F "." '{print $3}'`
if [ -z "${SourceOSPoint}" ] ; then SourceOSPoint=0 ; fi
case ${SourceOSMinor} in
5 ) OSname="leopard" ;;
6 ) OSname="snowleopard" ;;
7 ) OSname="lion" ;;
8 ) OSname="mountainlion" ;;
esac
if [ -e "/Volumes/${Volume}/System/Library/CoreServices/ServerVersion.plist" ] || [ -e "/Volumes/${Volume}/Applications/Server.app" ] || [ -e "/Volumes/${Volume}/Packages/Server.pkg" ] ; then
ProductName="Mac OS X Server"
ProductType="server"
else
ProductName="Mac OS X"
ProductType="user"
fi
ProductVersion=`defaults read "/Volumes/${Volume}/System/Library/CoreServices/SystemVersion" ProductVersion`
ProductBuildVersion=`defaults read "/Volumes/${Volume}/System/Library/CoreServices/SystemVersion" ProductBuildVersion`
if [ -e "/Volumes/${Volume}/System/Installation/Packages/OSInstall.mpkg" ] || [ -e "/Volumes/${Volume}/Packages/OSInstall.mpkg" ] ; then
if [ ${SourceOSMinor} -eq ${SystemOSMinor} ] ; then
if [ ${SystemOSMinor} -eq 7 -a ${SystemOSPoint} -gt 3 ] ; then
if [ ${SourceOSPoint} -gt 3 ] ; then
SourceVersions=( "${SourceVersions[@]}" "${ProductName} ${ProductVersion} (${ProductBuildVersion}) Installer" )
SourceBuilds=( "${SourceBuilds[@]}" "${ProductBuildVersion}" )
SourceVolumes=( "${SourceVolumes[@]}" "${Volume}" )
ImageNames=( "${ImageNames[@]}" `echo "${OSname}_${ProductBuildVersion}_${ProductType}" | awk {'print tolower()'}` )
fi
else
SourceVersions=( "${SourceVersions[@]}" "${ProductName} ${ProductVersion} (${ProductBuildVersion}) Installer" )
SourceBuilds=( "${SourceBuilds[@]}" "${ProductBuildVersion}" )
SourceVolumes=( "${SourceVolumes[@]}" "${Volume}" )
ImageNames=( "${ImageNames[@]}" `echo "${OSname}_${ProductBuildVersion}_${ProductType}" | awk {'print tolower()'}` )
fi
fi
else
SourceVersions=( "${SourceVersions[@]}" "${ProductName} ${ProductVersion} (${ProductBuildVersion})" )
SourceBuilds=( "${SourceBuilds[@]}" "${ProductBuildVersion}" )
SourceVolumes=( "${SourceVolumes[@]}" "${Volume}" )
ImageNames=( "${ImageNames[@]}" `echo "${OSname}_${ProductBuildVersion}_${ProductType}" | awk {'print tolower()'}` )
fi
fi
done
}
function display_Source {
printf "Source: "
if [ -n "${SourceVersion}" ] ; then printf "${SourceVersion}" ; else printf "-" ; fi
printf "\nImage Name: "
if [ -n "${ImageName}" ] ; then printf "${ImageName}.dmg" ; else printf "-" ; fi
printf "\n"
printf "\n"
}
function select_Source {
detect_Sources
display_Subtitle "Select Source"
if [ ${#SourceVersions[@]} -eq 0 ] ; then
unset SourceVersion
unset SourceBuild
unset SourceVolume
unset ImageName
press_anyKey "No sources available, please insert or mount an OS X Installer, or attach or mount an un-booted system volume."
else
display_Source
display_Options "Sources" "Select a source: "
select SourceVersion in "${SourceVersions[@]}" ; do
if [ -n "${SourceVersion}" ] ; then break ; fi
done
i=0 ; for Element in "${SourceVersions[@]}" ; do
if [ "${Element}" == "${SourceVersion}" ] ; then
SourceVolume="${SourceVolumes[i]}"
SourceBuild="${SourceBuilds[i]}"
ImageName="${ImageNames[i]}"
break
fi
let i++
done
fi
}
function install_Package {
# ${1}: Path to package
# ${2}: Installation target
# ${3}: InstallType
unset allowUntrusted
if [ ${SystemOSMinor} -eq 7 -a ${SystemOSPoint} -gt 3 ] || [ ${SystemOSMinor} -gt 7 ] ; then GateKeeper="-allowUntrusted" ; fi
if [ -e "${1}" ] ; then
IFS=$'\n'
PackageTitles=( `installer -pkginfo -pkg "${1}"` )
unset IFS
s=1
if [ ${3} -eq 1 ] ; then
printf "\ninstaller: Package name is ${PackageTitles[0]}\n"
open "${1}"
printf "installer:PHASE:Waiting for installation to complete…\n"
while [ `ps eax | grep -i "Installer.app" | grep -v "grep" | awk '{print $1}'` ] ; do
printf "\b${spin:s++%${#spin}:1}"
done
printf "\binstaller: The install is complete.\n"
else
unset Previous
unset InstallerStatus
unset InstallerProgress
installer -verboseR "${GateKeeper}" -pkg "${1}" -target "${2}" 2>/dev/null | while read Line ; do
if echo "${Line}" | grep -q "installer: " ; then printf "${Line}\n" ; fi
if echo "${Line}" | grep -q "\(installer:PHASE:\|installer:STATUS:\)" ; then
if [ "${InstallerStatus}" != "${Line}" ] ; then
InstallerStatus="${Line}"
if echo "${Previous}" | grep -q "installer:%" ; then
printf "\n${InstallerStatus}\n"
else
printf "${InstallerStatus}\n"
fi
fi
fi
if echo "${Line}" | grep -q "installer:%" ; then
if [ "${InstallerProgress}" != "${Line}" ] ; then
InstallerProgress="${Line//%/%%}"
printf "\r${InstallerProgress}"
fi
fi
Previous="${Line}"
done
fi
echo
else
press_anyKey "The package selection is invalid, please review your settings."
fi
}
function create_Image {
display_Subtitle "Create Image"
display_Source
if [ -z "${SourceVersion}" ] ; then press_anyKey "No source selected, please select a source first." ; return 1 ; fi
if [ ! -e "/Volumes/${SourceVolume}" ] ; then press_anyKey "The source volume is no longer available, please re-select the source." ; return 1 ; fi
ProductBuildVersion=`defaults read "/Volumes/${SourceVolume}/System/Library/CoreServices/SystemVersion" ProductBuildVersion`
if [ "${SourceBuild}" != "${ProductBuildVersion}" ] ; then press_anyKey "The source volume is no longer available, please re-select the source." ; return 1 ; fi
while [ -e "${LibraryFolder}/${ImageName}.dmg" ] ; do
printf "An image already exists named \033[1m${ImageName}.dmg\033[m.\n"
read -sn 1 -p "Would you like to overwrite it (y/N)? " Overwrite < /dev/tty ; echo
if [ -z "${Overwrite}" ] ; then Overwrite="n" ; fi
case "${Overwrite}" in
"Y" | "y" ) echo ; rm -f "${LibraryFolder}/${ImageName}.dmg" ; break ;;
"N" | "n" ) echo ; break ;;
esac
done
unset Overwrite
if [ ! -e "${LibraryFolder}/${ImageName}.dmg" ] ; then
Removables=(
".Spotlight-V100"
".Trashes"
".fseventsd"
)
if [ ! -e "${LibraryFolder}" ] ; then mkdir -p "${LibraryFolder}" ; chown 99:99 "${LibraryFolder}" ; fi
if [ -e "/Volumes/${SourceVolume}/System/Installation/Packages/OSInstall.mpkg" ] || [ -e "/Volumes/${SourceVolume}/Packages/OSInstall.mpkg" ] ; then
rm -f "/tmp/${ImageName}.sparseimage" &>/dev/null
hdiutil create -size "${ImageSize}g" -type SPARSE -fs HFS+J -volname "${VolumeName}" "/tmp/${ImageName}.sparseimage"
Volume=`hdiutil attach -owners on -noverify "/tmp/${ImageName}.sparseimage" | grep "/Volumes/${VolumeName}" | awk -F "/Volumes/" '{print $NF}'`
set_Target "${Volume}"
chown 0:80 "${Target}"
chmod 1775 "${Target}"
if [ -e "/Volumes/${SourceVolume}/System/Installation/Packages/OSInstall.mpkg" ] ; then
unset Customize
while [ -z "${Customize}" ] ; do
echo
read -sn 1 -p "Customize Installation (y/N)? " Customize < /dev/tty
if [ -z "${Customize}" ] ; then Customize="n" ; fi ; echo
case "${Customize}" in
"Y" | "y" ) InstallType=1 ; break ;;
"N" | "n" ) InstallType=0 ; break ;;
* ) unset Customize ;;
esac
echo
done
install_Package "/Volumes/${SourceVolume}/System/Installation/Packages/OSInstall.mpkg" "${Target}" ${InstallType}
else
install_Package "/Volumes/${SourceVolume}/Packages/OSInstall.mpkg" "${Target}" 0
fi
bless --folder "${Target}/System/Library/CoreServices" --bootefi 2>/dev/null
touch "${Target}/private/var/db/.RunLanguageChooserToo"
for Removable in "${Removables[@]}" ; do
if [ -e "${Target}/${Removable}" ] ; then rm -rf "${Target}/${Removable}" ; fi
done
Device=`diskutil info "${Target}" | grep "Part of Whole:" | awk '{print $NF}'`
diskutil unmountDisk force "/dev/${Device}" &>/dev/null
printf "Unmount of all volumes on ${Device} was successful\n"
diskutil eject "/dev/${Device}" &>/dev/null
printf "Disk /dev/${Device} ejected\n"
hdiutil convert -format UDZO "/tmp/${ImageName}.sparseimage" -o "${LibraryFolder}/${ImageName}.dmg"
rm -f "/tmp/${ImageName}.sparseimage" &>/dev/null
echo
press_anyKey
else
for Removable in "${Removables[@]}" ; do
if [ -e "/Volumes/${SourceVolume}/${Removable}" ] ; then rm -rf "/Volumes/${SourceVolume}/${Removable}" ; fi
done
Device=`diskutil info "/Volumes/${SourceVolume}" | grep "Part of Whole:" | awk '{print $NF}'`
DeviceNodes=`diskutil list "/dev/${Device}" | grep -c "Apple_HFS"`
if [ ${DeviceNodes} -gt 1 ] ; then
Device=`diskutil info "/Volumes/${SourceVolume}" | grep "Device Node:" | awk -F "/dev/" '{print $NF}'`
diskutil unmount force "/dev/${Device}" &>/dev/null
printf "Volume ${SourceVolume} on ${Device} unmounted\n"
else
diskutil unmountDisk force "/dev/${Device}" &>/dev/null
printf "Unmount of all volumes on ${Device} was successful\n"
fi
hdiutil create -srcdevice "/dev/${Device}" -o "${LibraryFolder}/${ImageName}.dmg"
diskutil mountDisk "/dev/${Device}"
echo
press_anyKey
fi
fi
}
function display_Images {
printf "Images: ${Images[0]}\n"
i=0 ; for Image in "${Images[@]}" ; do
if [ ${i} -ne 0 ] ; then printf " ${Image}\n" ; fi
let i++
done
printf "\n"
}
function menu_CreateImage {
CreateOptions=( "Main Menu" "Select Source" "Create Image" )
while [ "${Option}" != "Main Menu" ] ; do
get_Images
display_Subtitle "Create Image"
display_Source
display_Images
display_Options "Options" "Select an option: "
select Option in "${CreateOptions[@]}" ; do
case "${Option}" in
"Main Menu" ) break ;;
"Select Source" ) select_Source ; unset Option ; break ;;
"Create Image" ) create_Image ; unset Option ; break ;;
esac
done
done
unset SourceVersion
unset SourceBuild
unset SourceVolume
unset ImageName
unset Option
}