-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·122 lines (101 loc) · 2.66 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
set -e
trap 'catch' ERR
catch() {
echo " ERROR: could not complete installation"
exit 1
}
EXTRA_CONFIG_FILE=${1}
set -a
source config
[ ! -z "${EXTRA_CONFIG_FILE}" ] && source "${EXTRA_CONFIG_FILE}"
set +a
##### functions
logger (){
date +"%F %T | PID $$ | $0: $*" | tee -a "${LOG_FILE}"
}
export -f logger
nb_chroot (){
setarch "${SETARCH_ARCHITECTURE}" capsh --drop=cap_setfcap "--chroot=${ROOTFS}" -- -e "$@"
}
export -f nb_chroot
export DEBIAN_FRONTEND=noninteractive
unmount (){
if [ -z "$1" ]; then
DIR=$PWD
else
DIR=$1
fi
while mount | grep -q "$DIR"; do
local LOCS
LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
for loc in $LOCS; do
umount "$loc"
done
done
}
export -f unmount
unmount_image (){
sync
sleep 1
local LOOP_DEVICES
LOOP_DEVICES=$(losetup --list | grep "$(basename "${1}")" | cut -f1 -d' ')
for LOOP_DEV in ${LOOP_DEVICES}; do
if [ -n "${LOOP_DEV}" ]; then
local MOUNTED_DIR
MOUNTED_DIR=$(mount | grep "$(basename "${LOOP_DEV}")" | head -n 1 | cut -f 3 -d ' ')
if [ -n "${MOUNTED_DIR}" ] && [ "${MOUNTED_DIR}" != "/" ]; then
unmount "$(dirname "${MOUNTED_DIR}")"
fi
sleep 1
losetup -d "${LOOP_DEV}"
fi
done
}
export -f unmount_image
##### end of functions
# where the Debian filesystem is mounted
ROOTFS=${ROOTFS:-""}
if [ -z "${ROOTFS}" ]; then
echo "ERROR: ROOTFS is not defined! Maybe you're missing WORKDIR from your config"
exit 1
fi
[ -z "${IMAGE_NAME}" ] && logger "Please define IMAGE_NAME in the configuration file" && exit 1
export IMAGE_NAME_SUFFIX=$(date +"%s")
mkdir -p "${WORKDIR}"
# only for simple-cdd builds
export PROFILES="${WORKDIR}/profiles"
##
logger "Starting image builder for ${IMAGE_NAME} at ${WORKDIR}"
for folder in ${STAGE_BUNDLES}
do
# remove trailing slash just in case
folder=${folder%"/"}
logger " ##### Running in stage bundle ${folder}"
# check if there's an execution order for the stages, otherwise they'll be executed alphabetically
if [ -f "${folder}/.order" ]
then
stages=$(cat "${folder}/.order")
else
stages=$(ls "${folder}")
fi
# Execute stages one by one
for stage in ${stages}
do
export STAGE="${folder}/${stage}"
if [ -f "${folder}/${stage}/run_stage.sh" ]
then
logger " ########## Running stage ${stage}"
pushd "${folder}/${stage}" > /dev/null
bash run_stage.sh
popd > /dev/null
else
logger " *x*x*x*x*x*x*x*x* Stage ${stage} is missing the run_stage.sh executable...skipping stage"
fi
done
done
for image in $(find "${WORKDIR}" -name "*.zip")
do
cp -fr "${image}" .
logger "Copied ${image} to current directory"
done