xref: /qemu/tests/docker/dockerfiles/debian-bootstrap.pre (revision ae2f659ca5eb8e39a83b11dc55e7e474b536e4e6)
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`
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
3595c97501SAlex Bennée# We check in order for
3695c97501SAlex Bennée#
3795c97501SAlex Bennée#  - DEBOOTSTRAP_DIR pointing at a development checkout
3895c97501SAlex Bennée#  - PATH for the debootstrap script (installed)
3995c97501SAlex Bennée#
4095c97501SAlex Bennée# If neither option works then we checkout debootstrap from its
4195c97501SAlex Bennée# upstream SCM and run it from there.
4295c97501SAlex Bennée#
4395c97501SAlex Bennée
4495c97501SAlex Bennéeif [ -z $DEBOOTSTRAP_DIR ]; then
4500263139SSascha Silbe    NEED_DEBOOTSTRAP=false
4695c97501SAlex Bennée    DEBOOTSTRAP=`which debootstrap 2> /dev/null`
4795c97501SAlex Bennée    if [ -z $DEBOOTSTRAP ]; then
4895c97501SAlex Bennée        echo "No debootstrap installed, attempting to install from SCM"
4900263139SSascha Silbe        NEED_DEBOOTSTRAP=true
5000263139SSascha Silbe    elif ! (echo "${MIN_DEBOOTSTRAP_VERSION}" ; "${DEBOOTSTRAP}" --version \
5100263139SSascha Silbe            | cut -d ' ' -f 2) | sort -t . -n -k 1,1 -k 2,2 -k 3,3 -c &>/dev/null; then
5200263139SSascha Silbe        echo "debootstrap too old, attempting to install from SCM"
5300263139SSascha Silbe        NEED_DEBOOTSTRAP=true
5400263139SSascha Silbe    fi
5500263139SSascha Silbe    if $NEED_DEBOOTSTRAP; then
5695c97501SAlex Bennée        DEBOOTSTRAP_SOURCE=https://anonscm.debian.org/git/d-i/debootstrap.git
5795c97501SAlex Bennée        git clone ${DEBOOTSTRAP_SOURCE} ./debootstrap.git
5895c97501SAlex Bennée        export DEBOOTSTRAP_DIR=./debootstrap.git
5995c97501SAlex Bennée        DEBOOTSTRAP=./debootstrap.git/debootstrap
60*ae2f659cSSascha Silbe        (cd "${DEBOOTSTRAP_DIR}" && "${FAKEROOT}" make )
6195c97501SAlex Bennée    fi
6295c97501SAlex Bennéeelse
6395c97501SAlex Bennée    DEBOOTSTRAP=${DEBOOTSTRAP_DIR}/debootstrap
6495c97501SAlex Bennée    if [ ! -f $DEBOOTSTRAP ]; then
65b5dc88ceSSascha Silbe        echo "Couldn't find script at ${DEBOOTSTRAP}" >&2
6695c97501SAlex Bennée        exit_and_skip
6795c97501SAlex Bennée    fi
6895c97501SAlex Bennéefi
6995c97501SAlex Bennée
7095c97501SAlex Bennée#
7195c97501SAlex Bennée# Finally check to see if any qemu's are installed
7295c97501SAlex Bennée#
7395c97501SAlex BennéeBINFMT_DIR=/proc/sys/fs/binfmt_misc
7495c97501SAlex Bennéeif [ ! -e $BINFMT_DIR ]; then
75b5dc88ceSSascha Silbe   echo "binfmt_misc needs enabling for a QEMU bootstrap to work" >&2
7695c97501SAlex Bennée   exit_and_skip
7795c97501SAlex Bennéeelse
7895c97501SAlex Bennée    # DEB_ARCH and QEMU arch names are not totally aligned
7995c97501SAlex Bennée    case "${DEB_ARCH}" in
8095c97501SAlex Bennée        amd64)
8195c97501SAlex Bennée            QEMU=qemu-i386
8295c97501SAlex Bennée            ;;
8395c97501SAlex Bennée        armel|armhf)
8495c97501SAlex Bennée            QEMU=qemu-arm
8595c97501SAlex Bennée            ;;
8695c97501SAlex Bennée        arm64)
8795c97501SAlex Bennée            QEMU=qemu-aarch64
8895c97501SAlex Bennée            ;;
8995c97501SAlex Bennée        powerpc)
9095c97501SAlex Bennée            QEMU=qemu-ppc
9195c97501SAlex Bennée            ;;
9295c97501SAlex Bennée        ppc64el)
9395c97501SAlex Bennée            QEMU=qemu-ppc64le
9495c97501SAlex Bennée            ;;
9595c97501SAlex Bennée        s390)
9695c97501SAlex Bennée            QEMU=qemu-s390x
9795c97501SAlex Bennée            ;;
9895c97501SAlex Bennée        *)
9995c97501SAlex Bennée            QEMU=qemu-${DEB_ARCH}
10095c97501SAlex Bennée        ;;
10195c97501SAlex Bennée    esac
10295c97501SAlex Bennée    if [ ! -e "${BINFMT_DIR}/$QEMU" ]; then
103b5dc88ceSSascha Silbe        echo "No binfmt_misc rule to run $QEMU, can't bootstrap" >&2
10495c97501SAlex Bennée        exit_and_skip
10595c97501SAlex Bennée    fi
10695c97501SAlex Bennéefi
10795c97501SAlex Bennée
10895c97501SAlex Bennéeecho "Building a rootfs using ${FAKEROOT} and ${DEBOOTSTRAP} ${DEB_ARCH}/${DEB_TYPE}"
10995c97501SAlex Bennée
11095c97501SAlex Bennée${FAKEROOT} ${DEBOOTSTRAP} --variant=buildd --foreign --arch=$DEB_ARCH $DEB_TYPE . http://httpredir.debian.org/debian || exit 1
11195c97501SAlex Bennéeexit 0
112