docker-images/images/minideb/minideb.sh

148 lines
4.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -e;
###
# Settings
###
# These can all be changed via environment variables.
clone_url="${clone_url:-https://github.com/bitnami/minideb.git}";
# The ref to checkout. Could be a branch name or a specific commit.
# In this case this is the last commit before google cloud was added
ref="e8efb17bd6859d2a40205e6ddfcd633d662968cf";
image_version="${image_version:-buster}";
apply_patch_raspbian="${apply_patch_raspbian:-true}";
setup_proxy="${setup_proxy:-true}";
proxy_address="${proxy_address:-http://172.16.230.100:3142}";
###############################################################################
target_directory="${1}";
# Ref https://stackoverflow.com/a/911213/1460422
if [ -t 1 ] || [[ ! -z "${FORCE_COLOUR}" ]]; then
###################
# From lantern.sh #
RS="\033[0m" # reset
HC="\033[1m" # hicolor
LC="\033[2m" # locolor / dim
###################
fi
if [[ -z "${target_directory}" ]]; then
echo "Usage:" >&2;
echo " $0 {target_directory}" >&2;
echo "" >&2;
echo "" >&2;
exit 0;
fi
if [[ "${UID}" -ne "0" ]]; then
echo "Error: This script must be run as root (as we need to chroot to complete the setup)" >&2;
echo "Additional information: You ran this script as ${USER} with uid ${UID}, but we expected uid 0" >&2;
exit 1;
fi
if [[ ! -d "${target_directory}" ]]; then
mkdir -p "${target_directory}";
fi
target_directory="$(realpath "${target_directory}")";
###############################################################################
run_in_chroot() {
if [[ ! -d "${temp_dir_postprocess}" ]] || [[ -z "${temp_dir_postprocess}" ]]; then
echo "Error: Target directory '${temp_dir_postprocess}' does not exist, so can't chroot into it." >&2;
echo "Additional Information: You tried to run '${*}'" >&2;
return 1;
fi
chroot "${temp_dir_postprocess}" "$@";
}
log_msg() {
echo -e "\n${HC}>>>${RS} ${LC}[ ${SECONDS} ]${RS} $*\n"
}
###############################################################################
# Make sure the current directory is the location of this script to simplify matters
cd "$(dirname "$(readlink -f "$0")")";
config_dir="${PWD}";
temp_dir="$(mktemp --tmpdir -d "minideb-build-XXXXXXX")";
temp_dir_postprocess="${temp_dir}/minideb-${image_version}";
temp_dir_build="$(mktemp -d "${config_dir}/minideb-tmp-XXXXXXX")";
on_exit() {
rm -rf "${temp_dir}";
if [[ -e "${temp_dir_build}" ]]; then rm -rf "${temp_dir_build}"; fi
}
trap on_exit EXIT;
###############################################################################
log_msg "Cloning minideb";
git clone "${clone_url}" "${temp_dir}/minideb";
cd "${temp_dir}/minideb" || { echo "Error: Failed to cd into minideb cloned git repository" >&2; exit 1; };
git checkout "${ref}";
if [[ "${apply_patch_raspbian}" == "true" ]]; then
log_msg "Applying Raspbian patch";
git apply "${config_dir}/minideb-raspbian.patch";
fi
log_msg "Building minideb base image";
# Must be run as root
time TMPDIR="${temp_dir_build}" make "${image_version}";
cp -r "${temp_dir}/minideb/build" "${temp_dir}/build";
filename_tar="$(find "${temp_dir}/build" -name '*.tar' -print -quit)";
log_msg "Unpacking resulting tar archive";
mkdir "${temp_dir_postprocess}";
tar -xf "${filename_tar}" -C "${temp_dir_postprocess}";
# We're finished with minideb now
rm -rf "${temp_dir}/minideb";
###############################################################################
if [[ "${setup_proxy}" == "true" ]]; then
log_msg "Applying apt proxy settings";
echo "Acquire::http { Proxy \"${proxy_address}\"; }" | run_in_chroot tee /etc/apt/apt.conf.d/proxy
fi
# # Fix /dev/null - ref https://unix.stackexchange.com/a/146639/64687
# run_in_chroot rm -f /dev/null;
# run_in_chroot mknod -m 666 /dev/null c 1 3;
# run_in_chroot mknod -m 666 /dev/zero c 1 5
# run_in_chroot chown root:root /dev/null /dev/zero
# run_in_chroot install_packages gnupg;
log_msg "Adding aptosaurus GPG key";
# Add apt.starbeamrainbowlabs.com
# No need to apt update, because install_packages will do this for us
# Ref https://github.com/bitnami/minideb/blob/6c039b8/mkimage#L181-L206
# Also ref https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=851774
gpg_key_url="https://apt.starbeamrainbowlabs.com/aptosaurus.asc";
echo "deb http://apt.starbeamrainbowlabs.com/ /" | run_in_chroot tee /etc/apt/sources.list.d/sbrl.list;
curl -sSL "${gpg_key_url}" | run_in_chroot sh -c 'cat >/etc/apt/trusted.gpg.d/sbrl-aptosaurus.asc';
###############################################################################
# Repack the tar file
log_msg "Repacking archive";
cd "${temp_dir_postprocess}" || { echo "Failed to cd to unpacked archive directory"; exit 1; };
tar -caf "${target_directory}/minideb.tar.gz" .;
cp "${filename_tar}.log" "${target_directory}/minideb.log";
cp "${filename_tar}.manifest" "${target_directory}/minideb.manifest";
log_msg "Written result to ${target_directory}:";
ls -htFl "${target_directory}";