| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # Create a "bank" of tap network devices that can be used by the | 
|  | 4 | # runqemu script. This script needs to be run as root, and will | 
|  | 5 | # use the tunctl binary from the build system sysroot. Note: many Linux | 
|  | 6 | # distros these days still use an older version of tunctl which does not | 
|  | 7 | # support the group permissions option, hence the need to use the build | 
|  | 8 | # system provided version. | 
|  | 9 | # | 
|  | 10 | # Copyright (C) 2010 Intel Corp. | 
|  | 11 | # | 
|  | 12 | # This program is free software; you can redistribute it and/or modify | 
|  | 13 | # it under the terms of the GNU General Public License version 2 as | 
|  | 14 | # published by the Free Software Foundation. | 
|  | 15 | # | 
|  | 16 | # This program is distributed in the hope that it will be useful, | 
|  | 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the | 
|  | 19 | # GNU General Public License for more details. | 
|  | 20 | # | 
|  | 21 | # You should have received a copy of the GNU General Public License along | 
|  | 22 | # with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 24 |  | 
|  | 25 | usage() { | 
|  | 26 | echo "Usage: sudo $0 <uid> <gid> <num> <native-sysroot-basedir>" | 
|  | 27 | echo "Where <uid> is the numeric user id the tap devices will be owned by" | 
|  | 28 | echo "Where <gid> is the numeric group id the tap devices will be owned by" | 
|  | 29 | echo "<num> is the number of tap devices to create (0 to remove all)" | 
|  | 30 | echo "<native-sysroot-basedir> is the path to the build system's native sysroot" | 
|  | 31 | exit 1 | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | if [ $EUID -ne 0 ]; then | 
|  | 35 | echo "Error: This script must be run with root privileges" | 
|  | 36 | exit | 
|  | 37 | fi | 
|  | 38 |  | 
|  | 39 | if [ $# -ne 4 ]; then | 
|  | 40 | echo "Error: Incorrect number of arguments" | 
|  | 41 | usage | 
|  | 42 | fi | 
|  | 43 |  | 
|  | 44 | TUID=$1 | 
|  | 45 | GID=$2 | 
|  | 46 | COUNT=$3 | 
|  | 47 | SYSROOT=$4 | 
|  | 48 |  | 
|  | 49 | TUNCTL=$SYSROOT/usr/bin/tunctl | 
|  | 50 | if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then | 
|  | 51 | echo "Error: $TUNCTL is not an executable" | 
|  | 52 | usage | 
|  | 53 | fi | 
|  | 54 |  | 
|  | 55 | SCRIPT_DIR=`dirname $0` | 
|  | 56 | RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup" | 
|  | 57 | if [ ! -x "$RUNQEMU_IFUP" ]; then | 
|  | 58 | echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR" | 
|  | 59 | exit 1 | 
|  | 60 | fi | 
|  | 61 |  | 
|  | 62 | IFCONFIG=`which ip 2> /dev/null` | 
|  | 63 | if [ -z "$IFCONFIG" ]; then | 
|  | 64 | # Is it ever anywhere else? | 
|  | 65 | IFCONFIG=/sbin/ip | 
|  | 66 | fi | 
|  | 67 | if [ ! -x "$IFCONFIG" ]; then | 
|  | 68 | echo "$IFCONFIG cannot be executed" | 
|  | 69 | exit 1 | 
|  | 70 | fi | 
|  | 71 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | if [ $COUNT -ge 0 ]; then | 
|  | 73 | # Ensure we start with a clean slate | 
|  | 74 | for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do | 
|  | 75 | echo "Note: Destroying pre-existing tap interface $tap..." | 
|  | 76 | $TUNCTL -d $tap | 
|  | 77 | done | 
|  | 78 | rm -f /etc/runqemu-nosudo | 
|  | 79 | else | 
|  | 80 | echo "Error: Incorrect count: $COUNT" | 
|  | 81 | exit 1 | 
|  | 82 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 84 | if [ $COUNT -gt 0 ]; then | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 85 | echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..." | 
|  | 86 | for ((index=0; index < $COUNT; index++)); do | 
|  | 87 | echo "Creating tap$index" | 
|  | 88 | ifup=`$RUNQEMU_IFUP $TUID $GID $SYSROOT 2>&1` | 
|  | 89 | if [ $? -ne 0 ]; then | 
|  | 90 | echo "Error running tunctl: $ifup" | 
|  | 91 | exit 1 | 
|  | 92 | fi | 
|  | 93 | done | 
|  | 94 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 95 | echo "Note: For systems running NetworkManager, it's recommended" | 
|  | 96 | echo "Note: that the tap devices be set as unmanaged in the" | 
|  | 97 | echo "Note: NetworkManager.conf file. Add the following lines to" | 
|  | 98 | echo "Note: /etc/NetworkManager/NetworkManager.conf" | 
|  | 99 | echo "[keyfile]" | 
|  | 100 | echo "unmanaged-devices=interface-name:tap*" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 101 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | # The runqemu script will check for this file, and if it exists, | 
|  | 103 | # will use the existing bank of tap devices without creating | 
|  | 104 | # additional ones via sudo. | 
|  | 105 | touch /etc/runqemu-nosudo | 
|  | 106 | fi |