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