64e461e3099cf7ec4a1f6de0891e113d7b78a37a
[rrq/tiniest.git] / mkit.sh
1 #!/bin/bash
2 #
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.
6 #
7 # This script creates and packs that initrd, as well as the whole,
8 # boot image. 
9
10 set -e
11
12 ### Step 1. Download original .deb file from the source
13 REPO="deb.devuan.org/merged"
14 SUITE="daedalus"
15 SECTION="main"
16 ARCH="amd64"
17
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 | \
21         xzcat - > ${PKGFILE}
22 fi
23
24 # Reduce that Packages file into two maps for finding filename and depends
25 echo "# Creating mapdepends.txt and mapfile.txt"
26 awk '
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 }
31 ' ${PKGFILE}
32
33 # Function to find the filename (or an map file) for a given package
34 maplookup() {
35     awk -v P="$1" '$1==P {$1="" ;print; exit}' ${2-mapfile.txt} | \
36         sed 's/ //'
37 }
38
39 # Function to download a deb file and return its name
40 debfile() {
41     local F="$(maplookup $1 mapfile.txt)"
42     if [ ! -e "${F##*/}" ] ; then
43         wget "http://$REPO/$F" || return 1
44     fi
45     echo "${F##*/}"
46 }
47
48 # Function to extract from a deb without executing and pre/post scripts
49 # $1 = rootfs $2 = package
50 debextract() {
51     ar p $2 data.tar.xz | tar xJf - -C $1
52 }
53
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/')"
58 echo $KERNEL
59
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.
63
64 echo "# Create initrd filesystem"
65 rm -fr initrd
66
67 echo "# Install busybox, and fluff it up"
68 fakechroot fakeroot \
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)
72     case "$L" in
73         bin/busybox) : ;;
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 ;;
78     esac
79 done
80
81 echo "# Extract the kernel package ($KERNEL)"
82 echo "# .. and syslinux stuff if needed"
83 if [ ! -d kernel ] ; then
84     mkdir kernel
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)
90 fi
91
92 echo "# Include some kernel modules in the initrd"
93 MODULES=(
94     # disk
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
98     nls_cp437 nls_ascii fat vfat
99     crc32c_generic jbd2 mbcache crc16 ext4
100     isofs
101     overlay
102     # input
103     psmouse evdev
104     # network
105     e1000
106 )
107 MOODLES=""
108 B=$(pwd)
109 for m in ${MODULES[@]} ; do
110     km=$(find kernel/lib/modules -name $m.ko)
111     if [ -z "$km" ] ; then
112         echo "Missing module $m"
113         continue
114     fi
115     im=initrd/${km#kernel/}
116     MOODLES+=" $B/$im"
117     mkdir -p $(dirname $im)
118     cp -n $km $im
119 done
120 V=${KERNEL#linux-image-}
121 mkdir -p initrd/boot initrd/lib/modules/$V
122 cp kernel/boot/System.map-$V initrd/
123 cp kernel/lib/modules/$V/modules.order initrd/lib/modules/$V/
124 cp kernel/lib/modules/$V/modules.builtin initrd/lib/modules/$V/
125 depmod -F initrd/System.map-$V -b initrd $V $MOODLES
126
127 echo "# setup a scripted init. The kernel runs this via the #! interpreter"
128 rm -f initrd/sbin/init # just in case
129 cat <<EOF > initrd/init
130 #!/bin/sh
131 echo 
132 echo 
133 echo "Hi there, tiniest lover!"
134
135 mkdir /proc
136 mount -t proc proc /proc
137 mount -t devtmpfs devtmpfs /dev
138 mkdir /dev/pts
139 mount -t devpts devpts /dev/pts
140 mkdir /sys
141 mount -t sysfs sysfs /sys
142 $(for m in ${MODULES[@]} ; do echo modprobe $m ; done)
143 exec /bin/sh
144 EOF
145 chmod a+x initrd/init
146
147 echo "# Now pack up that initrd as initrd.gz"
148 ( cd initrd ; find . | fakeroot cpio -H newc -o | gzip ) >initrd.gz
149
150 ### Step 3. create a 32  Mb fat filesystem with bios and UEFI boot
151 rm -f bootimage.raw
152 dd if=/dev/zero of=bootimage.raw bs=32M count=1
153
154 # Prepare a dos partition table with a first partition marked as EFI
155 sfdisk bootimage.raw <<EOF
156 2048 32767 U *
157 - - L
158 EOF
159
160 # Add a fat filesystem at 2048 61440
161 mkfs.fat -n TINIEST --offset 2048 -F 16 bootimage.raw
162 IMG="-i bootimage.raw@@$((2048*512))"
163
164 # Add an ext2 filesystem at offset 61440*512
165 # Copy initrd.gz and kernel into the fat filesystem root
166 mke2fs -t ext4 -E offset=$((34816*512)) -F bootimage.raw
167
168 mcopy $IMG initrd.gz ::
169 mcopy $IMG kernel/boot/vm* ::/vmlinuz
170 mcopy $IMG bootmenu.cfg ::/
171 mcopy $IMG splash.png ::/
172
173 echo "# Set up legacy boot"
174 cat <<EOF > syslinux-legacy.cfg
175 path /boot/syslinux/bios
176 include /bootmenu.cfg
177 EOF
178
179 mmd $IMG ::/boot
180 mmd $IMG ::/boot/syslinux
181 mmd $IMG ::/boot/syslinux/bios
182 mcopy $IMG \
183       kernel/usr/lib/syslinux/modules/bios/* ::/boot/syslinux/bios
184 mcopy $IMG syslinux-legacy.cfg ::/syslinux.cfg
185
186 echo "# Set up UEFI boot"
187 cat <<EOF > syslinux-uefi.cfg
188 path /EFI/BOOT/efi64
189 include /bootmenu.cfg
190 EOF
191
192 mmd $IMG ::/EFI
193 mmd $IMG ::/EFI/BOOT
194 mmd $IMG ::/EFI/BOOT/efi64
195 mcopy $IMG kernel/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi \
196       ::/EFI/BOOT/bootx64.efi
197 mcopy $IMG \
198       kernel/usr/lib/syslinux/modules/efi64/* ::/EFI/BOOT
199 mcopy $IMG syslinux-uefi.cfg ::/EFI/BOOT/syslx64.cfg
200
201 syslinux --install --offset=${IMG#*@@} bootimage.raw
202 dd conv=notrunc of=bootimage.raw bs=440 count=1 \
203    if=kernel/usr/lib/syslinux/mbr/mbr.bin
204
205 exit