#!/bin/sh # Builds Tiny Linux for QuantaPrism Code (runs inside an i386 Alpine container, see build.sh). # Output in /out: bzImage (Alpine linux-virt kernel), initrd.gz (busybox + tcc + fun commands # + hello.ko), packages.txt (exact package versions, for THIRD-PARTY.md and the GPL source offer). set -eu SRC=/src OUT=/out ROOT=/rootfs PKGS="busybox-static tcc tcc-libs tcc-libs-static tcc-dev musl-dev figlet cmatrix sl fortune ncurses-terminfo-base" apk update -q apk add -q linux-virt linux-virt-dev build-base cpio # 1. the kernel and the hello module, built for exactly that kernel KVER=$(ls /lib/modules | head -n1) cp /boot/vmlinuz-virt "$OUT/bzImage" rm -rf /tmp/hello && cp -r "$SRC/hello" /tmp/hello make -C "/lib/modules/$KVER/build" M=/tmp/hello modules >/tmp/hello.log 2>&1 || { cat /tmp/hello.log; exit 1; } strip --strip-debug /tmp/hello/hello.ko # 2. the root file system: the packages, installed into an empty root rm -rf "$ROOT" && mkdir -p "$ROOT" apk add -q --root "$ROOT" --initdb --no-scripts --keys-dir /etc/apk/keys \ --repositories-file /etc/apk/repositories $PKGS mkdir -p "$ROOT/bin" "$ROOT/sbin" "$ROOT/usr/bin" "$ROOT/usr/sbin" chroot "$ROOT" /bin/busybox.static --install -s ln -sf busybox.static "$ROOT/bin/busybox" apk info -v --root "$ROOT" 2>/dev/null | sort > "$OUT/packages.txt" apk info -v 2>/dev/null | grep -E "^linux-virt-[0-9]" | sed "s/$/ (kernel)/" >> "$OUT/packages.txt" # 3. trim what a learner never needs (static libraries, manuals, apk database) rm -rf "$ROOT/lib/apk" "$ROOT/var/cache" "$ROOT/usr/share/man" "$ROOT/usr/share/doc" "$ROOT/etc/apk" find "$ROOT/usr/lib" -name '*.a' ! -name 'libtcc1.a' -delete # figlet: keep a few fonts find "$ROOT/usr/share/figlet" -type f ! -name 'standard.flf' ! -name 'small.flf' ! -name 'big.flf' ! -name 'banner.flf' ! -name 'slant.flf' -delete 2>/dev/null || true # 4. our files mkdir -p "$ROOT/proc" "$ROOT/sys" "$ROOT/dev" "$ROOT/tmp" "$ROOT/root" "$ROOT/lib/modules" cp -r "$SRC/rootfs/." "$ROOT/" chmod +x "$ROOT/init" "$ROOT/usr/local/bin/"* mkdir -p "$ROOT/lib/modules/$KVER/extra" cp /tmp/hello/hello.ko "$ROOT/lib/modules/$KVER/extra/hello.ko" ln -s "$KVER/extra/hello.ko" "$ROOT/lib/modules/hello.ko" cp /lib/modules/"$KVER"/modules.builtin /lib/modules/"$KVER"/modules.order "$ROOT/lib/modules/$KVER/" chroot "$ROOT" /bin/busybox.static depmod "$KVER" cp "$SRC/hello/hello.c" "$ROOT/root/hello_module.c" mknod -m 600 "$ROOT/dev/console" c 5 1 2>/dev/null || true mknod -m 666 "$ROOT/dev/ttyS0" c 4 64 2>/dev/null || true mknod -m 666 "$ROOT/dev/null" c 1 3 2>/dev/null || true # 5. pack (cd "$ROOT" && find . | cpio -o -H newc 2>/dev/null) | gzip -9 > "$OUT/initrd.gz" echo "$KVER" > "$OUT/kernel-version.txt" du -sh "$ROOT" ls -la "$OUT"