logo

bootstrap-initrd

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

init.sh (6264B)


  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 sort 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. build_fts() {
  146. set -xe
  147. cd /src/musl-fts-1.2.7/
  148. cp fts.h /usr/include/
  149. cat > config.h <<'EOF'
  150. #define HAVE_DECL_MAX 1
  151. #define HAVE_DECL_UINTMAX_MAX 0
  152. #define HAVE_DIRFD 1
  153. EOF
  154. $CC $CFLAGS -shared -o /usr/lib/libfts.so.0.0.0 fts.c
  155. ln -s libfts.so.0.0.0 /usr/lib/libfts.so.0
  156. ln -s libfts.so.0.0.0 /usr/lib/libfts.so
  157. sed \
  158. -e 's,@prefix@,/usr,' \
  159. -e 's,@exec_prefix@,${prefix},' \
  160. -e 's,@libdir@,${prefix}/lib,' \
  161. -e 's,@includedir@,${prefix}/include,' \
  162. -e 's,@VERSION@,1.2.7,' \
  163. musl-fts.pc.in > /usr/lib/pkgconfig/musl-fts.pc
  164. }
  165. build_pax() {
  166. set -xe
  167. cd /src/pax/
  168. LIBS=-lfts SIZE=true sh ./Build.sh
  169. install -c -o root -g root -m 555 pax /bin/pax
  170. ln -s pax /bin/cpio
  171. ln -s pax /bin/tar
  172. }
  173. export CC=tcc
  174. export CFLAGS="-Os -Wall -Wextra"
  175. build_oyacc
  176. export SHELL="/bin/oksh"
  177. export YACC="yacc"
  178. build_pdpmake || die
  179. build_sbase || die
  180. # stubs for utilities currently missing from utils-std
  181. build_stubs || die
  182. build_hierloom_devtools_m4 || die
  183. build_utils_std || die
  184. ln -s pdpmake /bin/make
  185. export MAKEFLAGS="-j$(nproc)"
  186. profile_export PATH="/bin:/usr/bin:/usr/local/bin"
  187. profile_export YACC="yacc"
  188. profile_export STRIP="true"
  189. profile_export INSTALL="install"
  190. profile_export AR="ar"
  191. # re-export due to prior lack of echo
  192. profile_export CC="$CC"
  193. profile_export CFLAGS="$CFLAGS"
  194. # needs: make cat $CC $YACC rm mv $AR true test install mkdir
  195. build_hierloom_devtools </dev/null || die
  196. cat >/bin/hostname <<'EOF'
  197. #!/bin/sh
  198. exec uname -n
  199. EOF
  200. chmod +x /bin/hostname
  201. build_stubs_symlinks || die
  202. build_awk || die
  203. build_hierloom || die
  204. build_bzip2 || die
  205. # Needs /bin/rm
  206. build_zlib_libs </dev/null || die
  207. build_pigz || die
  208. build_fts || die
  209. build_pax || die
  210. test -f /distfiles/cacert-*.pem && {
  211. mkdir -p /usr/share/cacert/ || die
  212. mv /distfiles/cacert-*.pem /usr/share/cacert/cert.pem || die
  213. mkdir -p /etc/ssl || die
  214. ln -s /usr/share/cacert/cert.pem /etc/ssl/cert.pem || die
  215. }
  216. sumfile="/src/$(arch).sha256"
  217. if ! test -f "$sumfile" || ! sha256sum -c "$sumfile"; then
  218. find / -xdev -type f \
  219. | grep -E -v -e '^/(src/|extras/|tmp/SHA26SUM\.txt$|etc/setdate\.sh|init\.sh|/share/man/)' \
  220. | sort \
  221. | xargs sha256sum \
  222. > /tmp/SHA26SUM.txt
  223. die "Checking $sumfile failed, check against newly checksumed /tmp/SHA26SUM.txt"
  224. fi
  225. mkdir -p $HOME
  226. cd
  227. if [ "$(date +%Y)" = 1970 ]; then
  228. /etc/setdate.sh
  229. else
  230. date -u -Ins
  231. fi
  232. # No /dev/tty1, no getty (as seen in containers)
  233. if ! test -e /dev/tty1
  234. then
  235. times
  236. echo 'Done bootstrapping! Dropping into a shell'
  237. exec /bin/sh -l
  238. fi
  239. cat /proc/uptime
  240. echo 'Done bootstrapping! Dropping into a TTY'
  241. if ! grep -q console= /proc/cmdline
  242. then
  243. getty "/dev/tty1" /bin/sh -l &
  244. else
  245. for arg in $(cat /proc/cmdline); do
  246. case $arg in
  247. console=*)
  248. target=${arg#console=}
  249. device=${target%,*}
  250. # baud=${target#*,}
  251. getty "/dev/${device}" /bin/sh -l &
  252. ;;
  253. esac
  254. done
  255. fi
  256. wait