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


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