-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-rootfs.sh
executable file
·78 lines (66 loc) · 2.76 KB
/
build-rootfs.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
#!/bin/bash
# Bootstraps a base Debian rootfs for aarch64 (using qemu-static for
# cross-installation).
set -eo pipefail
SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/" && pwd)"
source "$SRC_DIR/lib/common.sh"
[[ -n "$ROOTFS_DEST" ]] || log_fatal "No ROOTFS_DEST given!"
DEB_ARCH=${DEB_ARCH:-"arm64"}
DEB_VERSION=${DEB_VERSION:-"bookworm"}
DEB_INSTALL_BASE_PKGS=${DEB_INSTALL_BASE_PKGS:-"ca-certificates,"}
DEB_PROXY=${DEB_PROXY:-"http://ftp.de.debian.org/debian"}
QEMU_ARCH=${QEMU_ARCH:-"aarch64"}
# Runs debootstrap stage 1 (bare rootfs creation)
function debootstrap_stage1() {
local MOUNTPOINT="$1"
local DEBOOTSTRAP_VARIANT=minbase
local DEBOOTSTRAP_ARGS=(--foreign --arch=$DEB_ARCH --include="$DEB_INSTALL_BASE_PKGS" \
--variant=$DEBOOTSTRAP_VARIANT $DEB_VERSION "$MOUNTPOINT" $DEB_PROXY)
log_info "Running debootstrap stage 1..."
log_debug debootstrap "${DEBOOTSTRAP_ARGS[@]}"
$SUDO debootstrap "${DEBOOTSTRAP_ARGS[@]}"
}
function rootfs_copy_qemu_static() {
local MOUNTPOINT="$1"
local QEMU_STATIC_PATH= # do not assign here because the error won't be returned
if QEMU_STATIC_PATH=$(which qemu-$QEMU_ARCH-static); then
[[ -n "$QEMU_STATIC_PATH" ]] || log_fatal "Could not find qemu-$QEMU_ARCH-static!"
$SUDO install -D -m755 -oroot -groot --target-directory="$MOUNTPOINT/usr/bin/" "$QEMU_STATIC_PATH"
return 0
fi
log_fatal "Could not find qemu-$QEMU_ARCH-static!"
}
# Runs debootstrap stage 2 (chrooted base package installation)
function debootstrap_stage2() {
local MOUNTPOINT="$1"
log_info "Running debootstrap stage 2..."
"$SRC_DIR/chroot.sh" \
/debootstrap/debootstrap --second-stage --verbose
}
function provision_rootfs() {
local MOUNTPOINT="$1"
# copy provisioning files to the rootfs mountpoint
$SUDO rsync -a --delete --chown root --chmod=755 --mkpath --exclude=".*" \
"$SRC_DIR/" "$MOUNTPOINT/root/rpi-provisioning/"
if declare -f -F "custom_provision_script" >/dev/null; then
custom_provision_script "$MOUNTPOINT"
fi
# run the install script
"$SRC_DIR/chroot.sh" bash "/root/rpi-provisioning/rootfs-install/install.sh"
}
log_info "RootFS target: $ROOTFS_DEST"
HAS_STAGE1=
[[ -f "$ROOTFS_DEST/usr/bin/cp" || -f "$ROOTFS_DEST/debootstrap/debootstrap" ]] && HAS_STAGE1=1 || true
HAS_STAGE2=
[[ -f "$ROOTFS_DEST/usr/bin/cp" && ! -f "$ROOTFS_DEST/debootstrap/debootstrap" ]] && HAS_STAGE2=1 || true
if [[ -z "$HAS_STAGE1" ]]; then
mkdir -p "$(dirname "$ROOTFS_DEST")"
$SUDO mkdir -p "$ROOTFS_DEST"
debootstrap_stage1 "$ROOTFS_DEST"
fi
if [[ -z "$HAS_STAGE2" ]]; then
rootfs_copy_qemu_static "$ROOTFS_DEST"
debootstrap_stage2 "$ROOTFS_DEST"
fi
# last stage: run the provisioning tasks inside the container
provision_rootfs "$ROOTFS_DEST"