Skip to content

Commit 2701f10

Browse files
committed
Initial build script
1 parent 25b7d54 commit 2701f10

2 files changed

Lines changed: 226 additions & 0 deletions

File tree

build.sh

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/bin/bash -e
2+
3+
run_sub_stage()
4+
{
5+
log "Begin ${SUB_STAGE_DIR}"
6+
pushd ${SUB_STAGE_DIR} > /dev/null
7+
for i in {00..99}; do
8+
if [ -f ${i}-debconf ]; then
9+
log "Begin ${SUB_STAGE_DIR}/${i}-debconf"
10+
on_chroot sh -e - << EOF
11+
debconf-set-selections <<SELEOF
12+
`cat ${i}-debconf`
13+
SELEOF
14+
EOF
15+
log "End ${SUB_STAGE_DIR}/${i}-debconf"
16+
fi
17+
if [ -f ${i}-packages-nr ]; then
18+
log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
19+
PACKAGES=`cat $i-packages-nr | tr '\n' ' '`
20+
if [ -n "$PACKAGES" ]; then
21+
on_chroot sh -e - << EOF
22+
apt-get install --no-install-recommends -y $PACKAGES
23+
EOF
24+
fi
25+
log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
26+
fi
27+
if [ -f ${i}-packages ]; then
28+
log "Begin ${SUB_STAGE_DIR}/${i}-packages"
29+
PACKAGES=`cat $i-packages | tr '\n' ' '`
30+
if [ -n "$PACKAGES" ]; then
31+
on_chroot sh -e - << EOF
32+
apt-get install -y $PACKAGES
33+
EOF
34+
fi
35+
log "End ${SUB_STAGE_DIR}/${i}-packages"
36+
fi
37+
if [ -d ${i}-patches ]; then
38+
log "Begin ${SUB_STAGE_DIR}/${i}-patches"
39+
pushd ${STAGE_WORK_DIR} > /dev/null
40+
if [ "${CLEAN}" = "1" ]; then
41+
rm -rf .pc
42+
rm -rf *-pc
43+
fi
44+
QUILT_PATCHES=${SUB_STAGE_DIR}/${i}-patches
45+
mkdir -p ${i}-pc
46+
ln -sf .pc ${i}-pc
47+
if [ -e ${SUB_STAGE_DIR}/${i}-patches/EDIT ]; then
48+
echo "Dropping into bash to edit patches..."
49+
bash
50+
fi
51+
RC=0
52+
quilt push -a || RC=$?
53+
case "$RC" in
54+
0|2)
55+
;;
56+
*)
57+
false
58+
;;
59+
esac
60+
popd > /dev/null
61+
log "End ${SUB_STAGE_DIR}/${i}-patches"
62+
fi
63+
if [ -x ${i}-run.sh ]; then
64+
log "Begin ${SUB_STAGE_DIR}/${i}-run.sh"
65+
./${i}-run.sh
66+
log "End ${SUB_STAGE_DIR}/${i}-run.sh"
67+
fi
68+
if [ -f ${i}-run-chroot ]; then
69+
log "Begin ${SUB_STAGE_DIR}/${i}-run-chroot"
70+
on_chroot sh -e - < ${i}-run-chroot
71+
log "End ${SUB_STAGE_DIR}/${i}-run-chroot"
72+
fi
73+
done
74+
popd > /dev/null
75+
log "End ${SUB_STAGE_DIR}"
76+
}
77+
78+
run_stage(){
79+
log "Begin ${STAGE_DIR}"
80+
pushd ${STAGE_DIR} > /dev/null
81+
unmount ${WORK_DIR}/${STAGE}
82+
STAGE_WORK_DIR=${WORK_DIR}/${STAGE}
83+
ROOTFS_DIR=${STAGE_WORK_DIR}/rootfs
84+
if [ ! -f SKIP ]; then
85+
if [ "${CLEAN}" = "1" ]; then
86+
if [ -d ${ROOTFS_DIR} ]; then
87+
rm -rf ${ROOTFS_DIR}
88+
fi
89+
fi
90+
if [ -x prerun.sh ]; then
91+
log "Begin ${STAGE_DIR}/prerun.sh"
92+
./prerun.sh
93+
log "End ${STAGE_DIR}/prerun.sh"
94+
fi
95+
for SUB_STAGE_DIR in ${STAGE_DIR}/*; do
96+
if [ -d ${SUB_STAGE_DIR} ]; then
97+
run_sub_stage
98+
fi
99+
done
100+
fi
101+
unmount ${WORK_DIR}/${STAGE}
102+
PREV_STAGE=${STAGE}
103+
PREV_STAGE_DIR=${STAGE_DIR}
104+
PREV_ROOTFS_DIR=${ROOTFS_DIR}
105+
popd > /dev/null
106+
log "End ${STAGE_DIR}"
107+
}
108+
109+
if [ "$(id -u)" != "0" ]; then
110+
echo "Please run as root" 1>&2
111+
exit 1
112+
fi
113+
114+
if [ -f config ]; then
115+
source config
116+
fi
117+
118+
if [ -z "${IMG_NAME}" ]; then
119+
echo "IMG_NAME not set" 1>&2
120+
exit 1
121+
fi
122+
123+
export IMG_DATE=${IMG_DATE:-"$(date -u +%Y-%m-%d)"}
124+
125+
export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
126+
export SCRIPT_DIR="${BASE_DIR}/scripts"
127+
export WORK_DIR="${BASE_DIR}/work/${IMG_DATE}-${IMG_NAME}"
128+
export LOG_FILE="${WORK_DIR}/build.log"
129+
130+
export CLEAN
131+
export IMG_NAME
132+
export APT_PROXY
133+
134+
export STAGE
135+
export PREV_STAGE
136+
export STAGE_DIR
137+
export PREV_STAGE_DIR
138+
export ROOTFS_DIR
139+
export PREV_ROOTFS_DIR
140+
141+
export QUILT_PATCHES
142+
export QUILT_NO_DIFF_INDEX=1
143+
export QUILT_NO_DIFF_TIMESTAMPS=1
144+
export QUILT_REFRESH_ARGS="-p ab"
145+
146+
source ${SCRIPT_DIR}/common
147+
export -f log
148+
export -f bootstrap
149+
export -f unmount
150+
export -f on_chroot
151+
export -f copy_previous
152+
export -f update_issue
153+
154+
mkdir -p ${WORK_DIR}
155+
log "Begin ${BASE_DIR}"
156+
157+
for STAGE_DIR in ${BASE_DIR}/stage*; do
158+
STAGE=$(basename ${STAGE_DIR})
159+
run_stage
160+
done
161+
162+
log "End ${BASE_DIR}"

scripts/common

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
log (){
2+
date +"[%T] $@" | tee -a ${LOG_FILE}
3+
}
4+
5+
bootstrap(){
6+
ARCH=$(dpkg --print-architecture)
7+
8+
export http_proxy=${APT_PROXY}
9+
10+
if [ "$ARCH" != "armhf" ]; then
11+
BOOTSTRAP_CMD=qemu-debootstrap
12+
else
13+
BOOTSTRAP_CMD=debootstrap
14+
fi
15+
16+
${BOOTSTRAP_CMD} --components=main,contrib,non-free \
17+
--arch armhf\
18+
--no-check-gpg \
19+
$1 $2 $3
20+
}
21+
22+
copy_previous(){
23+
if [ ! -d ${PREV_ROOTFS_DIR} ]; then
24+
echo "Previous stage rootfs not found"
25+
false
26+
fi
27+
mkdir -p ${ROOTFS_DIR}
28+
rsync -aHAX ${PREV_ROOTFS_DIR}/ ${ROOTFS_DIR}/
29+
}
30+
31+
unmount(){
32+
if [ -z "$1" ]; then
33+
DIR=$PWD
34+
else
35+
DIR=$1
36+
fi
37+
38+
while mount | grep -q $DIR; do
39+
LOCS=`mount | grep $DIR | cut -f 3 -d ' ' | sort -r`
40+
for loc in $LOCS; do
41+
sudo umount $loc
42+
done
43+
done
44+
}
45+
46+
on_chroot() {
47+
if ! mount | grep -q `realpath ${ROOTFS_DIR}/proc`; then
48+
mount -t proc proc ${ROOTFS_DIR}/proc
49+
fi
50+
51+
if ! mount | grep -q `realpath ${ROOTFS_DIR}/dev`; then
52+
mount --bind /dev ${ROOTFS_DIR}/dev
53+
fi
54+
55+
if ! mount | grep -q `realpath ${ROOTFS_DIR}/sys`; then
56+
mount --bind /sys ${ROOTFS_DIR}/sys
57+
fi
58+
59+
chroot ${ROOTFS_DIR}/ "$@"
60+
}
61+
62+
update_issue() {
63+
echo -e "Raspberry Pi reference ${DATE}\nGenerated using Pi-gen, https://github.com/RPi-Distro/Pi-gen, ${1}" > ${ROOTFS_DIR}/etc/rpi-issue
64+
}

0 commit comments

Comments
 (0)