File: ikvswitch-setup

package info (click to toggle)
ikvswitch 1.0.4%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 108 kB
  • sloc: sh: 941; makefile: 2
file content (863 lines) | stat: -rwxr-xr-x 28,502 bytes parent folder | download | duplicates (2)
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#!/bin/sh

set -e

if ! [ -r /etc/ikvswitch/ikvswitch.conf ] ; then
        echo "Could not read configuration file: exiting."
        exit 1
fi
. /etc/ikvswitch/ikvswitch.conf

ME=$(basename $0)

if [ -r /root/.ssh/authorized_keys ] ; then
	ROOT_SSH_KEY_PARAM="--root-ssh-key /root/.ssh/authorized_keys"
else
	echo "Could not read /root/.ssh/authorized_keys, please make one."
	exit 1
fi

HOST_IPMI_PORT=9000
GUEST_IPMI_CHART_PORT=9100

mac_add () {
  if [ -z "$1" ]; then
    echo "Missing MAC address in mac_add"
    exit 1
  fi
  if [ -z "$2" ]; then
    echo "Missing Increment value in mac_add"
    exit 1
  fi
  MAC_ADDR="$1"
  VALUE="$2"

  printf "%X\n" $((0x${MAC_ADDR} + VALUE))
}

ikvswitch_build_template () {
	build-openstack-debian-image \
		--release ${DEBIAN_RELEASE} \
		--output ikvswitch-template \
		--debootstrap-url ${DEBIAN_MIRROR} \
		--sources.list-mirror ${DEBIAN_MIRROR} \
		--security-mirror ${DEBIAN_SECURITY_MIRROR} \
		--image-size 10 \
		--extra-packages frr,bind9-host,joe,rsyslog,most,lldpd,bridge-utils,vlan,bash-completion \
		--password ${VM_ROOT_PASS} \
		--no-cloud-init \
		${ROOT_SSH_KEY_PARAM} \
		--no-remove-host-keys \
		--permit-ssh-as-root \
		--static-iface type=normal,iface0=ens4,addr=${HOST_VIRTUAL_SUBNET_PREFIX}.3/24:${HOST_VIRTUAL_SUBNET_PREFIX}.1 \
		--no-qcow2 \
		--newer-qemu
}

