-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·359 lines (303 loc) · 7.32 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# Purpose: Build Tor gateway image and create VirtualBox VM
# by ra (2012)
VERSION="0.5.3"
NAME="Tor gateway ${VERSION}"
BUILD_THREADS='3'
FILENAME='OpenWrt-ImageBuilder-x86-for-Linux-i686.tar.bz2'
FILEURL='http://backfire.openwrt.org/10.03.1/x86_generic/'
FILEDIR='OpenWrt-ImageBuilder-x86-for-Linux-i686'
MD5SUM='c4f75b9f2350db7cfae0d5299688b8ff'
VMDKFILE='bin/x86/openwrt-x86-generic-combined-ext2.vmdk'
PROGS_NEEDED='which wget tar patch make VBoxManage rm echo svn cp'
SCRIPTDIR=$(cd `dirname ${0}`; echo `pwd`)
if [ "${UID}" -eq 0 ]; then
echo "ERROR: Do not run as root!"
exit 1
fi
# check for needed programs
for PROG in ${PROGS_NEEDED}; do
which ${PROG} 1> /dev/null 2> /dev/null
if [ "${?}" -ne 0 ]; then
echo "Error detecting program \"${PROG}\" which is necessary to run ${0}."
echo "Please install it or verify that it is in your \$PATH ($PATH)."
exit 1
fi
done
function cleanFile() {
echo
echo "Deleting \"${FILENAME}\""
echo
rm -f ${SCRIPTDIR}/${FILENAME}
}
function cleanDir() {
echo
echo "Deleting \"${FILEDIR}\""
echo
rm -rf ${SCRIPTDIR}/${FILEDIR}
}
function buildBuilder() {
# download OpenWrt ImageBuilder
if [ ! -e ${SCRIPTDIR}/${FILENAME} ]; then
wget ${FILEURL}${FILENAME} -O ${SCRIPTDIR}/${FILENAME}
if [ $? -ne 0 ]; then
echo
echo "ERROR: Downloading file."
echo
cleanFile
exit 1
fi
if [ `md5sum ${SCRIPTDIR}/${FILENAME} | cut -d " " -f1` != "${MD5SUM}" ]; then
echo
echo "ERROR: md5 mismatch!"
echo
cleanFile
exit 2
fi
fi
if [ ! -d ${SCRIPTDIR}/${FILEDIR} ]; then
tar xfj ${SCRIPTDIR}/${FILENAME} -C ${SCRIPTDIR}/
if [ $? -ne 0 ]; then
echo
echo "ERROR: Extracting file."
echo
cleanDir
exit 1
fi
cd ${SCRIPTDIR}/${FILEDIR}
for i in ../patches/imagebuilder/*; do
patch -p1 -i ${i}
if [ $? -ne 0 ]; then
echo
echo "ERROR: Applying patch."
echo
cleanDir
exit 1
fi
done
cd ${SCRIPTDIR}
fi
}
function buildDisk() {
# build image
cd ${SCRIPTDIR}/${FILEDIR}
make ${DEBUG} -j${BUILD_THREADS} image PROFILE="torgw" FILES="../overlay"
if [ $? -eq 0 ]; then
echo
echo
echo "-----------------------------------------------------------------------------------"
echo "VM disk image created: \"${SCRIPTDIR}/${FILEDIR}/${VMDKFILE}\""
echo "-----------------------------------------------------------------------------------"
echo
echo
else
echo
echo "ERROR: Creating image."
echo
exit 1
fi
cd ${CWD}
}
function cleanVM() {
echo
echo "Deleting VM."
echo
VBoxManage unregistervm "${NAME}" --delete
}
function cleanOVA() {
echo
echo "Deleting ova file."
echo
rm -f "${SCRIPTDIR}/${NAME}.ova"
}
function cleanSource() {
echo
echo "Deleting source files."
echo
rm -rf "${SCRIPTDIR}/backfire_10.03.1"
}
# create vm
function createVM() {
if [ ! -e ${SCRIPTDIR}/${FILEDIR}/${VMDKFILE} ]; then
echo
echo "ERROR: Disk file \"${SCRIPTDIR}/${FILEDIR}/${VMDKFILE}\" not found."
echo
exit 1
fi
VBoxManage createvm --name "${NAME}" --ostype "Linux26" --register
if [ $? -ne 0 ]; then
echo
echo "ERROR: Creating VM."
echo
cleanVM
exit 1
fi
# configure vm
VBoxManage modifyvm "${NAME}" --memory "48" --boot1 "disk" --boot2 "none" \
--boot3 "none" --boot4 "none" --vram "1" --nic1 "nat" --nictype1 "82543GC" \
--nic2 "intnet" --nictype2 "82543GC" --intnet2 "tor" --biosbootmenu "disabled" \
--rtcuseutc "on" --clipboard "disabled" --synthcpu "on" --natdnsproxy1 "on" \
--natdnshostresolver1 "on"
if [ $? -ne 0 ]; then
echo
echo "ERROR: Configuring VM."
echo
cleanVM
exit 1
fi
# add ide controller to vm
VBoxManage storagectl "${NAME}" --name "IDE Controller" --add ide --controller ICH6
if [ $? -ne 0 ]; then
echo
echo "ERROR: Adding storage controller to VM."
echo
cleanVM
exit 1
fi
# add disk to vm
VBoxManage storageattach "${NAME}" --storagectl "IDE Controller" --port 0 \
--device 0 --type hdd --medium "${SCRIPTDIR}/${FILEDIR}/${VMDKFILE}"
if [ $? -ne 0 ]; then
echo
echo "ERROR: Adding disk to VM."
echo
cleanVM
exit 1
fi
# export vm to ova file
if [ -f "${SCRIPTDIR}/${NAME}.ova" ]; then
cleanOVA
fi
VBoxManage export "${NAME}" --output "${SCRIPTDIR}/${NAME}.ova" --vsys 0 \
--version "${VERSION}" --vendor "ra" --vendorurl "https://github.com/ra--/Tor-gateway"
if [ $? -ne 0 ]; then
echo
echo "ERROR: Exporting VM."
echo
cleanVM
exit 1
fi
echo
echo
echo "-----------------------------------------------------------------------------------"
echo "${NAME} created: \"${SCRIPTDIR}/${NAME}.ova\""
echo "-----------------------------------------------------------------------------------"
echo
echo
# delete vm
VBoxManage storagectl "${NAME}" --name "IDE Controller" --remove
if [ $? -ne 0 ]; then
echo
echo "ERROR: Deleting storage controller from VM."
echo
cleanVM
exit 1
fi
UUID=`VBoxManage showhdinfo ${SCRIPTDIR}/${FILEDIR}/${VMDKFILE} | egrep '^UUID:' | awk '{print $2}'`
VBoxManage closemedium disk ${UUID} --delete
if [ $? -ne 0 ]; then
echo
echo "ERROR: Deleting disk from VM."
echo
cleanVM
exit 1
fi
cleanVM
}
# compile tor and tor-geoip packages
function compileTor() {
cd ${SCRIPTDIR}
if [ ! -d backfire_10.03.1 ]; then
svn co svn://svn.openwrt.org/openwrt/tags/backfire_10.03.1/
if [ $? -ne 0 ]; then
echo
echo "ERROR: Checking out source code from SVN."
echo
exit 1
fi
fi
cd backfire_10.03.1
# update package feeds
./scripts/feeds update -a
if [ $? -ne 0 ]; then
echo
echo "ERROR: Updating feeds."
echo
exit 1
fi
# update package feeds
./scripts/feeds install -a
if [ $? -ne 0 ]; then
echo
echo "ERROR: Installing feeds."
echo
exit 1
fi
# copy config file
cp -ap ../OpenWrt-ImageBuilder-x86-for-Linux-i686/.config .
if [ $? -ne 0 ]; then
echo
echo "ERROR: Copying config file."
echo
exit 1
fi
# build toolchain
make ${DEBUG} -j${BUILD_THREADS} prepare
if [ $? -ne 0 ]; then
echo
echo "ERROR: Preparing OpenWRT build environment."
echo
exit 1
fi
# apply patches and ignore if already applied
for i in ../patches/source/*; do
patch -p1 -t -i ${i}
done
# build tor and tor-geoip packages
make ${DEBUG} -j${BUILD_THREADS} package/tor/{clean,compile,install}
if [ $? -ne 0 ]; then
echo
echo "ERROR: Compiling tor package."
echo
exit 1
fi
# copy tor and tor-geoip packages to ImageBuilder directory
cp ./bin/x86/packages/tor*_x86.ipk ../OpenWrt-ImageBuilder-x86-for-Linux-i686/packages/
if [ $? -ne 0 ]; then
echo
echo "ERROR: Copying tor package."
echo
exit 1
fi
cd ..
}
function usage() {
echo "Usage: ${0} [clean|help]"
exit 1
}
if [ $# -eq 0 ]; then
buildBuilder
compileTor
buildDisk
createVM
elif [ $# -eq 1 ]; then
if [ ${1} = 'debug' ]; then
DEBUG='V=99'
BUILD_THREADS='1'
buildBuilder
compileTor
buildDisk
createVM
elif [ ${1} = 'clean' ]; then
cleanDir
cleanFile
cleanSource
cleanOVA
elif [ ${1} = 'help' ]; then
usage
else
${1}
fi
else
usage
fi