logo

bootstrap-initrd

Linux initrd to bootstrap from a small binary seed git clone https://hacktivis.me/git/make-initrd.git
commit: d8e0f335ee317f6793305c1d175319f4976708b3
parent 17e2bbac0f1bd9d7a2a061a703142bd19299e851
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 30 Apr 2024 00:52:47 +0200

make-initrd.sh: Move root generation out into make-root.sh

This way it is faster to regen, with root being usable via tools like bwrap.

Diffstat:

Mmake-initrd.sh81++-----------------------------------------------------------------------------
Amake-root.sh100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 102 insertions(+), 79 deletions(-)

diff --git a/make-initrd.sh b/make-initrd.sh @@ -3,17 +3,6 @@ # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> # SPDX-License-Identifier: MPL-2.0 -#elves="mksh" -tarballs=" - utils-std-656199b.tar.gz - mk-20210330.tar.gz - bmake-20230909.tar.gz - minised-1.16.tar.gz - yacc-1.9.1.tar.Z - nawk-20240422.tar.gz - loksh-7.3.tar.xz - bzip2-1.0.8.tar.gz -" WORKDIR="$(pwd)" name_base="initramfs-tcc" out_base="${WORKDIR}/${name_base}" @@ -23,77 +12,11 @@ die() { exit 1 } -gen_loksh_tcc_h() { - cd loksh-*/ || die - printf 'char *loksh_tcc_cmd[] = {' - # -DSMALL to not need ncurses - printf '"%s", ' /bin/tcc -D_GNU_SOURCE -DEMACS -DVI -DSMALL -DHAVE_STRLCPY -DHAVE_STRLCAT -DHAVE_ISSETUGID -I./subprojects/lolibc/include/ -o /bin/loksh *.c subprojects/lolibc/sys_signame.c subprojects/lolibc/strsignals.c subprojects/lolibc/unvis.c subprojects/lolibc/vis.c subprojects/lolibc/strtonum.c - printf 'NULL};' - cd "$out_base" || die "Failed: cd $out_base" -} - set -e -if test -e "$out_base"; then - rm -fr "$out_base" -fi -mkdir -p "$out_base" || die "Failed: mkdir $out_base" -cd "$out_base" || die "Failed: cd $out_base" - -for i in $tarballs; do - tar xf "/var/cache/distfiles/$i" || die "Failed extracting $i" -done -rm yacc-1.9.1/yacc || die - -deblob - -mkdir -p bin dev proc sys etc tmp || die "Failed creating base directories" -ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab" -ln -s . usr -chmod 777 tmp - -for elf in $elves; do - origin=$(command -v "$elf") - cp "$origin" bin/ || die -done - -for lib in $(find bin -type f -exec lddtree -l {} + | grep -v bin/ | sort | uniq); do - cp "$lib" lib/ || die -done +./make-root.sh || die "Failed: make-root" -for i in fd stderr stdin stdout; do - ln -fs proc/self/$i dev/$i -done - -cp "${WORKDIR}/init.c" ./init || die "copying init" -sed -i '1i#!/bin/tcc -run' ./init || die "failed adding tcc shebang to init" -chmod 755 init || die "init chmod" - -for i in init.sh ls-stub.c mv-stub.c grep-stub.c -do - cp -p "${WORKDIR}/$i" ./ || die "failed copying $i" -done - -QMERGE=1 qmerge -yOKv --root $PWD dev-lang/tcc || die "Failed qmerge'ing tcc" - -qlist musl | grep -v -e /bin/ -e /etc/ -e /sbin/ | while read f -do - mkdir -p "$(dirname "./$f")" || die - cp -pr "$f" "./$f" || die -done - -# For CLOCK_MONOTONIC and clock_gettime -sed -i -e '1a#include <time.h>' loksh-*/c_sh.c || die "Failed patching loksh/c_sh.c" -# For u_char -sed -i -e '1a#include <sys/types.h>' loksh-*/edit.c || die "Failed patching loksh/c_sh.c" -# For int64_t -sed -i -e '1a#include <stdint.h>' loksh-*/table.h || die "Failed patching loksh/table.h" -sed -i -e '1a#include <limits.h>' -e 's;_PW_NAME_LEN;LOGIN_NAME_MAX;' loksh-*/main.c || die "Failed patching loksh/main.c" - -gen_loksh_tcc_h > loksh_tcc.h - -sed -i -e 's;bzip2-shared;bzip2;' bzip2-*/Makefile-libbz2_so || die "Failed patching /bzip2-*/Makefile-libbz2_so" -sed -i -e 's;all: libbz2.a;all: libbz2.so;' -e 's;bzip2: libbz2.a;bzip2: libbz2.so;' bzip2-*/Makefile || die "Failed patching /bzip2-*/Makefile" +cd "$out_base" || die "Failed: cd $out_base" deblob -n diff --git a/make-root.sh b/make-root.sh @@ -0,0 +1,100 @@ +#!/bin/sh +# bootstrap-initrd: Linux initrd to bootstrap from a small binary seed +# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +# SPDX-License-Identifier: MPL-2.0 + +#elves="mksh" +tarballs=" + utils-std-656199b.tar.gz + mk-20210330.tar.gz + bmake-20230909.tar.gz + minised-1.16.tar.gz + yacc-1.9.1.tar.Z + nawk-20240422.tar.gz + loksh-7.3.tar.xz + bzip2-1.0.8.tar.gz +" +WORKDIR="$(pwd)" +name_base="initramfs-tcc" +out_base="${WORKDIR}/${name_base}" + +die() { + echo "[Error] die: ${*}" + exit 1 +} + +gen_loksh_tcc_h() { + cd loksh-*/ || die + printf 'char *loksh_tcc_cmd[] = {' + # -DSMALL to not need ncurses + printf '"%s", ' /bin/tcc -D_GNU_SOURCE -DEMACS -DVI -DSMALL -DHAVE_STRLCPY -DHAVE_STRLCAT -DHAVE_ISSETUGID -I./subprojects/lolibc/include/ -o /bin/loksh *.c subprojects/lolibc/sys_signame.c subprojects/lolibc/strsignals.c subprojects/lolibc/unvis.c subprojects/lolibc/vis.c subprojects/lolibc/strtonum.c + printf 'NULL};' + cd "$out_base" || die "Failed: cd $out_base" +} + +set -e + +if test -e "$out_base"; then + rm -fr "$out_base" +fi +mkdir -p "$out_base" || die "Failed: mkdir $out_base" +cd "$out_base" || die "Failed: cd $out_base" + +for i in $tarballs; do + tar xf "/var/cache/distfiles/$i" || die "Failed extracting $i" +done +rm yacc-1.9.1/yacc || die + +deblob + +mkdir -p bin dev proc sys etc tmp || die "Failed creating base directories" +ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab" +ln -s . usr +chmod 777 tmp + +for elf in $elves; do + origin=$(command -v "$elf") + cp "$origin" bin/ || die +done + +for lib in $(find bin -type f -exec lddtree -l {} + | grep -v bin/ | sort | uniq); do + cp "$lib" lib/ || die +done + +for i in fd stderr stdin stdout; do + ln -fs proc/self/$i dev/$i +done + +cp "${WORKDIR}/init.c" ./init || die "copying init" +sed -i '1i#!/bin/tcc -run' ./init || die "failed adding tcc shebang to init" +chmod 755 init || die "init chmod" + +for i in init.sh ls-stub.c mv-stub.c grep-stub.c +do + cp -p "${WORKDIR}/$i" ./ || die "failed copying $i" +done + +QMERGE=1 qmerge -yOKv --root $PWD dev-lang/tcc || die "Failed qmerge'ing tcc" + +qlist musl | grep -v -e /bin/ -e /etc/ -e /sbin/ | while read f +do + mkdir -p "$(dirname "./$f")" || die + cp -pr "$f" "./$f" || die +done + +# For CLOCK_MONOTONIC and clock_gettime +sed -i -e '1a#include <time.h>' loksh-*/c_sh.c || die "Failed patching loksh/c_sh.c" +# For u_char +sed -i -e '1a#include <sys/types.h>' loksh-*/edit.c || die "Failed patching loksh/c_sh.c" +# For int64_t +sed -i -e '1a#include <stdint.h>' loksh-*/table.h || die "Failed patching loksh/table.h" +sed -i -e '1a#include <limits.h>' -e 's;_PW_NAME_LEN;LOGIN_NAME_MAX;' loksh-*/main.c || die "Failed patching loksh/main.c" + +gen_loksh_tcc_h > loksh_tcc.h + +sed -i -e 's;bzip2-shared;bzip2;' bzip2-*/Makefile-libbz2_so || die "Failed patching /bzip2-*/Makefile-libbz2_so" +sed -i -e 's;all: libbz2.a;all: libbz2.so;' -e 's;bzip2: libbz2.a;bzip2: libbz2.so;' bzip2-*/Makefile || die "Failed patching /bzip2-*/Makefile" + +deblob -n + +cd "${WORKDIR}"