| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
 | 3 | # QEMU network interface configuration script. This utility needs to | 
 | 4 | # be run as root, and will use the tunctl binary from a native sysroot. | 
 | 5 | # Note: many Linux distros these days still use an older version of | 
 | 6 | # tunctl which does not support the group permissions option, hence | 
 | 7 | # the need to use build system's version. | 
 | 8 | # | 
 | 9 | # If you find yourself calling this script a lot, you can add the | 
 | 10 | # the following to your /etc/sudoers file to be able to run this | 
 | 11 | # command without entering your password each time: | 
 | 12 | # | 
 | 13 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup | 
 | 14 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown | 
 | 15 | # | 
 | 16 | # If you'd like to create a bank of tap devices at once, you should use | 
 | 17 | # the runqemu-gen-tapdevs script instead. If tap devices are set up using | 
 | 18 | # that script, the runqemu script will never end up calling this | 
 | 19 | # script. | 
 | 20 | # | 
 | 21 | # Copyright (c) 2006-2011 Linux Foundation | 
 | 22 | # | 
 | 23 | # This program is free software; you can redistribute it and/or modify | 
 | 24 | # it under the terms of the GNU General Public License version 2 as | 
 | 25 | # published by the Free Software Foundation. | 
 | 26 | # | 
 | 27 | # This program is distributed in the hope that it will be useful, | 
 | 28 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 29 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the | 
 | 30 | # GNU General Public License for more details. | 
 | 31 | # | 
 | 32 | # You should have received a copy of the GNU General Public License along | 
 | 33 | # with this program; if not, write to the Free Software Foundation, Inc., | 
 | 34 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
 | 35 |  | 
 | 36 | usage() { | 
 | 37 | 	echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir>" | 
 | 38 | } | 
 | 39 |  | 
 | 40 | if [ $EUID -ne 0 ]; then | 
 | 41 | 	echo "Error: This script (runqemu-ifup) must be run with root privileges" | 
 | 42 | 	exit 1 | 
 | 43 | fi | 
 | 44 |  | 
 | 45 | if [ $# -ne 3 ]; then | 
 | 46 | 	usage | 
 | 47 | 	exit 1 | 
 | 48 | fi | 
 | 49 |  | 
 | 50 | USERID="-u $1" | 
 | 51 | GROUP="-g $2" | 
 | 52 | NATIVE_SYSROOT_DIR=$3 | 
 | 53 |  | 
 | 54 | TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl | 
 | 55 | if [ ! -x "$TUNCTL" ]; then | 
 | 56 |        echo "Error: Unable to find tunctl binary in '$NATIVE_SYSROOT_DIR/usr/bin', please bitbake qemu-helper-native" | 
 | 57 | 	exit 1 | 
 | 58 | fi | 
 | 59 |  | 
 | 60 | TAP=`$TUNCTL -b $GROUP 2>&1` | 
 | 61 | STATUS=$? | 
 | 62 | if [ $STATUS -ne 0 ]; then | 
 | 63 | # If tunctl -g fails, try using tunctl -u, for older host kernels  | 
 | 64 | # which do not support the TUNSETGROUP ioctl | 
 | 65 | 	TAP=`$TUNCTL -b $USERID 2>&1` | 
 | 66 | 	STATUS=$? | 
 | 67 | 	if [ $STATUS -ne 0 ]; then | 
 | 68 | 		echo "tunctl failed:" | 
 | 69 | 		exit 1 | 
 | 70 | 	fi | 
 | 71 | fi | 
 | 72 |  | 
 | 73 | IFCONFIG=`which ip 2> /dev/null` | 
 | 74 | if [ "x$IFCONFIG" = "x" ]; then | 
 | 75 | 	# better than nothing... | 
 | 76 | 	IFCONFIG=/sbin/ip | 
 | 77 | fi | 
 | 78 | if [ ! -x "$IFCONFIG" ]; then | 
 | 79 | 	echo "$IFCONFIG cannot be executed" | 
 | 80 | 	exit 1 | 
 | 81 | fi | 
 | 82 |  | 
 | 83 | IPTABLES=`which iptables 2> /dev/null` | 
 | 84 | if [ "x$IPTABLES" = "x" ]; then | 
 | 85 | 	IPTABLES=/sbin/iptables | 
 | 86 | fi | 
 | 87 | if [ ! -x "$IPTABLES" ]; then | 
 | 88 | 	echo "$IPTABLES cannot be executed" | 
 | 89 | 	exit 1 | 
 | 90 | fi | 
 | 91 |  | 
 | 92 | n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] | 
 | 93 | $IFCONFIG addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 94 | STATUS=$? | 
 | 95 | if [ $STATUS -ne 0 ]; then | 
 | 96 |     echo "Failed to set up IP addressing on $TAP" | 
 | 97 |     exit 1 | 
 | 98 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | $IFCONFIG link set dev $TAP up | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 100 | STATUS=$? | 
 | 101 | if [ $STATUS -ne 0 ]; then | 
 | 102 |     echo "Failed to bring up $TAP" | 
 | 103 |     exit 1 | 
 | 104 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 |  | 
 | 106 | dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ] | 
 | 107 | $IFCONFIG route add to 192.168.7.$dest dev $TAP | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 108 | STATUS=$? | 
 | 109 | if [ $STATUS -ne 0 ]; then | 
 | 110 |     echo "Failed to add route to 192.168.7.$dest using $TAP" | 
 | 111 |     exit 1 | 
 | 112 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 |  | 
 | 114 | # setup NAT for tap0 interface to have internet access in QEMU | 
 | 115 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32 | 
 | 116 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32 | 
 | 117 | echo 1 > /proc/sys/net/ipv4/ip_forward | 
 | 118 | echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp | 
 | 119 | $IPTABLES -P FORWARD ACCEPT | 
 | 120 |  | 
 | 121 | echo $TAP |