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
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 cat <<EOF > initrd/init
136 echo "Hi there, tiniest lover!"
139 mount -t proc proc /proc
140 mount -t devtmpfs devtmpfs /dev
142 mount -t devpts devpts /dev/pts
144 mount -t sysfs sysfs /sys
145 $(for m in ${MODULES[@]} ; do echo modprobe $m ; done)
148 chmod a+x initrd/init
150 echo "# Now pack up that initrd as initrd.gz"
151 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
153 ### Step 3. create a 32 Mb fat filesystem with bios and UEFI boot
155 dd if=/dev/zero of=bootimage.raw bs=32M count=1
157 # Prepare a gpt/dos partition table with a first partition marked as EFI
158 sfdisk bootimage.raw <<EOF
165 # Add a fat filesystem at 2048 61440
166 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
167 IMG="-i bootimage.raw@@$((2048*512))"
169 # Add an ext2 filesystem at offset 61440*512
170 # Copy initrd.gz and kernel into the fat filesystem root
171 mke2fs -t ext4 -E offset=$((34816*512)) -F bootimage.raw 15M
173 mcopy $IMG initrd.gz ::
174 mcopy $IMG kernel/boot/vm* ::/vmlinuz
175 mcopy $IMG bootmenu.cfg ::/
176 mcopy $IMG splash.png ::/
178 echo "# Set up legacy boot"
179 cat <<EOF > syslinux-legacy.cfg
180 path /boot/syslinux/bios
181 include /bootmenu.cfg
185 mmd $IMG ::/boot/syslinux
186 mmd $IMG ::/boot/syslinux/bios
188 kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
189 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
191 echo "# Set up UEFI boot"
192 cat <<EOF > syslinux-uefi.cfg
194 include /bootmenu.cfg
199 mmd $IMG ::/EFI/BOOT/efi64
200 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
201 ::/EFI/BOOT/bootx64.efi
203 kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
204 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslx64.cfg
207 syslinux --install --offset=${IMG#*@@} bootimage.raw
210 dos) MBRBIN=mbr.bin ;;
213 sfdisk --relocate gpt-bak-std bootimage.raw
214 sfdisk -f --part-attrs bootimage.raw 1 LegacyBIOSBootable
218 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
219 if=kernel/usr/lib/syslinux/mbr/$MBRBIN