From: Ralph Ronnquist Date: Fri, 9 Jun 2023 13:56:39 +0000 (+1000) Subject: initial X-Git-Tag: good-1~18 X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=4cacdc777c1fa8f8ddae16f3f100de6a3829cab7;p=rrq%2Ftiniest.git initial --- 4cacdc777c1fa8f8ddae16f3f100de6a3829cab7 diff --git a/bootmenu.cfg b/bootmenu.cfg new file mode 100644 index 0000000..e51abba --- /dev/null +++ b/bootmenu.cfg @@ -0,0 +1,14 @@ +default vesamenu.c32 + +menu margin 0 +menu rows 7 +menu background /splash.png +menu title Tiniest + +label linux + kernel /vmlinuz + append initrd=/initrd.gz init=/init console=ttyS0 root=/dev/ram0 + +label other + kernel /vmlinuz + append initrd=/initrd.gz init=/init console=ttyS0 root=/dev/ram0 diff --git a/mkit.sh b/mkit.sh new file mode 100755 index 0000000..710a123 --- /dev/null +++ b/mkit.sh @@ -0,0 +1,201 @@ +#!/bin/bash +# +# A script to prepare the tiniest "linux system". It's a kernel and an +# initrd within a FAT with a syslinux boot loader. The initrd contains +# a fully expanded busybox and uses /bin/sh as its init. +# +# This script creates and packs that initrd, as well as the whole, +# boot image. + +set -e + +### Step 1. Download original .deb file from the source +REPO="deb.devuan.org/merged" +SUITE="daedalus" +SECTION="main" +ARCH="amd64" + +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"; } +$1=="Package:" {P=$2; next} +$1=="Depends:" {print P,$2 >> "mapdepends.txt";next } +$1=="Filename:" {print P,$2 >> "mapfile.txt";next } +' ${PKGFILE} + +# Function to find the filename (or an map file) for a given package +maplookup() { + awk -v P="$1" '$1==P {$1="" ;print; exit}' ${2-mapfile.txt} | \ + sed 's/ //' +} + +# Function to download a deb file and return its name +debfile() { + local F="$(maplookup $1 mapfile.txt)" + if [ ! -e "${F##*/}" ] ; then + wget "http://$REPO/$F" || return 1 + fi + echo "${F##*/}" +} + +# Function to extract from a deb without executing and pre/post scripts +# $1 = rootfs $2 = package +debextract() { + ar p $2 data.tar.xz | tar xJf - -C $1 +} + +# Deteremine which kernel to use; this is +echo -n "# Determining kernel: " +KERNEL="$(maplookup linux-image-amd64 mapdepends.txt | \ + sed 's/.*\(linux-image[^ ]*\).*/\1/')" +echo $KERNEL + +### 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 -r initrd + +echo "# Install busybox, and fluff it up" +fakechroot fakeroot \ +dpkg --log=dpkg.log --root=initrd -i $(debfile busybox-static) +for L in $(initrd/bin/busybox --listfull) ; do + mkdir -p $(dirname initrd/$L) + case "$L" in + bin/busybox) : ;; + usr/*) ln -s ../../bin/busybox initrd/$L ;; + sbin/*) ln -s ../bin/busybox initrd/$L ;; + bin/*) ln -s busybox initrd/$L ;; + linuxrc) ln -s bin/busybox initrd/$L ;; + esac +done + +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 syslinux) + debextract kernel $(debfile syslinux-common) + debextract kernel $(debfile syslinux-efi) + debextract kernel $(debfile syslinux-utils) +fi + +echo "# Include some kernel modules in the initrd" +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 + # input + psmouse evdev + # network + e1000 +) +MOODLES="" +B=$(pwd) +for m in ${MODULES[@]} ; do + km=$(find kernel/lib/modules -name $m.ko) + if [ -z "$km" ] ; then + echo "Missing module $m" + continue + fi + im=initrd/${km#kernel/} + MOODLES+=" $B/$im" + mkdir -p $(dirname $im) + cp -n $km $im +done +V=${KERNEL#linux-image-} +mkdir -p initrd/boot initrd/lib/modules/$V +cp kernel/boot/System.map-$V initrd/ +cp kernel/lib/modules/$V/modules.order initrd/lib/modules/$V/ +cp kernel/lib/modules/$V/modules.builtin initrd/lib/modules/$V/ +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 < 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 +chmod a+x initrd/init + +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 +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 +sfdisk bootimage.raw < syslinux-legacy.cfg +path /boot/syslinux/bios +include /bootmenu.cfg +EOF + +mmd $IMG ::/boot +mmd $IMG ::/boot/syslinux +mmd $IMG ::/boot/syslinux/bios +mcopy $IMG \ + kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios +mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg + +echo "# Set up UEFI boot" +cat < syslinux-uefi.cfg +path /EFI/BOOT/efi64 +include /bootmenu.cfg +EOF + +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 +dd conv=notrunc of=bootimage.raw bs=440 count=1 \ + if=kernel/usr/lib/syslinux/mbr/mbr.bin + +exit diff --git a/splash.png b/splash.png new file mode 100644 index 0000000..0b4daa5 Binary files /dev/null and b/splash.png differ diff --git a/syslinux-legacy.cfg b/syslinux-legacy.cfg new file mode 100644 index 0000000..491c759 --- /dev/null +++ b/syslinux-legacy.cfg @@ -0,0 +1,2 @@ +path /boot/syslinux/bios +include /bootmenu.cfg diff --git a/syslinux-uefi.cfg b/syslinux-uefi.cfg new file mode 100644 index 0000000..ee8bd6c --- /dev/null +++ b/syslinux-uefi.cfg @@ -0,0 +1,2 @@ +path /EFI/BOOT/efi64 +include /bootmenu.cfg diff --git a/syslinux.cfg b/syslinux.cfg new file mode 100644 index 0000000..e7a530c --- /dev/null +++ b/syslinux.cfg @@ -0,0 +1,5 @@ +path /syslinux/bios +default linux auto +label linux +kernel /vmlinuz +append initrd=/initrd.gz init=/init console=ttyS0 root=/dev/ram0 diff --git a/vm.sh b/vm.sh new file mode 100755 index 0000000..74274e6 --- /dev/null +++ b/vm.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# +# QEMU setup for trial3 VM + +if [ -n "$EFI" ] ; then + BOOT="-bios /usr/share/OVMF/OVMF_CODE.fd" +else + BOOT="-boot menu=on" +fi +ARGS=( + -m 2G -M pc,accel=kvm -cpu host + -serial mon:stdio -echr 0x1c + -vga qxl + $BOOT + -drive index=0,id=disk,media=disk,format=raw,file=disk.raw + -drive index=1,id=boot,media=disk,format=raw,file=bootimage.raw +) + +qemu-system-x86_64 ${ARGS[@]}