195c97501SAlex Bennée#!/bin/sh
295c97501SAlex Bennée#
395c97501SAlex Bennée# Simple wrapper for debootstrap, run in the docker build context
495c97501SAlex Bennée#
595c97501SAlex BennéeFAKEROOT=`which fakeroot 2> /dev/null`
695c97501SAlex Bennée
795c97501SAlex Bennéeexit_and_skip()
895c97501SAlex Bennée{
995c97501SAlex Bennée    exit 3
1095c97501SAlex Bennée}
1195c97501SAlex Bennée
1295c97501SAlex Bennée#
1395c97501SAlex Bennée# fakeroot is needed to run the bootstrap stage
1495c97501SAlex Bennée#
1595c97501SAlex Bennéeif [ -z $FAKEROOT ]; then
16*b5dc88ceSSascha Silbe    echo "Please install fakeroot to enable bootstraping" >&2
1795c97501SAlex Bennée    exit_and_skip
1895c97501SAlex Bennéefi
1995c97501SAlex Bennée
2095c97501SAlex Bennée# We check in order for
2195c97501SAlex Bennée#
2295c97501SAlex Bennée#  - DEBOOTSTRAP_DIR pointing at a development checkout
2395c97501SAlex Bennée#  - PATH for the debootstrap script (installed)
2495c97501SAlex Bennée#
2595c97501SAlex Bennée# If neither option works then we checkout debootstrap from its
2695c97501SAlex Bennée# upstream SCM and run it from there.
2795c97501SAlex Bennée#
2895c97501SAlex Bennée
2995c97501SAlex Bennéeif [ -z $DEBOOTSTRAP_DIR ]; then
3095c97501SAlex Bennée    DEBOOTSTRAP=`which debootstrap 2> /dev/null`
3195c97501SAlex Bennée    if [ -z $DEBOOTSTRAP ]; then
3295c97501SAlex Bennée        echo "No debootstrap installed, attempting to install from SCM"
3395c97501SAlex Bennée        DEBOOTSTRAP_SOURCE=https://anonscm.debian.org/git/d-i/debootstrap.git
3495c97501SAlex Bennée        git clone ${DEBOOTSTRAP_SOURCE} ./debootstrap.git
3595c97501SAlex Bennée        export DEBOOTSTRAP_DIR=./debootstrap.git
3695c97501SAlex Bennée        DEBOOTSTRAP=./debootstrap.git/debootstrap
3795c97501SAlex Bennée    fi
3895c97501SAlex Bennéeelse
3995c97501SAlex Bennée    DEBOOTSTRAP=${DEBOOTSTRAP_DIR}/debootstrap
4095c97501SAlex Bennée    if [ ! -f $DEBOOTSTRAP ]; then
41*b5dc88ceSSascha Silbe        echo "Couldn't find script at ${DEBOOTSTRAP}" >&2
4295c97501SAlex Bennée        exit_and_skip
4395c97501SAlex Bennée    fi
4495c97501SAlex Bennéefi
4595c97501SAlex Bennée
4695c97501SAlex Bennée#
4795c97501SAlex Bennée# Finally check to see if any qemu's are installed
4895c97501SAlex Bennée#
4995c97501SAlex BennéeBINFMT_DIR=/proc/sys/fs/binfmt_misc
5095c97501SAlex Bennéeif [ ! -e $BINFMT_DIR ]; then
51*b5dc88ceSSascha Silbe   echo "binfmt_misc needs enabling for a QEMU bootstrap to work" >&2
5295c97501SAlex Bennée   exit_and_skip
5395c97501SAlex Bennéeelse
5495c97501SAlex Bennée    # DEB_ARCH and QEMU arch names are not totally aligned
5595c97501SAlex Bennée    case "${DEB_ARCH}" in
5695c97501SAlex Bennée        amd64)
5795c97501SAlex Bennée            QEMU=qemu-i386
5895c97501SAlex Bennée            ;;
5995c97501SAlex Bennée        armel|armhf)
6095c97501SAlex Bennée            QEMU=qemu-arm
6195c97501SAlex Bennée            ;;
6295c97501SAlex Bennée        arm64)
6395c97501SAlex Bennée            QEMU=qemu-aarch64
6495c97501SAlex Bennée            ;;
6595c97501SAlex Bennée        powerpc)
6695c97501SAlex Bennée            QEMU=qemu-ppc
6795c97501SAlex Bennée            ;;
6895c97501SAlex Bennée        ppc64el)
6995c97501SAlex Bennée            QEMU=qemu-ppc64le
7095c97501SAlex Bennée            ;;
7195c97501SAlex Bennée        s390)
7295c97501SAlex Bennée            QEMU=qemu-s390x
7395c97501SAlex Bennée            ;;
7495c97501SAlex Bennée        *)
7595c97501SAlex Bennée            QEMU=qemu-${DEB_ARCH}
7695c97501SAlex Bennée        ;;
7795c97501SAlex Bennée    esac
7895c97501SAlex Bennée    if [ ! -e "${BINFMT_DIR}/$QEMU" ]; then
79*b5dc88ceSSascha Silbe        echo "No binfmt_misc rule to run $QEMU, can't bootstrap" >&2
8095c97501SAlex Bennée        exit_and_skip
8195c97501SAlex Bennée    fi
8295c97501SAlex Bennéefi
8395c97501SAlex Bennée
8495c97501SAlex Bennéeecho "Building a rootfs using ${FAKEROOT} and ${DEBOOTSTRAP} ${DEB_ARCH}/${DEB_TYPE}"
8595c97501SAlex Bennée
8695c97501SAlex Bennée${FAKEROOT} ${DEBOOTSTRAP} --variant=buildd --foreign --arch=$DEB_ARCH $DEB_TYPE . http://httpredir.debian.org/debian || exit 1
8795c97501SAlex Bennéeexit 0
88