3 # A script to prepare the tiniest "linux system". It's a kernel and an
4 # initrd within a FAT with a syslinux boot loader. The initrd contains
5 # a fully expanded busybox and uses /bin/sh as its init.
7 # This script creates and packs that initrd, as well as the whole,
12 echo '### Step 1. Download original Packages files from sources'
14 [ -L save/save ] || ln -s . save/save
15 MAPFILE=save/mapfile.txt
16 DEPFILE=save/mapdepends.txt
19 deb.devuan.org/merged_daedalus_main
20 deb.devuan.org/merged_ceres_main
28 for REPO in ${REPOS[@]} ; do
33 PKGFILE=save/${W[0]/\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
34 if [ ! -r $PKGFILE ] ; then
35 XZSRC="http://${W[0]}/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz"
37 wget -qO - $XZSRC | xzcat - > ${PKGFILE}
40 echo '# Reduce Packages file into two maps for filename and depends'
41 echo "# ..creating $MAPFILE and $DEPFILE"
42 awk -v MAP=$MAPFILE -v DEP=$DEPFILE '
43 BEGIN { print "###" >> MAP; print "###" >> DEP; }
44 $1=="Package:" {P=$2; next}
45 $1=="Pre-Depends:" {print P,$0 >> DEP;next }
46 $1=="Depends:" {print P,$0 >> DEP;next }
47 $1=="Filename:" {print P,$2 >> MAP;next }
51 # Append any additional saved deb files
52 for DEB in save/*.deb ; do
55 echo "$P save/$DEB" >> $MAPFILE
56 dpkg-deb -f "$DEB" Depends Pre-Depends | sed "s/^/$P/" >> $DEPFILE
59 # Function to find the filename for a given package in the given mapfile
62 echo "maplookup $1 $2" >&2
64 grep "^$1 " $2 | $FN -n 1 | sed 's/[^ ]* //'
67 # Function to clean up a "depends' line
69 sed 's/^[^:]*: //;s/([^)]*)//g;s/|[^,]*/ /g;s/,/ /g;s/\s\+/ /g'
72 # Map all dependencies for a list of package
80 $(grep "^$D Pre-Depends:" $DEPFILE | head -n 1 | depclean)
81 $(grep "^$D Depends:" $DEPFILE | head -n 1 | depclean)
84 [ -z "${X%%* $P *}" ] && continue
96 # Function to download a deb file and return its name
99 local F="$(maplookup $1 $MAPFILE $FN)"
101 if [ ! -e "save/$P" ] ; then
102 for REPO in ${REPOS[@]} ; do
103 echo "download http://${REPO%%_*}/$F" >&2
104 ( cd save && wget -q "http://${REPO%%_*}/$F" ) && break
111 # Function to extract from a deb without executing and pre/post scripts
112 # $1 = rootfs $2 = package
114 ar p $2 data.tar.xz | tar xJf - -C $1
117 # Deteremine which kernel to use; this is
118 echo "# Determine kernel"
119 KERNEL="$(maplookup linux-image-amd64 $DEPFILE head | \
120 sed 's/.*\(linux-image[^ ]*\).*/\1/')"
123 echo '### Step 2. Create and populate the initrd, and packit up.'
124 # The initrd contains only a few kernel modules for coping with a
125 # later pivoting onto a "full" filesystem.
127 echo "# Create initrd filesystem"
130 # Helper function to copy a file or link, given full path
131 # $1 = full pathname, $2 = (relative) root path
133 echo "copylinks $*" >&2
134 [ -e "initrd/$1" ] && return 0
135 mkdir -p "initrd${1%/*}"
136 if [ -L "$1" ] ; then
137 local L=$(readlink $2$1)
139 [ "${L:0:1}" = "/" ] || L="${1%/*}/$L"
146 # Helper function to copy dynamic binary and its libraries
147 # $1 = full pathname, $2 = (relative) root path
150 [ -e "initrd/$1" ] && return 0
152 objdump -x $2$1 | while read A B ; do
153 [ "$A" == "NEEDED" ] || continue
154 local L="$(find ${2:-/} -name $B)"
155 [ -z "$L" ] && echo "MISSING $B" && exit 1
160 echo '# Include static vtoydump for Ventoy support'
161 VTOYDUMP=../ventoy/vtoydump/vtoydump
162 if [ -e "$VTOYDUMP" ] ; then
164 cp "$VTOYDUMP" initrd/bin
167 echo "# Extract static busybox, and fluff it up for more utilities"
169 debextract initrd $(debfile busybox-static head)
170 for L in $(initrd/bin/busybox --listfull) ; do
171 mkdir -p $(dirname initrd/$L)
172 [ -e "initrd/$L" ] && continue
175 usr/*) ln -s ../../bin/busybox initrd/$L ;;
176 sbin/*) ln -s ../bin/busybox initrd/$L ;;
177 bin/*) ln -s busybox initrd/$L ;;
178 linuxrc) ln -s bin/busybox initrd/$L ;;
182 echo "# Extract the kernel package ($KERNEL)"
183 echo "# .. and syslinux stuff if needed"
184 if [ ! -d kernel ] ; then
186 debextract kernel $(debfile $KERNEL head)
187 debextract kernel $(debfile squashfs-tools head)
188 debextract kernel $(debfile syslinux)
189 debextract kernel $(debfile syslinux-common)
190 debextract kernel $(debfile syslinux-efi)
191 debextract kernel $(debfile syslinux-utils)
192 debextract kernel $(debfile isolinux)
195 echo "# Include some kernel modules in the initrd"
198 scsi_common scsi_mod libata ata_piix ata_generic cdrom sr_mod
199 crc32-pclmul crct10dif_common crc-t10dif crc64 crc64-rocksoft
201 nls_cp437 nls_ascii fat vfat
202 crc32c_generic jbd2 mbcache crc16 ext4
203 usb-storage usbcore usb-common xhci-pci xhci-hcd
215 for m in ${MODULES[@]} ; do
216 km=$(find kernel/lib/modules -name $m.ko)
217 if [ -z "$km" ] ; then
218 echo "Missing module $m"
221 im=initrd/${km#kernel/}
223 mkdir -p $(dirname $im)
226 V=${KERNEL#linux-image-}
227 mkdir -p initrd/boot initrd/lib/modules/$V
228 cp kernel/boot/System.map-$V initrd/
229 cp kernel/lib/modules/$V/modules.order initrd/lib/modules/$V/
230 cp kernel/lib/modules/$V/modules.builtin initrd/lib/modules/$V/
231 depmod -F initrd/System.map-$V -b initrd $V $MOODLES
233 echo "# setup a scripted init. The kernel runs this via the #! interpreter"
234 rm -f initrd/sbin/init # just in case
235 cp init/init initrd/init
236 chmod a+x initrd/init
238 echo "# Declare password-less root"
240 echo 'root::0:0:root:/root:/bin/bash' > initrd/etc/passwd
242 echo "# Now pack up that initrd as initrd.gz"
243 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
245 echo '### Step 3. create a 32 Mb fat filesystem with bios and UEFI boot'
247 dd if=/dev/zero of=bootimage.raw bs=32M count=1
249 # Prepare a gpt/dos partition table with a first partition marked as EFI
250 sfdisk bootimage.raw <<EOF
257 # Add a fat filesystem at 2048 61440
258 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
259 IMG="-i bootimage.raw@@$((2048*512))"
261 # Add an ext2 filesystem at offset 34816*512
262 # Copy initrd.gz and kernel into the fat filesystem root
263 EXT=offset=$((34816*512))
264 mke2fs -t ext4 -E $EXT -F bootimage.raw 15M
266 mcopy $IMG initrd.gz ::
267 mcopy $IMG kernel/boot/vm* ::/vmlinuz
268 mcopy $IMG splash.png ::/
271 mmd $IMG ::/boot/syslinux
272 mmd $IMG ::/boot/syslinux/bios
274 kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
275 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
277 syslinux --install --offset=${IMG#*@@} bootimage.raw
280 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
281 ::/EFI/BOOT/bootx64.efi
283 kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
284 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslinux.cfg
285 ## Add lua boot script
286 mcopy $IMG muffin.lua ::/EFI/BOOT/muffin.lua
289 dos) MBRBIN=mbr.bin ;;
292 sfdisk --relocate gpt-bak-std bootimage.raw
293 sfdisk -f --part-attrs bootimage.raw 1 LegacyBIOSBootable
297 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
298 if=kernel/usr/lib/syslinux/mbr/$MBRBIN
300 echo "# populate the extra partition"
301 if [ -d extra ] ; then
303 fuse2fs -o fakeroot -o$EXT bootimage.raw X