logo

bootstrap-initrd

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

init.sh (5127B)


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