runqemu-ifdown 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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>"
  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 [ $# -gt 2 ] || [ $# -lt 1 ]; then
  25. usage
  26. exit 1
  27. fi
  28. # backward compatibility
  29. if [ $# -eq 2 ] ; then
  30. echo "Warning: native-sysroot-basedir parameter is ignored. It is no longer needed." >&2
  31. fi
  32. TAP=$1
  33. if ! ip tuntap del $TAP mode tap 2>/dev/null; then
  34. echo "Error: Unable to run up tuntap del"
  35. exit 1
  36. fi
  37. IPTOOL=`which ip 2> /dev/null`
  38. if [ "x$IPTOOL" = "x" ]; then
  39. # better than nothing...
  40. IPTOOL=/sbin/ip
  41. fi
  42. if [ -x "$IPTOOL" ]; then
  43. if `$IPTOOL link show $TAP > /dev/null 2>&1`; then
  44. $IPTOOL link del $TAP
  45. fi
  46. fi
  47. # cleanup the remaining iptables rules
  48. IPTABLES=`which iptables 2> /dev/null`
  49. if [ "x$IPTABLES" = "x" ]; then
  50. IPTABLES=/sbin/iptables
  51. fi
  52. if [ ! -x "$IPTABLES" ]; then
  53. echo "$IPTABLES cannot be executed"
  54. exit 1
  55. fi
  56. if [ -z "$OE_TAP_NAME" ]; then
  57. OE_TAP_NAME=tap
  58. fi
  59. n=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 1 ]
  60. dest=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 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
  63. true