runqemu-ifdown 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. #
  3. # QEMU network configuration script to bring down tap devices. This
  4. # utility needs to be run as root, and will use the ip utility
  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. #
  15. # SPDX-License-Identifier: GPL-2.0-only
  16. #
  17. usage() {
  18. echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir>"
  19. }
  20. if [ $EUID -ne 0 ]; then
  21. echo "Error: This script (runqemu-ifdown) must be run with root privileges"
  22. exit 1
  23. fi
  24. if [ $# -ne 2 ]; then
  25. usage
  26. exit 1
  27. fi
  28. TAP=$1
  29. STAGING_BINDIR_NATIVE=$2
  30. if !ip tuntap del $TAP mode tap 2>/dev/null; then
  31. echo "Error: Unable to run up tuntap del"
  32. exit 1
  33. fi
  34. IPTOOL=`which ip 2> /dev/null`
  35. if [ "x$IPTOOL" = "x" ]; then
  36. # better than nothing...
  37. IPTOOL=/sbin/ip
  38. fi
  39. if [ -x "$IPTOOL" ]; then
  40. if `$IPTOOL link show $TAP > /dev/null 2>&1`; then
  41. $IPTOOL link del $TAP
  42. fi
  43. fi
  44. # cleanup the remaining iptables rules
  45. IPTABLES=`which iptables 2> /dev/null`
  46. if [ "x$IPTABLES" = "x" ]; then
  47. IPTABLES=/sbin/iptables
  48. fi
  49. if [ ! -x "$IPTABLES" ]; then
  50. echo "$IPTABLES cannot be executed"
  51. exit 1
  52. fi
  53. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  54. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  55. $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  56. $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
  57. true