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 |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 4 | # be run as root, and will use the ip utility |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | # |
| 6 | # If you find yourself calling this script a lot, you can add the |
| 7 | # the following to your /etc/sudoers file to be able to run this |
| 8 | # command without entering your password each time: |
| 9 | # |
| 10 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup |
| 11 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown |
| 12 | # |
| 13 | # If you'd like to create a bank of tap devices at once, you should use |
| 14 | # the runqemu-gen-tapdevs script instead. If tap devices are set up using |
| 15 | # that script, the runqemu script will never end up calling this |
| 16 | # script. |
| 17 | # |
| 18 | # Copyright (c) 2006-2011 Linux Foundation |
| 19 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 20 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | usage() { |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 24 | echo "sudo $(basename $0) <gid>" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | if [ $EUID -ne 0 ]; then |
| 28 | echo "Error: This script (runqemu-ifup) must be run with root privileges" |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 32 | if [ $# -eq 2 ]; then |
| 33 | echo "Warning: uid parameter is ignored. It is no longer needed." |
| 34 | GROUP="$2" |
| 35 | elif [ $# -eq 1 ]; then |
| 36 | GROUP="$1" |
| 37 | else |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | usage |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 43 | if [ -z "$OE_TAP_NAME" ]; then |
| 44 | OE_TAP_NAME=tap |
| 45 | fi |
| 46 | |
| 47 | if taps=$(ip tuntap list 2>/dev/null); then |
| 48 | tap_no_last=$(echo "$taps" |cut -f 1 -d ":" |grep -E "^$OE_TAP_NAME.*" |sed "s/$OE_TAP_NAME//g" | sort -rn | head -n 1) |
| 49 | if [ -z "$tap_no_last" ]; then |
| 50 | tap_no=0 |
| 51 | else |
| 52 | tap_no=$(("$tap_no_last" + 1)) |
| 53 | fi |
| 54 | ip tuntap add "$OE_TAP_NAME$tap_no" mode tap group "$GROUP" && TAP=$OE_TAP_NAME$tap_no |
| 55 | fi |
| 56 | |
| 57 | if [ -z "$TAP" ]; then |
| 58 | echo "Error: Unable to find a tap device to use" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | exit 1 |
| 60 | fi |
| 61 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 62 | IPTOOL=`which ip 2> /dev/null` |
| 63 | if [ "x$IPTOOL" = "x" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | # better than nothing... |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 65 | IPTOOL=/sbin/ip |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | fi |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 67 | if [ ! -x "$IPTOOL" ]; then |
| 68 | echo "$IPTOOL cannot be executed" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | IPTABLES=`which iptables 2> /dev/null` |
| 73 | if [ "x$IPTABLES" = "x" ]; then |
| 74 | IPTABLES=/sbin/iptables |
| 75 | fi |
| 76 | if [ ! -x "$IPTABLES" ]; then |
| 77 | echo "$IPTABLES cannot be executed" |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 81 | n=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 1 ] |
| 82 | $IPTOOL 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] | 83 | STATUS=$? |
| 84 | if [ $STATUS -ne 0 ]; then |
| 85 | echo "Failed to set up IP addressing on $TAP" |
| 86 | exit 1 |
| 87 | fi |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 88 | $IPTOOL link set dev $TAP up |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 89 | STATUS=$? |
| 90 | if [ $STATUS -ne 0 ]; then |
| 91 | echo "Failed to bring up $TAP" |
| 92 | exit 1 |
| 93 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 95 | dest=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 2 ] |
| 96 | $IPTOOL route add to 192.168.7.$dest dev $TAP |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 97 | STATUS=$? |
| 98 | if [ $STATUS -ne 0 ]; then |
| 99 | echo "Failed to add route to 192.168.7.$dest using $TAP" |
| 100 | exit 1 |
| 101 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
| 103 | # setup NAT for tap0 interface to have internet access in QEMU |
| 104 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32 |
| 105 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32 |
| 106 | echo 1 > /proc/sys/net/ipv4/ip_forward |
| 107 | echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp |
| 108 | $IPTABLES -P FORWARD ACCEPT |
| 109 | |
| 110 | echo $TAP |