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


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