# Params: $1: name of the disk image to generate
ikvswitch_customize_images () {
	local HOSTNAME IP_ADDRESS RESULT_KPARTX MOUNT_DIR LOOP_DEVICE LOOPRAW_DEVICE DO_VLAN_SETUP

	DHCP_SERVER_SUBNET=""
	DO_VLAN_SETUP="no"
	for i in $@ ; do
	case "${1}" in
	"--hostname")
		HOSTNAME=${2}
		shift
		shift
	;;
	"--network-style")
		# This can be "SPINE", "INTERNET" or "LEAF"
		NETWORK_STYLE=${2}
		shift
		shift
	;;
	"--spine-loopback-ip")
		SPINE_LOOPBACK_IP=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--gw")
		GATEWAY=${2}
		shift
		shift
	;;
	"--as")
		AS=${2}
		shift
		shift
	;;
	"--dhcpd")
		DHCP_SERVER_SUBNET=${2}
		shift
		shift
	;;
	"--vlan-setup")
		DO_VLAN_SETUP="yes"
		shift
	;;
	esac
	done

	# Copy the VM template
	echo "===> Customizing template for virtual switch: ${HOSTNAME}"
	echo "cp /var/lib/ikvswitch/templates/ikvswitch-template.raw ./${HOSTNAME}.raw"
	cp /var/lib/ikvswitch/templates/ikvswitch-template.raw ./${HOSTNAME}.raw
	cd ../runtime
	# Mount the drive
	RESULT_KPARTX=$(kpartx -asv ${HOSTNAME}.raw 2>&1)
	if echo "${RESULT_KPARTX}" | grep "^add map" ; then
		LOOP_DEVICE=$(echo ${RESULT_KPARTX} | cut -d" " -f3)
		LOOPRAW_DEVICE=${LOOP_DEVICE%p*}
		echo "kpartx mounted using: ${LOOP_DEVICE} via ${LOOPRAW_DEVICE}"
	else
		echo "It seems kpartx didn't mount the image correctly: exiting."
		exit 1
	fi
	MOUNT_DIR=$(mktemp -d -t ${ME}-switch-mounted-partition.XXXXXX)
	mount /dev/mapper/${LOOP_DEVICE} ${MOUNT_DIR}
	# Start customizing
	echo "${HOSTNAME}" >${MOUNT_DIR}/etc/hostname
	sed -i s/bgpd=no/bgpd=yes/ ${MOUNT_DIR}/etc/frr/daemons

	# make sure we're ip forwarding
        echo "net.ipv4.ip_forward=1" >${MOUNT_DIR}/etc/sysctl.d/00-forward-internet.conf
        echo "net.ipv6.conf.all.forwarding=1" >>${MOUNT_DIR}/etc/sysctl.d/00-forward-internet.conf

	# Configure FRR and /etc/network/interfaces
	###############################
	### SPINE OR INTERNET NODES ###
	###############################
	if [ "${NETWORK_STYLE}" = "SPINE" ] || [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
		if [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
			IP_STUFF="auto ens4
iface ens4 inet static
	address ${IP_ADDRESS}
	netmask 255.255.255.0
	gateway ${GATEWAY}
"
		else
			IP_STUFF="auto ens4
iface ens4 inet manual
	metric 4278198272
"
		fi

		echo "# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

${IP_STUFF}

auto ens5
iface ens5 inet manual
	metric 4278198272

auto ens6
iface ens6 inet manual
	metric 4278198272
" >${MOUNT_DIR}/etc/network/interfaces
		if [ "${NETWORK_STYLE}" = "SPINE" ] ; then
			echo "auto ens7
iface ens7 inet manual
	metric 4278198272

auto ens8
iface ens8 inet manual
	metric 4278198272

auto ens9
iface ens9 inet manual
	metric 4278198272

auto ens10
iface ens10 inet manual
	metric 4278198272

# Set this one last, so that cloud-init or user can
# override defaults.
source /etc/network/interfaces.d/*
" >>${MOUNT_DIR}/etc/network/interfaces
		fi
		echo "log syslog informational

!
int lo
 ip address ${SPINE_LOOPBACK_IP}/32
!
router bgp ${AS}
 bgp router-id ${IP_ADDRESS}
 no bgp default ipv4-unicast
 bgp bestpath as-path multipath-relax
 bgp bestpath compare-routerid
 neighbor leaf peer-group
 neighbor leaf remote-as external
 neighbor leaf capability extended-nexthop
 neighbor spine peer-group
 neighbor spine remote-as external
 neighbor spine capability extended-nexthop
 neighbor ens4 interface peer-group spine
 neighbor ens5 interface peer-group leaf
 neighbor ens6 interface peer-group leaf" >${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${NETWORK_STYLE}" = "SPINE" ] ; then
			echo " neighbor ens7 interface peer-group leaf
 neighbor ens8 interface peer-group leaf
 neighbor ens9 interface peer-group leaf
 neighbor ens10 interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
 		fi
echo "!
 address-family ipv4 unicast
  redistribute kernel route-map map-redistribute
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out
 exit-address-family
 !
 address-family ipv6 unicast
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out
 exit-address-family
exit
!
ip prefix-list pl-default seq 5 permit 0.0.0.0/0
!
route-map map-spine-out permit 10
exit
!
route-map map-spine-in permit 10
exit
!
route-map map-leaf-out permit 10
exit
!
route-map map-redistribute permit 10
 match interface lo
exit
!
route-map map-redistribute permit 20
 match ip address prefix-list pl-default
exit
!
route-map map-leaf-in permit 10
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
	##################
	### LEAF NODES ###
	##################
	else
		# The 2 interfaces connected to the spines switches
		echo "# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

auto ens4
iface ens4 inet manual

auto ens5
iface ens5 inet manual
" >${MOUNT_DIR}/etc/network/interfaces

		# N interfaces for connecting servers
		ENS_END=$(( 6 + ${NUM_U} - 3 ))
		for i in $(seq 6 ${ENS_END}) ; do
			echo "auto ens${i}
iface ens${i} inet manual
	mtu 9000
	metric 4278198272

auto ens${i}.${LEAF_TO_VM_VLAN_NUM}
iface ens${i}.${LEAF_TO_VM_VLAN_NUM} inet manual
	mtu 9000

" >>${MOUNT_DIR}/etc/network/interfaces
			if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
				VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} ))
				for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
					echo "auto ens${i}.${j}
iface ens${i}.${j} inet manual
	mtu 9000

" >>${MOUNT_DIR}/etc/network/interfaces
				done
			fi
		done
		####################################################
		### Setup the bridges for the VLANs (leaf1 only) ###
		####################################################
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} ))
			SUBNET_SUFFIX=${VLAN_SBUNETS_START_IP}
			for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
				MY_VLAN_SUBNET=${HOST_VIRTUAL_SUBNET_START_PREFIX}.${SUBNET_SUFFIX}
				IFACE_LIST=""
				ENS_END=$(( 6 + ${NUM_U} - 3 ))
				for IF in $(seq 6 ${ENS_END}) ; do
					IFACE_LIST="${IFACE_LIST} ens${IF}.${j}"
				done
				echo "auto brvlan${j}
iface brvlan${j} inet static
        bridge_ports ${IFACE_LIST}
        address ${MY_VLAN_SUBNET}.1
        broadcast ${MY_VLAN_SUBNET}.255
        netmask 255.255.255.0
" >>${MOUNT_DIR}/etc/network/interfaces

				SUBNET_SUFFIX=$(( ${SUBNET_SUFFIX} + 1 ))
			done
		fi
		# end of /etc/network/interfaces
		echo "# Set this one last, so that cloud-init or user can
# override defaults.
source /etc/network/interfaces.d/*
" >>${MOUNT_DIR}/etc/network/interfaces

		echo "log syslog informational

!
interface lo
 ip address ${IP_ADDRESS}/32
!
router bgp ${AS}
 bgp router-id ${IP_ADDRESS}
 no bgp default ipv4-unicast
 bgp bestpath as-path multipath-relax
 bgp bestpath compare-routerid
 neighbor leaf peer-group
 neighbor leaf remote-as external
 neighbor leaf capability extended-nexthop
 neighbor spine peer-group
 neighbor spine remote-as external
 neighbor spine capability extended-nexthop
 neighbor ens4 interface peer-group spine
 neighbor ens5 interface peer-group spine" >${MOUNT_DIR}/etc/frr/frr.conf

		ENS_END=$(( 6 + ${NUM_U} - 3 ))
		for IF in $(seq 6 ${ENS_END}) ; do
			echo " neighbor ens${IF}.${LEAF_TO_VM_VLAN_NUM} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
		done

		for VLAN_NEIGH in $(seq 11 16) ; do
			echo " neighbor brvlan${VLAN_NEIGH} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
		done


		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			# Allow both cl1-network-1 and cl2-network-2 to connect and advertize BGP.
			echo " neighbor ${VLAN_SBUNET1_PREFIX}.200 remote-as external" >>${MOUNT_DIR}/etc/frr/frr.conf
			echo " neighbor ${VLAN_SBUNET1_PREFIX}.201 remote-as external" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
		echo " !
 address-family ipv4 unicast
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			# Allow both cl1-network-1 and cl2-network-2 to connect and advertize BGP.
			echo "  neighbor ${VLAN_SBUNET1_PREFIX}.200 activate
  neighbor ${VLAN_SBUNET1_PREFIX}.200 route-map vm-in in
  neighbor ${VLAN_SBUNET1_PREFIX}.200 route-map vm-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
			echo "  neighbor ${HOST_VIRTUAL_SUBNET_START_PREFIX}.113.201 activate
  neighbor ${VLAN_SBUNET1_PREFIX}.201 route-map vm-in in
  neighbor ${VLAN_SBUNET1_PREFIX}.201 route-map vm-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
		echo " exit-address-family
 !
 address-family ipv6 unicast
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out
 exit-address-family
exit
!
route-map map-spine-out permit 10
exit
!
route-map map-spine-in permit 10
exit
!
route-map map-leaf-out permit 10
exit
!
route-map map-redistribute permit 10
 match interface lo
exit
!
route-map map-leaf-in permit 10
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			echo "route-map vm-in permit 10
exit
!
route-map vm-out permit 10
exit
!" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
		if [ -n "${DHCP_SERVER_SUBNET}" ] ; then
			SUBNET_DASH=$(echo ${DHCP_SERVER_SUBNET} | sed -e 's/\./-/g')
			echo "route-map map-redistribute permit 30
 match ip address prefix-list pl-${SUBNET_DASH}
exit
!
ip prefix-list pl-${SUBNET_DASH} seq 10 permit ${DHCP_SERVER_SUBNET}.0/24
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
			echo "DEBIAN_FRONTEND=noninteractive chroot ${MOUNT_DIR} apt-get install -y -o Dpkg::Options::=--force-confold isc-dhcp-server"
			DEBIAN_FRONTEND=noninteractive chroot ${MOUNT_DIR} apt-get install -y -o Dpkg::Options::=--force-confold isc-dhcp-server
			echo "allow booting;
allow bootp;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
ignore-client-uids On;

subnet ${DHCP_SERVER_SUBNET}.0 netmask 255.255.255.0 {
        range ${DHCP_SERVER_SUBNET}.20 ${DHCP_SERVER_SUBNET}.250;
        option domain-name \"example.com\";
        option domain-name-servers 84.16.67.69, 84.16.67.70;
        option routers ${DHCP_SERVER_SUBNET}.1;
        option subnet-mask 255.255.255.0;
        option broadcast-address ${DHCP_SERVER_SUBNET}.255;
        next-server ${DHCP_RELAY_DESTINATION};
        if exists user-class and option user-class = \"iPXE\" {
                filename \"http://${DHCP_RELAY_DESTINATION}/oci/ipxe.php\";
        } elsif exists pxe-system-type {
                if option pxe-system-type = 00:00 {
                        filename \"lpxelinux.0\";
                } elsif option pxe-system-type = 00:07 {
                        filename \"shimx64.efi.signed\";
                } elsif option pxe-system-type = 00:09 {
                        filename \"shimx64.efi.signed\";
                }
        } else {
                filename \"pxelinux.0\";
        }
}
">${MOUNT_DIR}/etc/dhcp/dhcpd.conf
			IFACE_LIST=""
			ENS_END=$(( 6 + ${NUM_U} - 3 ))
			for IF in $(seq 6 ${ENS_END}) ; do
				IFACE_LIST="${IFACE_LIST} ens${IF}"
			done
			echo "auto dhcp0
iface dhcp0 inet static
	bridge_ports ${IFACE_LIST}
	address ${DHCP_SERVER_SUBNET}.1
	broadcast ${DHCP_SERVER_SUBNET}.255
	netmask 255.255.255.0
" >>${MOUNT_DIR}/etc/network/interfaces
#			sed -i 's/INTERFACESv4=.*/INTERFACESv4="ens6 ens7 ens8 ens9 ens10 ens11 ens12 ens13 ens14 ens15 ens17 ens18"/' ${MOUNT_DIR}/etc/default/isc-dhcp-server
		fi
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} ))
			SUBNET_SUFFIX=${VLAN_SBUNETS_START_IP}
			SUBNET_DASH=$(echo ${HOST_VIRTUAL_SUBNET_START_PREFIX}.${VLAN_SBUNETS_START_IP} | sed -e 's/\./-/g')
			echo "route-map map-redistribute permit 31
 match ip address prefix-list pl-${SUBNET_DASH}
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
			SEQ=10
			for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
				MY_VLAN_SUBNET=${HOST_VIRTUAL_SUBNET_START_PREFIX}.${SUBNET_SUFFIX}
				echo " ip prefix-list pl-${SUBNET_DASH} seq ${SEQ} permit ${MY_VLAN_SUBNET}.0/24" >>${MOUNT_DIR}/etc/frr/frr.conf

				SEQ=$(( $SEQ + 1 ))
				SUBNET_SUFFIX=$(( ${SUBNET_SUFFIX} + 1 ))
			done
			echo "!" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
	fi
	# Customize /etc/hosts, so we get all switches to resolve
	echo "127.0.0.1	localhost
