logo

bootstrap-initrd

Linux initrd to bootstrap from a small binary seed git clone https://anongit.hacktivis.me/git/bootstrap-initrd.git/

init.sh (4507B)


  1. #!/bin/loksh
  2. # bootstrap-initrd: Linux initrd to bootstrap from a small binary seed
  3. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  4. # SPDX-License-Identifier: MPL-2.0
  5. die() {
  6. echo "die: $*"
  7. rescueshell
  8. }
  9. rescueshell() {
  10. exec $SHELL -l
  11. }
  12. profile_export() {
  13. export "$@"
  14. echo "export '$@'" >>/etc/profile
  15. }
  16. build_awk() {
  17. cd /src/awk-*/
  18. yacc -d -b awkgram awkgram.y
  19. pdpmake -e HOSTCC=$CC
  20. cp -p a.out /bin/awk
  21. }
  22. build_stubs() {
  23. for i in ls grep cp getty mount
  24. do
  25. $CC $CFLAGS -o "/bin/$i" "/src/${i}-stub.c" || die "Failed compiling $i stub"
  26. done
  27. }
  28. build_stubs_symlinks() {
  29. ln -s grep /bin/egrep || die
  30. ln -s grep /bin/fgrep || die
  31. ln -s cat /bin/sort || die
  32. }
  33. build_pdpmake() {
  34. cd /src/pdpmake-*/
  35. $CC $CFLAGS -o /bin/pdpmake *.c
  36. }
  37. build_bzip2() {
  38. cd /src/bzip2-*/
  39. pdpmake -f Makefile-libbz2_so CC=$CC || die
  40. ln -s libbz2.so.1.0.8 libbz2.so
  41. pdpmake -f Makefile bzip2 CC=$CC || die
  42. ln libbz2.so.1.0.8 /lib/libbz2.so.1.0.8
  43. ln -s libbz2.so.1.0.8 /lib/libbz2.so
  44. ln -s libbz2.so.1.0.8 /lib/libbz2.so.1.0
  45. ln bzip2 /bin/bzip2
  46. ln -s bzip2 /bin/bunzip2
  47. ln -s bzip2 /bin/bzcat
  48. pdpmake clean
  49. }
  50. build_zlib_libs() {
  51. cd /src/zlib-*/
  52. ./configure </dev/null
  53. # generate crc32.h
  54. pdpmake crc32 CFLAGS="${CFLAGS} -DMAKECRCH"
  55. ./crc32
  56. pdpmake install
  57. pdpmake clean
  58. }
  59. build_pigz() {
  60. cd /src/pigz-*/
  61. # Makefile placeholders for zopfli/deflate.h and zopfli/util.h
  62. touch deflate.h util.h
  63. pdpmake CC=$CC ZOP= ZOPFLI= CFLAGS="${CFLAGS} -DNOZOPFLI"
  64. cp -p pigz /bin
  65. ln -s pigz /bin/unpigz
  66. ln -s pigz /bin/gzip
  67. ln -s pigz /bin/ungzip
  68. pdpmake clean
  69. }
  70. build_hierloom_devtools() {
  71. cd /src/heirloom-devtools-*/
  72. for cmd in lex m4
  73. do
  74. pdpmake "$cmd"/Makefile
  75. pdpmake -C "$cmd" RANLIB=true
  76. pdpmake -C "$cmd" RANLIB=true install
  77. pdpmake -C "$cmd" RANLIB=true clean
  78. done
  79. }
  80. build_hierloom() {
  81. cd /src/heirloom-0*/
  82. mkdir -p /usr/5bin /usr/5lib /etc/default /usr/5bin/s42 /usr/5bin/posix2001 /usr/ucb/
  83. for cmd in libcommon diff tar sort comm find stty
  84. do
  85. pdpmake "$cmd"/Makefile
  86. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true
  87. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true install
  88. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true clean
  89. done
  90. ln -fs /usr/5bin/sort /bin/sort
  91. mv /usr/5bin/find /usr/bin/find
  92. }
  93. build_utils_std() {
  94. cd /src/utils-std-*/
  95. oldpath="$PATH"
  96. export PATH="$PATH:$PWD/cmd/"
  97. loksh ./bootstrap.sh || die
  98. MAKE=pdpmake loksh ./configure PREFIX=/usr M4=true || die
  99. rm cmd/cat cmd/printf cmd/tr || die
  100. pdpmake || die
  101. pdpmake selfcheck || die
  102. pdpmake install || die
  103. pdpmake clean || die
  104. export PATH="$oldpath"
  105. }
  106. build_sbase() {
  107. cd /src/sbase-*/
  108. for i in ed sed
  109. do
  110. $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
  111. done
  112. }
  113. build_oyacc() {
  114. cd /src/yacc-oyacc-*/
  115. # config.h wrote in make-root.sh due to lack of cat
  116. $CC $CFLAGS \
  117. -D_GNU_SOURCE -D__unused= \
  118. -o /bin/yacc *.c
  119. }
  120. set -ex -o pipefail
  121. export CC=tcc
  122. export CFLAGS="-Os -Wall -Wextra"
  123. build_oyacc
  124. export SHELL="/bin/loksh"
  125. build_pdpmake || die
  126. build_sbase || die
  127. # stubs for utilities currently missing from utils-std
  128. build_stubs || die
  129. build_utils_std || die
  130. export MAKEFLAGS="-j$(nproc)"
  131. profile_export PATH="/bin:/usr/bin:/usr/local/bin"
  132. profile_export YACC="yacc"
  133. profile_export STRIP="true"
  134. profile_export INSTALL="install"
  135. profile_export AR="ar"
  136. # re-export due to prior lack of echo
  137. profile_export CC="$CC"
  138. profile_export CFLAGS="$CFLAGS"
  139. # needs: make cat $CC $YACC rm mv $AR true test install mkdir
  140. build_hierloom_devtools || die
  141. cat >/bin/hostname <<'EOF'
  142. #!/bin/sh
  143. exec uname -n
  144. EOF
  145. chmod +x /bin/hostname
  146. build_stubs_symlinks || die
  147. build_awk || die
  148. build_hierloom || die
  149. build_bzip2 || die
  150. # Needs /bin/rm
  151. build_zlib_libs || die
  152. build_pigz || die
  153. test -f /cacert-*.pem && {
  154. mkdir -p /usr/share/cacert/ || die
  155. mv /cacert-*.pem /usr/share/cacert/cert.pem || die
  156. mkdir -p /etc/ssl || die
  157. ln -s /usr/share/cacert/cert.pem /etc/ssl/cert.pem || die
  158. }
  159. mkdir -p $HOME
  160. cd
  161. # No /dev/tty1, no getty (as seen in containers)
  162. if ! test -e /dev/tty1
  163. then
  164. times
  165. echo 'Done bootstrapping! Dropping into a shell'
  166. exec /bin/sh -l
  167. fi
  168. cat /proc/uptime
  169. echo 'Done bootstrapping! Dropping into a TTY'
  170. if ! grep -q console= /proc/cmdline
  171. then
  172. getty "/dev/tty1" /bin/sh -l &
  173. else
  174. for arg in $(cat /proc/cmdline); do
  175. case $arg in
  176. console=*)
  177. getty "/dev/${arg#console=}" /bin/sh -l &
  178. ;;
  179. esac
  180. done
  181. fi
  182. wait