diff options
Diffstat (limited to 'qtap-manipulate')
-rwxr-xr-x | qtap-manipulate | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/qtap-manipulate b/qtap-manipulate new file mode 100755 index 0000000..b75c5ef --- /dev/null +++ b/qtap-manipulate @@ -0,0 +1,55 @@ +#!/bin/sh +[ -z "$1" ] || ( [ "$1" != "create" ] && [ -z "$2" ] ) && { echo "invalid usage, require create [device] || |destroy device"; exit 1; } + +#KVM_USER="kvm-envs" + +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 |