${HOST_VIRTUAL_SUBNET_NET3}.1    internet
${HOST_VIRTUAL_SUBNET_NET3}.2    spine1
${HOST_VIRTUAL_SUBNET_NET3}.3    spine2
${HOST_VIRTUAL_SUBNET_NET3}.4    leaf1
${HOST_VIRTUAL_SUBNET_NET3}.5    leaf2
${HOST_VIRTUAL_SUBNET_NET3}.6    leaf3
${HOST_VIRTUAL_SUBNET_NET3}.7    leaf4
${HOST_VIRTUAL_SUBNET_NET3}.8    leaf5
${HOST_VIRTUAL_SUBNET_NET3}.9    leaf6

::1	localhost ip6-localhost ip6-loopback
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
" >${MOUNT_DIR}/etc/hosts
	# Unmount
	umount ${MOUNT_DIR}
	rm -r ${MOUNT_DIR}
	kpartx -d ${HOSTNAME}.raw
}

ikv_spine_switch_write_ipmisim () {
	local NAME VNC_PORT HOST_IPMI_PORT GUEST_IPMI_CHART_PORT SD_PATH VM_PID_FILE FIRST_DRIVE MONITOR_PORT TAP_IFNAME MAC_END
	local OTHER_IFS_NUMS OTHER_IFS_TAP_PREFIX OTHER_IFS_TAP_SUFIX_START OTHER_IFS_MAC_END IFS_DEF
	for i in $@ ; do
	case "${1}" in
	"--name")
		NAME=${2}
		shift
		shift
	;;
	"--vnc-port")
		VNC_PORT=${2}
		shift
		shift
	;;
	"--tap-ifname")
		TAP_IFNAME=${2}
		shift
		shift
	;;
	"--mac-end")
		MAC_END=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--other-ifs-num")
		OTHER_IFS_NUMS=${2}
		shift
		shift
	;;
	"--other-ifs-tap-prefix")
		OTHER_IFS_TAP_PREFIX=${2}
		shift
		shift
	;;
	"--other-ifs-tap-sufix-start")
		OTHER_IFS_TAP_SUFIX_START=${2}
		shift
		shift
	;;
	"--other-ifs-mac-end")
		OTHER_IFS_MAC_END=${2}
		shift
		shift
	;;
	esac
	done

	echo "===> Spinning up ${NAME}"
	SD_PATH=/var/lib/ikvswitch/runtime/${NAME}.raw
	VM_PID_FILE=/var/lib/ikvswitch/runtime/${NAME}.pid
	FIRST_DRIVE="-device virtio-scsi-pci,id=scsi0 -drive if=none,file=${SD_PATH},index=0,media=disk,format=raw,cache=none,discard=unmap,aio=native,id=drive-scsi0-0-0-0 -device scsi-hd,drive=drive-scsi0-0-0-0,bus=scsi0.0"

	HOST_IPMI_PORT=$(( ${VNC_PORT} + 9000 ))
	GUEST_IPMI_CHART_PORT=$(( ${VNC_PORT} + 9100 ))
	MONITOR_PORT=$(( 55000 + ${VNC_PORT}))

  # Generate network qemu parameters
	IFS_DEF="-device virtio-net-pci,netdev=net0,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC_END} -netdev tap,id=net0,ifname=${TAP_IFNAME}"

	if [ -n "${OTHER_IFS_NUMS}" ] ; then
		for i in $(seq 1 ${OTHER_IFS_NUMS}) ; do
			IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net${OTHER_IFS_TAP_SUFIX_START},mac=${GUEST_MAC_ADDRESS_PREFIX}${OTHER_IFS_MAC_END} -netdev tap,id=net${OTHER_IFS_TAP_SUFIX_START},ifname=${OTHER_IFS_TAP_PREFIX}${OTHER_IFS_TAP_SUFIX_START}"
			OTHER_IFS_MAC_END=$(printf "%X\n" $((0x$OTHER_IFS_MAC_END + 1)))
			OTHER_IFS_TAP_SUFIX_START=$(( $OTHER_IFS_TAP_SUFIX_START + 1 ))
		done
	fi

        echo "name \"ipmisim1\"
