From 2701f10c909dee234185ddf082728fd661b941de Mon Sep 17 00:00:00 2001 From: Serge Schneider Date: Mon, 11 Apr 2016 07:21:07 +0100 Subject: [PATCH] Initial build script --- build.sh | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ scripts/common | 64 +++++++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/build.sh b/build.sh index e69de29..e3f7a6e 100755 --- a/build.sh +++ b/build.sh @@ -0,0 +1,162 @@ +#!/bin/bash -e + +run_sub_stage() +{ + log "Begin ${SUB_STAGE_DIR}" + pushd ${SUB_STAGE_DIR} > /dev/null + for i in {00..99}; do + if [ -f ${i}-debconf ]; then + log "Begin ${SUB_STAGE_DIR}/${i}-debconf" + on_chroot sh -e - << EOF +debconf-set-selections < /dev/null + if [ "${CLEAN}" = "1" ]; then + rm -rf .pc + rm -rf *-pc + fi + QUILT_PATCHES=${SUB_STAGE_DIR}/${i}-patches + mkdir -p ${i}-pc + ln -sf .pc ${i}-pc + if [ -e ${SUB_STAGE_DIR}/${i}-patches/EDIT ]; then + echo "Dropping into bash to edit patches..." + bash + fi + RC=0 + quilt push -a || RC=$? + case "$RC" in + 0|2) + ;; + *) + false + ;; + esac + popd > /dev/null + log "End ${SUB_STAGE_DIR}/${i}-patches" + fi + if [ -x ${i}-run.sh ]; then + log "Begin ${SUB_STAGE_DIR}/${i}-run.sh" + ./${i}-run.sh + log "End ${SUB_STAGE_DIR}/${i}-run.sh" + fi + if [ -f ${i}-run-chroot ]; then + log "Begin ${SUB_STAGE_DIR}/${i}-run-chroot" + on_chroot sh -e - < ${i}-run-chroot + log "End ${SUB_STAGE_DIR}/${i}-run-chroot" + fi + done + popd > /dev/null + log "End ${SUB_STAGE_DIR}" +} + +run_stage(){ + log "Begin ${STAGE_DIR}" + pushd ${STAGE_DIR} > /dev/null + unmount ${WORK_DIR}/${STAGE} + STAGE_WORK_DIR=${WORK_DIR}/${STAGE} + ROOTFS_DIR=${STAGE_WORK_DIR}/rootfs + if [ ! -f SKIP ]; then + if [ "${CLEAN}" = "1" ]; then + if [ -d ${ROOTFS_DIR} ]; then + rm -rf ${ROOTFS_DIR} + fi + fi + if [ -x prerun.sh ]; then + log "Begin ${STAGE_DIR}/prerun.sh" + ./prerun.sh + log "End ${STAGE_DIR}/prerun.sh" + fi + for SUB_STAGE_DIR in ${STAGE_DIR}/*; do + if [ -d ${SUB_STAGE_DIR} ]; then + run_sub_stage + fi + done + fi + unmount ${WORK_DIR}/${STAGE} + PREV_STAGE=${STAGE} + PREV_STAGE_DIR=${STAGE_DIR} + PREV_ROOTFS_DIR=${ROOTFS_DIR} + popd > /dev/null + log "End ${STAGE_DIR}" +} + +if [ "$(id -u)" != "0" ]; then + echo "Please run as root" 1>&2 + exit 1 +fi + +if [ -f config ]; then + source config +fi + +if [ -z "${IMG_NAME}" ]; then + echo "IMG_NAME not set" 1>&2 + exit 1 +fi + +export IMG_DATE=${IMG_DATE:-"$(date -u +%Y-%m-%d)"} + +export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +export SCRIPT_DIR="${BASE_DIR}/scripts" +export WORK_DIR="${BASE_DIR}/work/${IMG_DATE}-${IMG_NAME}" +export LOG_FILE="${WORK_DIR}/build.log" + +export CLEAN +export IMG_NAME +export APT_PROXY + +export STAGE +export PREV_STAGE +export STAGE_DIR +export PREV_STAGE_DIR +export ROOTFS_DIR +export PREV_ROOTFS_DIR + +export QUILT_PATCHES +export QUILT_NO_DIFF_INDEX=1 +export QUILT_NO_DIFF_TIMESTAMPS=1 +export QUILT_REFRESH_ARGS="-p ab" + +source ${SCRIPT_DIR}/common +export -f log +export -f bootstrap +export -f unmount +export -f on_chroot +export -f copy_previous +export -f update_issue + +mkdir -p ${WORK_DIR} +log "Begin ${BASE_DIR}" + +for STAGE_DIR in ${BASE_DIR}/stage*; do + STAGE=$(basename ${STAGE_DIR}) + run_stage +done + +log "End ${BASE_DIR}" diff --git a/scripts/common b/scripts/common index e69de29..8c0e914 100644 --- a/scripts/common +++ b/scripts/common @@ -0,0 +1,64 @@ +log (){ + date +"[%T] $@" | tee -a ${LOG_FILE} +} + +bootstrap(){ + ARCH=$(dpkg --print-architecture) + + export http_proxy=${APT_PROXY} + + if [ "$ARCH" != "armhf" ]; then + BOOTSTRAP_CMD=qemu-debootstrap + else + BOOTSTRAP_CMD=debootstrap + fi + + ${BOOTSTRAP_CMD} --components=main,contrib,non-free \ + --arch armhf\ + --no-check-gpg \ + $1 $2 $3 +} + +copy_previous(){ + if [ ! -d ${PREV_ROOTFS_DIR} ]; then + echo "Previous stage rootfs not found" + false + fi + mkdir -p ${ROOTFS_DIR} + rsync -aHAX ${PREV_ROOTFS_DIR}/ ${ROOTFS_DIR}/ +} + +unmount(){ + if [ -z "$1" ]; then + DIR=$PWD + else + DIR=$1 + fi + + while mount | grep -q $DIR; do + LOCS=`mount | grep $DIR | cut -f 3 -d ' ' | sort -r` + for loc in $LOCS; do + sudo umount $loc + done + done +} + +on_chroot() { + if ! mount | grep -q `realpath ${ROOTFS_DIR}/proc`; then + mount -t proc proc ${ROOTFS_DIR}/proc + fi + + if ! mount | grep -q `realpath ${ROOTFS_DIR}/dev`; then + mount --bind /dev ${ROOTFS_DIR}/dev + fi + + if ! mount | grep -q `realpath ${ROOTFS_DIR}/sys`; then + mount --bind /sys ${ROOTFS_DIR}/sys + fi + + chroot ${ROOTFS_DIR}/ "$@" +} + +update_issue() { + echo -e "Raspberry Pi reference ${DATE}\nGenerated using Pi-gen, https://github.com/RPi-Distro/Pi-gen, ${1}" > ${ROOTFS_DIR}/etc/rpi-issue +}