Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/scripts/chroot-buildpackages.sh
Views: 3960
#!/bin/bash1#2# Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com3#4# This file is licensed under the terms of the GNU General Public5# License version 2. This program is licensed "as is" without any6# warranty of any kind, whether express or implied.789# Functions:1011# create_chroot12# chroot_prepare_distccd13# chroot_build_packages14# chroot_installpackages_local15# chroot_installpackages1617181920# create_chroot <target_dir> <release> <arch>21#22create_chroot()23{24local target_dir="$1"25local release=$226local arch=$327declare -A qemu_binary apt_mirror components28qemu_binary['armhf']='qemu-arm-static'29qemu_binary['arm64']='qemu-aarch64-static'30apt_mirror['stretch']="$DEBIAN_MIRROR"31apt_mirror['buster']="$DEBIAN_MIRROR"32apt_mirror['bullseye']="$DEBIAN_MIRROR"33apt_mirror['bookworm']="$DEBIAN_MIRROR"34apt_mirror['xenial']="$UBUNTU_MIRROR"35apt_mirror['bionic']="$UBUNTU_MIRROR"36apt_mirror['focal']="$UBUNTU_MIRROR"37apt_mirror['hirsute']="$UBUNTU_MIRROR"38apt_mirror['impish']="$UBUNTU_MIRROR"39components['stretch']='main,contrib'40apt_mirror['jammy']="$UBUNTU_MIRROR"41apt_mirror['noble']="$UBUNTU_MIRROR"42components['buster']='main,contrib'43components['bullseye']='main,contrib'44components['bookworm']='main,contrib'45components['sid']='main'46components['xenial']='main,universe,multiverse'47components['bionic']='main,universe,multiverse'48components['focal']='main,universe,multiverse'49components['hirsute']='main,universe,multiverse'50components['impish']='main,universe,multiverse'51components['jammy']='main,universe,multiverse'52components['noble']='main,universe,multiverse'53display_alert "Creating build chroot" "$release/$arch" "info"54local includes="ccache,locales,git,ca-certificates,devscripts,libfile-fcntllock-perl,debhelper,rsync,python3,distcc,apt-utils"5556# perhaps a temporally workaround57case $release in58buster|bullseye|focal|hirsute|sid|bookworm)59includes=${includes}",perl-openssl-defaults,libnet-ssleay-perl"60;;61esac6263if [[ $NO_APT_CACHER != yes ]]; then64local mirror_addr="http://localhost:3142/${apt_mirror[${release}]}"65else66local mirror_addr="http://${apt_mirror[${release}]}"67fi6869mkdir -p "${target_dir}"70cd "${target_dir}"7172debootstrap --variant=buildd \73--components="${components[${release}]}" \74--arch="${arch}" $DEBOOTSTRAP_OPTION \75--foreign \76--include="${includes}" "${release}" "${target_dir}" "${mirror_addr}"7778[[ $? -ne 0 || ! -f "${target_dir}"/debootstrap/debootstrap ]] && \79exit_with_error "Create chroot first stage failed"8081cp /usr/bin/${qemu_binary[$arch]} "${target_dir}"/usr/bin/82[[ ! -f "${target_dir}"/usr/share/keyrings/debian-archive-keyring.gpg ]] && \83mkdir -p "${target_dir}"/usr/share/keyrings/ && \84cp /usr/share/keyrings/debian-archive-keyring.gpg "${target_dir}"/usr/share/keyrings/8586chroot "${target_dir}" /bin/bash -c "/debootstrap/debootstrap --second-stage"87[[ $? -ne 0 || ! -f "${target_dir}"/bin/bash ]] && exit_with_error "Create chroot second stage failed"8889create_sources_list "$release" "${target_dir}"90[[ $NO_APT_CACHER != yes ]] && \91echo 'Acquire::http { Proxy "http://localhost:3142"; };' > "${target_dir}"/etc/apt/apt.conf.d/02proxy92cat <<-EOF > "${target_dir}"/etc/apt/apt.conf.d/71-no-recommends93APT::Install-Recommends "0";94APT::Install-Suggests "0";95EOF96[[ -f "${target_dir}"/etc/locale.gen ]] && \97sed -i "s/^# en_US.UTF-8/en_US.UTF-8/" "${target_dir}"/etc/locale.gen98chroot "${target_dir}" /bin/bash -c "locale-gen; update-locale LANG=en_US:en LC_ALL=en_US.UTF-8"99100printf '#!/bin/sh\nexit 101' > "${target_dir}"/usr/sbin/policy-rc.d101chmod 755 "${target_dir}"/usr/sbin/policy-rc.d102rm "${target_dir}"/etc/resolv.conf 2>/dev/null103echo "nameserver $NAMESERVER" > "${target_dir}"/etc/resolv.conf104rm "${target_dir}"/etc/hosts 2>/dev/null105echo "127.0.0.1 localhost" > "${target_dir}"/etc/hosts106mkdir -p "${target_dir}"/root/{build,overlay,sources} "${target_dir}"/selinux107if [[ -L "${target_dir}"/var/lock ]]; then108rm -rf "${target_dir}"/var/lock 2>/dev/null109mkdir -p "${target_dir}"/var/lock110fi111chroot "${target_dir}" /bin/bash -c "/usr/sbin/update-ccache-symlinks"112113display_alert "Upgrading packages in" "${target_dir}" "info"114chroot "${target_dir}" /bin/bash -c "apt-get -q update; apt-get -q -y upgrade; apt-get clean"115date +%s >"$target_dir/root/.update-timestamp"116117case $release in118bullseye|focal|hirsute|sid|bookworm)119chroot "${target_dir}" /bin/bash -c "apt-get install python-is-python3"120;;121esac122123touch "${target_dir}"/root/.debootstrap-complete124display_alert "Debootstrap complete" "${release}/${arch}" "info"125} #############################################################################126127128# chroot_prepare_distccd <release> <arch>129#130chroot_prepare_distccd()131{132local release=$1133local arch=$2134local dest=/tmp/distcc/${release}-${arch}135declare -A gcc_version gcc_type136gcc_version['stretch']='6.3'137gcc_version['buster']='8.3'138gcc_version['bullseye']='9.2'139gcc_version['bookworm']='10.2'140gcc_version['xenial']='5.4'141gcc_version['bionic']='5.4'142gcc_version['focal']='9.2'143gcc_version['jammy']='10.2'144gcc_version['noble']='13.2'145gcc_version['hirsute']='10.2'146gcc_version['sid']='10.2'147gcc_type['armhf']='arm-linux-gnueabihf-'148gcc_type['arm64']='aarch64-linux-gnu-'149rm -f "${dest}"/cmdlist150mkdir -p "${dest}"151local toolchain_path152#local toolchain_path=$(find_toolchain "${gcc_type[${arch}]}" "== ${gcc_version[${release}]}")153toolchain_path=${toolchain}154ln -sf "${toolchain_path}/${gcc_type[${arch}]}gcc" "${dest}"/cc155echo "${dest}/cc" >> "${dest}"/cmdlist156for compiler in gcc cpp g++ c++; do157echo "${dest}/$compiler" >> "${dest}"/cmdlist158echo "${dest}/${gcc_type[$arch]}${compiler}" >> "${dest}"/cmdlist159ln -sf "${toolchain_path}/${gcc_type[${arch}]}${compiler}" "${dest}/${compiler}"160ln -sf "${toolchain_path}/${gcc_type[${arch}]}${compiler}" "${dest}/${gcc_type[${arch}]}${compiler}"161done162mkdir -p /var/run/distcc/163touch /var/run/distcc/"${release}-${arch}".pid164chown -R distccd /var/run/distcc/165chown -R distccd /tmp/distcc166}167168# chroot_build_packages169#170chroot_build_packages()171{172local built_ok=()173local failed=()174175if [[ $IMAGE_TYPE == user-built ]]; then176# if user-built image compile only for selected arch/release177target_release="${RELEASE}"178target_arch="${ARCH}"179else180# only make packages for recent releases. There are no changes on older181target_release="stretch bionic buster bullseye bookworm focal hirsute jammy noble sid"182target_arch="armhf arm64"183fi184185for release in $target_release; do186for arch in $target_arch; do187display_alert "Starting package building process" "$release/$arch" "info"188189local target_dir190target_dir="${EXTER}/cache/buildpkg/${release}-${arch}-v${CHROOT_CACHE_VERSION}"191local distcc_bindaddr="127.0.0.2"192193[[ ! -f "${target_dir}"/root/.debootstrap-complete ]] && create_chroot "${target_dir}" "${release}" "${arch}"194[[ ! -f "${target_dir}"/root/.debootstrap-complete ]] && exit_with_error "Creating chroot failed" "${release}/${arch}"195196[[ -f /var/run/distcc/"${release}-${arch}".pid ]] && kill "$(<"/var/run/distcc/${release}-${arch}.pid")" > /dev/null 2>&1197198chroot_prepare_distccd "${release}" "${arch}"199200# DISTCC_TCP_DEFER_ACCEPT=0201DISTCC_CMDLIST=/tmp/distcc/${release}-${arch}/cmdlist TMPDIR=/tmp/distcc distccd --daemon \202--pid-file "/var/run/distcc/${release}-${arch}.pid" --listen $distcc_bindaddr --allow 127.0.0.0/24 \203--log-file "/tmp/distcc/${release}-${arch}.log" --user distccd204205local t=$target_dir/root/.update-timestamp206if [[ ! -f ${t} || $(( ($(date +%s) - $(<"${t}")) / 86400 )) -gt 7 ]]; then207display_alert "Upgrading packages" "$release/$arch" "info"208systemd-nspawn -a -q -D "${target_dir}" /bin/bash -c "apt-get -q update; apt-get -q -y upgrade; apt-get clean"209date +%s > "${t}"210fi211212for plugin in "${EXTER}"/packages/extras-buildpkgs/*.conf; do213unset package_name package_repo package_ref package_builddeps package_install_chroot package_install_target \214package_upstream_version needs_building plugin_target_dir package_component "package_builddeps_${release}"215source "${plugin}"216217# check build condition218if [[ $(type -t package_checkbuild) == function ]] && ! package_checkbuild; then219display_alert "Skipping building $package_name for" "$release/$arch"220continue221fi222223local plugin_target_dir=${DEB_STORAGE}/extra/$package_component/224mkdir -p "${plugin_target_dir}"225226# check if needs building227local needs_building=no228if [[ -n $package_install_target ]]; then229for f in $package_install_target; do230if [[ -z $(find "${plugin_target_dir}" -name "${f}_*$REVISION*_$arch.deb") ]]; then231needs_building=yes232break233fi234done235else236needs_building=yes237fi238if [[ $needs_building == no ]]; then239display_alert "Packages are up to date" "$package_name $release/$arch" "info"240continue241fi242display_alert "Building packages" "$package_name $release/$arch" "ext"243local dist_builddeps_name="package_builddeps_${release}"244[[ -v $dist_builddeps_name ]] && package_builddeps="${package_builddeps} ${!dist_builddeps_name}"245246# create build script247create_build_script248249fetch_from_repo "$package_repo" "${EXTER}/cache/sources/extra/$package_name" "$package_ref"250251eval systemd-nspawn -a -q \252--capability=CAP_MKNOD -D "${target_dir}" \253--tmpfs=/root/build \254--tmpfs=/tmp:mode=777 \255--bind-ro "${EXTER}"/packages/extras-buildpkgs/:/root/overlay \256--bind-ro "${EXTER}"/cache/sources/extra/:/root/sources /bin/bash -c "/root/build.sh" 2>&1 \257${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/buildpkg.log'}258259if [[ ${PIPESTATUS[0]} -eq 2 ]]; then260failed+=("$package_name:$release/$arch")261else262built_ok+=("$package_name:$release/$arch")263fi264mv "${target_dir}"/root/*.deb "${plugin_target_dir}" 2>/dev/null265done266# cleanup for distcc267kill "$(<"/var/run/distcc/${release}-${arch}.pid")"268done269done270if [[ ${#built_ok[@]} -gt 0 ]]; then271display_alert "Following packages were built without errors" "" "info"272for p in ${built_ok[@]}; do273display_alert "$p"274done275fi276if [[ ${#failed[@]} -gt 0 ]]; then277display_alert "Following packages failed to build" "" "wrn"278for p in ${failed[@]}; do279display_alert "$p"280done281fi282} #############################################################################283284# create build script285create_build_script ()286{287cat <<-EOF > "${target_dir}"/root/build.sh288#!/bin/bash289export PATH="/usr/lib/ccache:\$PATH"290export HOME="/root"291export DEBIAN_FRONTEND="noninteractive"292export DEB_BUILD_OPTIONS="nocheck noautodbgsym"293export CCACHE_TEMPDIR="/tmp"294# distcc is disabled to prevent compilation issues due295# to different host and cross toolchain configurations296#export CCACHE_PREFIX="distcc"297# uncomment for debug298#export CCACHE_RECACHE="true"299#export CCACHE_DISABLE="true"300export DISTCC_HOSTS="$distcc_bindaddr"301export DEBFULLNAME="$MAINTAINER"302export DEBEMAIL="$MAINTAINERMAIL"303$(declare -f display_alert)304305cd /root/build306if [[ -n "${package_builddeps}" ]]; then307# can be replaced with mk-build-deps308deps=()309installed=\$(310dpkg-query -W -f '\${db:Status-Abbrev}|\${binary:Package}\n' '*' 2>/dev/null | \311grep '^ii' | \312awk -F '|' '{print \$2}' | \313cut -d ':' -f 1314)315316for packet in $package_builddeps317do318grep -q -x -e "\$packet" <<< "\$installed" || deps+=("\$packet")319done320321if [[ \${#deps[@]} -gt 0 ]]; then322display_alert "Installing build dependencies"323apt-get -y -q update324apt-get -y -q \325--no-install-recommends \326--show-progress \327-o DPKG::Progress-Fancy=1 install "\${deps[@]}"328fi329fi330331display_alert "Copying sources"332rsync -aq /root/sources/"${package_name}" /root/build/333334cd /root/build/"${package_name}"335# copy overlay / "debianization" files336[[ -d "/root/overlay/${package_name}/" ]] && rsync -aq /root/overlay/"${package_name}" /root/build/337338# set upstream version339[[ -n "${package_upstream_version}" ]] && debchange --preserve --newversion "${package_upstream_version}" "Import from upstream"340341# set local version342# debchange -l~orangepi${REVISION}-${builddate}+ "Custom $VENDOR release"343debchange -l~orangepi"${REVISION}"+ "Custom $VENDOR release"344345display_alert "Building package"346dpkg-buildpackage -b -us -j2347348if [[ \$? -eq 0 ]]; then349cd /root/build350# install in chroot if other libraries depend on them351if [[ -n "$package_install_chroot" ]]; then352display_alert "Installing packages"353for p in $package_install_chroot; do354dpkg -i \${p}_*.deb355done356fi357display_alert "Done building" "$package_name $release/$arch" "ext"358ls *.deb 2>/dev/null359mv *.deb /root 2>/dev/null360exit 0361else362display_alert "Failed building" "$package_name $release/$arch" "err"363exit 2364fi365EOF366367chmod +x "${target_dir}"/root/build.sh368}369370# chroot_installpackages_local371#372chroot_installpackages_local()373{374local conf=$EXTER/config/aptly-temp.conf375rm -rf /tmp/aptly-temp/376mkdir -p /tmp/aptly-temp/377aptly -config="${conf}" repo create temp >> "${DEST}"/${LOG_SUBPATH}/install.log378# NOTE: this works recursively379if [[ $EXTERNAL_NEW == prebuilt ]]; then380aptly -config="${conf}" repo add temp "${DEB_ORANGEPI}/extra/${RELEASE}-desktop/" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1381aptly -config="${conf}" repo add temp "${DEB_ORANGEPI}/extra/${RELEASE}-utils/" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1382else383aptly -config="${conf}" repo add temp "${DEB_STORAGE}/extra/${RELEASE}-desktop/" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1384aptly -config="${conf}" repo add temp "${DEB_STORAGE}/extra/${RELEASE}-utils/" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1385fi386387# -gpg-key="925644A6"388[[ ! -d /root/.gnupg ]] && mkdir -p /root/.gnupg389aptly -keyring="$EXTER/packages/extras-buildpkgs/buildpkg-public.gpg" -secret-keyring="$EXTER/packages/extras-buildpkgs/buildpkg.gpg" -batch=true -config="${conf}" \390-gpg-key="925644A6" -passphrase="testkey1234" -component=temp -distribution="${RELEASE}" publish repo temp >> "${DEST}"/${LOG_SUBPATH}/install.log391#aptly -config="${conf}" -listen=":8189" serve &392aptly -config="${conf}" -listen=":8189" serve >> "${DEST}"/debug/install.log 2>&1 &393local aptly_pid=$!394cp $EXTER/packages/extras-buildpkgs/buildpkg.key "${SDCARD}"/tmp/buildpkg.key395cat <<-'EOF' > "${SDCARD}"/etc/apt/preferences.d/90-orangepi-temp.pref396Package: *397Pin: origin "localhost"398Pin-Priority: 550399EOF400cat <<-EOF > "${SDCARD}"/etc/apt/sources.list.d/orangepi-temp.list401deb http://localhost:8189/ $RELEASE temp402EOF403chroot_installpackages404kill "${aptly_pid}" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1405} #############################################################################406407# chroot_installpackages408#409chroot_installpackages()410{411local install_list=""412for plugin in "${EXTER}"/packages/extras-buildpkgs/*.conf; do413source "${plugin}"414if [[ $(type -t package_checkinstall) == function ]] && package_checkinstall; then415install_list="$install_list $package_install_target"416fi417unset package_install_target package_checkinstall418done419if [[ -n $PACKAGE_LIST_RM ]]; then420install_list=$(sed -r "s/\W($(tr ' ' '|' <<< ${PACKAGE_LIST_RM}))\W/ /g" <<< " ${install_list} ")421install_list="$(echo ${install_list})"422fi423display_alert "Installing extras-buildpkgs" "$install_list"424425[[ $NO_APT_CACHER != yes ]] && local apt_extra="-o Acquire::http::Proxy=\"http://${APT_PROXY_ADDR:-localhost:3142}\" -o Acquire::http::Proxy::localhost=\"DIRECT\""426cat <<-EOF > "${SDCARD}"/tmp/install.sh427#!/bin/bash428apt-key add /tmp/buildpkg.key429apt-get $apt_extra -q update430apt-get -q ${apt_extra} --show-progress -o DPKG::Progress-Fancy=1 install -y ${install_list}431apt-get clean432apt-key del "925644A6"433rm /etc/apt/sources.list.d/orangepi-temp.list 2>/dev/null434rm /etc/apt/preferences.d/90-orangepi-temp.pref 2>/dev/null435rm /tmp/buildpkg.key 2>/dev/null436rm -- "\$0"437EOF438chmod +x "${SDCARD}"/tmp/install.sh439chroot "${SDCARD}" /bin/bash -c "/tmp/install.sh" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1440441[[ -f ${SDCARD}/etc/hostapd.conf ]] && sed -i "s/^ssid=.*/ssid=OrangePi/" ${SDCARD}/etc/hostapd.conf442} #############################################################################443444445