1#!/bin/bash 2############################################################################### 3# 4# This build script is for running the OpenBMC builds as Docker containers. 5# 6############################################################################### 7# 8# Script Variables: 9# build_scripts_dir The path of the openbmc-build-scripts directory. 10# Default: The directory containing this script 11# http_proxy The HTTP address of the proxy server to connect to. 12# Default: "", proxy is not setup if this is not set 13# WORKSPACE Path of the workspace directory where some intermediate 14# files and the images will be saved to. 15# Default: "~/{RandomNumber}" 16# num_cpu Number of cpu's to give bitbake, default is total amount 17# in system 18# UBUNTU_MIRROR [optional] The URL of a mirror of Ubuntu to override the 19# default ones in /etc/apt/sources.list 20# default is empty, and no mirror is used. 21# ENV_LOCAL_CONF [optional] The environment variables to inject into the 22# build, which will be written into local.conf. 23# default is empty. 24# 25# Docker Image Build Variables: 26# BITBAKE_OPTS Set to "-c populate_sdk" or whatever other BitBake options 27# you'd like to pass into the build. 28# Default: "", no options set 29# build_dir Path where the actual BitBake build occurs inside the 30# container, path cannot be located on network storage. 31# Default: "$WORKSPACE/build" 32# distro The distro used as the base image for the build image: 33# fedora|ubuntu 34# Default: "ubuntu" 35# img_name The name given to the target build's docker image. 36# Default: "openbmc/${distro}:${imgtag}-${target}-${ARCH}" 37# img_tag The base docker image distro tag: 38# ubuntu: latest|16.04|14.04|trusty|xenial 39# fedora: 23|24|25 40# Default: "latest" 41# target The target we aim to build. Any system supported by 42# the openbmc/openbmc `setup` script is an option. 43# Default: "qemuarm" 44# no_tar Set to true if you do not want the debug tar built 45# Default: "false" 46# nice_priority Set nice priotity for bitbake command. 47# Nice: 48# Run with an adjusted niceness, which affects process 49# scheduling. Nice values range from -20 (most favorable 50# to the process) to 19 (least favorable to the process). 51# Default: "", nice is not used if nice_priority is not set 52# 53# Deployment Variables: 54# obmc_dir Path of the OpenBMC repo directory used as a reference 55# for the build inside the container. 56# Default: "${WORKSPACE}/openbmc" 57# ssc_dir Path of the OpenBMC shared directory that contains the 58# downloads dir and the sstate dir. 59# Default: "${HOME}" 60# xtrct_small_copy_dir 61# Directory within build_dir that should be copied to 62# xtrct_path. The directory and all parents up to, but not 63# including, build_dir will be copied. For example, if 64# build_dir is set to "/tmp/openbmc" and this is set to 65# "build/tmp", the directory at xtrct_path will have the 66# following directory structure: 67# xtrct_path 68# | - build 69# | - tmp 70# ... 71# Can also be set to the empty string to copy the entire 72# contents of build_dir to xtrct_path. 73# Default: "deploy/images". 74# 75############################################################################### 76# Trace bash processing. Set -e so when a step fails, we fail the build 77set -xeo pipefail 78 79# Script Variables: 80build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"} 81http_proxy=${http_proxy:-} 82WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}} 83num_cpu=${num_cpu:-$(nproc)} 84UBUNTU_MIRROR=${UBUNTU_MIRROR:-""} 85ENV_LOCAL_CONF=${ENV_LOCAL_CONF:-""} 86 87# Docker Image Build Variables: 88build_dir=${build_dir:-${WORKSPACE}/build} 89distro=${distro:-ubuntu} 90img_tag=${img_tag:-latest} 91target=${target:-qemuarm} 92no_tar=${no_tar:-false} 93nice_priority=${nice_priority:-} 94 95# Deployment variables 96obmc_dir=${obmc_dir:-${WORKSPACE}/openbmc} 97ssc_dir=${ssc_dir:-${HOME}} 98xtrct_small_copy_dir=${xtrct_small_copy_dir:-deploy/images} 99xtrct_path="${obmc_dir}/build/tmp" 100xtrct_copy_timeout="300" 101 102bitbake_target="obmc-phosphor-image" 103PROXY="" 104 105MIRROR="" 106if [[ -n "${UBUNTU_MIRROR}" ]]; then 107 MIRROR="RUN echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME) main restricted universe multiverse\" > /etc/apt/sources.list && \ 108 echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-updates main restricted universe multiverse\" >> /etc/apt/sources.list && \ 109 echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-security main restricted universe multiverse\" >> /etc/apt/sources.list && \ 110 echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-proposed main restricted universe multiverse\" >> /etc/apt/sources.list && \ 111 echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-backports main restricted universe multiverse\" >> /etc/apt/sources.list" 112fi 113 114# Determine the architecture 115ARCH=$(uname -m) 116 117# Determine the prefix of the Dockerfile's base image 118case ${ARCH} in 119 "ppc64le") 120 DOCKER_BASE="ppc64le/" 121 ;; 122 "x86_64") 123 DOCKER_BASE="" 124 ;; 125 *) 126 echo "Unsupported system architecture(${ARCH}) found for docker image" 127 exit 1 128esac 129 130# Timestamp for job 131echo "Build started, $(date)" 132 133# If the obmc_dir directory doesn't exist clone it in 134if [ ! -d "${obmc_dir}" ]; then 135 echo "Clone in openbmc master to ${obmc_dir}" 136 git clone https://github.com/openbmc/openbmc "${obmc_dir}" 137fi 138 139# Make and chown the xtrct_path directory to avoid permission errors 140if [ ! -d "${xtrct_path}" ]; then 141 mkdir -p "${xtrct_path}" 142fi 143chown "${UID}:${GROUPS[0]}" "${xtrct_path}" 144 145# Perform overrides for specific machines as required. 146DISTRO=${DISTRO:-} 147 148# Set build target and BitBake command 149MACHINE="${target}" 150BITBAKE_CMD="source ./setup ${MACHINE} ${build_dir}" 151 152# Configure Docker build 153if [[ "${distro}" == fedora ]];then 154 155 if [[ -n "${http_proxy}" ]]; then 156 PROXY="RUN echo \"proxy=${http_proxy}\" >> /etc/dnf/dnf.conf" 157 fi 158 159 Dockerfile=$(cat << EOF 160 FROM ${DOCKER_BASE}${distro}:${img_tag} 161 162 ${PROXY} 163 164 RUN dnf --refresh install -y \ 165 bzip2 \ 166 chrpath \ 167 cpio \ 168 diffstat \ 169 findutils \ 170 gcc \ 171 gcc-c++ \ 172 git \ 173 make \ 174 patch \ 175 perl-bignum \ 176 perl-Data-Dumper \ 177 perl-Thread-Queue \ 178 python-devel \ 179 python3-devel \ 180 SDL-devel \ 181 socat \ 182 subversion \ 183 tar \ 184 texinfo \ 185 wget \ 186 which \ 187 file \ 188 hostname \ 189 rpcgen \ 190 glibc-langpack-en \ 191 glibc-locale-source 192 193 # Set the locale 194 ENV LANG=en_US.utf8 195 RUN localedef -f UTF-8 -i en_US en_US.UTF-8 196 197 RUN grep -q ${GROUPS[0]} /etc/group || groupadd -g ${GROUPS[0]} ${USER} 198 RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS[0]} ${USER} 199 200 USER ${USER} 201 ENV HOME ${HOME} 202 RUN /bin/bash 203EOF 204) 205 206elif [[ "${distro}" == ubuntu ]]; then 207 208 if [[ -n "${http_proxy}" ]]; then 209 PROXY="RUN echo \"Acquire::http::Proxy \\"\"${http_proxy}/\\"\";\" > /etc/apt/apt.conf.d/000apt-cacher-ng-proxy" 210 fi 211 212 Dockerfile=$(cat << EOF 213 FROM ${DOCKER_BASE}${distro}:${img_tag} 214 215 ${PROXY} 216 ${MIRROR} 217 218 ENV DEBIAN_FRONTEND noninteractive 219 220 RUN apt-get update && apt-get install -yy \ 221 build-essential \ 222 chrpath \ 223 debianutils \ 224 diffstat \ 225 gawk \ 226 git \ 227 libdata-dumper-simple-perl \ 228 libsdl1.2-dev \ 229 libthread-queue-any-perl \ 230 locales \ 231 python \ 232 python3 \ 233 socat \ 234 subversion \ 235 texinfo \ 236 cpio \ 237 wget \ 238 iputils-ping 239 240 # Set the locale 241 RUN locale-gen en_US.UTF-8 242 ENV LANG en_US.UTF-8 243 ENV LANGUAGE en_US:en 244 ENV LC_ALL en_US.UTF-8 245 246 RUN grep -q ${GROUPS[0]} /etc/group || groupadd -g ${GROUPS[0]} ${USER} 247 RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS[0]} ${USER} 248 249 USER ${USER} 250 ENV HOME ${HOME} 251 RUN /bin/bash 252EOF 253) 254fi 255 256# Create the Docker run script 257export PROXY_HOST=${http_proxy/#http*:\/\/} 258export PROXY_HOST=${PROXY_HOST/%:[0-9]*} 259export PROXY_PORT=${http_proxy/#http*:\/\/*:} 260 261mkdir -p "${WORKSPACE}" 262 263# Determine command for bitbake image build 264if [ "$no_tar" = "false" ]; then 265 bitbake_target="${bitbake_target} obmc-phosphor-debug-tarball" 266fi 267 268cat > "${WORKSPACE}"/build.sh << EOF_SCRIPT 269#!/bin/bash 270 271set -xeo pipefail 272 273# Go into the OpenBMC directory, the build will handle changing directories 274cd ${obmc_dir} 275 276# Set up proxies 277export ftp_proxy=${http_proxy} 278export http_proxy=${http_proxy} 279export https_proxy=${http_proxy} 280 281mkdir -p ${WORKSPACE}/bin 282 283# Configure proxies for BitBake 284if [[ -n "${http_proxy}" ]]; then 285 286 cat > ${WORKSPACE}/bin/git-proxy << \EOF_GIT 287 #!/bin/bash 288 # \$1 = hostname, \$2 = port 289 PROXY=${PROXY_HOST} 290 PROXY_PORT=${PROXY_PORT} 291 exec socat STDIO PROXY:\${PROXY}:\${1}:\${2},proxyport=\${PROXY_PORT} 292EOF_GIT 293 294 chmod a+x ${WORKSPACE}/bin/git-proxy 295 export PATH=${WORKSPACE}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH} 296 297 lock=${HOME}/build-setup.lock 298 flock \${lock} git config --global core.gitProxy ${WORKSPACE}/bin/git-proxy 299 flock \${lock} git config --global http.proxy ${http_proxy} 300 301 flock \${lock} mkdir -p ~/.subversion 302 flock \${lock} cat > ~/.subversion/servers << EOF_SVN 303 [global] 304 http-proxy-host = ${PROXY_HOST} 305 http-proxy-port = ${PROXY_PORT} 306EOF_SVN 307 308 flock \${lock} cat > ~/.wgetrc << EOF_WGETRC 309 https_proxy = ${http_proxy} 310 http_proxy = ${http_proxy} 311 use_proxy = on 312EOF_WGETRC 313 314 flock \${lock} cat > ~/.curlrc << EOF_CURLRC 315 proxy = ${PROXY_HOST}:${PROXY_PORT} 316EOF_CURLRC 317fi 318 319# Source our build env 320${BITBAKE_CMD} 321 322if [[ -z "${MACHINE}" ]]; then 323 echo "MACHINE is not configured for ${target}" 324 exit 1 325fi 326 327export MACHINE="${MACHINE}" 328if [[ -z "${DISTRO}" ]]; then 329 echo "DISTRO is not configured for ${target} so will use default" 330 unset DISTRO 331else 332 export DISTRO="${DISTRO}" 333fi 334 335# bitbake requires SDKMACHINE be x86 336export SDKMACHINE=x86_64 337 338# Custom BitBake config settings 339cat >> conf/local.conf << EOF_CONF 340BB_NUMBER_THREADS = "$(nproc)" 341PARALLEL_MAKE = "-j$(nproc)" 342INHERIT += "rm_work" 343BB_GENERATE_MIRROR_TARBALLS = "1" 344DL_DIR="${ssc_dir}/bitbake_downloads" 345SSTATE_DIR="${ssc_dir}/bitbake_sharedstatecache" 346USER_CLASSES += "buildstats" 347INHERIT_remove = "uninative" 348TMPDIR="${build_dir}" 349${ENV_LOCAL_CONF} 350EOF_CONF 351 352# Kick off a build 353if [[ -n "${nice_priority}" ]]; then 354 nice -${nice_priority} bitbake ${BITBAKE_OPTS} ${bitbake_target} 355else 356 bitbake ${BITBAKE_OPTS} ${bitbake_target} 357fi 358 359# Copy internal build directory into xtrct_path directory 360if [[ ${xtrct_small_copy_dir} ]]; then 361 mkdir -p ${xtrct_path}/${xtrct_small_copy_dir} 362 timeout ${xtrct_copy_timeout} cp -r ${build_dir}/${xtrct_small_copy_dir}/* ${xtrct_path}/${xtrct_small_copy_dir} 363else 364 timeout ${xtrct_copy_timeout} cp -r ${build_dir}/* ${xtrct_path} 365fi 366 367if [[ 0 -ne $? ]]; then 368 echo "Received a non-zero exit code from timeout" 369 exit 1 370fi 371 372EOF_SCRIPT 373 374chmod a+x "${WORKSPACE}/build.sh" 375 376# Give the Docker image a name based on the distro,tag,arch,and target 377img_name=${img_name:-openbmc/${distro}:${img_tag}-${target}-${ARCH}} 378 379# Build the Docker image 380docker build -t "${img_name}" - <<< "${Dockerfile}" 381 382# If obmc_dir or ssc_dir are ${HOME} or a subdirectory they will not be mounted 383mount_obmc_dir="-v ""${obmc_dir}"":""${obmc_dir}"" " 384mount_ssc_dir="-v ""${ssc_dir}"":""${ssc_dir}"" " 385mount_workspace_dir="-v ""${WORKSPACE}"":""${WORKSPACE}"" " 386if [[ "${obmc_dir}" = "${HOME}/"* || "${obmc_dir}" = "${HOME}" ]];then 387mount_obmc_dir="" 388fi 389if [[ "${ssc_dir}" = "${HOME}/"* || "${ssc_dir}" = "${HOME}" ]];then 390mount_ssc_dir="" 391fi 392if [[ "${WORKSPACE}" = "${HOME}/"* || "${WORKSPACE}" = "${HOME}" ]];then 393mount_workspace_dir="" 394fi 395 396# Run the Docker container, execute the build.sh script 397# shellcheck disable=SC2086 # mount commands word-split purposefully 398docker run \ 399--cap-add=sys_admin \ 400--cap-add=sys_nice \ 401--net=host \ 402--rm=true \ 403-e WORKSPACE="${WORKSPACE}" \ 404-w "${HOME}" \ 405-v "${HOME}:${HOME}" \ 406${mount_obmc_dir} \ 407${mount_ssc_dir} \ 408${mount_workspace_dir} \ 409--cpus="$num_cpu" \ 410"${img_name}" \ 411"${WORKSPACE}/build.sh" 412 413# To maintain function of resources that used an older path, add a link 414ln -sf "${xtrct_path}/deploy" "${WORKSPACE}/deploy" 415 416# Timestamp for build 417echo "Build completed, $(date)" 418