set_working_mc 0x20
  startlan 1
    addr 0.0.0.0 ${HOST_IPMI_PORT}
    priv_limit admin
    allowed_auths_callback none md2 md5 straight
    allowed_auths_user none md2 md5 straight
    allowed_auths_operator none md2 md5 straight
    allowed_auths_admin none md2 md5 straight
    guid a123456789abcdefa123456789abcdef
  endlan
  serial 15 localhost ${GUEST_IPMI_CHART_PORT} codec VM
  startcmd \"qemu-system-x86_64 -enable-kvm -m size=512M -smp cpus=2 -cpu host,+spec-ctrl -vnc :${VNC_PORT} -monitor tcp:127.0.0.1:${MONITOR_PORT},server,nowait -pidfile ${VM_PID_FILE} -daemonize ${FIRST_DRIVE} -boot c ${IFS_DEF} -smbios type=1,manufacturer=LinuxKVM,product=qemu-oci,serial=${NAME} -smbios type=3,manufacturer=LinuxKVM,serial=${NAME} -chardev socket,id=ipmi0,host=localhost,port=${GUEST_IPMI_CHART_PORT},reconnect=10 -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0 -device isa-ipmi-kcs,bmc=bmc0,irq=5\"
  startnow true
  user 1 true  \"\"        \"test\" user     10       none md2 md5 straight
  user 2 true  \"ipmiusr\" \"test\" admin    10       none md2 md5 straight
