capture
[rrq/rescue-boot.git] / minbase-strap-initrd.sh
1 # Include snippet for minbase-strap.sh
2
3 #============================================================
4 # Prepare a temporary directory tree with a boot kernel and a portion
5 # of $FS packed up into an initrd.gz
6 TMP=$(mktemp -d XXXX)
7 trap "rm -r $TMP" 0 2 15
8
9 mkdir -p $TMP/prep/{proc,dev,tmp,sys,mnt,usr}
10 stupid_links $TMP/prep
11
12 # copy FS tree(s) in full into the TMP/prep tree
13 copyFStree() {
14     local D
15     for D in $* ; do
16         tar cf - -C "$FS" $D \
17             | tar -xf - --skip-old-files --keep-directory-symlink -C $TMP/prep
18     done
19 }
20
21 # Copy FS binary(s) with all its libraries into the TMP/prep tree
22 copyFSbin() {
23     local B BIN
24     for B in $* ; do
25         BIN="$(chroot "$FS" which "$B")"
26         tar cf - --dereference -C "$FS" ${BIN#/} \
27             $(chroot "$FS" ldd "$BIN" | grep -oE '/[^ ]*'|sed 's|/||')\
28             | tar -xf - --keep-directory-symlink -C $TMP/prep
29     done
30 }
31
32 # Grab the "installed" initrd
33 INITRD="$FS"/boot/initrd.img${LINUX#*/vmlinuz}
34 zstdcat $INITRD | ( cd $TMP/prep && cpio -i )
35
36 # Prepare kernel loading
37 for V in $TMP/prep/lib/modules/* ; do
38     depmod -a -b $TMP/prep ${V#$TMP/prep/lib/modules/}
39 done
40
41 ## Change to use customm init script
42 mv $TMP/prep/init{,.FS}
43 echo "** Please select /init template"
44 select INIT in init.FS $(echo init*.template) ; do 
45     if [ -n "$INIT" ] ; then
46         cp $INIT $TMP/prep/init
47         chmod a+x $TMP/prep/init
48         break
49     fi
50 done
51
52 # Populate $TMP with /initrd.gz. /vmlinuz and /menu.cfg
53 ( cd "$TMP/prep" && find -printf '%P\n' | cpio -o -H newc ) | \
54     zstd > $TMP/initrd.img
55 cp $LINUX $TMP/vmlinuz
56 rm -r $TMP/prep