195c97501SAlex Bennée#!/bin/sh
295c97501SAlex Bennée#
395c97501SAlex Bennée# Simple wrapper for debootstrap, run in the docker build context
495c97501SAlex Bennée#
5b91068cdSMao ZhongyiFAKEROOT=$(which fakeroot 2> /dev/null)
600263139SSascha Silbe# debootstrap < 1.0.67 generates empty sources.list, see Debian#732255
700263139SSascha SilbeMIN_DEBOOTSTRAP_VERSION=1.0.67
895c97501SAlex Bennée
995c97501SAlex Bennéeexit_and_skip()
1095c97501SAlex Bennée{
1195c97501SAlex Bennée    exit 3
1295c97501SAlex Bennée}
1395c97501SAlex Bennée
1495c97501SAlex Bennée#
1595c97501SAlex Bennée# fakeroot is needed to run the bootstrap stage
1695c97501SAlex Bennée#
1795c97501SAlex Bennéeif [ -z $FAKEROOT ]; then
18b5dc88ceSSascha Silbe    echo "Please install fakeroot to enable bootstraping" >&2
1995c97501SAlex Bennée    exit_and_skip
20341edc0cSSascha Silbe
21341edc0cSSascha Silbefi
22341edc0cSSascha Silbe
23341edc0cSSascha Silbeif [ -z "${DEB_ARCH}" ]; then
24341edc0cSSascha Silbe    echo "Please set DEB_ARCH to choose an architecture (e.g. armhf)" >&2
25341edc0cSSascha Silbe    exit_and_skip
26341edc0cSSascha Silbe
27341edc0cSSascha Silbefi
28341edc0cSSascha Silbe
29341edc0cSSascha Silbeif [ -z "${DEB_TYPE}" ]; then
30341edc0cSSascha Silbe    echo "Please set DEB_TYPE to a Debian archive name (e.g. testing)" >&2
31341edc0cSSascha Silbe    exit_and_skip
32341edc0cSSascha Silbe
3395c97501SAlex Bennéefi
3495c97501SAlex Bennée
35300cf467SAlex Bennée# The following allow finer grain control over the defaults
36300cf467SAlex Bennéeif [ -z "${DEB_VARIANT}" ]; then
37300cf467SAlex Bennée    DEB_VARIANT=buildd
38300cf467SAlex Bennéefi
39300cf467SAlex Bennée
40300cf467SAlex Bennéeif [ -z "${DEB_URL}" ]; then
41300cf467SAlex Bennée    DEB_URL="http://httpredir.debian.org/debian"
42300cf467SAlex Bennéefi
43300cf467SAlex Bennée
4495c97501SAlex Bennée# We check in order for
4595c97501SAlex Bennée#
4695c97501SAlex Bennée#  - DEBOOTSTRAP_DIR pointing at a development checkout
4795c97501SAlex Bennée#  - PATH for the debootstrap script (installed)
4895c97501SAlex Bennée#
4995c97501SAlex Bennée# If neither option works then we checkout debootstrap from its
5095c97501SAlex Bennée# upstream SCM and run it from there.
5195c97501SAlex Bennée#
5295c97501SAlex Bennée
5395c97501SAlex Bennéeif [ -z $DEBOOTSTRAP_DIR ]; then
5400263139SSascha Silbe    NEED_DEBOOTSTRAP=false
55b91068cdSMao Zhongyi    DEBOOTSTRAP=$(which debootstrap 2> /dev/null)
5695c97501SAlex Bennée    if [ -z $DEBOOTSTRAP ]; then
5795c97501SAlex Bennée        echo "No debootstrap installed, attempting to install from SCM"
5800263139SSascha Silbe        NEED_DEBOOTSTRAP=true
59f7b446b5SAlex Bennée    else
60f7b446b5SAlex Bennée        INSTALLED_VERSION=$(${DEBOOTSTRAP} --version | sed 's/debootstrap \([0-9\.]*\)[^0-9\.]*.*/\1/')
61f7b446b5SAlex Bennée        if ! (echo "${MIN_DEBOOTSTRAP_VERSION}" ; echo "${INSTALLED_VERSION}") \
62f7b446b5SAlex Bennée                | sort -t . -n -k 1,1 -k 2,2 -k 3,3 -C ; then
6300263139SSascha Silbe            echo "debootstrap too old, attempting to install from SCM"
6400263139SSascha Silbe            NEED_DEBOOTSTRAP=true
6500263139SSascha Silbe        fi
66f7b446b5SAlex Bennée    fi
6700263139SSascha Silbe    if $NEED_DEBOOTSTRAP; then
68571fef5eSPhilippe Mathieu-Daudé        DEBOOTSTRAP_SOURCE=https://salsa.debian.org/installer-team/debootstrap.git
6995c97501SAlex Bennée        git clone ${DEBOOTSTRAP_SOURCE} ./debootstrap.git
7095c97501SAlex Bennée        export DEBOOTSTRAP_DIR=./debootstrap.git
7195c97501SAlex Bennée        DEBOOTSTRAP=./debootstrap.git/debootstrap
72ae2f659cSSascha Silbe        (cd "${DEBOOTSTRAP_DIR}" && "${FAKEROOT}" make )
7395c97501SAlex Bennée    fi
7495c97501SAlex Bennéeelse
7595c97501SAlex Bennée    DEBOOTSTRAP=${DEBOOTSTRAP_DIR}/debootstrap
7695c97501SAlex Bennée    if [ ! -f $DEBOOTSTRAP ]; then
77b5dc88ceSSascha Silbe        echo "Couldn't find script at ${DEBOOTSTRAP}" >&2
7895c97501SAlex Bennée        exit_and_skip
7995c97501SAlex Bennée    fi
8095c97501SAlex Bennéefi
8195c97501SAlex Bennée
8295c97501SAlex Bennée#
83*4c5aeb12SAlex Bennée# Add optional args
84*4c5aeb12SAlex Bennée#
85*4c5aeb12SAlex Bennéeif [ -n "${DEB_KEYRING}" ]; then
86*4c5aeb12SAlex Bennée    DEBOOTSTRAP="${DEBOOTSTRAP} --keyring=${DEB_KEYRING}"
87*4c5aeb12SAlex Bennéefi
88*4c5aeb12SAlex Bennée
89*4c5aeb12SAlex Bennée#
9095c97501SAlex Bennée# Finally check to see if any qemu's are installed
9195c97501SAlex Bennée#
9295c97501SAlex BennéeBINFMT_DIR=/proc/sys/fs/binfmt_misc
9395c97501SAlex Bennéeif [ ! -e $BINFMT_DIR ]; then
94b5dc88ceSSascha Silbe   echo "binfmt_misc needs enabling for a QEMU bootstrap to work" >&2
9595c97501SAlex Bennée   exit_and_skip
9695c97501SAlex Bennéeelse
9795c97501SAlex Bennée    # DEB_ARCH and QEMU arch names are not totally aligned
9895c97501SAlex Bennée    case "${DEB_ARCH}" in
9995c97501SAlex Bennée        amd64)
10095c97501SAlex Bennée            QEMU=qemu-i386
10195c97501SAlex Bennée            ;;
10295c97501SAlex Bennée        armel|armhf)
10395c97501SAlex Bennée            QEMU=qemu-arm
10495c97501SAlex Bennée            ;;
10595c97501SAlex Bennée        arm64)
10695c97501SAlex Bennée            QEMU=qemu-aarch64
10795c97501SAlex Bennée            ;;
10895c97501SAlex Bennée        powerpc)
10995c97501SAlex Bennée            QEMU=qemu-ppc
11095c97501SAlex Bennée            ;;
11195c97501SAlex Bennée        ppc64el)
11295c97501SAlex Bennée            QEMU=qemu-ppc64le
11395c97501SAlex Bennée            ;;
11495c97501SAlex Bennée        s390)
11595c97501SAlex Bennée            QEMU=qemu-s390x
11695c97501SAlex Bennée            ;;
11795c97501SAlex Bennée        *)
11895c97501SAlex Bennée            QEMU=qemu-${DEB_ARCH}
11995c97501SAlex Bennée        ;;
12095c97501SAlex Bennée    esac
12195c97501SAlex Bennée    if [ ! -e "${BINFMT_DIR}/$QEMU" ]; then
122b5dc88ceSSascha Silbe        echo "No binfmt_misc rule to run $QEMU, can't bootstrap" >&2
12395c97501SAlex Bennée        exit_and_skip
12495c97501SAlex Bennée    fi
12595c97501SAlex Bennéefi
12695c97501SAlex Bennée
12795c97501SAlex Bennéeecho "Building a rootfs using ${FAKEROOT} and ${DEBOOTSTRAP} ${DEB_ARCH}/${DEB_TYPE}"
12895c97501SAlex Bennée
129300cf467SAlex Bennée${FAKEROOT} ${DEBOOTSTRAP} --variant=$DEB_VARIANT --foreign --arch=$DEB_ARCH $DEB_TYPE . $DEB_URL || exit 1
13095c97501SAlex Bennéeexit 0
131