" >/var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf

	start-stop-daemon \
		--start \
		--quiet \
		--background \
		--pidfile ${VM_PID_FILE}.ipmisim.pid \
		--make-pidfile \
		--startas /usr/bin/ipmi_sim \
		--      -n \
			-c /var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf \
			-f /etc/ikvswitch/ipmisim1.emu \
		|| return 2

}

ikv_leaf_switch_write_ipmisim () {
	local NAME VNC_PORT HOST_IPMI_PORT GUEST_IPMI_CHART_PORT SD_PATH VM_PID_FILE FIRST_DRIVE MONITOR_PORT TAP_IF1_NAME TAP_IF2_NAME MAC1_END MAC2_END
	local OTHER_IFS_NUMS OTHER_IFS_TAP_PREFIX OTHER_IFS_TAP_SUFIX_START OTHER_IFS_MAC_END IFS_DEF
	for i in $@ ; do
	case "${1}" in
	"--name")
		NAME=${2}
		shift
		shift
	;;
	"--vnc-port")
		VNC_PORT=${2}
		shift
		shift
	;;
	"--tap-if1-name")
		TAP_IF1_NAME=${2}
		shift
		shift
	;;
	"--mac1-end")
		MAC1_END=${2}
		shift
		shift
	;;
	"--tap-if2-name")
		TAP_IF2_NAME=${2}
		shift
		shift
	;;
	"--mac2-end")
		MAC2_END=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--other-ifs-num")
		OTHER_IFS_NUMS=${2}
		shift
		shift
	;;
	"--other-ifs-tap-prefix")
		OTHER_IFS_TAP_PREFIX=${2}
		shift
		shift
	;;
	"--other-ifs-tap-sufix-start")
		OTHER_IFS_TAP_SUFIX_START=${2}
		shift
		shift
	;;
	"--other-ifs-mac-end")
		OTHER_IFS_MAC_END=${2}
		shift
		shift
	;;
	esac
	done

	echo "===> Spinning up ${NAME}"
	SD_PATH=/var/lib/ikvswitch/runtime/${NAME}.raw
	VM_PID_FILE=/var/lib/ikvswitch/runtime/${NAME}.pid
	FIRST_DRIVE="-device virtio-scsi-pci,id=scsi0 -drive if=none,file=${SD_PATH},index=0,media=disk,format=raw,cache=none,discard=unmap,aio=native,id=drive-scsi0-0-0-0 -device scsi-hd,drive=drive-scsi0-0-0-0,bus=scsi0.0"

	HOST_IPMI_PORT=$(( ${VNC_PORT} + 9000 ))
	GUEST_IPMI_CHART_PORT=$(( ${VNC_PORT} + 9100 ))
	MONITOR_PORT=$(( 55000 + ${VNC_PORT}))

  # Define connection the to current rack leafs
	IFS_DEF="-device virtio-net-pci,netdev=net0,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC1_END} -netdev tap,id=net0,ifname=${TAP_IF1_NAME}"
	IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net1,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC2_END} -netdev tap,id=net1,ifname=${TAP_IF2_NAME}"

  # Connection to the other VMs
	if [ -n "${OTHER_IFS_NUMS}" ] ; then
		for i in $(seq 3 ${OTHER_IFS_NUMS}) ; do
			IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net${OTHER_IFS_TAP_SUFIX_START},mac=${GUEST_MAC_ADDRESS_PREFIX}${OTHER_IFS_MAC_END} -netdev tap,id=net${OTHER_IFS_TAP_SUFIX_START},ifname=${OTHER_IFS_TAP_PREFIX}${OTHER_IFS_TAP_SUFIX_START}"
			OTHER_IFS_MAC_END="$(mac_add ${OTHER_IFS_MAC_END} 1)"
			OTHER_IFS_TAP_SUFIX_START=$(( $OTHER_IFS_TAP_SUFIX_START + 1 ))
		done
	fi

        echo "name \"ipmisim1\"
