-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
189 lines (166 loc) · 4.92 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
set -e
set -o pipefail
VANIR_DIST="vanir-rolling"
VANIR_VERSION=""
VANIR_VARIANT="default"
TARGET_DIR="$(dirname $0)/images"
TARGET_SUBDIR=""
SUDO="sudo"
VERBOSE=""
HOST_ARCH=$(dpkg --print-architecture)
image_name() {
local arch=$1
case "$arch" in
i386|amd64)
IMAGE_TEMPLATE="live-image-ARCH.hybrid.iso"
;;
armel|armhf)
IMAGE_TEMPLATE="live-image-ARCH.img"
;;
esac
echo $IMAGE_TEMPLATE | sed -e "s/ARCH/$arch/"
}
target_image_name() {
local arch=$1
IMAGE_NAME="$(image_name $arch)"
IMAGE_EXT="${IMAGE_NAME##*.}"
if [ "$IMAGE_EXT" = "$IMAGE_NAME" ]; then
IMAGE_EXT="img"
fi
if [ "$VANIR_VARIANT" = "default" ]; then
echo "${TARGET_SUBDIR:+$TARGET_SUBDIR/}vanir-linux-$VANIR_VERSION-$VANIR_ARCH.$IMAGE_EXT"
else
echo "${TARGET_SUBDIR:+$TARGET_SUBDIR/}vanir-linux-$VANIR_VARIANT-$VANIR_VERSION-$VANIR_ARCH.$IMAGE_EXT"
fi
}
target_build_log() {
TARGET_IMAGE_NAME=$(target_image_name $1)
echo ${TARGET_IMAGE_NAME%.*}.log
}
default_version() {
case "$1" in
vanir-*)
echo "${1#vanir-}"
;;
*)
echo "$1"
;;
esac
}
failure() {
# Cleanup update-vanir-menu that might stay around so that the
# build chroot can be properly unmounted
$SUDO pkill -f update-vanir-menu || true
echo "Build of $VANIR_DIST/$VANIR_VARIANT/$VANIR_ARCH live image failed (see build.log for details)" >&2
exit 2
}
run_and_log() {
if [ -n "$VERBOSE" ]; then
"$@" 2>&1 | tee -a build.log
else
"$@" >>build.log 2>&1
fi
return $?
}
. $(dirname $0)/.getopt.sh
# Parsing command line options
temp=$(getopt -o "$BUILD_OPTS_SHORT" -l "$BUILD_OPTS_LONG,get-image-path" -- "$@")
eval set -- "$temp"
while true; do
case "$1" in
-d|--distribution) VANIR_DIST="$2"; shift 2; ;;
-p|--proposed-updates) OPT_pu="1"; shift 1; ;;
-a|--arch) VANIR_ARCHES="${VANIR_ARCHES:+$VANIR_ARCHES } $2"; shift 2; ;;
-v|--verbose) VERBOSE="1"; shift 1; ;;
-s|--salt) shift; ;;
--variant) VANIR_VARIANT="$2"; shift 2; ;;
--version) VANIR_VERSION="$2"; shift 2; ;;
--subdir) TARGET_SUBDIR="$2"; shift 2; ;;
--get-image-path) ACTION="get-image-path"; shift 1; ;;
--) shift; break; ;;
*) echo "ERROR: Invalid command-line option: $1" >&2; exit 1; ;;
esac
done
# Set default values
VANIR_ARCHES=${VANIR_ARCHES:-$HOST_ARCH}
if [ -z "$VANIR_VERSION" ]; then
VANIR_VERSION="$(default_version $VANIR_DIST)"
fi
# Check parameters
for arch in $VANIR_ARCHES; do
if [ "$arch" = "$HOST_ARCH" ]; then
continue
fi
case "$HOST_ARCH/$arch" in
amd64/i386|i386/amd64)
;;
*)
echo "Can't build $arch image on $HOST_ARCH system." >&2
exit 1
;;
esac
done
if [ ! -d "$(dirname $0)/vanir-config/variant-$VANIR_VARIANT" ]; then
echo "ERROR: Unknown variant of Vanir configuration: $VANIR_VARIANT" >&2
fi
# Build parameters for lb config
VANIR_CONFIG_OPTS="--distribution $VANIR_DIST -- --variant $VANIR_VARIANT"
if [ -n "$OPT_pu" ]; then
VANIR_CONFIG_OPTS="$VANIR_CONFIG_OPTS --proposed-updates"
VANIR_DIST="$VANIR_DIST+pu"
fi
# helper for apt-cacher-ng
if [ -n "$http_proxy" ]; then
VANIR_CONFIG_OPTS="$VANIR_CONFIG_OPTS --apt-http-proxy $http_proxy"
fi
# Set sane PATH (cron seems to lack /sbin/ dirs)
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Either we use a git checkout of live-build
# export LIVE_BUILD=/srv/cdimage.vanir.org/live/live-build
# Or we ensure we have proper version installed
ver_live_build=$(dpkg-query -f '${Version}' -W live-build)
if dpkg --compare-versions "$ver_live_build" lt 1:20151215kali1; then
echo "ERROR: You need live-build (>= 1:20151215kali1), you have $ver_live_build" >&2
exit 1
fi
# Check we have a good debootstrap
ver_debootstrap=$(dpkg-query -f '${Version}' -W debootstrap)
if dpkg --compare-versions "$ver_debootstrap" lt "1.0.97"; then
if ! echo "$ver_debootstrap" | grep -q vanir; then
echo "ERROR: You need debootstrap >= 1.0.97 (or a Vanir patched debootstrap). Your current version: $ver_debootstrap" >&2
exit 1
fi
fi
# We need root rights at some point
if [ "$(whoami)" != "root" ]; then
if ! which $SUDO >/dev/null; then
echo "ERROR: $0 is not run as root and $SUDO is not available" >&2
exit 1
fi
else
SUDO="" # We're already root
fi
if [ "$ACTION" = "get-image-path" ]; then
for VANIR_ARCH in $VANIR_ARCHES; do
echo $(target_image_name $VANIR_ARCH)
done
exit 0
fi
cd $(dirname $0)
mkdir -p $TARGET_DIR/$TARGET_SUBDIR
for VANIR_ARCH in $VANIR_ARCHES; do
IMAGE_NAME="$(image_name $VANIR_ARCH)"
set +e
: > build.log
run_and_log $SUDO lb clean --purge
[ $? -eq 0 ] || failure
run_and_log lb config -a $VANIR_ARCH $VANIR_CONFIG_OPTS "$@"
[ $? -eq 0 ] || failure
run_and_log $SUDO lb build
if [ $? -ne 0 ] || [ ! -e $IMAGE_NAME ]; then
failure
fi
set -e
mv -f $IMAGE_NAME $TARGET_DIR/$(target_image_name $VANIR_ARCH)
mv -f build.log $TARGET_DIR/$(target_build_log $VANIR_ARCH)
done