Prepare automatically FS.sqfs unless it existsa already.
[rrq/rescue-boot.git] / minbase-strap.sh
index 852e9cbc382b61a25d5619ebf97601ddfe6fd394..82cc91a4ab8ec1b218296dd1ee043486f8337612 100755 (executable)
@@ -30,6 +30,11 @@ if [ -d "$FS" ] ; then
     [ "$REPLY" = 1 ] && DEBSTRAP=false
 fi
 
+stupid_links() {
+    mkdir -p $1/usr
+    for d in bin sbin lib ; do mkdir $1/usr/$d ; ln -s usr/$d $1/$d ; done
+}
+
 if $DEBSTRAP ; then
     ## Optionally add the stupid-links
     read -n 1 -p "prime $FS with stupid-links? [Yn]" x
@@ -37,13 +42,7 @@ if $DEBSTRAP ; then
 
     rm -rf "$FS" # remove if existin
 
-    if $USRMERGE ; then
-       mkdir -p "$FS"/usr
-       for d in bin sbin lib ; do
-           mkdir "$FS"/usr/$d
-           ln -s usr/$d "$FS"/$d
-       done
-    fi
+    $USRMERGE && stupid_links "$FS"
 
     ## bootstrap a filesystem, with exclusions
     echo "http_proxy=$http_proxy debootstrap ..."
@@ -56,8 +55,10 @@ if $DEBSTRAP ; then
                    sed 's/\s.*//' | sort -h ) )
     echo "** Please select kernel **"
     select KERNEL in "${KERNELS[@]}" ; do [ -n "$KERNEL" ] && break ; done
