-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·357 lines (290 loc) · 10.7 KB
/
build.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
#!/bin/bash
set -uo pipefail
IGTOP=$(readlink -f "$(dirname "$0")")
source "${IGTOP}/scripts/common"
source "${IGTOP}/scripts/dependencies_check"
dependencies_check "${IGTOP}/depends" || exit 1
usage()
{
cat <<-EOF >&2
Usage
$(basename "$0") [options]
Root filesystem and image generation utility.
Options:
[-c <config>] Name of config file, location defaults to config/
[-D <directory>] Directory that takes precedence over the default in-tree
hierarchy when searching for config files, profiles, meta
layers and image layouts.
[-N <namespace>] Optional namespace to specify an additional sub-directory
hierarchy within the directory provided by -D of where to
search for meta layers.
[-o <file>] Path to shell-style fragment specifying variables as
key=value. These variables can override the defaults, those
set by the config file, or provide completely new variables
available to both rootfs and image generation stages.
Developer Options
[-r] Establish configuration, build rootfs, exit after post-build.
[-i] Establish configuration, skip rootfs, run hooks, generate image.
EOF
}
# Arg parser and defaults
EXT_DIR=
EXT_META=
EXT_NS=
EXT_NSDIR=
EXT_NSMETA=
INOPTIONS=
INCONFIG=generic64-apt-simple
ONLY_ROOTFS=0
ONLY_IMAGE=0
while getopts "c:D:hiN:o:r" flag ; do
case "$flag" in
c)
INCONFIG="$OPTARG"
;;
h)
usage ; exit 0
;;
D)
EXT_DIR=$(realpath -m "$OPTARG")
[[ -d $EXT_DIR ]] || { usage ; die "Invalid external directory: $EXT_DIR" ; }
;;
i)
ONLY_IMAGE=1
;;
N)
EXT_NS="$OPTARG"
;;
o)
INOPTIONS=$(realpath -m "$OPTARG")
[[ -s $INOPTIONS ]] || { usage ; die "Invalid options file: $INOPTIONS" ; }
;;
r)
ONLY_ROOTFS=1
;;
?|*)
usage ; exit 1
;;
esac
done
[[ -d $EXT_DIR ]] && EXT_META=$(realpath -e "${EXT_DIR}/meta" 2>/dev/null)
[[ -n $EXT_NS && ! -d $EXT_DIR ]] && die "External namespace supplied without external dir"
if [[ -d $EXT_DIR && -n $EXT_NS ]] ; then
EXT_NSDIR=$(realpath -e "${EXT_DIR}/$EXT_NS" 2>/dev/null)
[[ -d $EXT_NSDIR ]] || die "External namespace dir $EXT_NS does not exist in $EXT_DIR"
EXT_NSMETA=$(realpath -e "${EXT_DIR}/$EXT_NS/meta" 2>/dev/null)
fi
# Constants
IGTOP_CONFIG="${IGTOP}/config"
IGTOP_BOARD="${IGTOP}/board"
IGTOP_IMAGE="${IGTOP}/image"
IGTOP_PROFILE="${IGTOP}/profile"
IGTOP_SBOM="${IGTOP}/sbom"
META="${IGTOP}/meta"
META_HOOKS="${IGTOP}/meta-hooks"
RPI_TEMPLATES="${IGTOP}/templates/rpi"
# Establish the top level directory hierarchy by first reading the config
if [[ -d "${EXT_DIR}/config" && -s "${EXT_DIR}/config/${INCONFIG}.cfg" ]] ; then
IGTOP_CONFIG="${EXT_DIR}/config"
elif [[ -s "${IGTOP}/config/${INCONFIG}.cfg" ]] ; then
IGTOP_CONFIG="${IGTOP}/config"
else
die "config "$INCONFIG" not found or invalid"
fi
msg "Reading $INCONFIG from $IGTOP_CONFIG with options [$INOPTIONS]"
[[ -d $EXT_META ]] && msg "External meta at $EXT_META"
[[ -d $EXT_NSMETA ]] && msg "External [$EXT_NS] meta at $EXT_NSMETA"
# Defaults
IGconf_target_board=pi5
IGconf_image_version=$(date +%Y-%m-%d)
IGconf_image_name="${IGconf_target_board}-$(echo "${INCONFIG}"|sed -s 's|\/|\-|g')-${IGconf_image_version}"
IGconf_image_suffix=img
IGconf_image_compression=none
unset IGconf_apt_proxy
IGconf_target_hostname=raspberrypi
IGconf_first_user_name=pi
unset IGconf_first_user_pass
IGconf_locale_default="en_GB.UTF-8"
IGconf_keyboard_keymap=gb
IGconf_keyboard_layout="English (UK)"
IGconf_timezone_default="Europe/London"
unset IGconf_ext_dir
unset IGconf_ext_nsdir
unset IGconf_apt_keydir
IGconf_sbom_output_format="spdx-json"
# Provide external directory paths
[[ -d $EXT_DIR ]] && IGconf_ext_dir="$EXT_DIR"
[[ -d $EXT_NSDIR ]] && IGconf_ext_nsdir="$EXT_NSDIR"
read_config "${IGTOP_CONFIG}/${INCONFIG}.cfg"
# Config must provide
[[ -z ${IGconf_image_layout+x} ]] && die "Config has no image layout"
[[ -z ${IGconf_system_profile+x} ]] && die "Config has no system profile"
# Internalise hierarchy paths, prioritising the external sub-directory tree
[[ -d $EXT_DIR ]] && IGBOARD=$(realpath -e "${EXT_DIR}/board/$IGconf_target_board" 2>/dev/null)
: ${IGBOARD:=${IGTOP_BOARD}/$IGconf_target_board}
[[ -d $EXT_DIR ]] && IGIMAGE=$(realpath -e "${EXT_DIR}/image/$IGconf_image_layout" 2>/dev/null)
: ${IGIMAGE:=${IGTOP_IMAGE}/$IGconf_image_layout}
[[ -d $EXT_DIR ]] && IGPROFILE=$(realpath -e "${EXT_DIR}/profile/$IGconf_system_profile" 2>/dev/null)
: ${IGPROFILE:=${IGTOP_PROFILE}/$IGconf_system_profile}
# Final path validation
for i in IGBOARD IGIMAGE IGPROFILE ; do
msg "$i ${!i}"
realpath -e ${!i} > /dev/null 2>&1 || die "$i is invalid"
done
# Load options
[[ -s "$INOPTIONS" ]] && read_options "$INOPTIONS"
# Remaining defaults
: "${IGconf_work_dir:=${IGTOP}/work/${IGconf_image_name}}"
: "${IGconf_image_outputdir:=${IGconf_work_dir}/artefacts}"
: "${IGconf_image_deploydir:=${IGconf_work_dir}/deploy}"
# Assemble keys
if [[ -z ${IGconf_apt_keydir+z} ]] ; then
IGconf_apt_keydir="${IGconf_work_dir}/keys"
mkdir -p "$IGconf_apt_keydir"
[[ -d /usr/share/keyrings ]] && rsync -a /usr/share/keyrings/ $IGconf_apt_keydir
[[ -d "$USER/.local/share/keyrings" ]] && rsync -a "$USER/.local/share/keyrings/" $IGconf_apt_keydir
rsync -a "$IGTOP/keydir/" $IGconf_apt_keydir
fi
[[ -d $IGconf_apt_keydir ]] || die "apt keydir $IGconf_apt_keydir is invalid"
# Assemble environment for rootfs and image creation, propagating IG variables
# to rootfs and post-build stages as appropriate.
ENV_ROOTFS=()
ENV_POST_BUILD=()
for v in $(compgen -A variable -X '!IGconf*') ; do
case $v in
IGconf_timezone_default)
ENV_ROOTFS+=('--env' IGconf_timezone_area="${!v%%/*}")
ENV_ROOTFS+=('--env' IGconf_timezone_city="${!v##*/}")
ENV_POST_BUILD+=(IGconf_timezone_area="${!v%%/*}")
ENV_POST_BUILD+=(IGconf_timezone_city="${!v##*/}")
;;
IGconf_apt_proxy)
IGconf_apt_proxy_http="${!v}"
;&
IGconf_apt_proxy_http)
err=$(curl --head --silent --write-out "%{http_code}" --output /dev/null "${!v}")
[[ $? -ne 0 ]] && die "unreachable proxy : ${!v}"
msg "$err ${!v}"
ENV_ROOTFS+=('--aptopt' "Acquire::http { Proxy \"${!v}\"; }")
ENV_ROOTFS+=('--env' IGconf_apt_proxy_http="${!v}")
;;
IGconf_apt_keydir)
ENV_ROOTFS+=('--aptopt' "Dir::Etc::TrustedParts ${!v}")
ENV_ROOTFS+=('--env' IGconf_apt_keydir="${!v}")
;;
IGconf_ext_dir|IGconf_ext_nsdir )
ENV_ROOTFS+=('--env' ${v}="${!v}")
ENV_POST_BUILD+=(${v}="${!v}")
if [ -d "${!v}/bin" ] ; then
PATH="${!v}/bin:${PATH}"
ENV_ROOTFS+=('--env' PATH="$PATH")
ENV_POST_BUILD+=(PATH="${PATH}")
fi
;;
*)
ENV_ROOTFS+=('--env' ${v}="${!v}")
ENV_POST_BUILD+=(${v}="${!v}")
;;
esac
done
ENV_ROOTFS+=('--env' IGTOP=$IGTOP)
ENV_ROOTFS+=('--env' META_HOOKS=$META_HOOKS)
ENV_ROOTFS+=('--env' RPI_TEMPLATES=$RPI_TEMPLATES)
for i in IGBOARD IGIMAGE IGPROFILE ; do
ENV_ROOTFS+=('--env' ${i}="${!i}")
ENV_POST_BUILD+=(${i}="${!i}")
done
# Final PATH setup
ENV_ROOTFS+=('--env' PATH="${IGTOP}/bin:$PATH")
mkdir -p ${IGconf_work_dir}/host/bin
ENV_POST_BUILD+=(PATH="${IGTOP}/bin:${IGconf_work_dir}/host/bin:${PATH}")
# Assemble meta layers from profile
ARGS_LAYERS=()
while read -r line; do
[[ "$line" =~ ^#.*$ ]] && continue
[[ "$line" =~ ^$ ]] && continue
if [[ -n $EXT_NSMETA && -s ${EXT_NSMETA}/$line.yaml ]] ; then
ARGS_LAYERS+=('--config' "${EXT_NSMETA}/$line.yaml")
elif [[ -n $EXT_META && -s ${EXT_META}/$line.yaml ]] ; then
ARGS_LAYERS+=('--config' "${EXT_META}/$line.yaml")
elif [[ -s ${META}/$line.yaml ]] ; then
ARGS_LAYERS+=('--config' "${META}/$line.yaml")
else
die "Invalid meta specifier: $line (not found)"
fi
done < "${IGPROFILE}"
# Generate rootfs
[[ $ONLY_IMAGE = 1 ]] && true || rund "$IGTOP" podman unshare bdebstrap \
"${ARGS_LAYERS[@]}" \
"${ENV_ROOTFS[@]}" \
--force \
--name "$IGconf_image_name" \
--hostname "$IGconf_target_hostname" \
--output "$IGconf_image_outputdir" \
--target "${IGconf_work_dir}/rootfs" \
--setup-hook 'bin/runner setup "${IGconf_work_dir}/rootfs"' \
--essential-hook 'bin/runner essential "${IGconf_work_dir}/rootfs"' \
--customize-hook 'bin/runner customize "${IGconf_work_dir}/rootfs"' \
--cleanup-hook 'bin/runner cleanup "${IGconf_work_dir}/rootfs"'
# hook execution
runh()
{
local hookdir=$(dirname "$1")
local hook=$(basename "$1")
shift 1
msg "$hookdir"["$hook"] "$@"
env -C $hookdir "${ENV_POST_BUILD[@]}" ./"$hook" "$@"
ret=$?
if [[ $ret -ne 0 ]]
then
die "Hook Error: ["$hookdir"/"$hook"] ($ret)"
fi
}
# post-build: apply rootfs overlays - image layout then board
if [ -d ${IGIMAGE}/rootfs-overlay ] ; then
run rsync -a ${IGIMAGE}/rootfs-overlay/ ${IGconf_work_dir}/rootfs
fi
if [ -d ${IGBOARD}/rootfs-overlay ] ; then
run rsync -a ${IGBOARD}/rootfs-overlay/ ${IGconf_work_dir}/rootfs
fi
# post-build: hooks - image layout then board
if [ -x ${IGIMAGE}/post-build.sh ] ; then
runh ${IGIMAGE}/post-build.sh ${IGconf_work_dir}/rootfs
fi
if [ -x ${IGBOARD}/post-build.sh ] ; then
runh ${IGBOARD}/post-build.sh ${IGconf_work_dir}/rootfs
fi
[[ $ONLY_ROOTFS = 1 ]] && exit $?
# pre-image: hooks - board has priority over image layout
if [ -x ${IGBOARD}/pre-image.sh ] ; then
runh ${IGBOARD}/pre-image.sh ${IGconf_work_dir}/rootfs ${IGconf_image_outputdir}
elif [ -x ${IGIMAGE}/pre-image.sh ] ; then
runh ${IGIMAGE}/pre-image.sh ${IGconf_work_dir}/rootfs ${IGconf_image_outputdir}
else
die "no pre-image hook"
fi
# SBOM
if [ -x ${IGTOP_SBOM}/gen.sh ] ; then
runh ${IGTOP_SBOM}/gen.sh ${IGconf_work_dir}/rootfs ${IGconf_image_outputdir}
fi
GTMP=$(mktemp -d)
trap 'rm -rf $GTMP' EXIT
mkdir -p "$IGconf_image_deploydir"
# Generate image(s)
for f in "${IGconf_image_outputdir}"/genimage*.cfg; do
run podman unshare env "${ENV_POST_BUILD[@]}" genimage \
--rootpath ${IGconf_work_dir}/rootfs \
--tmppath $GTMP \
--inputpath ${IGconf_image_outputdir} \
--outputpath ${IGconf_image_outputdir} \
--loglevel=1 \
--config $f | pv -t -F 'Generating image...%t' || die "genimage error"
done
# post-image: hooks - board has priority over image layout
if [ -x ${IGBOARD}/post-image.sh ] ; then
runh ${IGBOARD}/post-image.sh $IGconf_image_deploydir
elif [ -x ${IGIMAGE}/post-image.sh ] ; then
runh ${IGIMAGE}/post-image.sh $IGconf_image_deploydir
else
runh ${IGTOP_IMAGE}/post-image.sh $IGconf_image_deploydir
fi