set_working_mc 0x20
  startlan 1
    addr 0.0.0.0 ${HOST_IPMI_PORT}
    priv_limit admin
    allowed_auths_callback none md2 md5 straight
    allowed_auths_user none md2 md5 straight
    allowed_auths_operator none md2 md5 straight
    allowed_auths_admin none md2 md5 straight
    guid a123456789abcdefa123456789abcdef
  endlan
  serial 15 localhost ${GUEST_IPMI_CHART_PORT} codec VM
  startcmd \"qemu-system-x86_64 -enable-kvm -m size=512M -smp cpus=2 -cpu host,+spec-ctrl -vnc :${VNC_PORT} -monitor tcp:127.0.0.1:${MONITOR_PORT},server,nowait -pidfile ${VM_PID_FILE} -daemonize ${FIRST_DRIVE} -boot c ${IFS_DEF} -smbios type=1,manufacturer=LinuxKVM,product=qemu-oci,serial=${NAME} -smbios type=3,manufacturer=LinuxKVM,serial=${NAME} -chardev socket,id=ipmi0,host=localhost,port=${GUEST_IPMI_CHART_PORT},reconnect=10 -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0 -device isa-ipmi-kcs,bmc=bmc0,irq=5\"
  startnow true
  user 1 true  \"\"        \"test\" user     10       none md2 md5 straight
  user 2 true  \"ipmiusr\" \"test\" admin    10       none md2 md5 straight
