1#!/bin/sh
2#
3# Simple wrapper for debootstrap, run in the docker build context
4#
5FAKEROOT=`which fakeroot 2> /dev/null`
6# debootstrap < 1.0.67 generates empty sources.list, see Debian#732255
7MIN_DEBOOTSTRAP_VERSION=1.0.67
8
9exit_and_skip()
10{
11    exit 3
12}
13
14#
15# fakeroot is needed to run the bootstrap stage
16#
17if [ -z $FAKEROOT ]; then
18    echo "Please install fakeroot to enable bootstraping" >&2
19    exit_and_skip
20
21fi
22
23if [ -z "${DEB_ARCH}" ]; then
24    echo "Please set DEB_ARCH to choose an architecture (e.g. armhf)" >&2
25    exit_and_skip
26
27fi
28
29if [ -z "${DEB_TYPE}" ]; then
30    echo "Please set DEB_TYPE to a Debian archive name (e.g. testing)" >&2
31    exit_and_skip
32
33fi
34
35# We check in order for
36#
37#  - DEBOOTSTRAP_DIR pointing at a development checkout
38#  - PATH for the debootstrap script (installed)
39#
40# If neither option works then we checkout debootstrap from its
41# upstream SCM and run it from there.
42#
43
44if [ -z $DEBOOTSTRAP_DIR ]; then
45    NEED_DEBOOTSTRAP=false
46    DEBOOTSTRAP=`which debootstrap 2> /dev/null`
47    if [ -z $DEBOOTSTRAP ]; then
48        echo "No debootstrap installed, attempting to install from SCM"
49        NEED_DEBOOTSTRAP=true
50    elif ! (echo "${MIN_DEBOOTSTRAP_VERSION}" ; "${DEBOOTSTRAP}" --version \
51            | cut -d ' ' -f 2) | sort -t . -n -k 1,1 -k 2,2 -k 3,3 -c &>/dev/null; then
52        echo "debootstrap too old, attempting to install from SCM"
53        NEED_DEBOOTSTRAP=true
54    fi
55    if $NEED_DEBOOTSTRAP; then
56        DEBOOTSTRAP_SOURCE=https://anonscm.debian.org/git/d-i/debootstrap.git
57        git clone ${DEBOOTSTRAP_SOURCE} ./debootstrap.git
58        export DEBOOTSTRAP_DIR=./debootstrap.git
59        DEBOOTSTRAP=./debootstrap.git/debootstrap
60        (cd "${DEBOOTSTRAP_DIR}" && "${FAKEROOT}" make )
61    fi
62else
63    DEBOOTSTRAP=${DEBOOTSTRAP_DIR}/debootstrap
64    if [ ! -f $DEBOOTSTRAP ]; then
65        echo "Couldn't find script at ${DEBOOTSTRAP}" >&2
66        exit_and_skip
67    fi
68fi
69
70#
71# Finally check to see if any qemu's are installed
72#
73BINFMT_DIR=/proc/sys/fs/binfmt_misc
74if [ ! -e $BINFMT_DIR ]; then
75   echo "binfmt_misc needs enabling for a QEMU bootstrap to work" >&2
76   exit_and_skip
77else
78    # DEB_ARCH and QEMU arch names are not totally aligned
79    case "${DEB_ARCH}" in
80        amd64)
81            QEMU=qemu-i386
82            ;;
83        armel|armhf)
84            QEMU=qemu-arm
85            ;;
86        arm64)
87            QEMU=qemu-aarch64
88            ;;
89        powerpc)
90            QEMU=qemu-ppc
91            ;;
92        ppc64el)
93            QEMU=qemu-ppc64le
94            ;;
95        s390)
96            QEMU=qemu-s390x
97            ;;
98        *)
99            QEMU=qemu-${DEB_ARCH}
100        ;;
101    esac
102    if [ ! -e "${BINFMT_DIR}/$QEMU" ]; then
103        echo "No binfmt_misc rule to run $QEMU, can't bootstrap" >&2
104        exit_and_skip
105    fi
106fi
107
108echo "Building a rootfs using ${FAKEROOT} and ${DEBOOTSTRAP} ${DEB_ARCH}/${DEB_TYPE}"
109
110${FAKEROOT} ${DEBOOTSTRAP} --variant=buildd --foreign --arch=$DEB_ARCH $DEB_TYPE . http://httpredir.debian.org/debian || exit 1
111exit 0
112