1#!/bin/bash -xe
2#
3# Build the required docker image to run rootfs_size.py
4#
5# Script Variables:
6#   DOCKER_IMG_NAME:  <optional, the name of the docker image to generate>
7#                     default is openbmc/ubuntu-rootfs-size
8#   DISTRO:           <optional, the distro to build a docker image against>
9#   UBUNTU_MIRROR:    [optional] The URL of a mirror of Ubuntu to override the
10#                     default ones in /etc/apt/sources.list
11#                     default is empty, and no mirror is used.
12#   http_proxy:       The HTTP address of the proxy server to connect to.
13#                     Default: "", proxy is not setup if this is not set
14
15http_proxy=${http_proxy:-}
16UBUNTU_MIRROR=${UBUNTU_MIRROR:-""}
17
18set -uo pipefail
19
20DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-rootfs-size"}
21DISTRO=${DISTRO:-"ubuntu:bionic"}
22
23# Determine the architecture
24ARCH=$(uname -m)
25case ${ARCH} in
26    "ppc64le")
27        DOCKER_BASE="ppc64le/"
28        ;;
29    "x86_64")
30        DOCKER_BASE=""
31        ;;
32    "aarch64")
33        DOCKER_BASE="arm64v8/"
34        ;;
35    *)
36        echo "Unsupported system architecture(${ARCH}) found for docker image"
37        exit 1
38esac
39
40PROXY=""
41
42MIRROR=""
43if [[ -n "${UBUNTU_MIRROR}" ]]; then
44    MIRROR="RUN echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME) main restricted universe multiverse\" > /etc/apt/sources.list && \
45        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-updates main restricted universe multiverse\" >> /etc/apt/sources.list && \
46        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-security main restricted universe multiverse\" >> /etc/apt/sources.list && \
47        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-proposed main restricted universe multiverse\" >> /etc/apt/sources.list && \
48        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-backports main restricted universe multiverse\" >> /etc/apt/sources.list"
49fi
50
51################################# docker img # #################################
52
53if [[ "${DISTRO}" == "ubuntu"* ]]; then
54
55if [[ -n "${http_proxy}" ]]; then
56  PROXY="RUN echo \"Acquire::http::Proxy \\"\"${http_proxy}/\\"\";\" > /etc/apt/apt.conf.d/000apt-cacher-ng-proxy"
57fi
58
59Dockerfile=$(cat << EOF
60FROM ${DOCKER_BASE}${DISTRO}
61
62${PROXY}
63${MIRROR}
64
65ENV DEBIAN_FRONTEND noninteractive
66
67RUN apt-get update && apt-get install -yy \
68    python3 \
69    python3-dev\
70    python3-yaml \
71    python3-mako \
72    python3-pip \
73    python3-setuptools \
74    curl \
75    git \
76    wget \
77    sudo \
78    squashfs-tools
79
80# Final configuration for the workspace
81RUN grep -q ${GROUPS[0]} /etc/group || groupadd -g ${GROUPS[0]} ${USER}
82RUN mkdir -p $(dirname "${HOME}")
83RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS[0]} ${USER}
84RUN sed -i '1iDefaults umask=000' /etc/sudoers
85RUN echo "${USER} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
86
87RUN /bin/bash
88EOF
89)
90fi
91################################# docker img # #################################
92
93# Build above image
94docker build --network=host -t "${DOCKER_IMG_NAME}" - <<< "${Dockerfile}"
95