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 (9021B)


  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-oksh-7.7.tar.gz
  7. yacc-oyacc-6.6.tar.gz
  8. utils-std-0.0.8.tar.gz
  9. pdpmake-2.0.3.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. src/reboot-stub.c
  26. "
  27. extra_tarballs="
  28. make-4.4.1.tar.gz
  29. lua-5.4.6.tar.gz
  30. pkgconf-pkgconf-2.4.3.tar.gz
  31. iproute2-6.14.0.tar.gz
  32. BearSSL-3c040368f6791553610e362401db1efff4b4c5b8.tar.gz
  33. tiny-curl-8.4.0.tar.gz
  34. git-2.50.0.tar.gz
  35. e2fsprogs-1.47.2.tar.gz
  36. muon-v0.5.0.tar.gz
  37. "
  38. extra_distfiles="
  39. cacert-2024-11-26.pem
  40. "
  41. extra_local_files="
  42. extras-build-all.sh
  43. extras/bearssl.sh
  44. extras/e2fsprogs.sh
  45. extras/git.sh
  46. extras/iproute2.sh
  47. extras/lua.sh
  48. extras/make.sh
  49. extras/muon-stage1.sh
  50. extras/muon-stage2.sh
  51. extras/pkgconf-lite.sh
  52. extras/pkgconf.sh
  53. extras/tiny-curl.sh
  54. "
  55. WORKDIR="$(dirname "$0")"
  56. WORKDIR="$(realpath "$WORKDIR")"
  57. . "$WORKDIR/common.sh"
  58. : ${ALPINE_ARCH:=x86_64}
  59. name_base="bootstrap-initrd"
  60. out_base="${WORKDIR}/${name_base}/${ALPINE_ARCH}"
  61. test -z "${EXCLUDE_EXTRAS}" && out_base="${out_base}+extras"
  62. gen_oksh_tcc_h() {
  63. set -e
  64. cd oksh-*/
  65. cp "${WORKDIR}/src/oksh-7.6_pconfig.h" ./pconfig.h
  66. echo 'char *oksh_tcc_cmd[] = {'
  67. # -DSMALL to not need ncurses
  68. printf '\t"%s",\n' /usr/bin/tcc -o /bin/oksh \
  69. -D_GNU_SOURCE -DEMACS -DVI -DSMALL \
  70. *.c
  71. printf '\t0\n'
  72. echo '};'
  73. cd - >/dev/null
  74. }
  75. set -e
  76. sha512sum -c distfiles.SHA512SUM
  77. if test -e "$out_base"; then
  78. rm -fr "$out_base"
  79. fi
  80. mkdir -p "$out_base" || die "Failed: mkdir $out_base"
  81. cd "$out_base" || die "Failed: cd $out_base"
  82. mkdir -p src
  83. cd "${out_base}/src"
  84. for i in $tarballs; do
  85. tar xof "${WORKDIR}/distfiles/$i" || die "Failed extracting $i"
  86. done
  87. cd -
  88. for i in $local_files; do
  89. cp "${WORKDIR}/$i" ./"$i" || die "Failed copying $i"
  90. done
  91. if ! test "${EXCLUDE_EXTRAS:+y}" = "y"; then
  92. mkdir -p ./extras || die
  93. for i in $extra_tarballs; do
  94. tar xof "${WORKDIR}/distfiles/$i" -C ./extras/ || die "Failed extracting $i"
  95. done
  96. # remove blob
  97. rm ./extras/BearSSL-*/T0Comp.exe || die
  98. # Remove useless files to lower the size of the initrd (and so require less ram)
  99. rm -r ./extras/git-*/t ./extras/git-*/po ./extras/git-*/Documentation/RelNotes || die
  100. rm -r ./extras/e2fsprogs-*/tests ./extras/e2fsprogs-*/po || die
  101. # config.status: error: cannot find input file: `tests/Makefile.in'
  102. #rm -r ./extras/tiny-curl-*/tests || die
  103. # config.status: error: cannot find input file: `po/Makefile.in.in'
  104. #rm -r ./extras/make-*/po || die
  105. mkdir -p ./distfiles
  106. for i in $extra_distfiles
  107. do
  108. cp "${WORKDIR}/distfiles/$i" ./distfiles/ || die "Failed copying $i"
  109. done
  110. for i in $extra_local_files; do
  111. cp "${WORKDIR}/$i" ./"$i" || die "Failed copying $i"
  112. done
  113. fi
  114. deblob -c || die
  115. mkdir -p dev proc sys etc usr/bin var/empty || die "Failed creating base directories"
  116. ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab"
  117. ln -s bin sbin
  118. ln -s usr/lib lib
  119. ln -s usr/bin bin
  120. ln -s usr/bin sbin
  121. mkdir -m 777 tmp
  122. cat >etc/passwd <<EOF
  123. root:x:0:0:root:/root:/bin/sh
  124. nobody:x:65534:65534:nobody:/var/empty:/bin/false
  125. EOF
  126. cat >etc/group <<EOF
  127. root:x:0:root
  128. nobody:x:65534:
  129. nogroup:x:65533:
  130. EOF
  131. cat >etc/setdate.sh <<EOF
  132. #!/bin/sh
  133. exec date -f %Y-%m-%dT%TZ $(date +%Y-%m-%dT%TZ)
  134. EOF
  135. chmod +x etc/setdate.sh
  136. for i in fd stderr stdin stdout; do
  137. ln -fs proc/self/$i dev/$i
  138. done
  139. cp "${WORKDIR}/init.c" ./init || die "copying init"
  140. sed -i '1i#!/usr/bin/tcc -run' ./init || die "failed adding tcc shebang to init"
  141. chmod 755 init || die "init chmod"
  142. cp -p "${WORKDIR}/ar-stub.sh" ./bin/ar
  143. for apk in $APKS_main $APKS_community
  144. do
  145. tar xof "${WORKDIR}/distfiles/$apk.${ALPINE_ARCH}" --exclude '.*'
  146. done
  147. # Allows to shave off ~9.1M from the binary seed
  148. rm usr/lib/libc.a
  149. # tcc-0.9.27_git20250106-r0 only supports alloca(3) on __i386__, __x86_64__, __arm__ (32-bit)
  150. # removing the musl-provided header allows to compile git
  151. # (still breaks GNU make though)
  152. case "${ALPINE_ARCH}" in
  153. x86|x86_64|armhf)
  154. ;;
  155. *)
  156. rm usr/include/alloca.h
  157. sed -i '/alloca\.h/d' usr/include/stdlib.h
  158. ;;
  159. esac
  160. if [ "${ALPINE_ARCH}" = aarch64 ]; then
  161. # Compiling oksh-7.6
  162. # In file included from alloc.c:12:
  163. # In file included from sh.h:16:
  164. # /usr/lib/tcc/include/stddef.h:6: error: incompatible redefinition of 'wchar_t'
  165. #
  166. # Make it match musl's /usr/include/bits/alltypes.h
  167. #sed -i 's;#define __WCHAR_TYPE__ int;#define __WCHAR_TYPE__ unsigned;' usr/lib/tcc/include/tccdefs.h
  168. # Make musl match tcc (yuck)
  169. sed -i 's;typedef unsigned wchar_t;typedef int wchar_t;' usr/include/bits/alltypes.h
  170. fi
  171. cd "${out_base}/src"
  172. ln -s oksh "${out_base}/bin/sh"
  173. gen_oksh_tcc_h > oksh_tcc.h
  174. cd "${out_base}/src"
  175. oyacc=$(echo ./yacc-oyacc-*/)
  176. cat >"${oyacc}/config.h" <<EOF
  177. // __dead and __dead2 are absent in musl
  178. #define __dead __attribute__((__no_return__))
  179. // HAVE_PROGNAME: Absent in musl
  180. #define HAVE_ASPRINTF
  181. // HAVE_PLEDGE: Absent in musl
  182. #define HAVE_REALLOCARRAY
  183. #define HAVE_STRLCPY
  184. EOF
  185. sed -i -e '/CFLAGS/s; -g;;' bzip2-*/Makefile bzip2-*/Makefile-libbz2_so || die "Failed patching out `-g` in /bzip2-*/Makefile*"
  186. sed -i -e 's;bzip2-shared;bzip2;' bzip2-*/Makefile-libbz2_so || die "Failed patching /bzip2-*/Makefile-libbz2_so"
  187. 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"
  188. rm bzip2-*/sample* || die "Failed removing sample bzip2 files"
  189. patch -p0 <"${WORKDIR}/zlib-1.3.1_no_staticlib.patch"
  190. patch -p0 <"${WORKDIR}/zlib-1.3.1-use-LDFLAGS-in-configure.patch"
  191. patch -p0 <"${WORKDIR}/zlib-1.3.1-use-LDFLAGS-in-configure_no_gcc.patch"
  192. rm zlib-*/doc/crc-doc.1.0.pdf || die "Failed removing zlib crc-doc.1.0.pdf"
  193. rm zlib-*/crc32.h || die "Failed removing autogenerated zlib-*/crc32.h"
  194. rm -r zlib-*/examples/ || die "Failed removing zlib-*/examples/"
  195. rm -r zlib-*/contrib/ || die "Failed removing zlib-*/contrib/"
  196. sed -i 's;^clean: minizip-clean;clean:;' zlib-*/Makefile.in || die "Failed disabling minizip cleanup"
  197. sed -i \
  198. -e 's;INSTALL=.*;INSTALL=install;' \
  199. -e 's;PREFIX=.*;PREFIX=/usr;;' \
  200. -e 's;STRIP=.*;STRIP=true;' \
  201. -e 's;#?AR=.*;AR ?= ar;' \
  202. -e 's;RANLIB=.*;RANLIB ?= ranlib;' \
  203. -e 's;YACC=.*;YACC ?= yacc;' \
  204. heirloom-devtools-*/mk.config \
  205. || die "Failed configuring heirloom-devtools"
  206. # pdpmake-1.4.3+ consider them invalid "macros"
  207. sed -i \
  208. -e '/^\.c\.o:/s/;/\n\t/' \
  209. heirloom-devtools-*/lex/Makefile.mk heirloom-devtools-*/m4/Makefile.mk \
  210. || die "Failed patching .c.o: inferred targets in heirloom-devtools"
  211. # Only pick what we need from heirloom
  212. mkdir -p heirloom
  213. cd heirloom-0*/
  214. patch -p1 <"${WORKDIR}/heirloom-no_alloca.patch"
  215. mv \
  216. makefile Makefile.mk build \
  217. libcommon diff tar sort comm find stty \
  218. ../heirloom/ || die "Failed copying heirloom files"
  219. cd -
  220. rm -r heirloom-0*/ || die "Failed removing old heirloom unpacked directory"
  221. # - maninst Fails to create parent dir, just noop it, we don't have man anyway
  222. # -e 's;MANINST =.*;MANINST = $(SHELL) ../build/maninst.sh;' \
  223. # - Turns out tcc comes with an ar(1)
  224. # -e 's;LCOMMON =.*;LCOMMON = ../libcommon/*.c;' \
  225. sed -i \
  226. -e 's;DEFBIN =.*;DEFBIN = /bin;' \
  227. -e 's;DEFSBIN =.*;DEFSBIN = /bin;' \
  228. -e 's;STRIP =.*;STRIP = true;' \
  229. -e 's;SPELLHIST =.*;SPELLHIST = /dev/null;' \
  230. -e 's;UCBINST =.*;UCBINST = install;' \
  231. -e 's;MANINST =.*;MANINST = true;' \
  232. heirloom/build/mk.config \
  233. || die "Failed configuring heirloom (toolchest)"
  234. sed -i \
  235. -e 's;__GLIBC__;__linux__;' \
  236. -e '/#define getdents/s;^;//;' \
  237. heirloom/libcommon/getdir.c || die "Failed fixing heirloom libcommon for musl"
  238. sed -i \
  239. -e 's;#ifdef _AIX;#if defined(_AIX) || defined(__linux__);' \
  240. -e '/static int utf8/avoid writerr(struct oblok *op, int count, int written) {}' \
  241. heirloom/tar/tar.c || die "Failed fixing heirloom tar for musl"
  242. sed -i \
  243. -e 's;#ifdef _AIX;#if defined(_AIX) || defined(__linux__);' \
  244. heirloom/find/find.c || die "Failed fixing heirloom find for musl"
  245. sed -i -e 's;getdir.o regexpr.o gmatch.o;getdir.o gmatch.o;' heirloom/libcommon/Makefile.mk || die
  246. rm -r pigz-*/zopfli || die
  247. rm -r heirloom-devtools-*/make heirloom-devtools-*/sccs || die
  248. rm -r awk-*/testdir || die
  249. cd "${out_base}/"
  250. deblob -n -j "${WORKDIR}"/make-root-deblob.json || die
  251. if command -v jq >/dev/null; then
  252. jq --raw-output0 '.[].path' "${WORKDIR}"/make-root-deblob.json \
  253. | xargs -0 du -bach \
  254. | sort -h
  255. elif command -v qjs >/dev/null; then
  256. <"${WORKDIR}"/make-root-deblob.json \
  257. qjs --std \
  258. -e 'let deblob_raw = std.in.readAsString(); let deblob = JSON.parse(deblob_raw); deblob.forEach((e) => {std.printf("%s\0", e.path);})' \
  259. | xargs -0 du -bach \
  260. | sort -h
  261. else
  262. echo "make-root: Command 'jq' and 'qjs' not found. Not going to list sizes of blobs" 1>&2
  263. fi
  264. cd "${WORKDIR}"