| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # QEMU network configuration script to bring down tap devices. This | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 4 | # utility needs to 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 | # Copyright (c) 2006-2011 Linux Foundation | 
|  | 14 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 15 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 |  | 
|  | 18 | usage() { | 
|  | 19 | echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir>" | 
|  | 20 | } | 
|  | 21 |  | 
|  | 22 | if [ $EUID -ne 0 ]; then | 
|  | 23 | echo "Error: This script (runqemu-ifdown) must be run with root privileges" | 
|  | 24 | exit 1 | 
|  | 25 | fi | 
|  | 26 |  | 
|  | 27 | if [ $# -ne 2 ]; then | 
|  | 28 | usage | 
|  | 29 | exit 1 | 
|  | 30 | fi | 
|  | 31 |  | 
|  | 32 | TAP=$1 | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | STAGING_BINDIR_NATIVE=$2 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 |  | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 35 | if !ip tuntap del $TAP mode tap 2>/dev/null; then | 
|  | 36 | echo "Error: Unable to run up tuntap del" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | exit 1 | 
|  | 38 | fi | 
|  | 39 |  | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 40 | IPTOOL=`which ip 2> /dev/null` | 
|  | 41 | if [ "x$IPTOOL" = "x" ]; then | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 42 | # better than nothing... | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 43 | IPTOOL=/sbin/ip | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 44 | fi | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 45 | if [ -x "$IPTOOL" ]; then | 
|  | 46 | if `$IPTOOL link show $TAP > /dev/null 2>&1`; then | 
|  | 47 | $IPTOOL link del $TAP | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 48 | fi | 
|  | 49 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | # cleanup the remaining iptables rules | 
|  | 51 | IPTABLES=`which iptables 2> /dev/null` | 
|  | 52 | if [ "x$IPTABLES" = "x" ]; then | 
|  | 53 | IPTABLES=/sbin/iptables | 
|  | 54 | fi | 
|  | 55 | if [ ! -x "$IPTABLES" ]; then | 
|  | 56 | echo "$IPTABLES cannot be executed" | 
|  | 57 | exit 1 | 
|  | 58 | fi | 
|  | 59 | n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] | 
|  | 60 | dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ] | 
|  | 61 | $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32 | 
|  | 62 | $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32 | 
| Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 63 | true |