-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createinstalliso
executable file
·268 lines (230 loc) · 9.9 KB
/
createinstalliso
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
#!/bin/zsh
# Copyright (c) 2020 Ryan Schmidt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Creates an ISO file from a macOS Install app or InstallESD.dmg, especially
# for use with VMware.
set -euo pipefail
scriptname=${0##*/}
usage() {
printf "usage: %s [ Install macOS XXX.app | InstallOS.dmg | InstallESD.dmg ]\n" $scriptname
exit
}
err() {
local errmsg=$1
local exitcode=${2-1}
printf "%s: %s\n" $scriptname $errmsg 1>&2
exit $exitcode
}
# Based on vercomp by Paused until further notice but updated with zsh compatibility
# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
vercomp() {
[[ $1 == $2 ]] && return 0
local i n num1 num2 ver1 ver2 IFS=.
if [[ -n ${ZSH_VERSION-} ]]; then
ver1=(${=1}) ver2=(${=2})
else
ver1=($1) ver2=($2)
fi
if ((${#ver1[@]} > ${#ver2[@]})); then
n=${#ver1[@]}
else
n=${#ver2[@]}
fi
for ((i=0; i<n; i++)); do
num1=$((10#${ver1[@]:$i:1}))
num2=$((10#${ver2[@]:$i:1}))
((num1 > num2)) && return 1
((num1 < num2)) && return 2
done
return 0
}
versiontest() {
local op
vercomp $1 $3
case $? in
0) op="=";;
1) op=">";;
2) op="<";;
esac
[[ $op == $2 ]]
}
(($# > 0)) || usage
#command -v unbuffer > /dev/null || err "unbuffer is required. You can install it with MacPorts: sudo port install expect"
command -v rsync > /dev/null || err "rsync 3.1.0 or later is required. You can install it with MacPorts: sudo port install rsync"
rsyncversion=$(rsync --version | sed -En '1s/^rsync version ([^ ]+).*$/\1/p')
versiontest $rsyncversion "<" "3.1" && err "rsync 3.1.0 or later is required. You can install it with MacPorts: sudo port install rsync"
#(( $UID == 0 )) || exec sudo $0 "$@"
#origuid=${SUDO_UID:-$UID}
#origgid=${SUDO_GID:-$GID}
plistbuddy=/usr/libexec/PlistBuddy
lastprog=1
printprog() {
printf "PROGRESS:%.2f " $lastprog
echo "$@"
}
scaleprog() {
local msg=$1
local scaledmin=$lastprog
local scaledmax=${2-$scaledmin}
local scaledrange=$((scaledmax - scaledmin))
local unscaledmax=${3-100}
printprog $msg
while IFS= read -r line; do
[[ -z $line ]] && continue
unscaledprog=${line%% *}
lastprog=$(echo "scale=2; $scaledmin + $scaledrange * ($unscaledprog / $unscaledmax)" | bc)
printprog ${line:$(($#unscaledprog+1))}
done
lastprog=$scaledmax
}
cleanup() {
printprog "Cleaning up"
((progpid != 0)) && kill $progpid
[[ -d $mount ]] && hdiutil detach $mount
[[ -d $esdmount ]] && hdiutil detach $esdmount
[[ -d $outermount ]] && hdiutil detach $outermount
# sudo chown -R $origuid:$origgid $builddir
# rm -rf $builddir
}
traphandler() {
cleanup
trap - EXIT
exit 0
}
trap 'traphandler' EXIT INT TERM
: ${TMPDIR=/tmp}
builddir=$(mktemp -d $TMPDIR/$scriptname.XXXXXX)
plist=$builddir/tmp.plist
outermount=$builddir/outer
outershadow=$builddir/outershadow
esddmg=
esdmount=$builddir/esd
esdshadow=$builddir/esdshadow
input=$1
progpid=0
mount=
if [[ -f $input && ${input##*.} == "dmg" ]]; then
# User specified a dmg. Mount it.
outerdmg=$input
printprog "Mounting ${outerdmg##*/}"
hdiutil attach $outerdmg -noverify -mountpoint $outermount -nobrowse -puppetstrings -shadow $outershadow | sed -Eln 's/^PERCENT:([0-9.]+).*$/\1/p' | scaleprog "Mounting ${outerdmg##*/}" 5
if [[ -f $outermount/Packages/OSInstall.mpkg ]]; then
# The dmg is an ESD. Use it.
esddmg=$outerdmg
esdmount=$outermount
esdshadow=$outershadow
else
# The dmg is not ESD. Find the newest pkg in its root directory.
pkg=$(find $outermount -depth 1 -type f -name '*.pkg' -exec stat -f '%m,%N' {} \; | sort -n | head -n1 | cut -d, -f2-)
if [[ -n $pkg ]]; then
# Found a pkg. Expand it.
printprog "Expanding ${pkg##*/}"
pkgdir=$builddir/pkg
# Sigh, pkgutil provides no status information, so make our
# own by monitoring the size of the expanded pkg dir.
pkgsize=$(du -sk $pkg | awk '{print $1}')
(while :; do [[ -d $pkgdir ]] && du -sk $pkgdir; sleep 2; done) | awk '{print $1; fflush()}' | scaleprog "Expanding ${pkg##*/}" 20 $pkgsize &
progpid=$!
pkgutil --expand $pkg $pkgdir
kill $progpid
progpid=0
# Look for an ESD in the expanded pkg.
dir=$pkgdir
else
# No pkg found. Look for an ESD on the disk image.
dir=$outermount
fi
fi
elif [[ -d $input ]]; then
# User specified a directory. Look for an ESD in the directory.
dir=$input
else
# User specified something we can't use.
usage
fi
if [[ -z $esddmg ]]; then
# Find newest ESD in dir.
esddmg=$(find $dir -type f -name InstallESD.dmg -exec stat -f '%m,%N' {} \; | sort -n | head -n1 | cut -d, -f2-)
if [[ -n $esddmg ]]; then
printprog "Mounting ${esddmg##*/}"
hdiutil attach $esddmg -mountpoint $esdmount -nobrowse -puppetstrings -shadow $esdshadow | sed -Eln 's/^PERCENT:([0-9.]+).*$/\1/p' | scaleprog "Mounting ${esddmg##*/}" 25
fi
fi
if [[ ! -f $esddmg ]]; then
# Still didn't find an ESD. Give up.
usage
fi
#printprog "Mounting electronic software distribution image"
#hdiutil attach $esddmg -mountpoint $esdmount -nobrowse -puppetstrings -shadow $esdshadow | sed -Eln 's/^PERCENT:([0-9.]+).*$/\1/p' | scaleprog "Mounting electronic software distribution image" 5
if [[ -f $esdmount/boot.efi ]]; then
# The ESD has a bootable system on it (10.7-10.8). Use that as our dmg.
shadow=$esdshadow
mount=$esdmount
dmg=$esddmg
diskutil info -plist $mount > $plist
dev=$($plistbuddy $plist -c 'Print :DeviceNode')
else
# The ESD doesn't have a bootable system (10.9+). Use the base system as our dmg.
shadow=$builddir/baseshadow
mount=$builddir/basesystem
# Look for the base system in the same directory as the ESD (10.13+).
basedir=$(dirname $esddmg)
# Look for the base system on the ESD (10.7-10.12).
[[ -f $basedir/BaseSystem.dmg ]] || basedir=$esdmount
dmg=$basedir/BaseSystem.dmg
# Make room on the base system for the files we'll copy.
printprog "Resizing base system"
basedmgsize=$(hdiutil resize -limits $dmg | awk '{print int($2/2)+1}')
filessize=$(du -cks $esdmount/Packages $basedir/BaseSystem.* | tail -n1 | cut -d$'\t' -f1)
dmgsize=$((basedmgsize + filessize))
hdiutil resize -shadow $shadow -size ${dmgsize}k $dmg
printprog "Mounting base system"
# hdiutil attach $dmg -mountpoint $mount -nobrowse -puppetstrings -shadow $shadow | unbuffer -p sed -En 's/^PERCENT:([0-9.]+).*$/\1/p' | scaleprog "Mounting base system image" 0 100
#script -q /dev/null
hdiutil attach $dmg -mountpoint $mount -nobrowse -puppetstrings -shadow $shadow | sed -Eln 's/^PERCENT:([0-9.]+).*$/\1/p' | scaleprog "Mounting base system" 30
diskutil info -plist $mount > $plist
dev=$($plistbuddy $plist -c 'Print :DeviceNode')
# printprog "Copying packages and other files"
# rsync -a --info=progress2 $esdmount/ --include='BaseSystem.*' --include='Packages/***' --exclude='*' $mount | unbuffer -p tr '\r' '\n' | unbuffer -p sed -En 's/^ *[^ ]+ +([0-9]+).*$/\1/p' | scaleprog "Copying packages and other files" 0 100
#script -q /dev/null
printprog "Copying base system"
rsync -a --info=progress2 $basedir/ --include='BaseSystem.*' --exclude='*' $mount | tr -u '\r' '\n' | sed -Eln 's/^ *[^ ]+ +([0-9]+).*$/\1/p' | scaleprog "Copying base system" 35
printprog "Copying packages"
rsync -a --info=progress2 $esdmount/ --include='Packages/***' --exclude='*' $mount | tr -u '\r' '\n' | sed -Eln 's/^ *[^ ]+ +([0-9]+).*$/\1/p' | scaleprog "Copying packages" 60
rm -f $mount/System/Installation/Packages
mv $mount/Packages $mount/System/Installation/
fi
systemversionplist=$mount/System/Library/CoreServices/SystemVersion.plist
osversion=$($plistbuddy $systemversionplist -c 'Print :ProductVersion')
osbuild=$($plistbuddy $systemversionplist -c 'Print :ProductBuildVersion')
appdisplayname=$($plistbuddy $mount/Install\ *.app/Contents/Info.plist -c 'Print :CFBundleDisplayName')
installername="$appdisplayname $osversion $osbuild"
diskutil rename $dev $installername
diskutil info -plist $dev > $plist
mount=$($plistbuddy $plist -c 'Print :MountPoint')
find $mount -depth 1 \! -name $appdisplayname.app -print0 | xargs -0 chflags -h hidden
printprog "Unmounting image"
iso=$builddir/$installername.iso
hdiutil detach $mount
printprog "Creating ISO image"
#sudo COLUMNS=100 hdiutil makehybrid -o $iso $mount/ | unbuffer -p tr '.' '\n' | unbuffer -p awk 'BEGIN {n=1} /^$/ {print n++}' | scaleprog "Creating ISO image" 0 100
# "script -q /dev/null" unbuffers the output.
sudo script -q /dev/null hdiutil makehybrid -o $iso -shadow $shadow $dmg | tr -u '.' '\n' | awk 'BEGIN {n=1} /^$/ {print n++; fflush()}' | scaleprog "Creating ISO image" 100 $COLUMNS