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