summaryrefslogtreecommitdiff
blob: 0c92633a01d691f1a24bae3ac0fe82d5007657d5 (plain)
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
#!/bin/sh
[ -z "$1" ] || ( [ "$1" != "create" ] && [ -z "$2" ] ) && { echo "invalid usage, require create [device] || |destroy device"; exit 1; }

KVM_USER=${KVM_USER:-"root"}

has() {
	local desired=$1 x
	shift
	for x in "$@"; do
		[ "$desired" = "$x" ] && return 0;
	done
	return 1
}

find_node() {
	local val=$(ifconfig -a | grep -i ^qtap | cut -d ' ' -f1)
	local pos=0
	while has qtap${pos} $val; do
		pos=$(( $pos + 1 ))
	done
	echo qtap${pos}
}

create_node() {
	tunctl -b -u "${KVM_USER}" -t ${1} > /dev/null || { echo "tunctl failed"; exit 2; }
	brctl addif br0 ${1} || { echo "brctl failed"; exit 2; }
	ifconfig ${1} up 0.0.0.0 promisc || { echo "ifconfig failed"; exit 2; }
}

destroy_node() {
	issue=
	ifconfig ${1} down || { echo "ifconfig failed";issue=1; }
	brctl delif br0 ${1} || { echo "brctl failed";issue=2; }
	tunctl -d ${1} > /dev/null || { echo "tunctl failed";issue=3;}
	[ -n "${issue}" ] && exit -$(( $issue ))
}

command=$1
qtap=$2

if [ -z "$qtap" ]; then
	qtap=$(find_node)
fi

if [ "$command" = "create" ]; then
	create_node "$qtap"
	[ "$2" != "${qtap}" ] && echo "${qtap}"
elif [ "$command" = "destroy" ]; then
	destroy_node "$qtap"
else
	echo "$command isn't a valid command; must be create or destroy";
	exit 1
fi

exit 0