logo

bootstrap-initrd

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

make-root.sh (7231B)


  1. #!/bin/sh
  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. tarballs="
  6. oksh-7.6.tar.gz
  7. yacc-oyacc-6.6.tar.gz
  8. utils-std-0.0.1.tar.gz
  9. pdpmake-2.0.2.tgz
  10. nawk-20250116.tar.gz
  11. bzip2-bzip2-1.0.8.tar.gz
  12. zlib-1.3.1.tar.gz
  13. pigz-2.8.tar.gz
  14. heirloom-070715.tar.bz2
  15. heirloom-devtools-070527.tar.bz2
  16. sbase-b30fb568.tar.gz
  17. "
  18. local_files="
  19. init.sh
  20. src/ls-stub.c
  21. src/grep-stub.c
  22. src/cp-stub.c
  23. src/getty-stub.c
  24. src/mount-stub.c
  25. "
  26. distfiles="
  27. make-4.4.1.tar.gz
  28. xz-5.6.3.tar.gz
  29. lua-5.4.6.tar.gz
  30. pkgconf-pkgconf-2.3.0.tar.gz
  31. iproute2-6.12.0.tar.gz
  32. bearssl-0.6.tar.gz
  33. tiny-curl-8.4.0.tar.gz
  34. cacert-2024-11-26.pem
  35. gettext-tiny-0.3.2.tar.gz
  36. git-2.45.2.tar.gz
  37. e2fsprogs-1.47.1.tar.gz
  38. "
  39. local_distfiles="
  40. extras/make.sh
  41. extras/xz.sh
  42. extras/lua.sh
  43. extras/pkgconf.sh
  44. extras/iproute2.sh
  45. extras/bearssl.sh
  46. extras/tiny-curl.sh
  47. extras/gettext-tiny.sh
  48. extras/git.sh
  49. extras/e2fsprogs.sh
  50. extras-build-all.sh
  51. "
  52. WORKDIR="$(dirname "$0")"
  53. WORKDIR="$(realpath "$WORKDIR")"
  54. . "$WORKDIR/common.sh"
  55. : ${ALPINE_ARCH:=x86_64}
  56. name_base="bootstrap-initrd"
  57. out_base="${WORKDIR}/${name_base}/${ALPINE_ARCH}"
  58. test -z "${EXCLUDE_EXTRAS}" && out_base="${out_base}+extras"
  59. gen_oksh_tcc_h() {
  60. set -e
  61. cd oksh-*/
  62. cp "${WORKDIR}/src/oksh-7.6_pconfig.h" ./pconfig.h
  63. echo 'char *oksh_tcc_cmd[] = {'
  64. # -DSMALL to not need ncurses
  65. printf '\t"%s",\n' /usr/bin/tcc -o /bin/oksh \
  66. -D_GNU_SOURCE -DEMACS -DVI -DSMALL \
  67. *.c
  68. printf '\t0\n'
  69. echo '};'
  70. cd - >/dev/null
  71. }
  72. set -e
  73. sha512sum -c distfiles.SHA512SUM
  74. if test -e "$out_base"; then
  75. rm -fr "$out_base"
  76. fi
  77. mkdir -p "$out_base" || die "Failed: mkdir $out_base"
  78. cd "$out_base" || die "Failed: cd $out_base"
  79. mkdir -p src
  80. cd "${out_base}/src"
  81. for i in $tarballs; do
  82. tar xof "${WORKDIR}/distfiles/$i" || die "Failed extracting $i"
  83. done
  84. cd -
  85. for i in $local_files; do
  86. cp "${WORKDIR}/$i" ./"$i" || die "Failed copying $i"
  87. done
  88. if ! test "${EXCLUDE_EXTRAS:+y}" = "y"; then
  89. mkdir -p ./extras
  90. for i in $local_distfiles; do
  91. cp "${WORKDIR}/$i" ./"$i" || die "Failed copying $i"
  92. done
  93. mkdir -p ./distfiles
  94. for i in $distfiles
  95. do
  96. cp "${WORKDIR}/distfiles/$i" ./distfiles/ || die "Failed copying $i"
  97. done
  98. fi
  99. deblob
  100. mkdir -p dev proc sys etc usr/bin var/empty || die "Failed creating base directories"
  101. ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab"
  102. ln -s bin sbin
  103. ln -s usr/lib lib
  104. ln -s usr/bin bin
  105. ln -s usr/bin sbin
  106. mkdir -m 777 tmp
  107. cat >etc/passwd <<EOF
  108. root:x:0:0:root:/root:/bin/sh
  109. nobody:x:65534:65534:nobody:/var/empty:/bin/false
  110. EOF
  111. cat >etc/group <<EOF
  112. root:x:0:root
  113. nobody:x:65534:
  114. nogroup:x:65533:
  115. EOF
  116. for i in fd stderr stdin stdout; do
  117. ln -fs proc/self/$i dev/$i
  118. done
  119. cp "${WORKDIR}/init.c" ./init || die "copying init"
  120. sed -i '1i#!/usr/bin/tcc -run' ./init || die "failed adding tcc shebang to init"
  121. chmod 755 init || die "init chmod"
  122. cp -p "${WORKDIR}/ar-stub.sh" ./bin/ar
  123. for apk in $APKS_main $APKS_testing
  124. do
  125. tar xof "${WORKDIR}/distfiles/$apk.${ALPINE_ARCH}" --exclude '.*'
  126. done
  127. # Allows to shave off ~9.1M from the binary seed
  128. rm usr/lib/libc.a
  129. cd "${out_base}/src"
  130. ln -s oksh "${out_base}/bin/sh"
  131. gen_oksh_tcc_h > oksh_tcc.h
  132. cd "${out_base}/src"
  133. oyacc=$(echo ./yacc-oyacc-*/)
  134. cat >"${oyacc}/config.h" <<EOF
  135. // __dead and __dead2 are absent in musl
  136. #define __dead __attribute__((__no_return__))
  137. // HAVE_PROGNAME: Absent in musl
  138. #define HAVE_ASPRINTF
  139. // HAVE_PLEDGE: Absent in musl
  140. #define HAVE_REALLOCARRAY
  141. #define HAVE_STRLCPY
  142. EOF
  143. sed -i -e 's;bzip2-shared;bzip2;' bzip2-*/Makefile-libbz2_so || die "Failed patching /bzip2-*/Makefile-libbz2_so"
  144. sed -i -e 's;all: libbz2.a;all: libbz2.so;' -e 's;bzip2: libbz2.a;bzip2: libbz2.so;' bzip2-*/Makefile || die "Failed patching /bzip2-*/Makefile"
  145. rm bzip2-*/sample* || die "Failed removing sample bzip2 files"
  146. patch -p0 <"${WORKDIR}/zlib-1.3.1_no_staticlib.patch"
  147. patch -p0 <"${WORKDIR}/zlib-1.3.1-use-LDFLAGS-in-configure.patch"
  148. patch -p0 <"${WORKDIR}/zlib-1.3.1-use-LDFLAGS-in-configure_no_gcc.patch"
  149. rm zlib-*/doc/crc-doc.1.0.pdf || die "Failed removing zlib crc-doc.1.0.pdf"
  150. rm zlib-*/crc32.h || die "Failed removing autogenerated zlib-*/crc32.h"
  151. rm -r zlib-*/examples/ || die "Failed removing zlib-*/examples/"
  152. rm -r zlib-*/contrib/ || die "Failed removing zlib-*/contrib/"
  153. sed -i 's;^clean: minizip-clean;clean:;' zlib-*/Makefile.in || die "Failed disabling minizip cleanup"
  154. sed -i \
  155. -e 's;INSTALL=.*;INSTALL=install;' \
  156. -e 's;PREFIX=.*;PREFIX=/usr;;' \
  157. -e 's;STRIP=.*;STRIP=true;' \
  158. -e 's;#?AR=.*;AR ?= ar;' \
  159. -e 's;RANLIB=.*;RANLIB ?= ranlib;' \
  160. -e 's;YACC=.*;YACC ?= yacc;' \
  161. heirloom-devtools-*/mk.config \
  162. || die "Failed configuring heirloom-devtools"
  163. patch -p0 <"${WORKDIR}/pdpmake-2.0.2-fix_single_suffix.patch"
  164. # pdpmake-1.4.3+ consider them invalid "macros"
  165. sed -i \
  166. -e '/^\.c\.o:/s/;/\n\t/' \
  167. heirloom-devtools-*/lex/Makefile.mk heirloom-devtools-*/m4/Makefile.mk \
  168. || die "Failed patching .c.o: inferred targets in heirloom-devtools"
  169. # Only pick what we need from heirloom
  170. mkdir -p heirloom
  171. cd heirloom-0*/
  172. mv \
  173. makefile Makefile.mk build \
  174. libcommon diff tar sort comm find stty \
  175. ../heirloom/ || die "Failed copying heirloom files"
  176. cd -
  177. rm -r heirloom-0*/ || die "Failed removing old heirloom unpacked directory"
  178. # - maninst Fails to create parent dir, just noop it, we don't have man anyway
  179. # -e 's;MANINST =.*;MANINST = $(SHELL) ../build/maninst.sh;' \
  180. # - Turns out tcc comes with an ar(1)
  181. # -e 's;LCOMMON =.*;LCOMMON = ../libcommon/*.c;' \
  182. sed -i \
  183. -e 's;DEFBIN =.*;DEFBIN = /bin;' \
  184. -e 's;DEFSBIN =.*;DEFSBIN = /bin;' \
  185. -e 's;STRIP =.*;STRIP = true;' \
  186. -e 's;SPELLHIST =.*;SPELLHIST = /dev/null;' \
  187. -e 's;UCBINST =.*;UCBINST = install;' \
  188. -e 's;MANINST =.*;MANINST = true;' \
  189. heirloom/build/mk.config \
  190. || die "Failed configuring heirloom (toolchest)"
  191. sed -i \
  192. -e 's;__GLIBC__;__linux__;' \
  193. -e '/#define getdents/s;^;//;' \
  194. heirloom/libcommon/getdir.c || die "Failed fixing heirloom libcommon for musl"
  195. sed -i \
  196. -e 's;#ifdef _AIX;#if defined(_AIX) || defined(__linux__);' \
  197. -e '/static int utf8/avoid writerr(struct oblok *op, int count, int written) {}' \
  198. heirloom/tar/tar.c || die "Failed fixing heirloom tar for musl"
  199. sed -i \
  200. -e 's;#ifdef _AIX;#if defined(_AIX) || defined(__linux__);' \
  201. heirloom/find/find.c || die "Failed fixing heirloom find for musl"
  202. sed -i -e 's;getdir.o regexpr.o gmatch.o;getdir.o gmatch.o;' heirloom/libcommon/Makefile.mk || die
  203. rm -r pigz-*/zopfli || die
  204. rm -r heirloom-devtools-*/make heirloom-devtools-*/sccs || die
  205. rm -r awk-*/testdir || die
  206. cd "${out_base}/"
  207. deblob -n -j "${WORKDIR}"/make-root-deblob.json || die
  208. if command -v jq >/dev/null; then
  209. jq --raw-output0 '.[].path' "${WORKDIR}"/make-root-deblob.json \
  210. | xargs -0 du -bach \
  211. | sort -h
  212. elif command -v qjs >/dev/null; then
  213. <"${WORKDIR}"/make-root-deblob.json \
  214. qjs --std \
  215. -e 'let deblob_raw = std.in.readAsString(); let deblob = JSON.parse(deblob_raw); deblob.forEach((e) => {std.printf("%s\0", e.path);})' \
  216. | xargs -0 du -bach \
  217. | sort -h
  218. else
  219. echo "make-root: Command 'jq' and 'qjs' not found. Not going to list sizes of blobs" 1>&2
  220. fi
  221. cd "${WORKDIR}"