xref: /openbmc/qemu/tests/docker/dockerfiles/debian-bootstrap.pre (revision db817b8c500a60873eba80cbf047900ae5b32766)
1#!/bin/sh
2#
3# Simple wrapper for debootstrap, run in the docker build context
4#
5FAKEROOT=`which fakeroot 2> /dev/null`
6# debootstrap < 1.0.67 generates empty sources.list, see Debian#732255
7MIN_DEBOOTSTRAP_VERSION=1.0.67
8
9exit_and_skip()
10{
11    exit 3
12}
13
14#
15# fakeroot is needed to run the bootstrap stage
16#
17if [ -z $FAKEROOT ]; then
18    echo "Please install fakeroot to enable bootstraping" >&2
19    exit_and_skip
20
21fi
22
23if [ -z "${DEB_ARCH}" ]; then
24    echo "Please set DEB_ARCH to choose an architecture (e.g. armhf)" >&2
25    exit_and_skip
26
27fi
28
29if [ -z "${DEB_TYPE}" ]; then
30    echo "Please set DEB_TYPE to a Debian archive name (e.g. testing)" >&2
31    exit_and_skip
32
33fi
34
35# The following allow finer grain control over the defaults
36if [ -z "${DEB_VARIANT}" ]; then
37    DEB_VARIANT=buildd
38fi
39
40if [ -z "${DEB_URL}" ]; then
41    DEB_URL="http://httpredir.debian.org/debian"
42fi
43
44# We check in order for
45#
46#  - DEBOOTSTRAP_DIR pointing at a development checkout
47#  - PATH for the debootstrap script (installed)
48#
49# If neither option works then we checkout debootstrap from its
50# upstream SCM and run it from there.
51#
52
53if [ -z $DEBOOTSTRAP_DIR ]; then
54    NEED_DEBOOTSTRAP=false
55    DEBOOTSTRAP=`which debootstrap 2> /dev/null`
56    if [ -z $DEBOOTSTRAP ]; then
57        echo "No debootstrap installed, attempting to install from SCM"
58        NEED_DEBOOTSTRAP=true
59    elif ! (echo "${MIN_DEBOOTSTRAP_VERSION}" ; "${DEBOOTSTRAP}" --version \
60            | cut -d ' ' -f 2) | sort -t . -n -k 1,1 -k 2,2 -k 3,3 -c &>/dev/null; then
61        echo "debootstrap too old, attempting to install from SCM"
62        NEED_DEBOOTSTRAP=true
63    fi
64    if $NEED_DEBOOTSTRAP; then
65        DEBOOTSTRAP_SOURCE=https://anonscm.debian.org/git/d-i/debootstrap.git
66        git clone ${DEBOOTSTRAP_SOURCE} ./debootstrap.git
67        export DEBOOTSTRAP_DIR=./debootstrap.git
68        DEBOOTSTRAP=./debootstrap.git/debootstrap
69        (cd "${DEBOOTSTRAP_DIR}" && "${FAKEROOT}" make )
70    fi
71else
72    DEBOOTSTRAP=${DEBOOTSTRAP_DIR}/debootstrap
73    if [ ! -f $DEBOOTSTRAP ]; then
74        echo "Couldn't find script at ${DEBOOTSTRAP}" >&2
75        exit_and_skip
76    fi
77fi
78
79#
80# Finally check to see if any qemu's are installed
81#
82BINFMT_DIR=/proc/sys/fs/binfmt_misc
83if [ ! -e $BINFMT_DIR ]; then
84   echo "binfmt_misc needs enabling for a QEMU bootstrap to work" >&2
85   exit_and_skip
86else
87    # DEB_ARCH and QEMU arch names are not totally aligned
88    case "${DEB_ARCH}" in
89        amd64)
90            QEMU=qemu-i386
91            ;;
92        armel|armhf)
93            QEMU=qemu-arm
94            ;;
95        arm64)
96            QEMU=qemu-aarch64
97            ;;
98        powerpc)
99            QEMU=qemu-ppc
100            ;;
101        ppc64el)
102            QEMU=qemu-ppc64le
103            ;;
104        s390)
105            QEMU=qemu-s390x
106            ;;
107        *)
108            QEMU=qemu-${DEB_ARCH}
109        ;;
110    esac
111    if [ ! -e "${BINFMT_DIR}/$QEMU" ]; then
112        echo "No binfmt_misc rule to run $QEMU, can't bootstrap" >&2
113        exit_and_skip
114    fi
115fi
116
117echo "Building a rootfs using ${FAKEROOT} and ${DEBOOTSTRAP} ${DEB_ARCH}/${DEB_TYPE}"
118
119${FAKEROOT} ${DEBOOTSTRAP} --variant=$DEB_VARIANT --foreign --arch=$DEB_ARCH $DEB_TYPE . $DEB_URL || exit 1
120exit 0
121