diff options
Diffstat (limited to 'scripts/runqemu-ifup')
| -rwxr-xr-x | scripts/runqemu-ifup | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/scripts/runqemu-ifup b/scripts/runqemu-ifup new file mode 100755 index 0000000000..c963af5df9 --- /dev/null +++ b/scripts/runqemu-ifup | |||
| @@ -0,0 +1,95 @@ | |||
| 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 Poky 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 Poky'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 poky-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-2010 Intel Corp. | ||
| 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) <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 2 ]; then | ||
| 46 | usage | ||
| 47 | exit 1 | ||
| 48 | fi | ||
| 49 | |||
| 50 | GROUP="-g $1" | ||
| 51 | NATIVE_SYSROOT_DIR=$2 | ||
| 52 | |||
| 53 | TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl | ||
| 54 | if [ ! -x "$TUNCTL" ]; then | ||
| 55 | echo "Error: Unable to find tunctl binary in '$NATIVE_SYSROOT_DIR/usr/bin'" | ||
| 56 | exit 1 | ||
| 57 | fi | ||
| 58 | |||
| 59 | TAP=`$TUNCTL -b $GROUP 2>&1` | ||
| 60 | STATUS=$? | ||
| 61 | if [ $STATUS -ne 0 ]; then | ||
| 62 | echo "tunctl failed:" | ||
| 63 | echo $TAP | ||
| 64 | exit 1 | ||
| 65 | fi | ||
| 66 | |||
| 67 | IFCONFIG=`which ifconfig` | ||
| 68 | if [ "x$IFCONFIG" = "x" ]; then | ||
| 69 | # better than nothing... | ||
| 70 | IFCONFIG=/sbin/ifconfig | ||
| 71 | fi | ||
| 72 | |||
| 73 | ROUTE=`which route` | ||
| 74 | if [ "x$ROUTE" = "x" ]; then | ||
| 75 | # better than nothing... | ||
| 76 | ROUTE=/sbin/route | ||
| 77 | fi | ||
| 78 | |||
| 79 | n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] | ||
| 80 | $IFCONFIG $TAP 192.168.7.$n | ||
| 81 | |||
| 82 | dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ] | ||
| 83 | $ROUTE add -host 192.168.7.$dest $TAP | ||
| 84 | |||
| 85 | # setup NAT for tap0 interface to have internet access in QEMU | ||
| 86 | IPTABLES=`which iptables` | ||
| 87 | if [ "x$IPTABLES" = "x" ]; then | ||
| 88 | IPTABLES=/sbin/iptables | ||
| 89 | fi | ||
| 90 | |||
| 91 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.0/24 | ||
| 92 | echo 1 > /proc/sys/net/ipv4/ip_forward | ||
| 93 | $IPTABLES -P FORWARD ACCEPT | ||
| 94 | |||
| 95 | echo $TAP | ||
