forked from TritonDataCenter/openbsd-kvm-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-iso
executable file
·307 lines (248 loc) · 7.14 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
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
#!/usr/bin/env bash
#
# Copyright (c) 2017 Joyent Inc., All rights reserved.
#
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'
AUTO_INST="auto_install.conf"
RAMDISK=/mnt/ramdisk
RDSETROOT=/usr/local/bin/rdsetroot
TRITON_DIST_FILE="site61.tgz"
RELEASE=
MIRROR=
MIRROR_PATH=
ISO=
ISO_DIR=
ISO_MOUNT=
ISO_LAYOUT=
ISO_CHECKSUM=
ISO_FILENAME=
usage() {
cat <<EOF
Create a custom OpenBSD 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 6.1 -m ftp5.usa.openbsd.org -p /pub/OpenBSD -i install61.iso -c SHA256 -d /data/openbsd-6.1-iso -M /mnt/openbsd-6.1-iso -l /data/openbsd-custom-6.1 -f openbsd-6.1-custom.iso
OPTIONS:
-r The desired OpenBSD release (e.g., 6.1)
-m The mirror to use when downloading the release ISO. Do not include protocol (e.g., http://). Assumes http mirror
-p The desired mirror path to the ISO (e.g., /pub/OpenBSD)
-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 [[ "$#" -eq 0 ]]; then
usage
exit 1
fi
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 interrupted
trap 'rm -rf ${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} https://${MIRROR}${MIRROR_PATH}/${RELEASE}/amd64/${ISO_CHECKSUM}
curl -sS -o ${ISO_DIR}/${ISO_CHECKSUM}.sig https://${MIRROR}${MIRROR_PATH}/${RELEASE}/amd64/${ISO_CHECKSUM}.sig
echo "==> Checking for local copy of $ISO..."
if [[ -e $ISO_DIR/$ISO ]]; then
echo "==> Found local copy of $ISO"
echo "==> Verifying $ISO with ${ISO_CHECKSUM}"
(cd $ISO_DIR && signify -C -p /etc/signify/openbsd-61-base.pub -x ${ISO_CHECKSUM}.sig ${ISO})
if [[ $? != 0 ]]; then
exit 1
fi
else
echo "==> Local copy not found."
echo "==> Fetching ISO from $MIRROR..."
curl -sS -o ${ISO_DIR}/${ISO} https://${MIRROR}${MIRROR_PATH}/${RELEASE}/amd64/${ISO}
echo "==> Done!"
echo "==> ${ISO} saved to $ISO_DIR/"
echo "==> Verifying $ISO with ${ISO_CHECKSUM}"
(cd $ISO_DIR && signify -C -p /etc/signify/openbsd-61-base.pub -x ${ISO_CHECKSUM}.sig ${ISO})
if [[ $? != 0 ]]; then
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 -p $ISO_MOUNT
fi
if df | grep -q $ISO_MOUNT; then
echo "==> Unmounting previous $ISO_MOUNT..."
umount $ISO_MOUNT
vnconfig -u vnd0
fi
echo "==> Mounting $ISO to $ISO_MOUNT"
vnconfig vnd0 $ISO_DIR/$ISO
mount -t cd9660 /dev/vnd0c $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-openbsd-guesttools
tar -czpf ../${TRITON_DIST_FILE} ./etc ./lib ./usr
)
cp $TRITON_DIST_FILE ${ISO_LAYOUT}/${RELEASE}/amd64/${TRITON_DIST_FILE}
echo "==> Generating shasum for $TRITON_DIST_FILE"
SITE_SHASUM=$(cksum -a sha256 $TRITON_DIST_FILE | cut -f1 -d' ')
echo "$SITE_SHASUM" >> $ISO_LAYOUT/$RELEASE/amd64/SHA256
echo "==> Copying $AUTO_INST to ramdisk"
if df | grep -q $RAMDISK; then
echo "===> Unmounting previous $RAMDISK..."
umount $RAMDISK
vnconfig -u vnd1
fi
if [[ ! -e $RDSETROOT ]]; then
if [[ ! -d /usr/src/distrib/common ]]; then
(
echo "===> Downloading src.tar.gz from $MIRROR..."
curl -sS -o /tmp/src.tar.gz https://${MIRROR}${MIRROR_PATH}/${RELEASE}/src.tar.gz
echo "===> Installing source tree..."
cd /usr/src
tar xzf /tmp/src.tar.gz
rm -rf /tmp/src.tar.gz
)
fi
(
echo "===> Building $RDSETROOT"
cd /usr/src/distrib/common
cc -o $RDSETROOT elf32.c elf64.c elfrdsetroot.c
)
fi
echo "===> Creating ramdisk.img"
$RDSETROOT -x ${ISO_LAYOUT}/${RELEASE}/amd64/bsd.rd ramdisk.img
if [[ ! -d $RAMDISK ]]; then
echo "===> Creating $RAMDISK ..."
mkdir -p $RAMDISK
fi
echo "===> Mounting ramdisk to $RAMDISK"
vnconfig vnd1 ramdisk.img
mount /dev/vnd1a $RAMDISK
echo "===> Copying $AUTO_INST to $RAMDISK"
cp $AUTO_INST $RAMDISK
echo "===> Unmounting $RAMDISK"
umount $RAMDISK
vnconfig -u vnd1
echo "===> Putting ramdisk.img back to bsd.rd"
$RDSETROOT ${ISO_LAYOUT}/${RELEASE}/amd64/bsd.rd ramdisk.img
echo "==> Cleaing up ramdisk.img"
rm -rf ramdisk.img
echo "==> Unmounting $ISO_MOUNT"
umount $ISO_MOUNT
vnconfig -u vnd0
}
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 $RELEASE/amd64/cdboot \
-o $ISO_FILENAME $ISO_LAYOUT
echo "==> Custom ISO now ready: $ISO_FILENAME"
}
echo "==> ISO Build Starting!"
fetch_iso
create_layout
create_newiso