Using save/ to keep downloads and partial progress
[rrq/tiniest.git] / mkit.sh
diff --git a/mkit.sh b/mkit.sh
index 4012566984b7ecc7a311c656b340468ab9f84d9b..2fb62d0e5e20594bf7dff267ea2e4f2bbae05d12 100755 (executable)
--- a/mkit.sh
+++ b/mkit.sh
 
 set -e
 
-echo '### Step 1. Download original Packages file from the source'
-REPO="deb.devuan.org/merged"
-SUITE="daedalus"
-SECTION="main"
+echo '### Step 1. Download original Packages files from sources'
+mkdir -p save
+[ -L save/save ] || ln -s . save/save
+MAPFILE=save/mapfile.txt
+DEPFILE=save/mapdepends.txt
+
+REPOS=(
+    deb.devuan.org/merged_daedalus_main
+    deb.devuan.org/merged_ceres_main
+)
 ARCH="amd64"
 MBR=dos # gpt
 
-PKGFILE=${REPO//\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
-if [ ! -r $PKGFILE ] ; then
-    wget -qO - http://$REPO/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz | \
-       xzcat - > ${PKGFILE}
-fi
+: > $MAPFILE
+: > $DEPFILE
 
-echo '# Reduce Packages file into two maps for finding filename and depends'
-echo "# ..creating mapdepends.txt and mapfile.txt"
-awk '
-BEGIN { print "###" > "mapdepends.txt"; print "###" > "mapfile.txt"; }
+for REPO in ${REPOS[@]} ; do
+    W=( ${REPO//_/ } )
+    SUITE=${W[1]}
+    SECTION=${W[2]}
+    
+    PKGFILE=save/${W[0]/\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
+    if [ ! -r $PKGFILE ] ; then
+       XZSRC="http://${W[0]}/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz"
+       echo "$XZSRC"
+       wget -qO - $XZSRC | xzcat - > ${PKGFILE}
+    fi
+
+    echo '# Reduce Packages file into two maps for filename and depends'
+    echo "# ..creating $MAPFILE and $DEPFILE"
+    awk -v MAP=$MAPFILE -v DEP=$DEPFILE '
+    BEGIN { print "###" >> MAP; print "###" >> DEP; }
 $1=="Package:" {P=$2; next}
-$1=="Pre-Depends:" {print P,$0 >> "mapdepends.txt";next }
-$1=="Depends:" {print P,$0 >> "mapdepends.txt";next }
-$1=="Filename:" {print P,$2 >> "mapfile.txt";next }
+$1=="Pre-Depends:" {print P,$0 >> DEP;next }
+$1=="Depends:" {print P,$0 >> DEP;next }
+$1=="Filename:" {print P,$2 >> MAP;next }
 ' ${PKGFILE}
+done
+
+# Append any additional saved deb files
+for DEB in save/*.deb ; do
+    P="${DEB%%_*}"
+    P="${P#save/}"
+    echo "$P save/$DEB" >> $MAPFILE
+    dpkg-deb -f "$DEB" Depends Pre-Depends | sed "s/^/$P/" >> $DEPFILE
+done
 
-# Function to find the filename (or an map file) for a given package
+# Function to find the filename for a given package in the given mapfile
 maplookup() {
-    awk -v P="$1" '$1==P {$1="" ;print; exit}' ${2-mapfile.txt} | \
-       sed 's/ //'
+    local FN=${3:-tail}
+    echo "maplookup $1 $2" >&2
+    grep "^$1 " $2 >&2
+    grep "^$1 " $2 | $FN -n 1 | sed 's/[^ ]* //'
+}
+
+# Function to clean up a "depends' line
+depclean() {
+    sed 's/^[^:]*: //;s/([^)]*)//g;s/|[^,]*/ /g;s/,/ /g;s/\s\+/ /g'
+}
+
+# Map all dependencies for a list of package
+#
+mapdeps() {
+    local D
+    local B=( )
+    local X=" $* "
+    for D in $* ; do
+       local A=(
+           $(grep "^$D Pre-Depends:" $DEPFILE | head -n 1 | depclean)
+           $(grep "^$D Depends:" $DEPFILE | head -n 1 | depclean)
+       )
+       for P in ${A[@]} ; do
+           [ -z "${X%%* $P *}" ] && continue
+           B=( $B $P )
+           X="$X$P "
+       done
+    done
+    if [ -z "$B" ] ; then
+       echo $X
+    else
+       mapdeps $X
+    fi
 }
 
 # Function to download a deb file and return its name
 debfile() {
-    local F="$(maplookup $1 mapfile.txt)"
-    if [ ! -e "${F##*/}" ] ; then
-       wget -q "http://$REPO/$F" || return 1
+    local FN=${2-tail}
+    local F="$(maplookup $1 $MAPFILE $FN)"
+    local P="${F##*/}"
+    if [ ! -e "save/$P" ] ; then
+       for REPO in ${REPOS[@]} ; do
+           echo "download http://${REPO%%_*}/$F" >&2
+           ( cd save && wget -q "http://${REPO%%_*}/$F" ) && break
+           ls -l save/$P >&2
+       done || return 1
     fi
-    echo "${F##*/}"
+    echo "save/$P"
 }
 
 # Function to extract from a deb without executing and pre/post scripts
@@ -55,7 +116,7 @@ debextract() {
 
 # Deteremine which kernel to use; this is
 echo "# Determine kernel"
-KERNEL="$(maplookup linux-image-amd64 mapdepends.txt | \
+KERNEL="$(maplookup linux-image-amd64 $DEPFILE head | \
                    sed 's/.*\(linux-image[^ ]*\).*/\1/')"
 echo "# ... $KERNEL"
 
@@ -66,11 +127,49 @@ echo '### Step 2. Create and populate the initrd, and packit up.'
 echo "# Create initrd filesystem"
 rm -fr initrd
 
-echo "# Extract busybox, and fluff it up"
-mkdir initrd
-debextract initrd $(debfile busybox-static)
+# Helper function to copy a file or link, given full path
+# $1 = full pathname, $2 = (relative) root path
+copylinks() {
+    echo "copylinks $*" >&2
+    [ -e "initrd/$1" ] && return 0
+    mkdir -p "initrd${1%/*}"
+    if [ -L "$1" ] ; then
+       local L=$(readlink $2$1)
+       ln -s $L initrd$1
+       [ "${L:0:1}" = "/" ] || L="${1%/*}/$L"
+       copylinks $L $2
+    else
+       cp $2$1 initrd/$1
+    fi
+}
+
+# Helper function to copy dynamic binary and its libraries
+# $1 = full pathname, $2 = (relative) root path
+bincp() {
+    echo "bincp $*" >&2
+    [ -e "initrd/$1" ] && return 0
+    copylinks $1 $2
+    objdump -x $2$1 | while read A B ; do
+       [ "$A" == "NEEDED" ] || continue
+       local L="$(find ${2:-/} -name $B)"
+       [ -z "$L" ] && echo "MISSING $B" && exit 1
+       copylinks ${L#$2} $2
+    done
+}
+
+echo '# Include static vtoydump for Ventoy support'
+VTOYDUMP=../ventoy/vtoydump/vtoydump
+if [ -e "$VTOYDUMP" ] ; then
+    mkdir -p initrd/bin
+    cp "$VTOYDUMP" initrd/bin
+fi
+
+echo "# Extract static busybox, and fluff it up for more utilities"
+mkdir -p initrd
+debextract initrd $(debfile busybox-static head)
 for L in $(initrd/bin/busybox --listfull) ; do
     mkdir -p $(dirname initrd/$L)
+    [ -e "initrd/$L" ] && continue
     case "$L" in
        bin/busybox) : ;;
        usr/*) ln -s ../../bin/busybox initrd/$L ;;
@@ -84,7 +183,8 @@ echo "# Extract the kernel package ($KERNEL)"
 echo "# .. and syslinux stuff if needed"
 if [ ! -d kernel ] ; then
     mkdir kernel
-    debextract kernel $(debfile $KERNEL)
+    debextract kernel $(debfile $KERNEL head)
+    debextract kernel $(debfile squashfs-tools head)
     debextract kernel $(debfile syslinux)
     debextract kernel $(debfile syslinux-common)
     debextract kernel $(debfile syslinux-efi)
@@ -101,11 +201,14 @@ MODULES=(
     nls_cp437 nls_ascii fat vfat
     crc32c_generic jbd2 mbcache crc16 ext4
     usb-storage usbcore usb-common xhci-pci xhci-hcd
-    isofs
+    isofs exfat loop
+    efivarfs
     # input
     psmouse evdev
     # network
     e1000
+    # overlay
+    overlay squashfs
 )
 MOODLES=""
 B=$(pwd)
@@ -162,15 +265,8 @@ mke2fs -t ext4 -E $EXT -F bootimage.raw 15M
 
 mcopy $IMG initrd.gz ::
 mcopy $IMG kernel/boot/vm* ::/vmlinuz
-mcopy $IMG bootmenu.cfg ::/
 mcopy $IMG splash.png ::/
 
-echo "# Set up legacy boot"
-cat <<EOF > syslinux-legacy.cfg
-path /boot/syslinux/bios
-include /bootmenu.cfg
-EOF
-
 mmd $IMG ::/boot
 mmd $IMG ::/boot/syslinux
 mmd $IMG ::/boot/syslinux/bios
@@ -179,13 +275,6 @@ mcopy $IMG \
 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
 
 syslinux --install --offset=${IMG#*@@} bootimage.raw
-
-echo "# Set up UEFI boot"
-cat <<EOF > syslinux-uefi.cfg
-path /EFI/BOOT/efi64
-include /bootmenu.cfg
-EOF
-
 mmd $IMG ::/EFI
 mmd $IMG ::/EFI/BOOT
 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
@@ -193,9 +282,8 @@ mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
 mcopy $IMG \
       kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslinux.cfg
-
-#echo "# Set up isolinux boot"
-#mcopy $IMG kernel/usr/lib/ISOLINUX/isolinux.bin ::/
+## Add lua boot script
+mcopy $IMG muffin.lua ::/EFI/BOOT/muffin.lua
 
 case "$MBR" in
     dos) MBRBIN=mbr.bin ;;