-
Notifications
You must be signed in to change notification settings - Fork 12
/
create-iso
executable file
·253 lines (207 loc) · 6.23 KB
/
create-iso
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
#!/usr/bin/env bash
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -euo pipefail
IFS=$'\n\t'
INSTALLER_CFG="installerconfig"
TRITON_DIST_FILE="triton.txz"
RELEASE=
MIRROR=
MIRROR_PATH=
ISO=
ISO_DIR=
ISO_MOUNT=
ISO_LAYOUT=
ISO_CHECKSUM=
ISO_FILENAME=
usage() {
cat <<EOF
Create a custom FreeBSD ISO with the necessary packages and tooling installed
for deploying on SmartOS, Triton and the Joyent Public Cloud.
Usage:
$0 -r <RELEASE> -m <MIRROR> -p <MIRROR_PATH> -i <ISO> -c <ISO_CHECKSUM> -d <ISO_DIR> -M <ISO_MOUNT> -l <ISO_LAYOUT> -f <ISO_FILENAME>
Example:
$0 -r 11.0 -m ftp.freebsd.org -p /pub/FreeBSD/releases/ISO-IMAGES -i FreeBSD-11.0-RELEASE-amd64-disc1.iso -c CHECKSUM.SHA256-FreeBSD-11.0-RELEASE-amd64 -d /data/freebsd-iso -M /mnt/freebsd-iso -l /data/freebsd-custom-11.0 -f freebsd-11.0-custom.iso
OPTIONS:
-r The desired FreeBSD release (e.g., 11.0)
-m The mirror to use when downloading the release ISO. Do not include protocol (e.g., ftp://). Assumes ftp mirror
-p The desired mirror path to the ISO (e.g., /pub/FreeBSD/releases/ISO-IMAGES)
-i The ISO filename
-c The name of the checksum file for the ISO
-d The location to save the dowloaded ISO. Must be absolute path.
-M The mount point for the downloaded ISO
-l The directory location for saving a copy of the ISO layout. Must be absolute path.
-f The name to use for the custom ISO
-h Show this message
EOF
}
while getopts "hr:m:p:i:c:d:M:l:f:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
r)
RELEASE=${OPTARG}
;;
m)
MIRROR=${OPTARG%/}
;;
p)
MIRROR_PATH=${OPTARG%/}
;;
i)
ISO=${OPTARG}
;;
c)
ISO_CHECKSUM=${OPTARG}
;;
d)
ISO_DIR=${OPTARG%/}
;;
M)
ISO_MOUNT=${OPTARG%/}
;;
l)
ISO_LAYOUT=${OPTARG%/}
;;
f)
ISO_FILENAME="./${OPTARG}"
;;
?)
usage
exit
;;
esac
done
if [[ -z ${RELEASE} ]]; then
echo "Error: missing release value (-r)"
exit 1
fi
if [[ -z ${MIRROR} ]]; then
echo "Error: missing mirror url (-m)"
exit 1
fi
if [[ -z ${MIRROR_PATH} ]]; then
echo "Error: missing mirror path (-p)"
exit 1
fi
if [[ -z ${ISO} ]]; then
echo "Error: missing ISO (-i) value"
exit 1
fi
if [[ -z ${ISO} ]]; then
echo "Error: missing ISO checksum (-c) value"
exit 1
fi
if [[ -z ${ISO_DIR} ]]; then
echo "Error: missing ISO directory (-d) value"
exit 1
fi
if [[ -z ${ISO_MOUNT} ]]; then
echo "Error: missing mount point directory (-M) value"
exit 1
fi
if [[ -z ${ISO_LAYOUT} ]]; then
echo "Error: missing DVD layout directory (-l) value"
exit 1
fi
if [[ -z ${ISO_FILENAME} ]]; then
echo "Error: missing custom ISO file name (-f) value"
exit 1
fi
# Clean up ISO file if download is interupted
trap 'rm -rf $PWD/${ISO_DIR}/${ISO}' SIGHUP SIGINT SIGTERM
# Delete Previous custom layout
trap 'rm -rf $ISO_LAYOUT' SIGHUP SIGINT SIGTERM
fetch_iso() {
if [[ ! -d $ISO_DIR ]]; then
mkdir -p $ISO_DIR
fi
echo "==> Fetching ${ISO_CHECKSUM}"
curl -sS -o ${ISO_DIR}/${ISO_CHECKSUM} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${ISO_CHECKSUM}
echo "==> Checking for local copy of $ISO..."
if [[ -e $ISO_DIR/$ISO ]]; then
echo "==> Found local copy of $ISO"
else
echo "==> Local copy not found."
echo "==> Fetching ISO from $MIRROR..."
curl -sS -o ${ISO_DIR}/${ISO} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${ISO}
echo "==> Done!"
echo "==> ${ISO} saved to $ISO_DIR/"
echo "==> Verifying $ISO with ${ISO_CHECKSUM}"
if [[ "$(sha256 $ISO_DIR/$ISO | cut -f2 -d= | tr -d '[:space:]')" == "$(grep "($ISO)" $ISO_DIR/${ISO_CHECKSUM} | cut -f2 -d= | tr -d '[:space:]')" ]]; then
echo "==> Checksums match."
else
echo "==> Checksums don't match!"
exit 1
fi
fi
}
create_layout() {
echo "==> Creating custom ISO Layout"
if [[ -d $ISO_LAYOUT ]]; then
echo "==> Previous layout $ISO_LAYOUT exists...deleting"
rm -rf $ISO_LAYOUT
fi
echo "==> Creating $ISO_LAYOUT"
mkdir -p $ISO_LAYOUT
if [[ ! -d $ISO_MOUNT ]]; then
echo "==> Creating $ISO_MOUNT ..."
mkdir $ISO_MOUNT
fi
if df | grep -q $ISO_MOUNT; then
echo "==> Unmounting previous $ISO_MOUNT..."
umount $ISO_MOUNT
fi
echo "==> Mounting $ISO to $ISO_MOUNT"
mount -t cd9660 /dev/"$(mdconfig -f $ISO_DIR/$ISO)" $ISO_MOUNT
echo "==> Copying layout from $ISO_MOUNT to $ISO_LAYOUT"
rsync -aq $ISO_MOUNT/ $ISO_LAYOUT
echo "==> Copying and integrating Triton guesttools:"
# Cleanup previous distribution file
[[ -f $TRITON_DIST_FILE ]] && rm $TRITON_DIST_FILE
( cd triton-freebsd-guesttools
tar -cvJpf ../${TRITON_DIST_FILE} ./etc ./lib ./usr
)
DISTFILE_SHASUM_STDOUT=$(shasum -a 256 $TRITON_DIST_FILE)
DISTFILE_SHASUM_PLAIN=${DISTFILE_SHASUM_STDOUT% *}
DISTFILE_FILECOUNT=$(tar -tf $TRITON_DIST_FILE | wc -l | tr -d ' ')
echo -e "${TRITON_DIST_FILE}\t${DISTFILE_SHASUM_PLAIN}\t${DISTFILE_FILECOUNT}\t${TRITON_DIST_FILE%.*}\t\"Triton guesttools\"\ton" >> ${ISO_LAYOUT}/usr/freebsd-dist/MANIFEST
cp $TRITON_DIST_FILE ${ISO_LAYOUT}/usr/freebsd-dist/${TRITON_DIST_FILE}
echo "==> Copying $INSTALLER_CFG to $ISO_LAYOUT"
cp $INSTALLER_CFG $ISO_LAYOUT/etc/
echo "==> Enable serial and internal consoles"
echo "-Dhv" > $ISO_LAYOUT/boot.config
echo "cuau0 \"/usr/libexec/getty std.38400\" xterm on secure" >> $ISO_LAYOUT/etc/ttys
echo "==> Setting autoboot delay to 5 seconds"
echo "autoboot_delay=\"5\"" >> $ISO_LAYOUT/boot/loader.conf
echo "==> Get network up automatically in the blank VM"
echo "ifconfig_vtnet0=\"DHCP\"" >> $ISO_LAYOUT/etc/rc.conf
echo "ifconfig_vtnet1=\"DHCP\"" >> $ISO_LAYOUT/etc/rc.conf
echo "==> Remove resolv.conf symlink"
rm $ISO_LAYOUT/etc/resolv.conf
echo "==> Set resolvers in the blank VM"
echo "nameserver 8.8.8.8" >> $ISO_LAYOUT/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $ISO_LAYOUT/etc/resolv.conf
echo "==> Unmounting $ISO_MOUNT"
umount $ISO_MOUNT
}
create_newiso() {
echo "==> Getting Volume ID for ${ISO}"
CUSTOM_ISO_TITLE=$(isoinfo -d -i ${ISO_DIR}/${ISO} | grep "Volume id" | awk '{print $3}')
echo "==> Volume ID is $CUSTOM_ISO_TITLE"
echo "==> Preparing NEW ISO"
mkisofs -J -R -no-emul-boot \
-V "$CUSTOM_ISO_TITLE" \
-p "Joyent" -b boot/cdboot \
-o $ISO_FILENAME $ISO_LAYOUT
echo "==> Custom ISO now ready: $ISO_FILENAME"
}
echo "==> ISO Build Starting!"
fetch_iso
create_layout
create_newiso