mkinitramfs (3043B)
1 #!/bin/sh 2 # mkinitramfs [-m modules-file] ROOTFS KVER OUT: a cpio of the few tools init needs, the modules named in the 3 # modules file with what they depend on, and init itself. the names after a [zfs] line are loaded only for root=ZFS=, 4 # and zpool goes in when the root has it. 5 set -e 6 here=$(cd "$(dirname "$0")" && pwd) 7 while getopts m: o; do case $o in m) list=$OPTARG;; *) exit 2;; esac; done 8 shift $((OPTIND - 1)) 9 [ $# = 3 ] || { echo "usage: mkinitramfs [-m modules] ROOTFS KVER OUT" >&2; exit 2; } 10 root=$1 kver=$2 out=$3 11 tmp=$(mktemp -d) 12 trap 'rm -rf "$tmp"' EXIT 13 mkdir -p "$tmp"/bin "$tmp"/dev "$tmp"/proc "$tmp"/sys "$tmp"/run "$tmp"/mnt "$tmp"/lib/modules 14 rd=$(command -v llvm-readelf || command -v readelf) || { echo "mkinitramfs: no readelf" >&2; exit 1; } 15 # a path in the root with its links followed inside the root, since a link there may point at /bin/busybox 16 inroot() { 17 f=$1 18 while [ -L "$f" ]; do 19 l=$(readlink "$f") 20 case $l in /*) f=$root$l;; *) f=${f%/*}/$l;; esac 21 done 22 echo "$f" 23 } 24 # a readelf that does not run copies no libraries, and an init that cannot mount /proc 25 "$rd" -h "$(inroot "$root/bin/busybox")" >/dev/null 2>&1 || { echo "mkinitramfs: $rd does not work" >&2; exit 1; } 26 # copy what a binary links against: the musl loader and every NEEDED library found in the root 27 needed() { 28 $rd -d "$1" | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p' | while read -r l; do 29 [ -e "$tmp/usr/lib/$l" ] && continue 30 for d in usr/lib lib; do 31 [ -e "$root/$d/$l" ] || [ -L "$root/$d/$l" ] || continue 32 mkdir -p "$tmp/usr/lib" 33 cp "$(inroot "$root/$d/$l")" "$tmp/usr/lib/$l" 34 needed "$(inroot "$root/$d/$l")" 35 break 36 done 37 done 38 } 39 mkdir -p "$tmp/lib" "$tmp/usr/lib" 40 for l in "$root"/lib/ld-musl-*.so.1; do cp "$(inroot "$l")" "$tmp/lib/${l##*/}"; done 41 for t in sh mount umount switch_root insmod mkdir sleep blkid; do 42 f= 43 for d in usr/bin bin usr/sbin sbin; do 44 [ -e "$root/$d/$t" ] || [ -L "$root/$d/$t" ] || continue 45 f=$(inroot "$root/$d/$t") 46 [ -x "$f" ] && break 47 f= 48 done 49 [ -n "$f" ] || { echo "mkinitramfs: no $t in $root" >&2; exit 1; } 50 cp "$f" "$tmp/bin/$t" 51 needed "$f" 52 done 53 # modules FILE: copies the modules named on stdin with what they depend on and lists their paths in FILE 54 modules() { 55 while read -r m; do 56 [ -n "$m" ] || continue 57 modprobe -d "$root" -S "$kver" --show-depends "$m" 2>/dev/null | sed -n 's/^insmod //p' 58 done | awk '!seen[$0]++' | while read -r ko; do 59 ko=${ko#$root} 60 mkdir -p "$tmp$(dirname "$ko")" 61 cp "$root$ko" "$tmp$ko" 62 echo "$ko" >> "$1" 63 done 64 } 65 : > "$tmp/modules" 66 : > "$tmp/modules.zfs" 67 if [ -n "$list" ]; then 68 grep -v '^#' "$list" | sed -n '/^\[zfs\]/q;p' | modules "$tmp/modules" 69 sed -n '/^\[zfs\]/,$p' "$list" | grep -v '^#\|^\[' | modules "$tmp/modules.zfs" 70 fi 71 for d in usr/sbin sbin usr/bin bin; do 72 [ -x "$root/$d/zpool" ] || continue 73 cp "$(inroot "$root/$d/zpool")" "$tmp/bin/zpool" 74 needed "$root/$d/zpool" 75 break 76 done 77 cp "$here/init" "$tmp/init" 78 chmod 755 "$tmp/init" 79 (cd "$tmp" && find . | bsdcpio -o -H newc 2>/dev/null) | zstd -19 -q -o "$out" -f