-
Notifications
You must be signed in to change notification settings - Fork 39
/
build.sh
executable file
·308 lines (261 loc) · 6.44 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh
set -e
arch_lxd=x86_64
ver=22.03.5
dist=openwrt
type=lxd
super=fakeroot
# iptables-mod-checksum is required by the work-around inserted by files/etc/uci-defaults/70_fill-dhcp-checksum.
packages=iptables-mod-checksum
upgrade=
# Workaround for Debian/Ubuntu systems which use C.UTF-8 which is unsupported by OpenWrt
export LC_ALL=C
usage() {
echo "Usage: $0 [-a|--arch x86_64|i686|aarch64|aarch32] [-v|--version <version>] [-p|--packages <packages>] [-f|--files] [-t|--type lxd|plain] [-s|--super fakeroot|sudo] [-u|--upgrade] [--help]"
exit 1
}
temp=$(getopt -o "a:v:p:f:t:s:u" -l "arch:,version:,packages:,files:,type:,super:,upgrade,help" -- "$@")
eval set -- "$temp"
while true; do
case "$1" in
-a|--arch)
arch_lxd="$2"; shift 2;;
-v|--version)
ver="$2"; shift 2;;
-p|--packages)
packages="$2"; shift 2;;
-f|--files)
files="$2"; shift 2;;
-t|--type)
type="$2"
shift 2
case "$type" in
lxd|plain)
;;
*)
usage;;
esac;;
-s|--super)
super="$2"
shift 2
case "$super" in
fakeroot|sudo)
;;
*)
usage;;
esac;;
-u|--upgrade)
upgrade="1"
shift 1
;;
--help)
usage;;
--)
shift; break;;
esac
done
if [ $# -ne 0 ]; then
usage
fi
case "$arch_lxd" in
i686)
arch=x86
subarch=generic
arch_ipk=i386_pentium4
;;
x86_64)
arch=x86
subarch=64
arch_ipk=x86_64
;;
aarch64)
arch=armvirt
subarch=64
arch_ipk=aarch64_generic
;;
aarch32)
arch=armvirt
subarch=32
arch_ipk=aarch32_generic
;;
*)
usage
;;
esac
branch_ver=$(echo "${ver}"|cut -d- -f1|cut -d. -f1,2)
if test $ver = snapshot; then
openwrt_branch=snapshot
procd_url=https://github.com/openwrt/openwrt/trunk/package/system/procd
openwrt_url=https://downloads.openwrt.org/snapshots/targets/${arch}/${subarch}
else
openwrt_branch=${dist}-${branch_ver}
procd_url=https://github.com/openwrt/openwrt/branches/${openwrt_branch}/package/system/procd
openwrt_url=https://downloads.openwrt.org/releases/${ver}/targets/${arch}/${subarch}
fi
procd_extra_ver=lxd-3
tarball=bin/${dist}-${ver}-${arch}-${subarch}-${type}.tar.gz
metadata=bin/metadata.yaml
pkgdir=bin/${ver}/packages/${arch}/${subarch}
detect_url() {
local pattern="$1"
download_sums ${openwrt_url}/dummy
local sums=$return
return=$(cat $sums|grep "$pattern"|cut -d' ' -f2-|cut -c2-)
if [ -z "$return" ]; then
echo "URL autodetection failed: $pattern"
exit 1
fi
}
download_rootfs() {
detect_url "rootfs\.tar"
local rootfs_url=$openwrt_url/$return
# global $rootfs
rootfs=dl/$(basename $rootfs_url)
download $rootfs_url $rootfs
check $rootfs $rootfs_url
}
download_sdk() {
detect_url "sdk"
local sdk_url=$openwrt_url/$return
local sdk_tar=dl/$(basename $sdk_url)
download $sdk_url $sdk_tar
check $sdk_tar $sdk_url
# global $sdk
sdk="build_dir/$(tar tf $sdk_tar|head -1)"
if ! test -e $sdk; then
test -e build_dir || mkdir build_dir
tar xvf $sdk_tar -C build_dir
fi
}
download() {
url=$1
dst=$2
dir=$(dirname $dst)
if ! test -e "$dst" ; then
echo Downloading $url
test -e $dir || mkdir $dir
wget -O $dst "$url"
fi
}
download_sums() {
local url=$1
local sums_url="$(dirname $url)/sha256sums"
local sums_file="dl/sha256sums_$(echo $sums_url|md5sum|cut -d ' ' -f 1)"
if ! test -e $sums_file; then
test -e "dl" || mkdir "dl"
wget -O $sums_file $sums_url
fi
return=$sums_file
}
check() {
local dst=$1
local dst_url=$2
download_sums $dst_url
local sums=$return
local dst_sum="$(grep $(basename $dst_url) $sums|cut -d ' ' -f 1)"
sum=$(sha256sum $dst| cut -d ' ' -f1)
if test -z "$dst_sum" -o "$dst_sum" != $sum; then
echo Bad checksum $sum of $dst
exit 1
fi
}
need_procd() {
if ls patches/procd-${openwrt_branch}/*.patch 2>/dev/null >/dev/null; then
return 0
else
return 1
fi
}
download_procd() {
if ! test -e dl/procd-${openwrt_branch}; then
svn export $procd_url dl/procd-${openwrt_branch}
sed -i -e "s/PKG_RELEASE:=\(\S\+\)/PKG_RELEASE:=\1-${procd_extra_ver}/" dl/procd-${openwrt_branch}/Makefile
fi
test -e dl/procd-${openwrt_branch}/patches || mkdir dl/procd-${openwrt_branch}/patches
cp -a patches/procd-${openwrt_branch}/*.patch dl/procd-${openwrt_branch}/patches
}
build_procd() {
rm $sdk/package/lxd-procd||true
ln -sfT $(pwd)/dl/procd-${openwrt_branch} $sdk/package/lxd-procd
local date=$(grep PKG_SOURCE_DATE:= dl/procd-${openwrt_branch}/Makefile | cut -d '=' -f 2)
local version=$(grep PKG_SOURCE_VERSION:= dl/procd-${openwrt_branch}/Makefile | cut -d '=' -f 2 | cut -b '1-8')
local release=$(grep PKG_RELEASE:= dl/procd-${openwrt_branch}/Makefile | cut -d '=' -f 2)
local ipk_old=$sdk/bin/targets/${arch}/${subarch}/packages/procd_${date}-${version}-${release}_*.ipk
local ipk_new=$sdk/bin/packages/${arch_ipk}/base/procd_${date}-${version}-${release}_*.ipk
if test $ver \< 18; then
local ipk=$ipk_old
else
local ipk=$ipk_new
fi
if ! test -s $ipk; then
(cd $sdk &&
./scripts/feeds update base &&
./scripts/feeds install libubox && test -d package/feeds/base/libubox &&
./scripts/feeds install ubus && test -d package/feeds/base/ubus &&
make defconfig &&
make package/lxd-procd/compile
)
fi
test -e ${pkgdir} || mkdir -p ${pkgdir}
(cd ${pkgdir} && ln -sf ../../../../../$ipk .)
}
build_tarball() {
export SDK="$(pwd)/${sdk}"
local opts=""
if test ${type} = lxd; then
opts="$opts -m $metadata"
fi
if test "${upgrade}" = "1"; then
opts="$opts --upgrade"
fi
local allpkgs="${packages}"
test -d $pkgdir && for pkg in $pkgdir/*.ipk; do
if [ -e "$pkg" ]; then
allpkgs="${allpkgs} $pkg"
fi
done
local cmd="scripts/build_rootfs.sh"
if test `id -u` != 0; then
case "$super" in
sudo)
cmd="sudo --preserve-env=SDK $cmd"
;;
*)
cmd="$super $cmd"
;;
esac
fi
$cmd $rootfs $opts -o $tarball --disable-services="sysfixtime sysntpd led" --arch=${arch} --subarch=${subarch} --version=${ver} --packages="${allpkgs}" --files="${files}"
}
build_metadata() {
local stat=`stat -c %Y $rootfs`
local date="`date -d \"@${stat}\" +%F`"
local desc="$(tar xf $rootfs ./etc/openwrt_release -O|grep DISTRIB_DESCRIPTION|sed -e "s/.*='\(.*\)'/\1/")"
test -e bin || mkdir bin
cat > $metadata <<EOF
architecture: "$arch_lxd"
creation_date: $(date +%s)
properties:
architecture: "$arch_lxd"
description: "$desc"
os: "OpenWrt"
release: "$ver"
templates:
EOF
## Add templates
#
# templates:
# /etc/hostname:
# when:
# - start
# template: hostname.tpl
}
download_rootfs
download_sdk
if need_procd; then
download_procd
build_procd
fi
build_metadata
build_tarball
echo "Tarball built: $tarball"