forked from ajacoutot/aws-openbsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsd-img-builder.sh
executable file
·471 lines (393 loc) · 12.2 KB
/
obsd-img-builder.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
#!/bin/ksh
#
# Copyright (c) 2015, 2016, 2019 Antoine Jacoutot <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
set -e
umask 022
create_ami() {
local _arch=${ARCH} _importsnapid _snap
! [[ ${_arch} == amd64 ]] || _arch=x86_64
pr_title "converting image to stream-based VMDK"
vmdktool -v ${IMGPATH}.vmdk ${IMGPATH}
pr_title "uploading image to S3"
aws s3api create-bucket --bucket ${_BUCKETNAME} \
--create-bucket-configuration \
LocationConstraint=$(aws configure get region)
aws s3 cp ${IMGPATH}.vmdk s3://${_BUCKETNAME}
pr_title "converting VMDK to snapshot"
cat <<-EOF >>${_WRKDIR}/containers.json
{
"Description": "${DESCR}",
"Format": "vmdk",
"UserBucket": {
"S3Bucket": "${_BUCKETNAME}",
"S3Key": "${_IMGNAME}.vmdk"
}
}
EOF
# Cannot use import-image: "ClientError: Unknown OS / Missing OS files."
#aws ec2 import-image --description "${DESCR}" --disk-containers \
# file://"${_WRKDIR}/containers.json"
_importsnapid=$(aws ec2 import-snapshot --description ${DESCR} \
--disk-container file://"${_WRKDIR}/containers.json" \
--role-name ${_IMGNAME} --query "ImportTaskId" --output text)
while true; do
set -A _snap -- $(aws ec2 describe-import-snapshot-tasks \
--output text --import-task-ids ${_importsnapid} \
--query \
"ImportSnapshotTasks[*].SnapshotTaskDetail.[Status,Progress,SnapshotId]")
echo -ne "\r Progress: ${_snap[1]}%"
[[ ${_snap[0]} == completed ]] && echo && break
sleep 10
done
pr_title "removing bucket ${_BUCKETNAME}"
aws s3 rb s3://${_BUCKETNAME} --force
pr_title "registering AMI"
aws ec2 register-image --name "${_IMGNAME}" --architecture ${_arch} \
--root-device-name /dev/sda1 --virtualization-type hvm \
--description "${DESCR}" --block-device-mappings \
DeviceName="/dev/sda1",Ebs={SnapshotId=${_snap[2]}}
}
create_autoinstallconf()
{
local _autoinstallconf=${_WRKDIR}/auto_install.conf
local _mirror=${MIRROR}
_mirror=${_mirror#*://}
_mirror=${_mirror%%/*}
pr_title "creating auto_install.conf"
cat <<-EOF >>${_autoinstallconf}
System hostname = openbsd
Password for root = *************
Change the default console to com0 = yes
Setup a user = ec2-user
Full name for user ec2-user = EC2 Default User
Password for user = *************
What timezone are you in = UTC
Location of sets = http
HTTP Server = ${_mirror}
Unable to connect using https = yes
Server directory = pub/OpenBSD/${RELEASE}/${ARCH}
Set name(s) = done
EOF
# XXX if checksum fails
for i in $(jot 11); do
echo "Checksum test for = yes" >>${_autoinstallconf}
done
echo "Continue without verification = yes" >>${_autoinstallconf}
cat <<-EOF >>${_autoinstallconf}
Location of sets = disk
Is the disk partition already mounted = no
Which disk contains the install media = sd1
Which sd1 partition has the install sets = a
INSTALL.${ARCH} not found. Use sets found here anyway = yes
Set name(s) = site*
Checksum test for = yes
Continue without verification = yes
EOF
}
create_iam_role()
{
pr_title "creating IAM role"
cat <<-'EOF' >>${_WRKDIR}/trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "vmie.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals":{
"sts:Externalid": "vmimport"
}
}
}
]
}
EOF
cat <<-EOF >>${_WRKDIR}/role-policy.json
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket"
],
"Resource":[
"arn:aws:s3:::${_BUCKETNAME}",
"arn:aws:s3:::${_BUCKETNAME}/*"
]
},
{
"Effect":"Allow",
"Action":[
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
"Resource":"*"
}
]
}
EOF
aws iam create-role --role-name ${_IMGNAME} \
--assume-role-policy-document \
"file://${_WRKDIR}/trust-policy.json"
aws iam put-role-policy --role-name ${_IMGNAME} --policy-name \
${_IMGNAME} --policy-document \
"file://${_WRKDIR}/role-policy.json"
}
create_img()
{
local _bsdrd=${_WRKDIR}/bsd.rd _rdextract=${_WRKDIR}/bsd.rd.extract
local _rdmnt=${_WRKDIR}/rdmnt _vndev
create_install_site_disk
create_autoinstallconf
pr_title "creating modified bsd.rd for autoinstall"
ftp -MV -o ${_bsdrd} ${MIRROR}/${RELEASE}/${ARCH}/bsd.rd
rdsetroot -x ${_bsdrd} ${_rdextract}
_vndev=$(vnconfig ${_rdextract})
install -d ${_rdmnt}
mount /dev/${_vndev}a ${_rdmnt}
cp ${_WRKDIR}/auto_install.conf ${_rdmnt}
umount ${_rdmnt}
vnconfig -u ${_vndev}
rdsetroot ${_bsdrd} ${_rdextract}
pr_title "starting autoinstall inside vmm(4)"
vmctl create ${IMGPATH} -s ${IMGSIZE}G
# handle cu(1) EOT
(sleep 10 && vmctl wait ${_IMGNAME} && _tty=$(get_tty ${_IMGNAME}) &&
vmctl stop ${_IMGNAME} -f && pkill -f "/usr/bin/cu -l ${_tty}")&
# XXX handle installation error
# (e.g. ftp: raw.githubusercontent.com: no address associated with name)
vmctl start ${_IMGNAME} -b ${_WRKDIR}/bsd.rd -c -L -d ${IMGPATH} -d \
${_WRKDIR}/siteXX.img
}
create_install_site()
{
# XXX bsd.mp + relink directory
pr_title "creating install.site"
cat <<-'EOF' >>${_WRKDIR}/install.site
ftp -V -o /usr/local/libexec/ec2-init \
https://raw.githubusercontent.com/ajacoutot/aws-openbsd/master/ec2-init.sh
chown root:bin /usr/local/libexec/ec2-init
chmod 0555 /usr/local/libexec/ec2-init
echo "!/usr/local/libexec/ec2-init" >>/etc/hostname.vio0
cp -p /etc/hostname.vio0 /etc/hostname.xnf0
echo "https://cdn.openbsd.org/pub/OpenBSD" >/etc/installurl
echo "sndiod_flags=NO" >/etc/rc.conf.local
echo "permit keepenv nopass ec2-user" >/etc/doas.conf
rm /install.site
EOF
chmod 0555 ${_WRKDIR}/install.site
}
create_install_site_disk()
{
# XXX trap vnd and mount
local _lrel _rel _rrel _vndev
local _siteimg=${_WRKDIR}/siteXX.img _sitemnt=${_WRKDIR}/siteXX
[[ ${RELEASE} == snapshots ]] && _rel=$(uname -r) || _rel=${RELEASE}
create_install_site
pr_title "creating sd1 and storing siteXX.tgz"
vmctl create ${_siteimg} -s 1G
_vndev="$(vnconfig ${_siteimg})"
fdisk -iy ${_vndev}
echo "a a\n\n\n\nw\nq\n" | disklabel -E ${_vndev}
newfs ${_vndev}a
install -d ${_sitemnt}
mount /dev/${_vndev}a ${_sitemnt}
install -d ${_sitemnt}/${_rel}/${ARCH}
# in case we're running an X.Y snapshot while X.Z is out;
# (e.g. running on 6.4-current and installing 6.5-beta)
_lrel=${_rel%.*}
_rrel=${_rel#*.}
if [[ ${_rrel} == 9 ]]; then
# e.g. ln 5.9 6.0
(cd ${_sitemnt} && ln -s ${_rel} $((_lrel+1)).0)
else
# e.g. ln 5.8 5.9
(cd ${_sitemnt} && ln -s ${_rel} ${_lrel}.$((_rrel+1)))
fi
cd ${_WRKDIR} && tar czf \
${_sitemnt}/${_rel}/${ARCH}/site${_rel%.[0-9]}${_rel#[0-9].}.tgz \
./install.site
umount ${_sitemnt}
vnconfig -u ${_vndev}
}
get_tty()
{
local _tty _vmname=$1
[[ -n ${_vmname} ]]
vmctl status | grep "${_vmname}" | while read -r _ _ _ _ _ _tty _ _; do
echo /dev/${_tty}
done
}
pr_err()
{
echo "${0##*/}: ${1}" 1>&2 && return ${2:-1}
}
pr_title()
{
local _line=$(printf "%80s" | tr ' ' '=')
echo "${_line}\n| ${@}\n${_line}"
}
setup_forwarding()
{
${NETCONF} || return 0
if [[ $(sysctl -n net.inet.ip.forwarding) != 1 ]]; then
pr_title "enabling paquet forwarding"
_RESET_FWD=true
sysctl -q net.inet.ip.forwarding=1
fi
}
setup_pf()
{
${NETCONF} || return 0
local _pfrules
pr_title "setting up pf(4) rules"
if ! $(pfctl -e >/dev/null); then
_RESET_PF=true
fi
print -- "pass out on egress from 100.64.0.0/10 to any nat-to (egress)
pass in proto { tcp, udp } from 100.64.0.0/10 to any port domain rdr-to 1.1.1.1" |
pfctl -f -
}
setup_vmd()
{
if ! $(rcctl check vmd >/dev/null); then
pr_title "starting vmd(8)"
rcctl start vmd
_RESET_VMD=true
fi
}
trap_handler()
{
set +e # we're trapped
if aws iam get-role --role-name ${_IMGNAME} >/dev/null 2>&1; then
pr_title "removing IAM role"
aws iam delete-role-policy --role-name ${_IMGNAME} \
--policy-name ${_IMGNAME} 2>/dev/null
aws iam delete-role --role-name ${_IMGNAME} 2>/dev/null
fi
if ${_RESET_FWD:-false}; then
pr_title "disabling paquet forwarding"
sysctl -q net.inet.ip.forwarding=0
fi
if ${_RESET_PF:-false}; then
pr_title "disabling pf(4)"
pfctl -d >/dev/null
pfctl -F rules >/dev/null
elif ${NETCONF}; then
pr_title "restoring pf(4) rules"
pfctl -f /etc/pf.conf
fi
if ${_RESET_VMD:-false}; then
pr_title "stopping vmd(8)"
rcctl stop vmd >/dev/null
fi
if [[ -n ${_WRKDIR} ]]; then
rmdir ${_WRKDIR} 2>/dev/null ||
pr_title "work directory: ${_WRKDIR}"
fi
}
usage()
{
echo "usage: ${0##*/}
-a \"architecture\" -- default to \"amd64\"
-c -- autoconfigure pf(4) and enable IP forwarding
-d \"description\" -- AMI description; defaults to \"openbsd-\$release-\$timestamp\"
-i \"path to RAW image\" -- use image at path instead of creating one
-m \"install mirror\" -- defaults to installurl(5) or \"https://cdn.openbsd.org/pub/OpenBSD\"
-n -- only create a RAW image (don't convert to an AMI nor push to AWS)
-r \"release\" -- e.g \"6.5\"; default to \"snapshots\"
-s \"image size in GB\" -- default to \"10\""
return 1
}
while getopts a:cd:i:m:nr:s: arg; do
case ${arg} in
a) ARCH="${OPTARG}" ;;
c) NETCONF=true ;;
d) DESCR="${OPTARG}" ;;
i) IMGPATH="${OPTARG}" ;;
m) MIRROR="${OPTARG}" ;;
n) CREATE_AMI=false ;;
r) RELEASE="${OPTARG}" ;;
s) IMGSIZE="${OPTARG}" ;;
*) usage ;;
esac
done
trap 'trap_handler' EXIT
trap exit HUP INT TERM
_TS=$(date -u +%G%m%dT%H%M%SZ)
_WRKDIR=$(mktemp -d -p ${TMPDIR:=/tmp} aws-ami.XXXXXXXXXX)
# XXX add support for installation proxy in create_img
if [[ -n ${http_proxy} ]]; then
export HTTP_PROXY=${http_proxy}
export HTTPS_PROXY=${http_proxy}
fi
ARCH=${ARCH:-amd64}
CREATE_AMI=${CREATE_AMI:-true}
IMGSIZE=${IMGSIZE:-10}
NETCONF=${NETCONF:-false}
RELEASE=${RELEASE:-snapshots}
if [[ -z ${MIRROR} ]]; then
MIRROR=$(while read _line; do _line=${_line%%#*}; [[ -n ${_line} ]] &&
print -r -- "${_line}"; done </etc/installurl | tail -1) \
2>/dev/null
[[ ${MIRROR} == @(http|https)://* ]] ||
MIRROR="https://cdn.openbsd.org/pub/OpenBSD"
fi
_IMGNAME=openbsd-${RELEASE}-${ARCH}-${_TS}
[[ ${RELEASE} == snapshots ]] &&
_IMGNAME=${_IMGNAME%snapshots*}current${_IMGNAME#*snapshots}
[[ -n ${IMGPATH} ]] && _IMGNAME=${IMGPATH##*/} ||
IMGPATH=${_WRKDIR}/${_IMGNAME}
_BUCKETNAME=$(echo ${_IMGNAME} | tr '[:upper:]' '[:lower:]')-${RANDOM}
DESCR=${DESCR:-${_IMGNAME}}
readonly _BUCKETNAME _IMGNAME _TS _WRKDIR HTTP_PROXY HTTPS_PROXY
readonly CREATE_AMI DESCR IMGPATH IMGSIZE MIRROR NETCONF RELEASE
# requirements checks to build the RAW image
if [[ ! -f ${IMGPATH} ]]; then
(($(id -u) != 0)) && pr_err "need root privileges"
grep -q ^vmm0 /var/run/dmesg.boot || pr_err "need vmm(4) support"
[[ ${_IMGNAME}} != [[:alpha:]]* ]] &&
pr_err "image name must start with a letter"
fi
# requirements checks to build and register the AMI
if ${CREATE_AMI}; then
[[ ${ARCH} == i386 ]] &&
pr_err "${ARCH} lacks xen(4) support to run on AWS"
type aws >/dev/null 2>&1 || pr_err "package \"awscli\" is not installed"
type vmdktool >/dev/null 2>&1 ||
pr_err "package \"vmdktool\" is not installed"
aws ec2 describe-regions --region-names us-east-1 >/dev/null ||
pr_err "you may need to export:
AWS_CONFIG_FILE
AWS_DEFAULT_PROFILE
AWS_SHARED_CREDENTIALS_FILE"
fi
if [[ ! -f ${IMGPATH} ]]; then
setup_vmd
setup_pf
setup_forwarding
create_img
fi
if ${CREATE_AMI}; then
create_iam_role
create_ami
fi