aboutsummaryrefslogtreecommitdiffstats
path: root/build.sh
blob: 57564396acad59adc9ff2cbffebac633ba215035 (plain)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/bin/bash -e

export DEBIAN_FRONTEND=noninteractive

# shellcheck disable=SC2119
run_sub_stage()
{
    local APT_GET_INSTALL_OPTS='-o APT::Acquire::Retries=3 -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"'

    local packfile;
    local PACKAGES;
	log "Begin ${SUB_STAGE_DIR}"
	pushd "${SUB_STAGE_DIR}" > /dev/null
	for i in {00..99}; do
		if [ -f "${i}-debconf" ]; then
			log "Begin ${SUB_STAGE_DIR}/${i}-debconf"
			on_chroot << EOF
debconf-set-selections <<SELEOF
$(cat "${i}-debconf")
SELEOF
EOF

			log "End ${SUB_STAGE_DIR}/${i}-debconf"
		fi
		if [ "${TARGET_RASPI}" = "1" -a \( -f "${i}-packages-sys-raspi" -o -f "${i}-packages-sys-raspi-${RELEASE}" \) ]; then
            if [ -f "${i}-packages-sys-raspi-${RELEASE}" ]; then
                packfile="${i}-packages-sys-raspi-${RELEASE}"
            else
                packfile="${i}-packages-sys-raspi"
            fi
			log "Begin ${SUB_STAGE_DIR}/${packfile}"
			PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${packfile}")"
			if [ -n "$PACKAGES" ]; then
				on_chroot << EOF
echo "Installing ${packfile} '${PACKAGES}'"
apt-get ${APT_GET_INSTALL_OPTS} install --no-install-recommends -y $PACKAGES
EOF
				if [ "${USE_QCOW2}" = "1" ]; then
					on_chroot << EOF
apt-get clean
EOF
				fi
			fi
			log "End ${SUB_STAGE_DIR}/${packfile}"
		elif [ "${TARGET_RASPI}" != "1" -a \( -f "${i}-packages-sys-debian" -o -f "${i}-packages-sys-debian-${RELEASE}" \) ]; then
            if [ -f "${i}-packages-sys-debian-${RELEASE}" ]; then
                packfile="${i}-packages-sys-debian-${RELEASE}"
            else
                packfile="${i}-packages-sys-debian"
            fi
			log "Begin ${SUB_STAGE_DIR}/${packfile}"
			PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${packfile}")"
			if [ -n "$PACKAGES" ]; then
				on_chroot << EOF
echo "Installing ${packfile} '${PACKAGES}'"
apt-get ${APT_GET_INSTALL_OPTS} install --no-install-recommends -y $PACKAGES
EOF
				if [ "${USE_QCOW2}" = "1" ]; then
					on_chroot << EOF
apt-get clean
EOF
				fi
			fi
			log "End ${SUB_STAGE_DIR}/${packfile}"
		fi
		if [ -f "${i}-packages-nr" -o -f "${i}-packages-nr-${RELEASE}" ]; then
            if [ -f "${i}-packages-nr-${RELEASE}" ]; then
                packfile="${i}-packages-nr-${RELEASE}"
            else
                packfile="${i}-packages-nr"
            fi
			log "Begin ${SUB_STAGE_DIR}/${packfile}"
			PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${packfile}")"
			if [ -n "$PACKAGES" ]; then
				on_chroot << EOF
echo "Installing ${packfile} '${PACKAGES}'"
apt-get ${APT_GET_INSTALL_OPTS} install --no-install-recommends -y $PACKAGES
EOF
				if [ "${USE_QCOW2}" = "1" ]; then
					on_chroot << EOF
apt-get clean
EOF
				fi
			fi
			log "End ${SUB_STAGE_DIR}/${packfile}"
		fi
		if [ -f "${i}-packages" -o -f "${i}-packages-${RELEASE}" ]; then
            if [ -f "${i}-packages-${RELEASE}" ]; then
                packfile="${i}-packages-${RELEASE}"
            else
                packfile="${i}-packages"
            fi
			log "Begin ${SUB_STAGE_DIR}/${packfile}"
			PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${packfile}")"
			if [ -n "$PACKAGES" ]; then
				on_chroot << EOF
echo "Installing ${packfile} '${PACKAGES}'"
if [ "${INSTALL_RECOMMENDS}" != "1" -o "${REDUCED_FOOTPRINT}" = "1" ]; then
    apt-get ${APT_GET_INSTALL_OPTS} install --no-install-recommends -y $PACKAGES
else
    # "${INSTALL_RECOMMENDS}" = "1" -a "${REDUCED_FOOTPRINT}" != "1"
    apt-get ${APT_GET_INSTALL_OPTS} install -y $PACKAGES
fi
EOF
				if [ "${USE_QCOW2}" = "1" ]; then
					on_chroot << EOF
apt-get clean
EOF
				fi
			fi
			log "End ${SUB_STAGE_DIR}/${packfile}"
		fi
		if [ -d "${i}-patches" ]; then
			log "Begin ${SUB_STAGE_DIR}/${i}-patches"
			pushd "${STAGE_WORK_DIR}" > /dev/null
			if [ "${CLEAN}" = "1" ]; then
				rm -rf .pc
				rm -rf ./*-pc
			fi
			QUILT_PATCHES="${SUB_STAGE_DIR}/${i}-patches"
			SUB_STAGE_QUILT_PATCH_DIR="$(basename "$SUB_STAGE_DIR")-pc"
			mkdir -p "$SUB_STAGE_QUILT_PATCH_DIR"
			ln -snf "$SUB_STAGE_QUILT_PATCH_DIR" .pc
			quilt upgrade
			if [ -e "${SUB_STAGE_DIR}/${i}-patches/EDIT" ]; then
				echo "Dropping into bash to edit patches..."
				bash
			fi
			RC=0
			quilt push -a || RC=$?
			case "$RC" in
				0|2)
					;;
				*)
					false
					;;
			esac
			popd > /dev/null
			log "End ${SUB_STAGE_DIR}/${i}-patches"
		fi
		if [ -x ${i}-run.sh ]; then
			log "Begin ${SUB_STAGE_DIR}/${i}-run.sh"
			./${i}-run.sh
			log "End ${SUB_STAGE_DIR}/${i}-run.sh"
		fi
		if [ -f ${i}-run-chroot.sh ]; then
			log "Begin ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
			on_chroot < ${i}-run-chroot.sh
			log "End ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
		fi
	done
	popd > /dev/null
	log "End ${SUB_STAGE_DIR}"
}


contains() { 
    if [[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]; then 
        echo -n "1"
    else 
        echo -n "0"
    fi 
}

run_stage(){
	STAGE="$(basename "${STAGE_DIR}")"
    SKIP_STAGE=`contains "${SKIP_STAGE_LIST}" ${STAGE}`
    SKIP_IMAGES=`contains "${SKIP_IMAGES_LIST}" ${STAGE}`
	log "Begin ${STAGE} ${STAGE_DIR} SKIP(STAGE ${SKIP_STAGE}, IMAGES ${SKIP_IMAGES})"

	pushd "${STAGE_DIR}" > /dev/null

	STAGE_WORK_DIR="${WORK_DIR}/${STAGE}"
	ROOTFS_DIR="${STAGE_WORK_DIR}"/rootfs

	if [ "${USE_QCOW2}" = "1" ]; then 
        if [ "${SKIP_STAGE}" != "1" ]; then
			load_qimage
		fi
	fi
	
    if [ "${SKIP_IMAGES}" != "1" ]; then
		if [ -f "${STAGE_DIR}/EXPORT_IMAGE" ]; then
			EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
		fi
	fi
    if [ "${SKIP_STAGE}" != "1" ]; then
		if [ "${CLEAN}" = "1" ] && [ "${USE_QCOW2}" = "0" ] ; then
			if [ -d "${ROOTFS_DIR}" ]; then
				rm -rf "${ROOTFS_DIR}"
			fi
		fi
		if [ -x prerun.sh ]; then
			log "Begin ${STAGE_DIR}/prerun.sh"
			./prerun.sh
			log "End ${STAGE_DIR}/prerun.sh"
		fi
		for SUB_STAGE_DIR in "${STAGE_DIR}"/*; do
			if [ -d "${SUB_STAGE_DIR}" ]; then
				run_sub_stage
			fi
		done
	fi

	if [ "${USE_QCOW2}" = "1" ]; then 
		unload_qimage
	fi

	PREV_STAGE="${STAGE}"
	PREV_STAGE_DIR="${STAGE_DIR}"
	PREV_ROOTFS_DIR="${ROOTFS_DIR}"
	popd > /dev/null
	log "End ${STAGE_DIR}"
}

if [ "$(id -u)" != "0" ]; then
	echo "Please run as root" 1>&2
	exit 1
fi

BASE_DIR=`dirname $(readlink -f "${BASH_SOURCE[0]}")`
export BASE_DIR

while getopts "c:" flag
do
	case "$flag" in
		c)
			EXTRA_CONFIG="$OPTARG"
			# shellcheck disable=SC1090
            echo "Reading config from ${EXTRA_CONFIG}"
			source "$EXTRA_CONFIG"
			;;
		*)
			;;
	esac
done

term() {
	if [ "${USE_QCOW2}" = "1" ]; then
		log "Unloading image"
		unload_qimage
	fi
}

trap term EXIT INT TERM

export PI_GEN=${PI_GEN:-pi-gen}
export PI_GEN_REPO=${PI_GEN_REPO:-https://jausoft.com/cgit/pi-gen.git/about/}

if [ -z "${IMG_NAME}" ]; then
	echo "IMG_NAME not set" 1>&2
	exit 1
fi

export USE_QEMU="${USE_QEMU:-0}"
export IMG_DATE="${IMG_DATE:-"$(date --utc +%Y-%m-%d_%H:%M:%S)"}"
export IMG_FILENAME="${IMG_FILENAME:-"${IMG_DATE}-${IMG_NAME}"}"
export ZIP_FILENAME="${ZIP_FILENAME:-"image_${IMG_DATE}-${IMG_NAME}"}"

export SCRIPT_DIR="${BASE_DIR}/scripts"
export WORK_DIR="${WORK_DIR:-"${BASE_DIR}/work/${IMG_DATE}-${IMG_NAME}"}"
export DEPLOY_DIR=${DEPLOY_DIR:-"${BASE_DIR}/deploy"}
export DEPLOY_ZIP="${DEPLOY_ZIP:-1}"
export LOG_FILE="${WORK_DIR}/build.log"

export TARGET_HOSTNAME=${TARGET_HOSTNAME:-raspberrypi}

export FIRST_USER_NAME=${FIRST_USER_NAME:-pi}
export FIRST_USER_PASS=${FIRST_USER_PASS:-raspberry}
export RELEASE=${RELEASE:-buster}

export TARGET_RASPI="${TARGET_RASPI:-1}"
export TARGET_ARCH="${TARGET_ARCH:-arm64}"

export WPA_ESSID
export WPA_PASSWORD
export WPA_COUNTRY
export ENABLE_SSH="${ENABLE_SSH:-0}"
export PUBKEY_ONLY_SSH="${PUBKEY_ONLY_SSH:-0}"

export LOCALE_DEFAULT="${LOCALE_DEFAULT:-en_US.UTF-8}"

export KEYBOARD_KEYMAP="${KEYBOARD_KEYMAP:-us}"
export KEYBOARD_LAYOUT="${KEYBOARD_LAYOUT:-English (US)}"

export TIMEZONE_DEFAULT="${TIMEZONE_DEFAULT:-Europe/Berlin}"

export GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}

export PUBKEY_SSH_FIRST_USER

export CLEAN
export IMG_NAME
export APT_PROXY

export STAGE
export STAGE_DIR
export STAGE_WORK_DIR
export PREV_STAGE
export PREV_STAGE_DIR
export ROOTFS_DIR
export PREV_ROOTFS_DIR
export IMG_SUFFIX
export NOOBS_NAME
export NOOBS_DESCRIPTION
export EXPORT_DIR
export EXPORT_ROOTFS_DIR

export QUILT_PATCHES
export QUILT_NO_DIFF_INDEX=1
export QUILT_NO_DIFF_TIMESTAMPS=1
export QUILT_REFRESH_ARGS="-p ab"

if [ -z "${DEB_SERVER}" ]; then
    export DEB_SERVER="http://deb.debian.org/debian"
fi

if [ -z "${IS_TESTING}" ]; then
    IS_TESTING=
    case "${RELEASE}" in
        "jessie")
            IS_TESTING=0 ;;
        "stretch")
            IS_TESTING=0 ;;
        "buster")
            IS_TESTING=0 ;;
        "bullseye")
            IS_TESTING=0 ;;
        "bookworm")
            IS_TESTING=0 ;;
        "trixie")
            IS_TESTING=1 ;;
        *)
            IS_TESTING=1 ;;
    esac
fi
export IS_TESTING

if [ "${PREFER_RASPI_SOURCE}" = "1" -a "${TARGET_RASPI}" = "1" -a "${TARGET_ARCH}" = "armhf" ]; then
    USE_RASPI_SOURCE=1
else
    USE_RASPI_SOURCE=0
fi
export USE_RASPI_SOURCE

# shellcheck source=scripts/common
source "${SCRIPT_DIR}/common"
# shellcheck source=scripts/dependencies_check
source "${SCRIPT_DIR}/dependencies_check"

# merely a local flag for `run_stage` for EXPORT_DIRS (images) only
export USE_QCOW2=1

export BASE_QCOW2_SIZE=${BASE_QCOW2_SIZE:-15200M}
source "${SCRIPT_DIR}/qcow2_handling"

export ROOTFS_RO_OVERLAY_TMPFS_SIZE=${ROOTFS_RO_OVERLAY_TMPFS_SIZE:-128M}
# vfat, ext2, ext4 or xfs
export BOOT_FSTYPE=${BOOT_FSTYPE:-vfat}
# codepage is hardcoded in 'loop_rootfs' and scripts, as well as required by pi-gen build
if [ -z ${FAT_CODEPAGE+x} ]; then
    readonly FAT_CODEPAGE="437"
    export FAT_CODEPAGE
fi
export BOOT_FSOPTIONS=${BOOT_FSOPTIONS:-rw,noatime,fmask=0022,dmask=0022,codepage=${FAT_CODEPAGE},iocharset=ascii,shortname=mixed,errors=remount-ro}

dependencies_check "${BASE_DIR}/depends"

#check username is valid
if [[ ! "$FIRST_USER_NAME" =~ ^[a-z][-a-z0-9_]*$ ]]; then
	echo "Invalid FIRST_USER_NAME: $FIRST_USER_NAME"
	exit 1
fi

if [[ -n "${APT_PROXY}" ]] && ! curl --silent "${APT_PROXY}" >/dev/null ; then
	echo "Could not reach APT_PROXY server: ${APT_PROXY}"
	exit 1
fi

if [[ -n "${WPA_PASSWORD}" && ${#WPA_PASSWORD} -lt 8 || ${#WPA_PASSWORD} -gt 63  ]] ; then
	echo "WPA_PASSWORD" must be between 8 and 63 characters
	exit 1
fi

if [[ "${PUBKEY_ONLY_SSH}" = "1" && -z "${PUBKEY_SSH_FIRST_USER}" ]]; then
	echo "Must set 'PUBKEY_SSH_FIRST_USER' to a valid SSH public key if using PUBKEY_ONLY_SSH"
	exit 1
fi

CLEAN=1
mkdir -p "${WORK_DIR}"
log "Begin ${BASE_DIR}"

STAGE_LIST=${STAGE_LIST:-${BASE_DIR}/stage*}
echo "Running  stages: \"${STAGE_LIST}\""

SKIP_STAGE_LIST=${SKIP_STAGE_LIST:-}
SKIP_IMAGES_LIST=${SKIP_IMAGES_LIST:-}
echo "Skipping stages: \"${SKIP_STAGE_LIST}\""
echo "Skipping images: \"${SKIP_IMAGES_LIST}\""

for STAGE_DIR in $STAGE_LIST; do
	STAGE_DIR=$(realpath "${STAGE_DIR}")
	run_stage
done

CLEAN=1
for EXPORT_DIR in ${EXPORT_DIRS}; do
	STAGE_DIR=${BASE_DIR}/export-image
	# shellcheck source=/dev/null
	source "${EXPORT_DIR}/EXPORT_IMAGE"
	EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename "${EXPORT_DIR}")/rootfs
    USE_QCOW2=0
    EXPORT_NAME="${IMG_FILENAME}${IMG_SUFFIX}"
    echo "------------------------------------------------------------------------"
    echo "Running export stage for ${EXPORT_NAME}"
    rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.img" || true
    rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" || true
    rm -f "${WORK_DIR}/${EXPORT_NAME}.img" || true
    rm -f "${WORK_DIR}/${EXPORT_NAME}.qcow2" || true
    EXPORT_STAGE=$(basename "${EXPORT_DIR}")
    for s in $STAGE_LIST; do
        TMP_LIST=${TMP_LIST:+$TMP_LIST }$(basename "${s}")
    done
    FIRST_STAGE=${TMP_LIST%% *}
    FIRST_IMAGE="image-${FIRST_STAGE}.qcow2"

    pushd "${WORK_DIR}" > /dev/null
    echo "Creating new base "${EXPORT_NAME}.qcow2" from ${FIRST_IMAGE}"
    cp "./${FIRST_IMAGE}" "${EXPORT_NAME}.qcow2"

    ARR=($TMP_LIST)
    # rebase stage images to new export base
    for CURR_STAGE in "${ARR[@]}"; do
        if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
            PREV_IMG="${EXPORT_NAME}"
            continue
        fi
    echo "Rebasing image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
        qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2 -F qcow2
        if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
            break
        fi
        PREV_IMG="image-${CURR_STAGE}"
    done

    # commit current export stage into base export image
    echo "Committing image-${EXPORT_STAGE}.qcow2 to ${EXPORT_NAME}.qcow2"
    qemu-img commit -f qcow2 -p -b "${EXPORT_NAME}.qcow2" image-${EXPORT_STAGE}.qcow2

    # rebase stage images back to original first stage for easy re-run
    for CURR_STAGE in "${ARR[@]}"; do
        if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
            PREV_IMG="image-${CURR_STAGE}"
            continue
        fi
    echo "Rebasing back image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
        qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2 -F qcow2
        if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
            break
        fi
        PREV_IMG="image-${CURR_STAGE}"
    done
    popd > /dev/null

    mkdir -p "${WORK_DIR}/export-image/rootfs"
    mv "${WORK_DIR}/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/"
    echo "Mounting image ${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2 to rootfs ${WORK_DIR}/export-image/rootfs"
    mount_qimage "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/rootfs"

    CLEAN=0
    run_stage
    CLEAN=1
    USE_QCOW2=1

	if [ "${USE_QEMU}" != "1" ]; then
		if [ -e "${EXPORT_DIR}/EXPORT_NOOBS" ]; then
			# shellcheck source=/dev/null
			source "${EXPORT_DIR}/EXPORT_NOOBS"
			STAGE_DIR="${BASE_DIR}/export-noobs"
            USE_QCOW2=0
            run_stage
            USE_QCOW2=1
		fi
	fi
done

if [ -x postrun.sh ]; then
	log "Begin postrun.sh"
	cd "${BASE_DIR}"
	./postrun.sh
	log "End postrun.sh"
fi

unload_qimage

log "End ${BASE_DIR}"