-
Notifications
You must be signed in to change notification settings - Fork 3
/
onion_buildenv
executable file
·214 lines (166 loc) · 4.61 KB
/
onion_buildenv
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
cd $(dirname $0)
. ./profile
[ -z "$OPENWRT_VERSION" ] && exit 1
[ -z "$TARGET" ] && exit 1
[ -z "$SUBTARGET" ] && exit 1
BUILD_DIR="$PWD"
TARGET_IMAGE_BUILDER_DIR="$BUILD_DIR/openwrt-imagebuilder"
IMAGE_BUILDER_ZIP="$BUILD_DIR/${IMAGE_BUILDER_URL##*/}"
IMAGE_BUILDER_FILE="${IMAGE_BUILDER_ZIP//.tar.xz}"
OUTPUT_DIR="$PWD/output"
DATE=$(date +"%Y%m%d")
VERSION_INFO="${OPENWRT_VERSION}-${DATE}"
download_file() {
local url=$1
local output=$2
[ -e $output ] && return 0
if ! curl "$url" -o "$output"; then
echo "ERROR downloading from $url"
return 1
fi
return 0
}
verify_archive() {
local file=$1
if ! tar tJf "$file" > /dev/null; then
echo "ERROR: invalid archive $file"
return 1
fi
return 0
}
extract_archive() {
local dir=$1
local file=$2
if ! tar -C "$dir" -xJf "$file"; then
echo "ERROR: decompression failed for $file"
return 1
fi
return 0
}
builder_exist() {
[ -d "$TARGET_IMAGE_BUILDER_DIR" ] && return 0
echo "image builder not found. To prepare image builder run"
echo "$0 setup_imagebuilder"
return 1
}
clean_imagebuilder() {
[ -d "$TARGET_IMAGE_BUILDER_DIR" ] && rm -rf "$TARGET_IMAGE_BUILDER_DIR"
[ -f "$IMAGE_BUILDER_ZIP" ] && rm -f "$IMAGE_BUILDER_ZIP"
}
verify_builder() {
echo "> Verifying download"
verify_archive "$IMAGE_BUILDER_ZIP"
}
extract_builder() {
echo "> Extracting compressed Image Builder"
extract_archive "$BUILD_DIR" "$IMAGE_BUILDER_ZIP" || return 1
clean_imagebuilder
mv "$IMAGE_BUILDER_FILE" "$TARGET_IMAGE_BUILDER_DIR" || return 1
cp $TARGET_IMAGE_BUILDER_DIR/repositories.conf $TARGET_IMAGE_BUILDER_DIR/repositories.conf.org
return 0
}
download_builder() {
echo "> Downloading compressed Image Builder"
download_file "$IMAGE_BUILDER_URL" "$IMAGE_BUILDER_ZIP"
return 0
}
prepare_imagebuilder() {
echo "> Preparing Image Builder"
if [ -n "$ADDITIONS_DIR" ] && [ -d "$ADDITIONS_DIR" ]; then
cp -a $ADDITIONS_DIR/* $TARGET_IMAGE_BUILDER_DIR
fi
}
update_package_repos() {
## add packages
echo "> Updating Image Builder Package Repos"
[ -z "$PACKAGE_REPOS" ] && return 0
echo -e "$PACKAGE_REPOS" > $TARGET_IMAGE_BUILDER_DIR/repositories.conf
cat $TARGET_IMAGE_BUILDER_DIR/repositories.conf.org >> $TARGET_IMAGE_BUILDER_DIR/repositories.conf
return 0
}
update_imagebuilder() {
builder_exist || return 1
update_package_repos || return 1
prepare_imagebuilder || return 1
}
setup_imagebuilder() {
download_builder || return 1
verify_builder || return 1
extract_builder || return 1
update_package_repos || return 1
prepare_imagebuilder || return 1
}
build_firmware() {
local profile files_dir packages
profile=$1
if [ -z "$profile" ]; then
echo "missing profile argument"
echo "build_firmware <model>"
return 1
fi
echo "> Building $profile firmware image"
files_dir=$ADDITIONS_DIR/files
[ -n "$IMAGE_BUILDER_PACKAGES" ] && packages=$(echo -e $IMAGE_BUILDER_PACKAGES | tr '\n' ' ')
make -C "$TARGET_IMAGE_BUILDER_DIR" image PROFILE=$profile PACKAGES="$packages" FILES="$files_dir" || return 1
# Config CONFIG_VERSION_NUMBER is empty in OpenWRT imagebuilder 23.05 version by default. But profiles.json file
# determines omega final image name with version field included. We can extract the version from our OPENWRT_VERSION
# variable in this case
if [ -z $(awk -F= '/^CONFIG_VERSION_NUMBER/{print $2}' $TARGET_IMAGE_BUILDER_DIR/.config | tr -d '"') ]; then
BUILT_VERSION=$OPENWRT_VERSION
else
BUILT_VERSION=$(awk -F= '/^CONFIG_VERSION_NUMBER/{print $2}' $TARGET_IMAGE_BUILDER_DIR/.config | tr -d '"')
fi
if [ $? -ne 0 ]; then
echo "ERROR during image building for $profile"
return 1
else
[ ! -d "$OUTPUT_DIR" ] && mkdir -p $OUTPUT_DIR
target_image="$OUTPUT_DIR/${profile}-${VERSION_INFO}.bin"
cp $TARGET_IMAGE_BUILDER_DIR/bin/targets/${TARGET}/${SUBTARGET}/openwrt-${BUILT_VERSION}-${TARGET}-${SUBTARGET}-${profile}-squashfs-sysupgrade.bin $target_image
fi
}
build_all_firmware() {
local profiles="$@"
local prof
echo "> Building all firmware images"
profiles="${profiles:-${BUILD_MODELS}}"
for prof in $profiles; do
if build_firmware "$prof"; then
echo "> Compiled firmware at $OUTPUT_DIR"
else
echo "> Compilation error!"
return 1
fi
done
}
if [ -z "$TARGET_IMAGE_BUILDER_DIR" ]; then
echo "TARGET_IMAGE_BUILDER_DIR can not be empty string"
exit 1
fi
commands="
setup_imagebuilder
update_imagebuilder
clean_imagebuilder
build_firmware
build_all_firmware
"
usage_help() {
local cmd
echo "$0: "
for cmd in $commands; do
echo -e "\t$cmd"
done
}
if [ $# -lt 1 ]; then
usage_help
exit 1
fi
if [ "$(type -t $1)" != "function" ]; then
echo "$1: function not found"
usage_help
exit 1
fi
cmd=$1
shift
$cmd $@