1#!/bin/bash 2# 3# Copyright (c) 2018 Intel Corporation 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17usage() { 18 cat <<EOF 19$0 [--config|-c <file>] [--iface|-i <interface>] [start|up] [stop|down] 20 [any optional parameters for ip command] 21 22If no parameters are given, then "zeth" network interface and "zeth.conf" 23configuration file are used. The script waits until user presses CTRL-c 24and then removes the network interface. 25 26Examples: 27 28$ net-setup.sh 29$ net-setup.sh --config zeth-vlan.conf 30$ net-setup.sh --config my-own-config.conf --iface foobar 31 32It is also possible to let the script return and then stop the network 33interface later. Is can be done by first creating the interface with 34"start" or "up" command, and then later remove the interface with 35"stop" or "down" command. 36 37$ net-setup.sh start 38do your things here 39$ net-setup.sh stop 40 41$ net-setup.sh --config my-own-config.conf up 42do your things here 43$ net-setup.sh --config my-own-config.conf down 44 45Any extra parameters that the script does not know, are passed directly 46to "ip" command. 47 48$ net-setup.sh --config my-own-config.conf --iface foo user bar 49 50EOF 51 exit 52} 53 54if [ "$1" = "-h" -o "$1" = "--help" ]; then 55 usage 56fi 57 58if [ `id -u` != 0 ]; then 59 echo "Run this script as a root user!" 60 sudo $0 $@ 61 exit 62fi 63 64IFACE=zeth 65 66# The counter variable is passed to configuration script where 67# it can be used for example to create multiple network interfaces etc. 68# The -t option can be used to set the variable. 69COUNTER=1 70 71# Default config file setups default connectivity IP addresses 72CONF_FILE=./zeth.conf 73 74while [ $# -gt 0 ] 75do 76 case $1 in 77 --config|-c) 78 CONF_FILE="$2" 79 shift 2 80 ;; 81 --iface|-i) 82 IFACE="$2" 83 shift 2 84 ;; 85 --times|-t) 86 COUNTER="$2" 87 shift 2 88 ;; 89 --help|-h) 90 usage 91 ;; 92 up|start) 93 ACTION=start 94 shift 95 ;; 96 down|stop) 97 ACTION=stop 98 shift 99 ;; 100 *) 101 break 102 ;; 103 esac 104done 105 106if [ ! -f "$CONF_FILE" ]; then 107 if [ ! -f "${0%/*}/$CONF_FILE" ];then 108 echo "No such file '$CONF_FILE'" 109 exit 110 fi 111 112 CONF_FILE="${0%/*}/$CONF_FILE" 113fi 114 115echo "Using $CONF_FILE configuration file." 116 117STOPPED=0 118trap ctrl_c INT TERM 119 120ctrl_c() { 121 STOPPED=1 122} 123 124if [ "$ACTION" != stop ]; then 125 echo "Creating $IFACE" 126 ip tuntap add $IFACE mode tap $@ 127 128 # The idea is that the configuration file will setup 129 # the IP addresses etc. for the created interface. 130 . "$CONF_FILE" $IFACE 131fi 132 133if [ "$ACTION" = "" ]; then 134 while [ $STOPPED -eq 0 ]; do 135 sleep 1d 136 done 137fi 138 139if [ "$ACTION" != start ]; then 140 ip link set $IFACE down 141 142 if [ -f "$CONF_FILE".stop ]; then 143 . "$CONF_FILE".stop $IFACE 144 fi 145 146 echo "Removing $IFACE" 147 ip tuntap del $IFACE mode tap 148fi 149