2016-04-11 06:21:07 +00:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
##---------------------------
|
|
|
|
## Functions
|
|
|
|
##---------------------------
|
|
|
|
|
2016-04-11 06:21:07 +00:00
|
|
|
run_sub_stage()
|
|
|
|
{
|
|
|
|
log "Begin ${SUB_STAGE_DIR}"
|
2016-05-02 19:14:23 +00:00
|
|
|
|
2016-04-11 06:21:07 +00:00
|
|
|
pushd ${SUB_STAGE_DIR} > /dev/null
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Loop through each substage
|
2016-04-11 06:21:07 +00:00
|
|
|
for i in {00..99}; do
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Check for debconf stage
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -f ${i}-debconf ]; then
|
|
|
|
log "Begin ${SUB_STAGE_DIR}/${i}-debconf"
|
|
|
|
on_chroot sh -e - << EOF
|
|
|
|
debconf-set-selections <<SELEOF
|
|
|
|
`cat ${i}-debconf`
|
|
|
|
SELEOF
|
|
|
|
EOF
|
|
|
|
log "End ${SUB_STAGE_DIR}/${i}-debconf"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Install any packages with no-install-recommends set
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -f ${i}-packages-nr ]; then
|
|
|
|
log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
|
|
|
|
PACKAGES=`cat $i-packages-nr | tr '\n' ' '`
|
|
|
|
if [ -n "$PACKAGES" ]; then
|
|
|
|
on_chroot sh -e - << EOF
|
|
|
|
apt-get install --no-install-recommends -y $PACKAGES
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Install any packages normally
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -f ${i}-packages ]; then
|
|
|
|
log "Begin ${SUB_STAGE_DIR}/${i}-packages"
|
|
|
|
PACKAGES=`cat $i-packages | tr '\n' ' '`
|
|
|
|
if [ -n "$PACKAGES" ]; then
|
|
|
|
on_chroot sh -e - << EOF
|
|
|
|
apt-get install -y $PACKAGES
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
log "End ${SUB_STAGE_DIR}/${i}-packages"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Apply any patches
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -d ${i}-patches ]; then
|
|
|
|
log "Begin ${SUB_STAGE_DIR}/${i}-patches"
|
|
|
|
pushd ${STAGE_WORK_DIR} > /dev/null
|
|
|
|
if [ "${CLEAN}" = "1" ]; then
|
|
|
|
rm -rf .pc
|
|
|
|
rm -rf *-pc
|
|
|
|
fi
|
|
|
|
QUILT_PATCHES=${SUB_STAGE_DIR}/${i}-patches
|
|
|
|
mkdir -p ${i}-pc
|
2016-05-05 15:55:35 +00:00
|
|
|
ln -sf ${i}-pc .pc
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -e ${SUB_STAGE_DIR}/${i}-patches/EDIT ]; then
|
|
|
|
echo "Dropping into bash to edit patches..."
|
|
|
|
bash
|
|
|
|
fi
|
2016-05-05 15:55:35 +00:00
|
|
|
quilt upgrade
|
2016-04-11 06:21:07 +00:00
|
|
|
RC=0
|
|
|
|
quilt push -a || RC=$?
|
|
|
|
case "$RC" in
|
|
|
|
0|2)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
false
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
popd > /dev/null
|
|
|
|
log "End ${SUB_STAGE_DIR}/${i}-patches"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Run the substages run script
|
2016-04-11 06:21:07 +00:00
|
|
|
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
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Run the substages chroot script
|
2016-04-11 06:21:07 +00:00
|
|
|
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}"
|
2016-05-05 22:32:31 +00:00
|
|
|
|
2016-05-04 14:51:41 +00:00
|
|
|
STAGE=$(basename ${STAGE_DIR})
|
2016-05-02 19:14:23 +00:00
|
|
|
|
2016-04-11 06:21:07 +00:00
|
|
|
pushd ${STAGE_DIR} > /dev/null
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Unmount this stage's folder on the filesystem
|
2016-04-11 06:21:07 +00:00
|
|
|
unmount ${WORK_DIR}/${STAGE}
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Set the working directory for this stage
|
2016-04-11 06:21:07 +00:00
|
|
|
STAGE_WORK_DIR=${WORK_DIR}/${STAGE}
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Set the root directory for this stage
|
2016-04-11 06:21:07 +00:00
|
|
|
ROOTFS_DIR=${STAGE_WORK_DIR}/rootfs
|
2016-05-05 22:32:31 +00:00
|
|
|
|
2016-05-04 14:51:41 +00:00
|
|
|
if [ -f ${STAGE_DIR}/EXPORT_IMAGE ]; then
|
|
|
|
EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Check to see if we should skip this stage (seemingly never)
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ ! -f SKIP ]; then
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Clean the rootfs, if requested
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ "${CLEAN}" = "1" ]; then
|
|
|
|
if [ -d ${ROOTFS_DIR} ]; then
|
|
|
|
rm -rf ${ROOTFS_DIR}
|
|
|
|
fi
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Run the pre-run script
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ -x prerun.sh ]; then
|
|
|
|
log "Begin ${STAGE_DIR}/prerun.sh"
|
|
|
|
./prerun.sh
|
|
|
|
log "End ${STAGE_DIR}/prerun.sh"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# For each substage, run the run_sub_stage command for it
|
2016-04-11 06:21:07 +00:00
|
|
|
for SUB_STAGE_DIR in ${STAGE_DIR}/*; do
|
2016-05-04 14:51:41 +00:00
|
|
|
if [ -d ${SUB_STAGE_DIR} ] &&
|
|
|
|
[ ! -f ${SUB_STAGE_DIR}/SKIP ]; then
|
2016-04-11 06:21:07 +00:00
|
|
|
run_sub_stage
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Unmount the stag again
|
2016-04-11 06:21:07 +00:00
|
|
|
unmount ${WORK_DIR}/${STAGE}
|
2016-05-02 19:14:23 +00:00
|
|
|
|
|
|
|
# Set the previous stage info to this stage for the next stage to use
|
2016-04-11 06:21:07 +00:00
|
|
|
PREV_STAGE=${STAGE}
|
|
|
|
PREV_STAGE_DIR=${STAGE_DIR}
|
|
|
|
PREV_ROOTFS_DIR=${ROOTFS_DIR}
|
2016-05-02 19:14:23 +00:00
|
|
|
|
2016-04-11 06:21:07 +00:00
|
|
|
popd > /dev/null
|
2016-05-02 19:14:23 +00:00
|
|
|
|
2016-04-11 06:21:07 +00:00
|
|
|
log "End ${STAGE_DIR}"
|
|
|
|
}
|
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
##---------------------------
|
|
|
|
## Start Build
|
|
|
|
##---------------------------
|
|
|
|
|
2016-05-02 19:14:23 +00:00
|
|
|
# Require Root to run
|
2016-04-11 06:21:07 +00:00
|
|
|
if [ "$(id -u)" != "0" ]; then
|
|
|
|
echo "Please run as root" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
# Handle input options
|
|
|
|
for i in "$@"
|
|
|
|
do
|
|
|
|
case $i in
|
|
|
|
|
|
|
|
--imagename=*)
|
|
|
|
IMG_NAME="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
# Username to use in rootfs
|
|
|
|
--username=*)
|
|
|
|
USER_NAME="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
# Hostname to use in rootfs
|
|
|
|
--password=*)
|
|
|
|
PASS_WORD="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
# Hostname to use in rootfs
|
|
|
|
--hostname=*)
|
|
|
|
HOST_NAME="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
# unknown option
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "${IMG_NAME}" ];
|
|
|
|
then
|
|
|
|
echo "No image name specified, defaulting to \"raspbian\""
|
|
|
|
IMG_NAME="raspbian"
|
2016-04-11 06:21:07 +00:00
|
|
|
fi
|
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
if [ -z "$USER_NAME" ]
|
|
|
|
then
|
|
|
|
echo "No username specified, defaulting to \"pi\""
|
|
|
|
USER_NAME="pi"
|
|
|
|
fi
|
2016-05-02 19:14:23 +00:00
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
if [ -z "$PASS_WORD" ]
|
|
|
|
then
|
|
|
|
echo "No username specified, defaulting to \"raspberry\""
|
|
|
|
PASS_WORD="raspberry"
|
2016-04-11 06:21:07 +00:00
|
|
|
fi
|
|
|
|
|
2016-05-05 22:08:10 +00:00
|
|
|
if [ -z "$HOST_NAME" ]
|
|
|
|
then
|
|
|
|
echo "No hostname specified, defaulting to \"raspberrypi\""
|
|
|
|
HOST_NAME="raspberrypi"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Source a config file if it exists
|
|
|
|
if [ -f config ]; then
|
|
|
|
source config
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set and export other env variables
|
|
|
|
export USER_NAME
|
|
|
|
export HOST_NAME
|
|
|
|
export PASS_WORD
|
|
|
|
export IMG_NAME
|
2016-04-11 06:21:07 +00:00
|
|
|
|
|
|
|
export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
export SCRIPT_DIR="${BASE_DIR}/scripts"
|
2016-05-05 22:08:10 +00:00
|
|
|
export WORK_DIR="${BASE_DIR}/work/${IMG_NAME}"
|
2016-05-04 14:51:41 +00:00
|
|
|
export DEPLOY_DIR="${BASE_DIR}/deploy"
|
2016-04-11 06:21:07 +00:00
|
|
|
export LOG_FILE="${WORK_DIR}/build.log"
|
|
|
|
|
|
|
|
export CLEAN
|
|
|
|
export APT_PROXY
|
|
|
|
|
|
|
|
export STAGE
|
|
|
|
export STAGE_DIR
|
2016-05-04 14:51:41 +00:00
|
|
|
export STAGE_WORK_DIR
|
|
|
|
export PREV_STAGE
|
2016-04-11 06:21:07 +00:00
|
|
|
export PREV_STAGE_DIR
|
|
|
|
export ROOTFS_DIR
|
|
|
|
export PREV_ROOTFS_DIR
|
2016-05-04 14:51:41 +00:00
|
|
|
export IMG_SUFFIX
|
|
|
|
export EXPORT_DIR
|
|
|
|
export EXPORT_ROOTFS_DIR
|
2016-04-11 06:21:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-05-02 19:14:23 +00:00
|
|
|
# Create working directory
|
2016-04-11 06:21:07 +00:00
|
|
|
mkdir -p ${WORK_DIR}
|
|
|
|
log "Begin ${BASE_DIR}"
|
|
|
|
|
2016-05-02 19:14:23 +00:00
|
|
|
# Successively build each stage
|
2016-04-11 06:21:07 +00:00
|
|
|
for STAGE_DIR in ${BASE_DIR}/stage*; do
|
2016-05-04 14:51:41 +00:00
|
|
|
run_stage
|
|
|
|
done
|
|
|
|
|
|
|
|
STAGE_DIR=${BASE_DIR}/export-image
|
|
|
|
|
|
|
|
CLEAN=1
|
|
|
|
for EXPORT_DIR in ${EXPORT_DIRS}; do
|
|
|
|
IMG_SUFFIX=$(cat ${EXPORT_DIR}/EXPORT_IMAGE)
|
|
|
|
EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename ${EXPORT_DIR})/rootfs
|
2016-04-11 06:21:07 +00:00
|
|
|
run_stage
|
|
|
|
done
|
|
|
|
|
|
|
|
log "End ${BASE_DIR}"
|