forked from vincentbernat/network-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
executable file
·614 lines (557 loc) · 18.7 KB
/
setup
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/bin/sh
LABNAME="arista-veos"
DEPS="bird"
set -e
ESC="$(printf '\033')"
NORMAL="${ESC}[0m"
RED="${ESC}[31;1m"
GREEN="${ESC}[32;1m"
YELLOW="${ESC}[33;1m"
BLUE="${ESC}[34;1m"
BEGINNING="$(printf '\015\033')[K"
log_begin_msg () {
printf "${BEGINNING}"
printf "${BLUE}[…]${NORMAL} $1${BLUE}...${NORMAL} "
}
log_ok_msg () {
printf "${BEGINNING}"
echo "$BOLD$GREEN[✔]$NORMAL $1."
}
log_warn_msg () {
printf "${BEGINNING}"
echo "$BOLD$YELLOW[⚡]$NORMAL $1!"
}
log_error_msg () {
printf "${BEGINNING}"
echo "$BOLD$RED[✘]$NORMAL $1!"
exit 1
}
log_info_msg () {
printf "${BEGINNING}"
echo "$BOLD$BLUE[∗]$NORMAL $1."
}
check_kernel() {
log_begin_msg "Checking kernel version"
[ -f "$KERNEL" ] || log_error_msg "Unable to find kernel $KERNEL"
[ -r "$KERNEL" ] || log_error_msg "Kernel $KERNEL is not readable.\n Try \`setfacl -m u:$USER:r $KERNEL'"
# A recent version of `file` is able to extract the
# information. Since it is not widely available, let use some hack
# method.
VERSION=$(cat <<EOF |
cat
gunzip \\\037\\\213\\\010 xy
unxz \\\3757zXZ\\\000 abcde
bunzip2 BZh xy
unlzma \\\135\\\0\\\0\\\0 xxx
EOF
while read cmd sig1 sig2; do
case $sig1,$sig2 in
,) poss="0:_" ;;
*) poss=$(tr "${sig1}\n${sig2}" "\n${sig2}=" < "$KERNEL" | grep -abo "^${sig2}" || true) ;;
esac
[ -n "$poss" ] || continue
for pos in $poss; do
pos=${pos%%:*}
tail -c+$pos "$KERNEL" | $cmd 2> /dev/null | strings -20 | \
grep ^Linux.version | head -1
done
done | head -1)
[ -n "$VERSION" ] || log_error_msg "Unable to determine version for $KERNEL"
VERSION="${VERSION#Linux version }"
VERSION="${VERSION%% *}"
log_ok_msg "Found kernel $VERSION"
log_begin_msg "Check kernel configuration"
CONFIG="$(dirname $KERNEL)/config-$VERSION"
[ -f "$CONFIG" ] || log_error_msg "Unable to find configuration file $CONFIG"
cat <<EOF | while read el; do
9P_FS=[ym]
NET_9P=[ym]
NET_9P_VIRTIO=[ym]
VIRTIO=[ym]
VIRTIO_PCI=[ym]
SERIAL_8250=y
SERIAL_8250_CONSOLE=y
TMPFS=y
BLK_DEV_INITRD=y
DEVTMPFS=[ym]
EOF
grep -qx "CONFIG_$el" $CONFIG || log_error_msg "Kernel not configured with CONFIG_$el"
done
log_begin_msg "Search for modules"
for dir in "$(dirname $KERNEL)/lib/modules/$VERSION" "/lib/modules/$VERSION"; do
[ -d $dir ] || continue
MODULES="$dir"
break
done
if [ -z "$MODULES" ]; then
log_warn_msg "Unable to find module directory"
else
log_ok_msg "Modules are in $MODULES"
fi
}
check_dependencies() {
log_begin_msg "Checking if dependencies are present"
for exec in \
busybox \
qemu-system-x86_64 \
tmux vde_switch \
start-stop-daemon \
guestfish \
$DEPS; do
which $exec 2> /dev/null > /dev/null || log_error_msg "$exec is not installed"
done
log_ok_msg "All dependencies are met"
}
setup_tmp () {
TMP=$(mktemp -d)
trap "ret=$? ; cleanup" EXIT
log_info_msg "TMP is $TMP"
}
# Run our lab in tmux
setup_screen() {
if [ -z "$TMUX" ] || [ x"$(tmux list-panes -F '#{session_name}')" != x"$LABNAME" ]; then
unset TMUX
exec tmux new-session -s "$LABNAME" -n main \
"env ROOT=$ROOT VERSION=$VERSION LINUX=$LINUX $PROGNAME $PROGARGS || read"
fi
sleep 1
tmux set-option -q prefix C-b
tmux set-option -q set-remain-on-exit on
tmux bind-key r respawn-window
}
# Setup a VDE switch
setup_switch() {
nb=$1 ; shift
log_begin_msg "Setup switch $nb"
cat <<EOF > "$TMP/switch-$nb.conf"
plugin/add /usr/lib/vde2/plugins/pdump.so
pdump/filename $TMP/switch-$nb.pcap
pdump/buffered 0
pdump/active 1
EOF
start-stop-daemon -b --no-close --make-pidfile --pidfile "$TMP/switch-$nb.pid" \
--start --startas $(which vde_switch) -- \
--sock "$TMP/switch-$nb.sock" --mgmt "$TMP/switch-management-$nb.sock" \
--rcfile "$TMP/switch-$nb.conf"
# Management socket can be used with:
# socat - UNIX-CONNECT:"$TMP/switch-management-$nb.sock"
log_ok_msg "Switch $nb started"
}
setup_initrd () {
log_begin_msg "Build initrd"
DESTDIR=$TMP/initrd
mkdir -p $DESTDIR
# Copy busybox and eventually insmod
bins="busybox"
busybox --list | grep -qFx insmod || bins="$bins insmod"
for bin in $bins; do
install -D "$(which $bin)" ${DESTDIR}/bin/$bin
for x in $(ldd "$(which $bin)" 2> /dev/null | sed -e '
/\//!d;
/linux-gate/d;
/=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
[ -f "${DESTDIR}/$x" ] || install -D "$x" "${DESTDIR}/$x"
done
done
# Configure busybox
for applet in $(${DESTDIR}/bin/busybox --list); do
ln -s busybox ${DESTDIR}/bin/${applet}
done
# Add modules
[ -z "$MODULES" ] || {
modules="9pnet_virtio 9p virtio_pci $UNION"
for mod in $modules; do
modprobe --all --set-version="${VERSION}" -d ${MODULES}/../../.. \
--ignore-install --quiet --show-depends $mod > /dev/null || {
log_warn_msg "Unable to find module $mod"
log_begin_msg "Continue building initrd"
}
modprobe --all --set-version="${VERSION}" -d ${MODULES}/../../.. \
--ignore-install --quiet --show-depends $mod |
while read prefix kmod options ; do
[ "${prefix}" = "insmod" ] || continue
grep -qFw "$kmod" ${DESTDIR}/modules 2> /dev/null || {
install -D "$kmod" "${DESTDIR}/${kmod}"
echo $prefix $kmod $options >> ${DESTDIR}/modules
}
done
done
}
# Copy this program
cp "$PROGNAME" ${DESTDIR}/init
# Create /tmp
mkdir ${DESTDIR}/tmp
# Build initrd
(cd "${DESTDIR}" && find . | cpio --quiet -R 0:0 -o -H newc | gzip > $TMP/initrd.gz)
log_ok_msg "initrd built in $TMP/initrd.gz"
}
random_mac () {
name=$1
net=$2
mac=$(echo $name-$net | sha1sum | \
awk '{print "50:54:" substr($1,0,2) ":" substr($1, 2, 2) ":" substr($1, 4, 2) ":" substr($1, 6, 2)}')
echo $mac
}
start_vm () {
name=$1
shift
netargs=""
saveifs="$IFS"
IFS=,
for net in $NET; do
mac=$(random_mac $name $net)
netargs="$netargs -net nic,model=virtio,macaddr=$mac,vlan=$net"
netargs="$netargs -net vde,sock=$TMP/switch-$net.sock,vlan=$net"
done
IFS="$saveifs"
log_info_msg "Start VM $name"
# /root is mounted with version 9p2000.u to allow access to /dev,
# /sys and to mount new partitions over them. This is not the case
# for 9p2000.L.
cat <<EOF > "$TMP/vm-$name.exec"
#!/bin/sh
echo
exec start-stop-daemon --make-pidfile --pidfile "$TMP/vm-$name.pid" \
--start --startas $(which qemu-system-x86_64) -- \
-enable-kvm \
-nodefconfig -nodefaults \
-display none \
-m ${MEM:-128M} \
\
-chardev stdio,id=charserial0,signal=off \
-device isa-serial,chardev=charserial0,id=serial0 \
-chardev socket,id=charserial1,path=$TMP/vm-$name-serial.pipe,server,nowait \
-device isa-serial,chardev=charserial1,id=serial1 \
\
-chardev socket,id=con0,path=$TMP/vm-$name-console.pipe,server,nowait \
-mon chardev=con0,mode=readline,default \
\
-fsdev local,security_model=passthrough,id=fsdev-root,path=${ROOT} \
-device virtio-9p-pci,id=fs-root,fsdev=fsdev-root,mount_tag=rootshare \
-fsdev local,security_model=none,id=fsdev-home,path=${HOME} \
-device virtio-9p-pci,id=fs-home,fsdev=fsdev-home,mount_tag=homeshare \
-fsdev local,security_model=none,id=fsdev-lab,path=${PWD} \
-device virtio-9p-pci,id=fs-lab,fsdev=fsdev-lab,mount_tag=labshare \
-fsdev local,security_model=none,id=fsdev-tmp,path=${TMP} \
-device virtio-9p-pci,id=fs-tmp,fsdev=fsdev-tmp,mount_tag=tmpshare \
-fsdev local,security_model=none,id=fsdev-modules,path=${MODULES}/..,readonly \
-device virtio-9p-pci,id=fs-modules,fsdev=fsdev-modules,mount_tag=moduleshare \
\
-gdb unix:$TMP/vm-$name-gdb.pipe,server,nowait \
-kernel $KERNEL \
-no-reboot \
-initrd $TMP/initrd.gz \
-append "uts=$name console=ttyS0 panic=1 TERM=$TERM quiet" \
$netargs \
$@
EOF
log_info_msg "GDB server listening on $TMP/vm-$name-gdb.pipe"
log_info_msg "monitor listening on $TMP/vm-$name-console.pipe"
log_info_msg "ttyS1 listening on $TMP/vm-$name-serial.pipe"
chmod +x "$TMP/vm-$name.exec"
tmux new-window -n $name "$TMP/vm-$name.exec"
tmux select-window -t ":^"
}
start_veos () {
name=$1
shift
netargs=""
saveifs="$IFS"
IFS=,
for net in $NET; do
mac=$(random_mac $name $net)
netargs="$netargs -net nic,model=virtio,macaddr=$mac,vlan=$net"
netargs="$netargs -net vde,sock=$TMP/switch-$net.sock,vlan=$net"
done
IFS="$saveifs"
log_info_msg "Start vEOS $name"
qemu-img create -q -f qcow2 -b $PWD/../images/vEOS.img $TMP/veos-$name.img
guestfish -a $TMP/veos-$name.img -m /dev/sda1 <<EOF
copy-in $name.conf /
cp /$name.conf /startup-config
EOF
cat <<EOF > "$TMP/vm-$name.exec"
#!/bin/sh
echo
start-stop-daemon --make-pidfile --pidfile "$TMP/vm-$name.pid" \
--start --startas $(which qemu-system-x86_64) -- \
-enable-kvm \
-nodefconfig -no-user-config -nodefaults \
-display none \
\
-machine pc-i440fx-1.5 \
-m 1536M \
\
-usb \
\
-boot d \
-drive file=$PWD/../images/Aboot-veos.iso,if=ide,media=cdrom,cache=none \
-drive file=$TMP/veos-$name.img,if=ide,media=disk,cache=none \
\
-chardev stdio,id=charserial0,signal=off \
-device isa-serial,chardev=charserial0,id=serial0 \
-chardev socket,id=charserial1,path=$TMP/vm-$name-serial.pipe,server,nowait \
-device isa-serial,chardev=charserial1,id=serial1 \
-chardev socket,id=con0,path=$TMP/vm-$name-console.pipe,server,nowait \
-mon chardev=con0,mode=readline,default \
$netargs \
\
$@
sleep 1
EOF
log_info_msg "monitor listening on $TMP/vm-$name-console.pipe"
log_info_msg "ttyS1 listening on $TMP/vm-$name-serial.pipe"
chmod +x "$TMP/vm-$name.exec"
tmux new-window -n $name "$TMP/vm-$name.exec"
tmux select-window -t ":^"
}
display_help() {
cat <<EOF
Some tmux commands (assuming default keybindings) :
C-b d - Detach the lab (resume with "tmux attach -t $LABNAME")
C-b w - Select a window
C-b n - Next window
C-b p - Previous window
C-b l - Last window
C-b ? - Get help
EOF
echo "Press enter to exit the lab"
read a
done=1
}
cleanup() {
set +e
[ x"$done" = x"1" ] || {
printf "${BEGINNING}"
echo "$BOLD$RED[✘]$NORMAL Got a fatal error. Press enter to terminate everything!"
read a
}
for pid in $TMP/*.pid; do
kill -15 -$(cat $pid) 2> /dev/null || true
done
sleep 1
for pid in $TMP/*.pid; do
kill -9 -$(cat $pid) 2> /dev/null || true
done
rm -rf $TMP
tmux kill-session -t $LABNAME
}
start_bird() {
log_begin_msg "Running bird"
[ -d /run/bird ] || mkdir -p /run/bird
bird -c /mnt/lab/bird.$uts.conf
log_ok_msg "bird started"
}
start_lldpd() {
if which lldpd > /dev/null 2> /dev/null; then
log_begin_msg "Starting lldpd"
lldpd
log_ok_msg "lldpd started"
fi
}
# FSM
export STATE=${STATE:-BEGIN}
case $$,$STATE in
1,BEGIN)
# In initrd
log_info_msg "initrd started"
hostname ${uts}
export PATH=/usr/local/bin:/usr/bin:/bin:/sbin:/usr/local/sbin:/usr/sbin
export HOME=/root
[ ! -f /modules ] || {
log_info_msg "Loading modules"
. /modules
}
log_begin_msg "Setup root file system"
mount -n -t tmpfs tmpfs /tmp -o rw
mkdir /tmp/target
mkdir /tmp/target/ro
mkdir /tmp/target/overlay
mount -n -t 9p rootshare /tmp/target/overlay -o trans=virtio,version=9p2000.u,ro
mount -n -t proc proc /tmp/target/overlay/proc
mount -n -t sysfs sys /tmp/target/overlay/sys
log_ok_msg "Root file system setup"
log_begin_msg "Clean /tmp and /run"
for fs in /run /var/run /var/tmp /var/log /tmp /mnt; do
if [ -d /tmp/target/overlay$fs ] && [ ! -h /tmp/target/overlay$fs ]; then
mount -t tmpfs tmpfs /tmp/target/overlay$fs -o rw,nosuid,nodev
fi
done
log_ok_msg "/tmp, /run and others are clean"
log_begin_msg "Mount /root"
mount -t 9p homeshare /tmp/target/overlay/root -o trans=virtio,version=9p2000.L,access=any,rw || \
log_error_msg "Unable to mount /root"
[ ! -d /tmp/target/overlay/root/.ssh ] || {
mount -t tmpfs tmpfs /tmp/target/overlay/root/.ssh -o rw,nosuid,nodev
chmod 0700 /tmp/target/overlay/root/.ssh
}
log_ok_msg "/root mounted"
log_begin_msg "Mount /lib/modules"
mount -t 9p moduleshare /tmp/target/overlay/lib/modules -o trans=virtio,version=9p2000.L,access=0,ro || \
log_error_msg "Unable to mount /lib/modules"
log_ok_msg "/root and /lib/modules mounted"
log_begin_msg "Mount /mnt/lab"
mkdir /tmp/target/overlay/mnt/lab
mount -t 9p labshare /tmp/target/overlay/mnt/lab -o trans=virtio,version=9p2000.L,access=any,rw || \
log_error_msg "Unable to mount /mnt/lab"
log_ok_msg "/mnt/lab mounted"
log_begin_msg "Mount /tmp/lab"
mkdir /tmp/target/overlay/tmp/lab
mount -t 9p tmpshare /tmp/target/overlay/tmp/lab -o trans=virtio,version=9p2000.L,access=any,rw || \
log_error_msg "Unable to mount /tmp/lab"
log_ok_msg "/tmp/lab mounted"
log_info_msg "Change root"
export STATE=CHROOTED
exec chroot /tmp/target/overlay /mnt/lab/setup
;;
1,CHROOTED)
log_begin_msg "Starting udev"
udev_log=err
mount -n -o size=10M,mode=0755 -t devtmpfs devtmpfs /dev
udevadm info --cleanup-db
for udev in /lib/systemd/systemd-udevd /usr/lib/systemd/systemd-udevd $(command -v udevd 2> /dev/null); do
[ ! -x $udev ] || break
done
$udev --daemon
udevadm trigger --action=add
udevadm settle
log_ok_msg "udev started"
log_info_msg "Setup interfaces"
modprobe dummy 2>/dev/null || true
sleep 0.5 # Some interfaces may take some time to appear
i=0
for intf in /sys/class/net/*; do
intf=${intf##*/}
ip a l dev $intf 2> /dev/null >/dev/null || continue
case $intf in
ens*)
ip link set name eth$i dev $intf
intf=eth$i
i=$(( i + 1 ))
;;
esac
ip link set up dev $intf
done
log_info_msg "Start syslog"
rsyslogd
log_info_msg "Setup terminal"
export STATE=GETTY
exec setsid /sbin/agetty -L ttyS0 -a root -l /mnt/lab/setup -i 115200 $TERM
;;
1,GETTY)
log_begin_msg "Setup terminal size"
# We are guaranteed to be in a tmux session, so we will get an
# answer to the escape code we send. Otherwise, we may just
# block!
stty -echo
stty raw
printf '[18t'
width=
height=
p=0
while true; do
char="$(timeout --foreground 1s dd bs=1 count=1 2> /dev/null || true)"
case "$p,$char" in
"0,;")
# End of CSI
p=1
;;
"1,;")
# End of height
stty rows $height
p=2
;;
"1,"*)
height="$height$char"
;;
"2,t"|"2,")
# End of width
stty columns $width
p=3
;;
"2,"*)
width="$width$char"
;;
esac
[ x"$p" != x"3" ] || break
done
stty -raw
stty echo
log_ok_msg "Terminal size is $(tput cols)x$(tput lines)"
export STATE=SETUP
. /mnt/lab/setup
log_begin_msg "Setup additional commands"
mkdir /tmp/tools
export PATH=/tmp/tools:$PATH
cat <<EOF > /tmp/tools/reboot
#!/bin/sh
echo b > /proc/sysrq-trigger
EOF
cat <<EOF > /tmp/tools/halt
#!/bin/sh
echo b > /proc/sysrq-trigger
EOF
chmod +x /tmp/tools/*
log_ok_msg "System can be halted with halt or reboot"
while true; do
log_info_msg "Spawning a shell"
cd $HOME
export SSH_TTY=$(tty)
if [ -f $HOME/.zshrc ]; then
/bin/zsh -i
else
/bin/bash -i
fi || sleep 1
done
;;
*,SETUP)
log_info_msg "Setup IP addresses"
case $uts in
R*)
ip addr add 203.0.113.1${uts#R}/24 dev eth0
start_bird
;;
C*)
ip addr add 198.51.100.1${uts#C}/24 dev eth0
ip route add default via 198.51.100.1
;;
esac
start_lldpd
;;
*,BEGIN)
# Initial state
[ $(id -u) != 0 ] || {
log_error_msg "You should not run this as root"
exit 1
}
PROGNAME="$(readlink -f "$0")"
PROGARGS="$@"
ROOT="$(readlink -f "${ROOT:-/}")" # Root filesystem
KERNEL="$(readlink -f "${1:-/boot/vmlinuz-$(uname -r)}")" # Kernel
PATH="$PATH":/usr/local/sbin:/usr/sbin:/sbin
[ $# -lt 1 ] || shift
check_kernel
check_dependencies
setup_screen
setup_tmp
setup_initrd
setup_switch 2 # Internet
setup_switch 3 # Clients
setup_switch 9 # Management
sleep 0.5
NET=2 start_vm R1
NET=2 start_vm R2
NET=9,2,3 start_veos vEOS1
NET=9,2,3 start_veos vEOS2
NET=3 start_vm C1
NET=3 start_vm C2
NET=3 start_vm C3
display_help
;;
esac
# Local Variables:
# mode: sh
# indent-tabs-mode: nil
# sh-basic-offset: 4
# End: