capture
[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 stupid_links() {
34     mkdir -p $1/usr
35     for d in bin sbin lib ; do mkdir $1/usr/$d ; ln -s usr/$d $1/$d ; done
36 }
37
38 if $DEBSTRAP ; then
39     ## Optionally add the stupid-links
40     read -n 1 -p "prime $FS with stupid-links? [Yn]" x
41     [ "$x" = "n" ] && USRMERGE=false
42
43     rm -rf "$FS" # remove if existin
44
45     $USRMERGE && stupid_links "$FS"
46
47     ## bootstrap a filesystem, with exclusions
48     echo "http_proxy=$http_proxy debootstrap ..."
49     debootstrap --exclude=logrotate,cron,cron-daemon-common \
50                 --arch=$ARCH $DIST "$FS" http://deb.devuan.org/merged || \
51         chroot "$FS" apt-get -f install -y
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     VERSION=${KERNEL#linux-image-}
60     VERSION=${VERSION%-unsigned}
61     chroot "$FS" apt-get install -y $KERNEL
62     chroot "$FS" depmod -a $VERSION
63
64     chroot "$FS" apt-get install -y busybox-static debootstrap
65     touch "$FS"/usr/bin{linuxrc,init} # block these
66     chroot "$FS" /usr/bin/busybox --install -s /usr/bin
67
68 fi # End of $DEBSTRAP actions
69
70 ## Pick boot kernel, if there are many
71 LINUXES=( $(cd "$FS"/boot ; ls vmlinuz* 2>/dev/null) )
72 if [ ${#LINUXES[@]} -gt 1 ] ; then
73     echo "** Please select boot kernel"
74     select LINUX in $LINUXES ; do [ -n "$LINUX" ] && break ; done
75 elif [ -n "$LINUXES" ] ; then
76     LINUX="$FS"/boot/${LINUXES[0]}
77 else
78     echo "** Oh No! There is no $FS/boot/vmlinux-* ... bailing out!" >&2
79     exit 1
80 fi
81
82 . ./minbase-strap-initrd.sh
83
84 #============================================================
85 # Common syslinux menu
86 CON="console=ttyS0,115200"
87 cat <<EOF > $TMP/menu.cfg
88 default menu.c32
89 label rescue (serial)
90     kernel /vmlinuz
91     append initrd=/initrd.img init=/init $CON rescue
92 label rescue2 (vt1)
93     kernel /vmlinuz
94     append initrd=/initrd.img init=/init rescue
95 label linux2 (sda2)
96     kernel /vmlinuz
97     append initrd=/initrd.img root=/dev/sda2 rootfstype=squashfs $CON
98 label linux (sdb1)
99     kernel /vmlinuz
100     append initrd=/initrd.img root=/dev/sdb1 $CON
101 EOF
102
103 . ./minbase-strap-bios.sh
104 . ./minbase-strap-uefi.sh
105 . ./minbase-strap-mkimg.sh
106
107 for E in $ENDING ; do $E ; done
108
109 #. ./minbase-strap-mkiso.sh $IMG