#!/usr/bin/env bash # cxos/vendor/busybox/build.sh — build a static busybox + install into rootfs. # # Usage: # build.sh # x86_64, install into dist/cxos/rootfs # build.sh --arch aarch64 # build.sh --rootfs /path/to/rootfs # build.sh --dry # # After install: # /bin/busybox static binary (~1 MB) # /bin/* ./sbin/* symlinks to busybox (one per applet) set -euo pipefail ARCH="x86_64" DRY=0 ROOTFS="" for ((i=1; i<=$#; i++)); do case "${!i}" in --arch) ((i++)); ARCH="${!i}" ;; --rootfs) ((i++)); ROOTFS="${!i}" ;; --dry) DRY=1 ;; -h|--help) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "build.sh: unknown arg: ${!i}" >&2; exit 2 ;; esac done HERE="$(cd "$(dirname "$0")" && pwd)" REPO_ROOT="$(cd "$HERE/../../.." && pwd)" PINNED="$HERE/PINNED.json" FRAGMENT="$HERE/cxos.config.fragment" VERSION="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["version"])' "$PINNED")" EXTRACTED_DIR="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["extracted_dir"])' "$PINNED")" SRC="$HERE/src/$EXTRACTED_DIR" ROOTFS="${ROOTFS:-$REPO_ROOT/dist/cxos/rootfs}" case "$ARCH" in x86_64) CROSS="" ;; aarch64) CROSS="${CROSS_COMPILE:-aarch64-linux-gnu-}" ;; *) echo "build.sh: unsupported arch: $ARCH" >&2; exit 2 ;; esac JOBS="${JOBS:-$(nproc 2>/dev/null || echo 2)}" echo "==> busybox ${VERSION} (${ARCH}) -> ${ROOTFS}" echo " src : ${SRC}" echo " fragment: ${FRAGMENT}" echo " cross : ${CROSS:-}" echo " jobs : ${JOBS}" if [[ "$DRY" == "1" ]]; then echo "==> dry-run; not compiling" exit 0 fi [[ -d "$SRC" ]] || { echo "build.sh: source not extracted; run fetch.sh first" >&2; exit 1; } cd "$SRC" make CROSS_COMPILE="$CROSS" defconfig # Apply fragment overrides line-by-line. Lines that start with '#' or are # blank are skipped. KEY=VALUE lines replace any existing KEY= line, or are # appended if absent. while IFS= read -r line; do [[ -z "$line" || "$line" == \#* ]] && continue key="${line%%=*}" if grep -qE "^# ${key} is not set$|^${key}=" .config; then sed -i -E "s|^# ${key} is not set\$|${line}|; s|^${key}=.*|${line}|" .config else printf '%s\n' "$line" >> .config fi done < "$FRAGMENT" make CROSS_COMPILE="$CROSS" oldconfig make CROSS_COMPILE="$CROSS" -j"$JOBS" busybox mkdir -p "$ROOTFS" make CROSS_COMPILE="$CROSS" CONFIG_PREFIX="$ROOTFS" install # busybox install creates /linuxrc symlink — pointless on initramfs (we have # our own /init). Drop it. rm -f "$ROOTFS/linuxrc" echo "==> installed: $(find "$ROOTFS/bin" "$ROOTFS/sbin" -maxdepth 1 -mindepth 1 2>/dev/null | wc -l) applets"