runqemu-ifdown 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 tunctl binary
  5. # from the native sysroot.
  6. #
  7. # If you find yourself calling this script a lot, you can add the
  8. # the following to your /etc/sudoers file to be able to run this
  9. # command without entering your password each time:
  10. #
  11. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup
  12. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown
  13. #
  14. # Copyright (c) 2006-2011 Linux Foundation
  15. #
  16. # SPDX-License-Identifier: GPL-2.0-only
  17. #
  18. usage() {
  19. echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir>"
  20. }
  21. if [ $EUID -ne 0 ]; then
  22. echo "Error: This script (runqemu-ifdown) must be run with root privileges"
  23. exit 1
  24. fi
  25. if [ $# -ne 2 ]; then
  26. usage
  27. exit 1
  28. fi
  29. TAP=$1
  30. STAGING_BINDIR_NATIVE=$2
  31. TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
  32. if [ ! -e "$TUNCTL" ]; then
  33. echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
  34. exit 1
  35. fi
  36. $TUNCTL -d $TAP
  37. IFCONFIG=`which ip 2> /dev/null`
  38. if [ "x$IFCONFIG" = "x" ]; then
  39. # better than nothing...
  40. IFCONFIG=/sbin/ip
  41. fi
  42. if [ -x "$IFCONFIG" ]; then
  43. if `$IFCONFIG link show $TAP > /dev/null 2>&1`; then
  44. $IFCONFIG 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. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  57. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  58. $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  59. $IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32