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"
18 PKGFILE=${REPO//\//_}_${SUITE}_${SECTION}_binary-${ARCH}_Packages
19 if [ ! -r $PKGFILE ] ; then
20 wget -O - http://$REPO/dists/$SUITE/$SECTION/binary-$ARCH/Packages.xz | \
24 # Reduce that Packages file into two maps for finding filename and depends
25 echo "# Creating mapdepends.txt and mapfile.txt"
27 BEGIN { print "###" > "mapdepends.txt"; print "###" > "mapfile.txt"; }
28 $1=="Package:" {P=$2; next}
29 $1=="Depends:" {print P,$2 >> "mapdepends.txt";next }
30 $1=="Filename:" {print P,$2 >> "mapfile.txt";next }
33 # Function to find the filename (or an map file) for a given package
35 awk -v P="$1" '$1==P {$1="" ;print; exit}' ${2-mapfile.txt} | \
39 # Function to download a deb file and return its name
41 local F="$(maplookup $1 mapfile.txt)"
42 if [ ! -e "${F##*/}" ] ; then
43 wget "http://$REPO/$F" || return 1
48 # Function to extract from a deb without executing and pre/post scripts
49 # $1 = rootfs $2 = package
51 ar p $2 data.tar.xz | tar xJf - -C $1
54 # Deteremine which kernel to use; this is
55 echo -n "# Determining kernel: "
56 KERNEL="$(maplookup linux-image-amd64 mapdepends.txt | \
57 sed 's/.*\(linux-image[^ ]*\).*/\1/')"
60 ### Step 2. Create and populate the initrd, and packit up.
61 # The initrd contains only a few kernel modules for coping with a
62 # later pivoting onto a "full" filesystem.
64 echo "# Create initrd filesystem"
67 echo "# Install busybox, and fluff it up"
69 dpkg --log=dpkg.log --root=initrd -i $(debfile busybox-static)
70 for L in $(initrd/bin/busybox --listfull) ; do
71 mkdir -p $(dirname initrd/$L)
74 usr/*) ln -s ../../bin/busybox initrd/$L ;;
75 sbin/*) ln -s ../bin/busybox initrd/$L ;;
76 bin/*) ln -s busybox initrd/$L ;;
77 linuxrc) ln -s bin/busybox initrd/$L ;;
81 echo "# Extract the kernel package ($KERNEL)"
82 echo "# .. and syslinux stuff if needed"
83 if [ ! -d kernel ] ; then
85 debextract kernel $(debfile $KERNEL)
86 debextract kernel $(debfile syslinux)
87 debextract kernel $(debfile syslinux-common)
88 debextract kernel $(debfile syslinux-efi)
89 debextract kernel $(debfile syslinux-utils)
92 echo "# Include some kernel modules in the initrd"
95 scsi_common scsi_mod libata ata_piix ata_generic cdrom sr_mod
96 crc32-pclmul crct10dif_common crc-t10dif crc64 crc64-rocksoft
97 t10-pi sd_mod sg nls_cp437 nls_ascii fat vfat ext4 isofs
105 for m in ${MODULES[@]} ; do
106 km=$(find kernel/lib/modules -name $m.ko)
107 if [ -z "$km" ] ; then
108 echo "Missing module $m"
111 im=initrd/${km#kernel/}
113 mkdir -p $(dirname $im)
116 V=${KERNEL#linux-image-}
117 mkdir -p initrd/boot initrd/lib/modules/$V
118 cp kernel/boot/System.map-$V initrd/
119 cp kernel/lib/modules/$V/modules.order initrd/lib/modules/$V/
120 cp kernel/lib/modules/$V/modules.builtin initrd/lib/modules/$V/
121 depmod -F initrd/System.map-$V -b initrd $V $MOODLES
123 echo "# setup a scripted init. The kernel runs this via the #! interpreter"
124 rm -f initrd/sbin/init # just in case
125 cat <<EOF > initrd/init
129 echo "Hi there, tiniest lover!"
132 mount -t proc proc /proc
133 mount -t devtmpfs devtmpfs /dev
135 mount -t devpts devpts /dev/pts
137 mount -t sysfs sysfs /sys
138 $(for m in ${MODULES[@]} ; do echo modprobe $m ; done)
141 chmod a+x initrd/init
143 echo "# Now pack up that initrd as initrd.gz"
144 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
146 ### Step 3. create a 32 Mb fat filesystem with bios and UEFI boot
148 dd if=/dev/zero of=bootimage.raw bs=32M count=1
150 # Prepare a dos partition table with a first partition marked as EFI
151 sfdisk bootimage.raw <<EOF
156 # Add a fat filesystem at 2048 61440
157 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
158 IMG="-i bootimage.raw@@$((2048*512))"
160 # Add an ext2 filesystem at offset 61440*512
161 # Copy initrd.gz and kernel into the fat filesystem root
162 mke2fs -E offset=$((61440*512)) -F bootimage.raw
164 mcopy $IMG initrd.gz ::
165 mcopy $IMG kernel/boot/vm* ::/vmlinuz
166 mcopy $IMG bootmenu.cfg ::/
167 mcopy $IMG splash.png ::/
169 echo "# Set up legacy boot"
170 cat <<EOF > syslinux-legacy.cfg
171 path /boot/syslinux/bios
172 include /bootmenu.cfg
176 mmd $IMG ::/boot/syslinux
177 mmd $IMG ::/boot/syslinux/bios
179 kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
180 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
182 echo "# Set up UEFI boot"
183 cat <<EOF > syslinux-uefi.cfg
185 include /bootmenu.cfg
190 mmd $IMG ::/EFI/BOOT/efi64
191 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
192 ::/EFI/BOOT/bootx64.efi
194 kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
195 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslx64.cfg
197 syslinux --install --offset=${IMG#*@@} bootimage.raw
198 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
199 if=kernel/usr/lib/syslinux/mbr/mbr.bin