+    VERSION=${KERNEL#linux-image-}
+    VERSION=${VERSION%-unsigned}
     chroot "$FS" apt-get install -y $KERNEL
-    chroot "$FS" depmod -a ${KERNEL#linux-image-}
+    chroot "$FS" depmod -a $VERSION
 
     chroot "$FS" apt-get install -y busybox-static debootstrap
     touch "$FS"/usr/bin{linuxrc,init} # block these
@@ -65,15 +66,6 @@ if $DEBSTRAP ; then
 
 fi # End of $DEBSTRAP actions
 
-## (re)install /init
-echo "** Please select /init template"
-select INIT in none $(echo init*.template) ; do [ -n "$INIT" ] && break ; done
-if [ "$INIT" != none ] ; then
-    cp $INIT "$FS"/init
-    cp message.txt "$FS"/
-    chmod a+x "$FS"/init
-fi
-
 ## Pick boot kernel, if there are many
 LINUXES=( $(cd "$FS"/boot ; ls vmlinuz* 2>/dev/null) )
 if [ ${#LINUXES[@]} -gt 1 ] ; then
@@ -92,49 +84,87 @@ fi
 TMP=$(mktemp -d XXXX)
 trap "rm -r $TMP" 0 2 15
 
-( cd "$FS" && find -printf '%P\n' | cpio -o -H newc ) | \
-    gzip > $TMP/initrd.gz
-cp $LINUX $TMP/vmlinuz
+mkdir -p $TMP/prep/{proc,dev,tmp,sys,mnt,usr}
+stupid_links $TMP/prep
 
-#============================================================
-# Add syslinux boot equipment for bios boot under $TMP/syslinux
-# (also includes the syslinux image mastering further below)
-echo "Please select boot console options"
+# copy FS tree(s) in full into the TMP/prep tree
+copyFStree() {
+    local D
+    for D in $* ; do
+       tar cf - -C "$FS" $D \
+           | tar -xf - --skip-old-files --keep-directory-symlink -C $TMP/prep
+    done
+}
+
+# Copy FS binary(s) with all its libraries into the TMP/prep tree
+copyFSbin() {
+    local B BIN
+    for B in $* ; do
+       BIN="$(chroot "$FS" which "$B")"
+       tar cf - --dereference -C "$FS" ${BIN#/} \
+           $(chroot "$FS" ldd "$BIN" | grep -oE '/[^ ]*'|sed 's|/||')\
+           | tar -xf - --keep-directory-symlink -C $TMP/prep
+    done
+}
+
+INITRD="$FS"/boot/initrd.img${LINUX#*/vmlinuz}
+if [ -r "$INITRD" ] ; then
+    #select X in my FS ; do [ -n "$X" ] && break ; done
+    X=FS
+else
+    X=my
+fi
+case "$X" in
+    FS)
+       copyFStree lib/modules
+       zstdcat $INITRD | ( cd $TMP/prep && cpio -i )
+       VERSION=( $TMP/prep/lib/modules/* )
+       for V in ${VERSION[@]} ; do
+           depmod -a -b $TMP/prep ${V#$TMP/prep/lib/modules/}
+       done
+       ;;
+    my)
+       copyFStree lib/modules lib/udev
+       copyFSbin busybox sh cttyhack setsid mount mkdir
+       copyFSbin udevd udevadm modprobe modinfo kmod blkid
+       ;;
+esac
 
-CONOPTS=( none ttyS0,115200 )
-select CON in "${CONOPTS[@]}" ; do [ -n "$CON" ] && break ; done
-case "$CON" in none) CON= ;; *) CON="console=$CON" ;; esac
+## (re)install /init
+mv $TMP/prep/init{,.FS}
+echo "** Please select /init template"
+select INIT in none $(echo init*.template) ; do [ -n "$INIT" ] && break ; done
+if [ "$INIT" != none ] ; then
+    cp $INIT $TMP/prep/init
+    chmod a+x $TMP/prep/init
+fi
 
-mkdir -p $TMP/syslinux
-cp -t $TMP/syslinux /usr/lib/syslinux/modules/bios/*
-cat <<EOF > $TMP/syslinux/syslinux.cfg
+# Populate $TMP with /initrd.gz. /vmlinuz and /menu.cfg
+( cd "$TMP/prep" && find -printf '%P\n' | cpio -o -H newc ) | \
+    gzip > $TMP/initrd.gz
+cp $LINUX $TMP/vmlinuz
+rm -r $TMP/prep
+
+# Common syslinux menu
+CON="console=ttyS0,115200"
+cat <<EOF > $TMP/menu.cfg
 default menu.c32
-label linux
+label rescue (serial)
+    kernel /vmlinuz
+    append initrd=/initrd.gz init=/init $CON rescue
+label rescue2 (vt1)
     kernel /vmlinuz
-    append initrd=/initrd.gz init=/init console=ttyS0,115200 
+    append initrd=/initrd.gz init=/init rescue
+label linux2 (sda2)
+    kernel /vmlinuz
+    append initrd=/initrd.gz root=/dev/sda2 rootfstype=squashfs $CON
+label linux (sdb1)
+    kernel /vmlinuz
+    append initrd=/initrd.gz root=/dev/sdb1 $CON
 EOF
 
-#============================================================
-[ -e "$IMG" ] && read -p "** Will overwrite $IMG (or ^C here and now)" x
-rm -f "$IMG"
-
-# Estimate the required disk image size in Mib
-DUM=$(( $(du -sB1 $TMP | sed 's/\s.*//') / 1048576 + 3 ))
+. ./minbase-strap-bios.sh
+. ./minbase-strap-uefi.sh
+. ./minbase-strap-mkimg.sh
 
-# Create the image, with the partition marked as EFI parition and
-# bootable; acutal format is FAT{12,16,32}
-dd if=/dev/zero of="$IMG" bs=${DUM}M count=0 seek=1 status=none
-cat <<EOF | sfdisk -q "$IMG"
-label: dos
-
-- - 0xef *
-EOF
-# First sector is 2048 by default, which is byte address 1048576
-mkfs.fat --offset 2048 "$IMG" >/dev/null
-mcopy -i "$IMG@@1048576" -s $TMP/* ::/
-[ -z "$SUDO_USER" ] || chown $SUDO_USER: "$IMG"
-
-#============================================================
-# Final syslinux bios boot image mastering
-dd if=/usr/lib/SYSLINUX/mbr.bin of="$IMG" bs=440 conv=notrunc status=none
-syslinux -i -d syslinux -t 1048576 "$IMG"
+for E in $ENDING ; do $E ; done