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 ### Step 1. Download original .deb file from the source
13 REPO="deb.devuan.org/merged"
19 PKGFILE=${REPO//\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
20 if [ ! -r $PKGFILE ] ; then
21 wget -O - http://$REPO/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz | \
25 # Reduce that Packages file into two maps for finding filename and depends
26 echo "# Creating mapdepends.txt and mapfile.txt"
28 BEGIN { print "###" > "mapdepends.txt"; print "###" > "mapfile.txt"; }
29 $1=="Package:" {P=$2; next}
30 $1=="Pre-Depends:" {print P,$0 >> "mapdepends.txt";next }
31 $1=="Depends:" {print P,$0 >> "mapdepends.txt";next }
32 $1=="Filename:" {print P,$2 >> "mapfile.txt";next }
35 # Function to find the filename (or an map file) for a given package
37 awk -v P="$1" '$1==P {$1="" ;print; exit}' ${2-mapfile.txt} | \
41 # Function to download a deb file and return its name
43 local F="$(maplookup $1 mapfile.txt)"
44 if [ ! -e "${F##*/}" ] ; then
45 wget "http://$REPO/$F" || return 1
50 # Function to extract from a deb without executing and pre/post scripts
51 # $1 = rootfs $2 = package
53 ar p $2 data.tar.xz | tar xJf - -C $1
56 # Deteremine which kernel to use; this is
57 echo -n "# Determining kernel: "
58 KERNEL="$(maplookup linux-image-amd64 mapdepends.txt | \
59 sed 's/.*\(linux-image[^ ]*\).*/\1/')"
62 ### Step 2. Create and populate the initrd, and packit up.
63 # The initrd contains only a few kernel modules for coping with a
64 # later pivoting onto a "full" filesystem.
66 echo "# Create initrd filesystem"
69 echo "# Extract busybox, and fluff it up"
71 debextract initrd $(debfile busybox-static)
72 for L in $(initrd/bin/busybox --listfull) ; do
73 mkdir -p $(dirname initrd/$L)
76 usr/*) ln -s ../../bin/busybox initrd/$L ;;
77 sbin/*) ln -s ../bin/busybox initrd/$L ;;
78 bin/*) ln -s busybox initrd/$L ;;
79 linuxrc) ln -s bin/busybox initrd/$L ;;
83 echo "# Extract the kernel package ($KERNEL)"
84 echo "# .. and syslinux stuff if needed"
85 if [ ! -d kernel ] ; then
87 debextract kernel $(debfile $KERNEL)
88 debextract kernel $(debfile syslinux)
89 debextract kernel $(debfile syslinux-common)
90 debextract kernel $(debfile syslinux-efi)
91 debextract kernel $(debfile syslinux-utils)
92 debextract kernel $(debfile isolinux)
95 echo "# Include some kernel modules in the initrd"
98 scsi_common scsi_mod libata ata_piix ata_generic cdrom sr_mod
99 crc32-pclmul crct10dif_common crc-t10dif crc64 crc64-rocksoft
101 nls_cp437 nls_ascii fat vfat
102 crc32c_generic jbd2 mbcache crc16 ext4
103 usb-storage usbcore usb-common xhci-pci xhci-hcd
112 for m in ${MODULES[@]} ; do
113 km=$(find kernel/lib/modules -name $m.ko)
114 if [ -z "$km" ] ; then
115 echo "Missing module $m"
118 im=initrd/${km#kernel/}
120 mkdir -p $(dirname $im)
123 V=${KERNEL#linux-image-}
124 mkdir -p initrd/boot initrd/lib/modules/$V
125 cp kernel/boot/System.map-$V initrd/
126 cp kernel/lib/modules/$V/modules.order initrd/lib/modules/$V/
127 cp kernel/lib/modules/$V/modules.builtin initrd/lib/modules/$V/
128 depmod -F initrd/System.map-$V -b initrd $V $MOODLES
130 echo "# setup a scripted init. The kernel runs this via the #! interpreter"
131 rm -f initrd/sbin/init # just in case
132 cp init/init initrd/init
133 chmod a+x initrd/init
135 echo "# Now pack up that initrd as initrd.gz"
136 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
138 ### Step 3. create a 32 Mb fat filesystem with bios and UEFI boot
140 dd if=/dev/zero of=bootimage.raw bs=32M count=1
142 # Prepare a gpt/dos partition table with a first partition marked as EFI
143 sfdisk bootimage.raw <<EOF
150 # Add a fat filesystem at 2048 61440
151 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
152 IMG="-i bootimage.raw@@$((2048*512))"
154 # Add an ext2 filesystem at offset 61440*512
155 # Copy initrd.gz and kernel into the fat filesystem root
156 mke2fs -t ext4 -E offset=$((34816*512)) -F bootimage.raw 15M
158 mcopy $IMG initrd.gz ::
159 mcopy $IMG kernel/boot/vm* ::/vmlinuz
160 mcopy $IMG bootmenu.cfg ::/
161 mcopy $IMG splash.png ::/
163 echo "# Set up legacy boot"
164 cat <<EOF > syslinux-legacy.cfg
165 path /boot/syslinux/bios
166 include /bootmenu.cfg
170 mmd $IMG ::/boot/syslinux
171 mmd $IMG ::/boot/syslinux/bios
173 kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
174 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
176 syslinux --install --offset=${IMG#*@@} bootimage.raw
178 echo "# Set up UEFI boot"
179 cat <<EOF > syslinux-uefi.cfg
181 include /bootmenu.cfg
186 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
187 ::/EFI/BOOT/bootx64.efi
189 kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
190 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslinux.cfg
192 #echo "# Set up isolinux boot"
193 #mcopy $IMG kernel/usr/lib/ISOLINUX/isolinux.bin ::/
196 dos) MBRBIN=mbr.bin ;;
199 sfdisk --relocate gpt-bak-std bootimage.raw
200 sfdisk -f --part-attrs bootimage.raw 1 LegacyBIOSBootable
204 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
205 if=kernel/usr/lib/syslinux/mbr/$MBRBIN