Set up for testing lua on uefi
[rrq/tiniest.git] / mkit.sh
diff --git a/mkit.sh b/mkit.sh
index 7637bf30b2c5db705b03c288d9bbacba8a8df210..8ebc8e7ece3108a9cef07b3083f66f745cb8df67 100755 (executable)
--- a/mkit.sh
+++ b/mkit.sh
@@ -9,38 +9,52 @@
 
 set -e
 
-### Step 1. Download original .deb file from the source
-REPO="deb.devuan.org/merged"
-SUITE="daedalus"
-SECTION="main"
+echo '### Step 1. Download original Packages file from the source'
+REPOS=( deb.devuan.org/{merged_daedalus_main,merged_ceres_main} )
 ARCH="amd64"
+MBR=dos # gpt
+
+for REPO in ${REPOS[@]} ; do
+    W=( ${REPO//_/ } )
+    SUITE=${W[1]}
+    SECTION=${W[2]}
+    
+    PKGFILE=${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
 
-PKGFILE=${REPO//\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
-if [ ! -r $PKGFILE ] ; then
-    wget -O - http://$REPO/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz | \
-       xzcat - > ${PKGFILE}
-fi
-
-# Reduce that 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"; }
+    echo '# Reduce Packages file into two maps for filename and depends'
+    echo "# ..creating mapdepends.txt and mapfile.txt"
+    awk '
+    BEGIN { print "###" >> "mapdepends.txt"; print "###" >> "mapfile.txt"; }
 $1=="Package:" {P=$2; next}
-$1=="Depends:" {print P,$2 >> "mapdepends.txt";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 }
 ' ${PKGFILE}
+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 download a deb file and return its name
 debfile() {
-    local F="$(maplookup $1 mapfile.txt)"
+    local FN=${2-tail}
+    local F="$(maplookup $1 mapfile.txt $FN)"
     if [ ! -e "${F##*/}" ] ; then
-       wget "http://$REPO/$F" || return 1
+       for REPO in ${REPOS[@]} ; do
+           echo "download http://${REPO%%_*}/$F" >&2
+           wget -q "http://${REPO%%_*}/$F" && break
+           ls -l $(basename $F) >&2
+       done || return 1
     fi
     echo "${F##*/}"
 }
@@ -52,21 +66,21 @@ debextract() {
 }
 
 # Deteremine which kernel to use; this is
-echo -n "# Determining kernel: "
-KERNEL="$(maplookup linux-image-amd64 mapdepends.txt | \
+echo "# Determine kernel"
+KERNEL="$(maplookup linux-image-amd64 mapdepends.txt head | \
                    sed 's/.*\(linux-image[^ ]*\).*/\1/')"
-echo $KERNEL
+echo "# ... $KERNEL"
 
-### Step 2. Create and populate the initrd, and packit up.
+echo '### Step 2. Create and populate the initrd, and packit up.'
 # The initrd contains only a few kernel modules for coping with a
 # later pivoting onto a "full" filesystem.
 
 echo "# Create initrd filesystem"
 rm -fr initrd
 
-echo "# Install busybox, and fluff it up"
-fakechroot fakeroot \
-dpkg --log=dpkg.log --root=initrd -i $(debfile busybox-static)
+echo "# Extract busybox, and fluff it up"
+mkdir initrd
+debextract initrd $(debfile busybox-static head)
 for L in $(initrd/bin/busybox --listfull) ; do
     mkdir -p $(dirname initrd/$L)
     case "$L" in
@@ -82,11 +96,12 @@ 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 syslinux)
     debextract kernel $(debfile syslinux-common)
     debextract kernel $(debfile syslinux-efi)
     debextract kernel $(debfile syslinux-utils)
+    debextract kernel $(debfile isolinux)
 fi
 
 echo "# Include some kernel modules in the initrd"
@@ -94,7 +109,11 @@ MODULES=(
     # disk
     scsi_common scsi_mod libata ata_piix ata_generic cdrom sr_mod
     crc32-pclmul crct10dif_common crc-t10dif crc64 crc64-rocksoft
-    t10-pi sd_mod sg nls_cp437 nls_ascii fat vfat ext4 isofs
+    t10-pi sd_mod sg
+    nls_cp437 nls_ascii fat vfat
+    crc32c_generic jbd2 mbcache crc16 ext4
+    usb-storage usbcore usb-common xhci-pci xhci-hcd
+    isofs exfat loop
     # input
     psmouse evdev
     # network
@@ -122,33 +141,24 @@ depmod -F initrd/System.map-$V -b initrd $V $MOODLES
 
 echo "# setup a scripted init. The kernel runs this via the #! interpreter"
 rm -f initrd/sbin/init # just in case
-cat <<EOF > initrd/init
-#!/bin/sh
-echo 
-echo 
-echo "Hi there, tiniest lover!"
-
-mkdir /proc
-mount -t proc proc /proc
-mount -t devtmpfs devtmpfs /dev
-mkdir /dev/pts
-mount -t devpts devpts /dev/pts
-mkdir /sys
-mount -t sysfs sysfs /sys
-$(for m in ${MODULES[@]} ; do echo modprobe $m ; done)
-exec /bin/sh
-EOF
+cp init/init initrd/init
 chmod a+x initrd/init
 
+echo "# Declare password-less root"
+mkdir -p initrd/etc
+echo 'root::0:0:root:/root:/bin/bash' > initrd/etc/passwd
+
 echo "# Now pack up that initrd as initrd.gz"
 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
 
-### Step 3. create a 32  Mb fat filesystem with bios and UEFI boot
+echo '### Step 3. create a 32  Mb fat filesystem with bios and UEFI boot'
 rm -f bootimage.raw
 dd if=/dev/zero of=bootimage.raw bs=32M count=1
 
-# Prepare a dos partition table with a first partition marked as EFI
+# Prepare a gpt/dos partition table with a first partition marked as EFI
 sfdisk bootimage.raw <<EOF
+label: $MBR
+
 2048 32767 U *
 - - L
 EOF
@@ -157,21 +167,15 @@ EOF
 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
 IMG="-i bootimage.raw@@$((2048*512))"
 
-# Add an ext2 filesystem at offset 61440*512
+# Add an ext2 filesystem at offset 34816*512
 # Copy initrd.gz and kernel into the fat filesystem root
-mke2fs -E offset=$((61440*512)) -F bootimage.raw
+EXT=offset=$((34816*512))
+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,23 +183,36 @@ mcopy $IMG \
       kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
 
-echo "# Set up UEFI boot"
-cat <<EOF > syslinux-uefi.cfg
-path /EFI/BOOT/efi64
-include /bootmenu.cfg
-EOF
+syslinux --install --offset=${IMG#*@@} bootimage.raw
 
 mmd $IMG ::/EFI
 mmd $IMG ::/EFI/BOOT
-mmd $IMG ::/EFI/BOOT/efi64
 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
       ::/EFI/BOOT/bootx64.efi
 mcopy $IMG \
       kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
-mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslx64.cfg
-
-syslinux --install --offset=${IMG#*@@} bootimage.raw
+mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslinux.cfg
+## Add lua boot script
+mcopy $IMG muffin.lua ::/EFI/BOOT/muffin.lua
+
+case "$MBR" in
+    dos) MBRBIN=mbr.bin ;;
+    gpt)
+       MBRBIN=gptmbr.bin
+       sfdisk --relocate gpt-bak-std bootimage.raw
+       sfdisk -f --part-attrs bootimage.raw 1 LegacyBIOSBootable
+       ;;
+    *) exit 1 ;;
+esac
 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
-   if=kernel/usr/lib/syslinux/mbr/mbr.bin
+   if=kernel/usr/lib/syslinux/mbr/$MBRBIN
+
+echo "# populate the extra partition"
+if [ -d extra ] ; then
+    mkdir -p X
+    fuse2fs -o fakeroot -o$EXT bootimage.raw X
+    rsync -r extra/. X/.
+    umount X
+fi
 
 exit