logo

bootstrap-initrd

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

init.sh (4515B)


  1. #!/bin/oksh
  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 check
  57. pdpmake install
  58. pdpmake clean
  59. }
  60. build_pigz() {
  61. cd /src/pigz-*/
  62. # Makefile placeholders for zopfli/deflate.h and zopfli/util.h
  63. touch deflate.h util.h
  64. pdpmake CC=$CC ZOP= ZOPFLI= CFLAGS="${CFLAGS} -DNOZOPFLI"
  65. cp -p pigz /bin
  66. ln -s pigz /bin/unpigz
  67. ln -s pigz /bin/gzip
  68. ln -s pigz /bin/ungzip
  69. pdpmake clean
  70. }
  71. build_hierloom_devtools() {
  72. cd /src/heirloom-devtools-*/
  73. for cmd in lex m4
  74. do
  75. pdpmake "$cmd"/Makefile
  76. pdpmake -C "$cmd" RANLIB=true
  77. pdpmake -C "$cmd" RANLIB=true install
  78. pdpmake -C "$cmd" RANLIB=true clean
  79. done
  80. }
  81. build_hierloom() {
  82. cd /src/heirloom/
  83. mkdir -p /usr/5bin /usr/5lib /etc/default /usr/5bin/s42 /usr/5bin/posix2001 /usr/ucb/
  84. for cmd in libcommon diff tar sort comm find stty
  85. do
  86. pdpmake "$cmd"/Makefile
  87. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true
  88. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true install
  89. pdpmake -C "$cmd" CC=$CC AR="ar" RANLIB=true clean
  90. done
  91. ln -fs /usr/5bin/sort /bin/sort
  92. mv /usr/5bin/find /usr/bin/find
  93. }
  94. build_utils_std() {
  95. cd /src/utils-std-*/
  96. oldpath="$PATH"
  97. export PATH="$PATH:$PWD/cmd/"
  98. oksh ./bootstrap.sh || die
  99. MAKE=pdpmake oksh ./configure PREFIX=/usr M4=true || die
  100. rm cmd/cat cmd/printf cmd/tr || die
  101. pdpmake || die
  102. pdpmake selfcheck || die
  103. pdpmake install || die
  104. pdpmake clean || die
  105. export PATH="$oldpath"
  106. }
  107. build_sbase() {
  108. cd /src/sbase-*/
  109. for i in ed sed
  110. do
  111. $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
  112. done
  113. }
  114. build_oyacc() {
  115. cd /src/yacc-oyacc-*/
  116. # config.h wrote in make-root.sh due to lack of cat
  117. $CC $CFLAGS \
  118. -D_GNU_SOURCE -D__unused= \
  119. -o /bin/yacc *.c
  120. }
  121. set -ex -o pipefail
  122. export CC=tcc
  123. export CFLAGS="-Os -Wall -Wextra"
  124. build_oyacc
  125. export SHELL="/bin/oksh"
  126. build_pdpmake || die
  127. build_sbase || die
  128. # stubs for utilities currently missing from utils-std
  129. build_stubs || die
  130. build_utils_std || die
  131. export MAKEFLAGS="-j$(nproc)"
  132. profile_export PATH="/bin:/usr/bin:/usr/local/bin"
  133. profile_export YACC="yacc"
  134. profile_export STRIP="true"
  135. profile_export INSTALL="install"
  136. profile_export AR="ar"
  137. # re-export due to prior lack of echo
  138. profile_export CC="$CC"
  139. profile_export CFLAGS="$CFLAGS"
  140. # needs: make cat $CC $YACC rm mv $AR true test install mkdir
  141. build_hierloom_devtools || die
  142. cat >/bin/hostname <<'EOF'
  143. #!/bin/sh
  144. exec uname -n
  145. EOF
  146. chmod +x /bin/hostname
  147. build_stubs_symlinks || die
  148. build_awk || die
  149. build_hierloom || die
  150. build_bzip2 || die
  151. # Needs /bin/rm
  152. build_zlib_libs || die
  153. build_pigz || die
  154. test -f /cacert-*.pem && {
  155. mkdir -p /usr/share/cacert/ || die
  156. mv /cacert-*.pem /usr/share/cacert/cert.pem || die
  157. mkdir -p /etc/ssl || die
  158. ln -s /usr/share/cacert/cert.pem /etc/ssl/cert.pem || die
  159. }
  160. mkdir -p $HOME
  161. cd
  162. # No /dev/tty1, no getty (as seen in containers)
  163. if ! test -e /dev/tty1
  164. then
  165. times
  166. echo 'Done bootstrapping! Dropping into a shell'
  167. exec /bin/sh -l
  168. fi
  169. cat /proc/uptime
  170. echo 'Done bootstrapping! Dropping into a TTY'
  171. if ! grep -q console= /proc/cmdline
  172. then
  173. getty "/dev/tty1" /bin/sh -l &
  174. else
  175. for arg in $(cat /proc/cmdline); do
  176. case $arg in
  177. console=*)
  178. getty "/dev/${arg#console=}" /bin/sh -l &
  179. ;;
  180. esac
  181. done
  182. fi
  183. wait