init.sh (4507B)
- #!/bin/loksh
- # 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
- die() {
- echo "die: $*"
- rescueshell
- }
- rescueshell() {
- exec $SHELL -l
- }
- profile_export() {
- export "$@"
- echo "export '$@'" >>/etc/profile
- }
- build_awk() {
- cd /src/awk-*/
- yacc -d -b awkgram awkgram.y
- pdpmake -e HOSTCC=$CC
- cp -p a.out /bin/awk
- }
- build_stubs() {
- for i in ls grep cp getty mount
- do
- $CC $CFLAGS -o "/bin/$i" "/src/${i}-stub.c" || die "Failed compiling $i stub"
- done
- }
- build_stubs_symlinks() {
- ln -s grep /bin/egrep || die
- ln -s grep /bin/fgrep || die
- ln -s cat /bin/sort || die
- }
- build_pdpmake() {
- cd /src/pdpmake-*/
- $CC $CFLAGS -o /bin/pdpmake *.c
- }
- build_bzip2() {
- cd /src/bzip2-*/
- pdpmake -f Makefile-libbz2_so CC=$CC || die
- ln -s libbz2.so.1.0.8 libbz2.so
- pdpmake -f Makefile bzip2 CC=$CC || die
- ln libbz2.so.1.0.8 /lib/libbz2.so.1.0.8
- ln -s libbz2.so.1.0.8 /lib/libbz2.so
- ln -s libbz2.so.1.0.8 /lib/libbz2.so.1.0
- ln bzip2 /bin/bzip2
- ln -s bzip2 /bin/bunzip2
- ln -s bzip2 /bin/bzcat
- pdpmake clean
- }
- build_zlib_libs() {
- cd /src/zlib-*/
- ./configure </dev/null
- # generate crc32.h
- pdpmake crc32 CFLAGS="${CFLAGS} -DMAKECRCH"
- ./crc32
- pdpmake install
- pdpmake clean
- }
- build_pigz() {
- cd /src/pigz-*/
- # Makefile placeholders for zopfli/deflate.h and zopfli/util.h
- touch deflate.h util.h
- pdpmake CC=$CC ZOP= ZOPFLI= CFLAGS="${CFLAGS} -DNOZOPFLI"
- cp -p pigz /bin
- ln -s pigz /bin/unpigz
- ln -s pigz /bin/gzip
- ln -s pigz /bin/ungzip
- pdpmake clean
- }
- build_hierloom_devtools() {
- cd /src/heirloom-devtools-*/
- for cmd in lex m4
- do
- pdpmake "$cmd"/Makefile
- pdpmake -C "$cmd" RANLIB=true
- pdpmake -C "$cmd" RANLIB=true install
- pdpmake -C "$cmd" RANLIB=true clean
- done
- }
- build_hierloom() {
- cd /src/heirloom-0*/
- mkdir -p /usr/5bin /usr/5lib /etc/default /usr/5bin/s42 /usr/5bin/posix2001 /usr/ucb/
- for cmd in libcommon diff tar sort comm find stty
- do
- pdpmake "$cmd"/Makefile
- pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true
- pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true install
- pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true clean
- done
- ln -fs /usr/5bin/sort /bin/sort
- mv /usr/5bin/find /usr/bin/find
- }
- build_utils_std() {
- cd /src/utils-std-*/
- oldpath="$PATH"
- export PATH="$PATH:$PWD/cmd/"
- loksh ./bootstrap.sh || die
- MAKE=pdpmake loksh ./configure PREFIX=/usr M4=true || die
- rm cmd/cat cmd/printf cmd/tr || die
- pdpmake || die
- pdpmake selfcheck || die
- pdpmake install || die
- pdpmake clean || die
- export PATH="$oldpath"
- }
- build_sbase() {
- cd /src/sbase-*/
- for i in ed sed
- do
- $CC -D_DEFAULT_SOURCE -D_NETBSD_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_FILE_OFFSET_BITS=64 -Os -o "/bin/${i}" "${i}.c" libutil/*.c libutf/*.c
- done
- }
- build_oyacc() {
- cd /src/yacc-oyacc-*/
- # config.h wrote in make-root.sh due to lack of cat
- $CC $CFLAGS \
- -D_GNU_SOURCE -D__unused= \
- -o /bin/yacc *.c
- }
- set -ex -o pipefail
- export CC=tcc
- export CFLAGS="-Os -Wall -Wextra"
- build_oyacc
- export SHELL="/bin/loksh"
- build_pdpmake || die
- build_sbase || die
- # stubs for utilities currently missing from utils-std
- build_stubs || die
- build_utils_std || die
- export MAKEFLAGS="-j$(nproc)"
- profile_export PATH="/bin:/usr/bin:/usr/local/bin"
- profile_export YACC="yacc"
- profile_export STRIP="true"
- profile_export INSTALL="install"
- profile_export AR="ar"
- # re-export due to prior lack of echo
- profile_export CC="$CC"
- profile_export CFLAGS="$CFLAGS"
- # needs: make cat $CC $YACC rm mv $AR true test install mkdir
- build_hierloom_devtools || die
- cat >/bin/hostname <<'EOF'
- #!/bin/sh
- exec uname -n
- EOF
- chmod +x /bin/hostname
- build_stubs_symlinks || die
- build_awk || die
- build_hierloom || die
- build_bzip2 || die
- # Needs /bin/rm
- build_zlib_libs || die
- build_pigz || die
- test -f /cacert-*.pem && {
- mkdir -p /usr/share/cacert/ || die
- mv /cacert-*.pem /usr/share/cacert/cert.pem || die
- mkdir -p /etc/ssl || die
- ln -s /usr/share/cacert/cert.pem /etc/ssl/cert.pem || die
- }
- mkdir -p $HOME
- cd
- # No /dev/tty1, no getty (as seen in containers)
- if ! test -e /dev/tty1
- then
- times
- echo 'Done bootstrapping! Dropping into a shell'
- exec /bin/sh -l
- fi
- cat /proc/uptime
- echo 'Done bootstrapping! Dropping into a TTY'
- if ! grep -q console= /proc/cmdline
- then
- getty "/dev/tty1" /bin/sh -l &
- else
- for arg in $(cat /proc/cmdline); do
- case $arg in
- console=*)
- getty "/dev/${arg#console=}" /bin/sh -l &
- ;;
- esac
- done
- fi
- wait