handle both noraml and rescue boot
[rrq/rescue-boot.git] / minbase-strap.sh
1 #!/bin/bash
2 #
3 # Utility script to debootstrap a minbase filesystem, with additions
4 # of kernel, busybox and an init script. Then make an initrd.gz from
5 # that and prepare a boot image file with syslinux from that.
6 #
7 if [ $(id -u) -ne 0 ] ; then
8     exec sudo env http_proxy=$http_proxy $0 $*
9     echo "** Abort: must be root" >&2
10     exit 1
11 fi
12 set -e -x
13
14 ARCH="${1-$(dpkg-architecture -q DEB_HOST_ARCH)}"
15 DIST=${2-ceres}
16 IMG=${3-boot-$DIST-$ARCH.img}
17 FS=${4-FS}
18
19 # Prepare or keep FS
20 DEBSTRAP=true
21 USRMERGE=true
22 if [ -d "$FS" ] ; then
23     select x in "keep existing $FS and skip debootstrap" \
24                     "remove existing $FS and debootstrap anew" \
25                     "abort" ; do
26         [ "$x" = abort ] && exit 1
27         [ -z "$x" ] || break
28     done
29     echo "selection: $REPLY"
30     [ "$REPLY" = 1 ] && DEBSTRAP=false
31 fi
32
33 if $DEBSTRAP ; then
34     ## Optionally add the stupid-links
35     read -n 1 -p "prime $FS with stupid-links? [Yn]" x
36     [ "$x" = "n" ] && USRMERGE=false
37
38     rm -rf "$FS" # remove if existin
39
40     if $USRMERGE ; then
41         mkdir -p "$FS"/usr
42         for d in bin sbin lib ; do
43             mkdir "$FS"/usr/$d
44             ln -s usr/$d "$FS"/$d
45         done
46     fi
47
48     ## bootstrap a filesystem, with exclusions
49     echo "http_proxy=$http_proxy debootstrap ..."
50     debootstrap --exclude=logrotate,cron,cron-daemon-common \
51                 --arch=$ARCH $DIST "$FS" http://deb.devuan.org/merged
52     chroot "$FS" apt-get install -y logrotate cron
53
54     ## Select and add a kernel
55     KERNELS=( $(chroot "$FS" apt-cache search linux-image-\* | \
56                     sed 's/\s.*//' | sort -h ) )
57     echo "** Please select kernel **"
58     select KERNEL in "${KERNELS[@]}" ; do [ -n "$KERNEL" ] && break ; done
59     chroot "$FS" apt-get install -y $KERNEL
60     chroot "$FS" depmod -a ${KERNEL#linux-image-}
61
62     chroot "$FS" apt-get install -y busybox-static debootstrap
63     touch "$FS"/usr/bin{linuxrc,init} # block these
64     chroot "$FS" /usr/bin/busybox --install -s /usr/bin
65
66 fi # End of $DEBSTRAP actions
67
68 ## (re)install /init
69 echo "** Please select /init template"
70 select INIT in none $(echo init*.template) ; do [ -n "$INIT" ] && break ; done
71 if [ "$INIT" != none ] ; then
72     cp $INIT "$FS"/init
73     cp message.txt "$FS"/
74     chmod a+x "$FS"/init
75 fi
76
77 ## Pick boot kernel, if there are many
78 LINUXES=( $(cd "$FS"/boot ; ls vmlinuz* 2>/dev/null) )
79 if [ ${#LINUXES[@]} -gt 1 ] ; then
80     echo "** Please select boot kernel"
81     select LINUX in $LINUXES ; do [ -n "$LINUX" ] && break ; done
82 elif [ -n "$LINUXES" ] ; then
83     LINUX="$FS"/boot/${LINUXES[0]}
84 else
85     echo "** Oh No! There is no $FS/boot/vmlinux-* ... bailing out!" >&2
86     exit 1
87 fi
88
89 #============================================================
90 # Prepare a temporary directory tree with a boot kernel and $FS packed
91 # up into an initrd.gz
92 TMP=$(mktemp -d XXXX)
93 trap "rm -r $TMP" 0 2 15
94
95 ( cd "$FS" && find -printf '%P\n' | cpio -o -H newc ) | \
96     gzip > $TMP/initrd.gz
97 cp $LINUX $TMP/vmlinuz
98
99 #============================================================
100 # Add syslinux boot equipment for bios boot under $TMP/syslinux
101 # (also includes the syslinux image mastering further below)
102 echo "Please select boot console options"
103
104 CONOPTS=( none ttyS0,115200 )
105 select CON in "${CONOPTS[@]}" ; do [ -n "$CON" ] && break ; done
106 case "$CON" in none) CON= ;; *) CON="console=$CON" ;; esac
107
108 mkdir -p $TMP/syslinux
109 cp -t $TMP/syslinux /usr/lib/syslinux/modules/bios/*
110 cat <<EOF > $TMP/syslinux/syslinux.cfg
111 default menu.c32
112 label linux
113     kernel /vmlinuz
114     append initrd=/initrd.gz init=/init console=ttyS0,115200 
115 EOF
116
117 #============================================================
118 [ -e "$IMG" ] && read -p "** Will overwrite $IMG (or ^C here and now)" x
119 rm -f "$IMG"
120
121 # Estimate the required disk image size in Mib
122 DUM=$(( $(du -sB1 $TMP | sed 's/\s.*//') / 1048576 + 3 ))
123
124 # Create the image, with the partition marked as EFI parition and
125 # bootable; acutal format is FAT{12,16,32}
126 dd if=/dev/zero of="$IMG" bs=${DUM}M count=0 seek=1 status=none
127 cat <<EOF | sfdisk -q "$IMG"
128 label: dos
129
130 - - 0xef *
131 EOF
132 # First sector is 2048 by default, which is byte address 1048576
133 mkfs.fat --offset 2048 "$IMG" >/dev/null
134 mcopy -i "$IMG@@1048576" -s $TMP/* ::/
135 [ -z "$SUDO_USER" ] || chown $SUDO_USER: "$IMG"
136
137 #============================================================
138 # Final syslinux bios boot image mastering
139 dd if=/usr/lib/SYSLINUX/mbr.bin of="$IMG" bs=440 conv=notrunc status=none
140 syslinux -i -d syslinux -t 1048576 "$IMG"