-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiZy_builder_vm
executable file
·419 lines (328 loc) · 8.23 KB
/
miZy_builder_vm
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/bin/sh
## hyphop ##
usage() { echo "
$0 - run miZy bulder vm image in qemu+kvm
USAGE:
./miZy_builder_vm start
EXAMPLES:
get ip config from default miZy_builder_vm.net.conf file and run miZyBldr.squashfs image
./miZy_builder_vm start
mem=4G ./miZy_builder_vm start
get ip config from MY_IP.conf file and run MY_IMAGE.squashfs image
conf=./MY_IP.conf image=./MY_IMAGE.squashfs ./miZy_builder_vm start
no use file config, only via env vars
noconf=1 ip0=11.168.1.1 ip0h=11.168.1.254 ip0g=11.168.1.254 ./miZy_builder_vm start
customs sripts
./miZy_builder_vm start run=tar=scripts
./miZy_builder_vm start run=http://...
NETWORK CONFIG VARS
on VM side: run two interfaces eth0 and eth1, eth0 used as WAN, eth1 used as LAN.
on HOST side: run two interfaces tap0 and tap1, tap0 <=> eth0 and tap1 <=> eth1
eth0 config vars
* ip0 - eth0 vm ip
* ip0h - tap0 host ip
* ip0g - eth0 vm gateway ip (usually its same ip0h) or empty value if gw not used
eth1 config vars
* ip1 - eth1 vm ip
* ip1h - tap1 host ip
* ip1g - eth1 vm gateway ip (usually its same ip1h) or empty value if gw not used
NOTE:
read $0 source - for more info, and change it if u need
";}
[ "$noconf" = "" ] && {
[ "$conf" = "" ] && conf="$0.net.conf"
[ -f $conf ] || {
echo "[e] not found config $conf"
exit 1
}
#echo "[i] read config $conf";
. $conf
}
## qemu net scripts there
[ "$VMSTARTED" = "1" ] && {
#echo "NET SCRIPT $0: $@"
netvar="/tmp/vm_net_$1";
case $1 in
tap0)
# nat rule
host_nat_rule="POSTROUTING -s $ip0 ! -d $ip0h -j MASQUERADE"
[ -f $netvar ] && {
# down eth0 tap0 script there
echo "[i] dev $1 $ip0 / host $ip0h DOWN"
ifconfig $1 down
[ "$ip0nat" = "" ] || {
iptables -t nat -D $host_nat_rule 1>/dev/null 2>&1
}
rm $netvar
exit 0
}
# up eth0 tap0 script there
echo "[i] dev $1 $ip0 / host $ip0h UP"
ifconfig $1 $ip0h netmask 255.255.255.0
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/conf/$1/proxy_arp
[ "$ip0nat" = "" ] || {
echo "NAT: $host_nat_rule"
iptables -t nat -D $host_nat_rule 1>/dev/null 2>&1
iptables -t nat -A $host_nat_rule
}
route add $ip0 dev $1
echo "$ip0 $ip0h $ip0g" > $netvar
exit 0
;;
tap1)
[ "$ip1ssh_port" = "" ] && ip1ssh_port=22
ssh_redirect_rule1="PREROUTING -p tcp -m tcp --dport $ip1ssh_host_port -j DNAT --to-destination $ip1:$ip1ssh_port"
ssh_redirect_rule2="POSTROUTING -o $1 -j MASQUERADE"
[ -f $netvar ] && {
# down eth0 tap0 script there
echo "[i] dev $1 $ip1 / host $ip1h DOWN"
[ "$ip1ssh_host_port" = "" ] || {
# remove redirect rescue ssh host port to vm ssh port 22
iptables -t nat -D $ssh_redirect_rule1 1>/dev/null 2>&1
iptables -t nat -D $ssh_redirect_rule2 1>/dev/null 2>&1
}
rm $netvar
exit 0
}
# up eth1 tap1 script there
echo "[i] dev $1 $ip1 / host $ip1h UP"
ifconfig $1 $ip1h netmask 255.255.255.0
echo 1 > /proc/sys/net/ipv4/ip_forward
route add $ip1 dev $1
[ "$ip1ssh_host_port" = "" ] || {
# add redirect rescue ssh host port to vm ssh port 22
echo "[i] add redirect rescue ssh host port $ip1ssh_host_port to vm ssh port $ip1ssh_port"
echo "NAT: $ssh_redirect_rule1"
echo "NAT: $ssh_redirect_rule2"
iptables -t nat -I $ssh_redirect_rule1
iptables -t nat -I $ssh_redirect_rule2
}
echo "$ip1 $ip1h $ip1g" > $netvar
exit 0
;;
*)
echo "[e] dev $1 not configured"
exit 1
esac
}
#set -e
#[ "$1" = "start" ] || { usage; exit 0; }
case $1 in
start)
;;
wheezy|jessie|stable|xenial)
mb="miZyBldr.$1"
;;
*.squashfs)
[ -f $1 ] && {
image=$1
mb=${image%.squashfs}
}
;;
*)
usage
exit 0
esac
# remove old
rm /tmp/vm_net_* 2>/dev/null
already=
[ -e /sys/class/net/tap0 ] && already="$already tap0"
[ -e /sys/class/net/tap1 ] && already="$already tap1"
[ "$already" = "" ] || {
echo "[e] looks like run already ( used:$already )" 1>&2
exit 1
}
# get config
. $0.conf
nomodules="modprobe.blacklist="
nomod_file="miZy_builder_vm.nomod"
[ -f $nomod_file ] && {
for nomod in `cat $nomod_file`; do
nomodules="$nomodules$nomod,"
done
}
#nomodules="$nomodules,ipv6"
#nomodules=""
[ "$mb" = "" ] && mb=miZyBldr
[ -d $mb ] || mkdir $mb
[ "$image" = "" ] && image=$mb.squashfs
[ -s $image ] || {
echo "[e] not found $image"
echo "[i] try generate it: ./miZy_builder_vm_generate yes"
echo "[i] or download it: ./miZy_builder_vm.get"
exit 1
}
[ -d $mb ] || mkdir $mb
[ -d $mb/etc ] || {
echo "[i] mount -o ro $image $mb"
mount -o ro $image $mb || {
echo "[e] mount -o ro $image $mb"
exit 1
}
}
kernel=`ls $mb/boot/vmlinuz-*`
initrd=`ls $mb/boot/initrd*`
[ "$kernel" ] || {
echo "[e] not found $kernel"
}
[ "$initrd" ] || {
echo "[e] not found $initrd"
}
[ "$host_name" = "" ] && host_name=$mb
[ "$log_options" = "" ] && log_options=quiet
ipcnf="ip0=$ip0 ip0g=$ip0g ip1=$ip1 ip1g=$ip1g"
printf "\
[i] vm_image: %s
vm_kernel: %s
vm_initrd: %s
" $image $kernel $initrd
echo "[i] read config $conf" # => $ipcnf";
[ "$host_name" = "" ] || userconfig="${userconfig} hostname=$host_name"
[ "$password" = "" ] || userconfig="${userconfig} password=$password"
userconfig="${userconfig} $ipcnf"
userconfig="${userconfig} $log_options"
#userconfig="${userconfig} $log_options $2 $3 $4 $5 $6"
#exit 0
export VMSTARTED=1
modprobe kvm-intel
#[ -f "$0.down" ] || netdown_script=$0.down
netdown_script=$0
overlays=""
aufs=""
vd=1
for v in $@; do
#echo $v
case $v in
#ovl=""
ovl=*)
ovl=${v#ovl=}
[ "$ovl" = "" ] && {
ovl=$mb.ovl
}
[ -f $ovl ] || {
echo "[e] ovl $ovl not found"
echo "[i] EXAMPLE: qemu-img create -f qcow2 miZyBldr.ovl 1G"
exit 1
}
overlays="$overlays -drive file=$ovl,if=virtio,index=$vd"
vd=$((vd+1))
aufs=vdb
;;
disk=*)
disk=${v#disk=}
[ -f $disk ] || {
[ -b $disk ] || {
echo "[e] disk image $disk not found"
exit 1
}
}
overlays="$overlays -drive file=$disk,if=virtio,index=$vd"
vd=$((vd+1))
;;
run=*)
src=${v#run=}
case $src in
http*)
echo "RUN: $src"
userconfig="$userconfig $v"
;;
*)
c="_UNDEF_"
targz="_UNDEF_"
[ "$src" = "" ] && {
echo "[e] run= "
exit 1
}
[ -f $src ] && {
echo $src | grep -q ".tar" && targz=$src
[ -f $targz ] || {
c=`dirname $src`
[ -d $c ] && {
#f=${src#$c}
#f=${f#/}
f=`basename $src`
echo "DIR $c / RUN $f"
}
}
}
[ -d $c ] && {
targz=/tmp/init.scripts.tar.gz
[ -f $targz ] && rm $targz
echo "[i] tar gz dir $c"
p=$PWD
cd $c
tar -czf- * > $targz
cd $p
}
[ -f $targz ] && {
tarsize=`stat -c%s $targz`
tarblocks=$((tarsize / 512))
newsize=$((tarblocks * 512))
[ $newsize -lt $tarsize ] && {
add=$((newsize + 512 - tarsize))
echo "[i] fix $targz size / add $add zero bytes for 512 div"
dd if=/dev/zero bs=$add count=1 2>/dev/null >> $targz
}
ls -l1 $targz
userconfig="$userconfig $v"
echo "[i] kernel args + $v"
overlays="$overlays -drive file=$targz,if=virtio,index=10"
continue
}
echo "[e] ignore RUN $src"
exit 1
esac
;;
esac
done
[ "$aufs" = "" ] && aufs=tmpfs
## aufs
userconfig="${userconfig} aufs=$aufs"
# add nomodules
userconfig="${userconfig} $nomodules"
echo "[i] userconfig: $userconfig"
#root=/dev/vda is loop device eq squashfs
[ "$test" = "" ] || exit 0
[ "$mem" = "" ] && mem=2G
[ "$mem" = "1G" ] && mem=1024
[ "$mem" = "2G" ] && mem=2048
[ "$mem" = "3G" ] && mem=3072
[ "$mem" = "4G" ] && mem=4096
bg=
console=${console:-console=ttyS0}
[ "$console" = "no" ] && {
console=
bg=1
}
ECHO() {
[ "$verbose" ] && echo $@
}
RUN() {
$1 qemu-system-x86_64 \
-enable-kvm \
-smp 2 \
-m $mem \
-cpu kvm64 \
-kernel $kernel \
-initrd $initrd \
-append "$console root=/dev/vda ro panic=15 vt.default_utf8=1 loop.max_loop=64 $userconfig" \
-net nic,vlan=0,model=virtio \
-net tap,vlan=0,script="$0",downscript="$netdown_script" \
-net nic,vlan=1,model=virtio \
-net tap,vlan=1,script="$0",downscript="$netdown_script" \
-drive file=$image,if=virtio,index=0 \
$overlays \
-no-fd-bootchk \
-nographic
}
[ "$bg" ] && {
echo "[i] goto bg" 1>&2
RUN ECHO
RUN &
exit 0
}
RUN ECHO
RUN
umount $mb
exit 0