-
Notifications
You must be signed in to change notification settings - Fork 88
/
TorBOX_Image
311 lines (232 loc) · 8.21 KB
/
TorBOX_Image
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
#!/bin/bash
# save as /home/user/TorBOX_source/TorBOX_Image
# Version: TorBOX 0.2.1
# Copyright: proper
#
# License: GPL v3 or any later
#
# Any changes you pull changes into this source will be also licensed
# under GPL v3 or any later. Additionally you grant proper the right to
# re-license your work under a different license. If that is not acceptable,
# you can either fork this source under GPL v3 or any later or contact proper.
# Contact proper, if you require this source code under different license.
TorBOX_DEBUG="0"
script_help() {
echo \
"
# FLAGS / WORKFLOW:
# -tg-mount
# Mounts the VM image.
#
# -tg-copyinto
# Copies TorBOX_Gateway and torcheck script into VM image.
#
# -tg-unmount
# Unmounts the VM image.
#
# -tw-mount
# -tw-copyinto
# -tw-unmount
"
}
# Enable debugging.
set -x
# Exit if there is an error
set -e
USERNAME="user"
##############################################################################################
# error_handler
##############################################################################################
error_handler() {
echo "
#!!! ERROR in TorBOX_Image !!!#
#!!! ERROR in TorBOX_Image !!!#
#!!! ERROR in TorBOX_Image !!!#
"
echo "TorBOX_Image: unmount_vm_image..."
unmount_vm_image
echo "TorBOX_Image: Done."
touch /home/$USERNAME/TorBOX_binary/TORBOX_BUILD_FAILED
exit 1
}
root_check() {
######################################################
# Checking script environment
######################################################
# Check if we are root
if [ "$(id -u)" != "0" ]; then
echo "ERROR: This must be run as root (sudo)!"
exit 1
else
echo "INFO: Script running as root."
fi
}
mount_vm_image() {
trap "error_handler" ERR INT TERM
# Ensure powered is off. Otherwise disk corruption is at high risk.
sudo -u $USERNAME VBoxManage controlvm "$VMNAME" poweroff || true
# Make sure required module to mount vdi images is installed.
modprobe nbd
# Create loopback dev for the image.
qemu-nbd -c /dev/nbd0 "/home/$USERNAME/VirtualBox VMs/""$VMNAME"/"$VMNAME".vdi
# Folder has to exist to mount the image.
mkdir -p $CHROOT_FOLDER
# Mount the partitions, that are exposed as /dev/nbd0pXXX.
mount -o noatime /dev/nbd0p1 $CHROOT_FOLDER
}
copy_into_vm_image() {
trap "error_handler" ERR INT TERM
# Make a backup of rc.local.
cp -n /etc/rc.local /etc/rc.local.backup
# Automatically start VM script after booting.
echo "#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will \"exit 0\" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
set -x
# Prevent running the script over and over again.
if [ -f \"/home/user/torboxinstalllog\" ];
then
echo \"rc.local INFO: File /home/user/torboxinstalllog exists. exit 0\"
exit 0
fi
echo \"rc.local INFO: File /home/user/torboxinstalllog does not exist.\"
# Create empty /home/user/torboxinstalllog.
touch \"/home/user/torboxinstalllog\"
# !HARDCODED!
# Set owner to user for /home/user/torboxinstalllog.
chown user \"/home/user/torboxinstalllog\"
TorBOX_DEBUG=\"$TorBOX_DEBUG\"
echo \"rc.local INFO: TorBOX_DEBUG = \$TorBOX_DEBUG\"
echo \"rc.local INFO: running /usr/local/bin/$VMSCRIPT...\"
if [ \"\$TorBOX_DEBUG\" = \"1\" ]; then
# The following line is tested.
# bash -x 2>/home/user/torboxinstalllog /usr/local/bin/$VMSCRIPT -install
# Need to overrule with || otherwise rc.local stops running.
bash -x 2>/home/user/torboxinstalllog /usr/local/bin/$VMSCRIPT -install || true
else
# Need to overrule with || otherwise rc.local stops running.
/usr/local/bin/$VMSCRIPT -install || true
fi
echo \"rc.local INFO: end of /usr/local/bin/$VMSCRIPT.\"
sleep 5
echo \"rc.local INFO: powering off...\"
sleep 5
# The & is important. Otherwise rc.local will wait for poweroff to finish,
# which is impossible.
poweroff &
echo \"rc.local INFO: exit 0...\"
exit 0
# end of rc.local
" > $CHROOT_FOLDER/etc/rc.local
# Copy VM script into VM image and make it executable.
cp /home/"$USERNAME"/TorBOX_source/$VMSCRIPT $CHROOT_FOLDER/usr/local/bin/
chmod +x $CHROOT_FOLDER/usr/local/bin/$VMSCRIPT
# Copy torcheck script into VM image and make it executable.
cp /home/"$USERNAME"/TorBOX_source/torcheck $CHROOT_FOLDER/usr/local/bin/
chmod +x $CHROOT_FOLDER/usr/local/bin/torcheck
# Delete old log, otherwise the VM script would not get executed.
# See rc.local above.
rm $CHROOT_FOLDER/home/user/torboxinstalllog || true
# Ensure changes get written before unmounting.
sync
}
unmount_vm_image() {
sync
# Shutdown the ndb.
qemu-nbd -d /dev/nbd0
# In the end unmount.
umount $CHROOT_FOLDER
# Delete temporary folder.
# It did not contain anything. It was only a mount point.
rm -r $CHROOT_FOLDER
}
################################################################
# -tg-mount #
################################################################
if [[ "$1" = "-tg-mount" ]]; then
root_check
VMNAME="TorBOX-Gateway"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
mount_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_Image -tg-copyinto"
exit 0
fi
################################################################
# -tg-copyinto #
################################################################
if [[ "$1" = "-tg-copyinto" ]]; then
root_check
VMNAME="TorBOX-Gateway"
VMSCRIPT="TorBOX_Gateway"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
copy_into_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_CreateVM -tg-unmount"
exit 0
fi
################################################################
# -tg-unmount #
################################################################
if [[ "$1" = "-tg-unmount" ]]; then
root_check
VMNAME="TorBOX-Gateway"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
unmount_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_CreateVM -tg-install-script"
exit 0
fi
################################################################
# -tw-mount #
################################################################
if [[ "$1" = "-tw-mount" ]]; then
root_check
VMNAME="TorBOX-Workstation"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
mount_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_Image -tw-copyinto"
exit 0
fi
################################################################
# -tw-copyinto #
################################################################
if [[ "$1" = "-tw-copyinto" ]]; then
root_check
VMNAME="TorBOX-Workstation"
VMSCRIPT="TorBOX_Workstation"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
copy_into_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_CreateVM -tw-unmount"
exit 0
fi
################################################################
# -tw-unmount #
################################################################
if [[ "$1" = "-tw-unmount" ]]; then
root_check
VMNAME="TorBOX-Workstation"
CHROOT_FOLDER=/home/"$USERNAME"/TorBOX_binary/"$VMNAME"_image
unmount_vm_image
echo "BUILD INFO: Done, if success, next stept should be sudo ./TorBOX_CreateVM -tw-install-script"
exit 0
fi
################################################################
# -help #
################################################################
if [[ "$1" = "-help" ]]; then
script_help
exit 0
fi
################################################################
# no option chosen #
################################################################
echo "No option choosen. Use -help for help."
touch /home/$USERNAME/TorBOX_binary/TORBOX_BUILD_FAILED
exit 1