" >/var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf

	start-stop-daemon \
		--start \
		--quiet \
		--background \
		--pidfile ${VM_PID_FILE}.ipmisim.pid \
		--make-pidfile \
		--startas /usr/bin/ipmi_sim \
		--      -n \
			-c /var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf \
			-f /etc/ikvswitch/ipmisim1.emu \
		|| return 2

}

MYDIR=$(pwd)

if ! [ -r /var/lib/ikvswitch/templates/ikvswitch-template.raw ] ; then
	cd /var/lib/ikvswitch/templates
	ikvswitch_build_template
fi
cd /var/lib/ikvswitch/runtime

usage () {
	echo "usage: $ME <start/stop>"
	exit 1
}

case "${1}" in
"start")
	rm /var/lib/ikvswitch/runtime/* || true
	# The "Internet" VM
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.1 --as ${AS_START} --hostname internet --network-style INTERNET --ip-address ${HOST_VIRTUAL_SUBNET_NET1}.2 --gw ${HOST_VIRTUAL_SUBNET_NET1}.1
	MY_AS=$(( ${AS_START} + 1 ))

	# The 2x "Spine" VMs
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.2 --as ${MY_AS} --hostname spine1 --network-style SPINE --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.2
	MY_AS=$(( ${MY_AS} + 1 ))
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.3 --as ${MY_AS} --hostname spine2 --network-style SPINE --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.3

	# The 6x "Leaf" VMs, with 1, 3 and 5 having DHCPd
	for i in $(seq 1 6) ; do
		MY_AS=$(( ${MY_AS} + 1 ))
		# We install a DHCP server only on left switch rack
		# ie: one switch per rack.
		case "${i}" in
		"1")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_1}"	;;
		"3")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_2}"	;;
		"5")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_3}"	;;
		*)	DHCP_SERVER=""	;;
		esac
		LEAF_LOOPBACK_IP=$(( $i + 3 ))
		if [ "${i}" = 2 ] ; then
			VLAN_OPT="--vlan-setup"
		else
			VLAN_OPT=""
		fi
		ikvswitch_customize_images ${VLAN_OPT} --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.${LEAF_LOOPBACK_IP} --as ${MY_AS} --hostname leaf${i} --network-style LEAF --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.${LEAF_LOOPBACK_IP} ${DHCP_SERVER}
	done

	# The "Internet" VM
	ikv_spine_switch_write_ipmisim --name internet --vnc-port 80 --tap-ifname ${HOST_VM_TAP_IFNAME_PREFIX} --mac-end 10 --other-ifs-num 2 --other-ifs-tap-prefix ${INTERNET_TO_SPINE_TAP_IFNAME_PREFIX} --other-ifs-tap-sufix-start 1 --other-ifs-mac-end 12

	# Both 2x "Spine" VMs
	ikv_spine_switch_write_ipmisim --name spine1 --vnc-port 81 --tap-ifname ${SPINE_TO_INTERNET_TAP_PREFIX}1 --mac-end 21 --other-ifs-num 6 --other-ifs-tap-prefix ${SPINE_VM_TAP_IFNAME_PREFIX}1 --other-ifs-tap-sufix-start 2 --other-ifs-mac-end 22
	ikv_spine_switch_write_ipmisim --name spine2 --vnc-port 82 --tap-ifname ${SPINE_TO_INTERNET_TAP_PREFIX}2 --mac-end 31 --other-ifs-num 6 --other-ifs-tap-prefix ${SPINE_VM_TAP_IFNAME_PREFIX}2 --other-ifs-tap-sufix-start 2 --other-ifs-mac-end 32

	MAC_END=41 # Used to compute mac addresses automatically according to the number of VM per rack

	# The 6x "Leaf" VMs
	ikv_leaf_switch_write_ipmisim --name leaf1 --vnc-port 83 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}11 --mac1-end "${MAC_END}" --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}12 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}1- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.4

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf2 --vnc-port 84 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}21 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}22 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}2- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.5

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf3 --vnc-port 85 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}31 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}32 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}3- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.6

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf4 --vnc-port 86 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}41 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}42 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}4- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.7

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf5 --vnc-port 87 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}51 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}52 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}5- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.8

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf6 --vnc-port 88 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}61 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}62 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}6- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.9

;;
"stop")
	echo "===> Killing all VMs and IPMI SIM:"
	for i in $(ls /var/lib/ikvswitch/runtime/*.pid) ; do
		PID=$(cat $i)
		echo " $PID"
		kill -9 ${PID} || true
	done
;;
*)
	usage
;;
esac

cd ${MYDIR}

echo "===> All done <==="