runqemu-ifup 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. #
  3. # QEMU network interface configuration script. This utility needs to
  4. # be run as root, and will use the tunctl binary from a native sysroot.
  5. # Note: many Linux distros these days still use an older version of
  6. # tunctl which does not support the group permissions option, hence
  7. # the need to use build system's version.
  8. #
  9. # If you find yourself calling this script a lot, you can add the
  10. # the following to your /etc/sudoers file to be able to run this
  11. # command without entering your password each time:
  12. #
  13. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup
  14. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown
  15. #
  16. # If you'd like to create a bank of tap devices at once, you should use
  17. # the runqemu-gen-tapdevs script instead. If tap devices are set up using
  18. # that script, the runqemu script will never end up calling this
  19. # script.
  20. #
  21. # Copyright (c) 2006-2011 Linux Foundation
  22. #
  23. # This program is free software; you can redistribute it and/or modify
  24. # it under the terms of the GNU General Public License version 2 as
  25. # published by the Free Software Foundation.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. #
  32. # You should have received a copy of the GNU General Public License along
  33. # with this program; if not, write to the Free Software Foundation, Inc.,
  34. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  35. usage() {
  36. echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir>"
  37. }
  38. if [ $EUID -ne 0 ]; then
  39. echo "Error: This script (runqemu-ifup) must be run with root privileges"
  40. exit 1
  41. fi
  42. if [ $# -ne 3 ]; then
  43. usage
  44. exit 1
  45. fi
  46. USERID="-u $1"
  47. GROUP="-g $2"
  48. NATIVE_SYSROOT_DIR=$3
  49. TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl
  50. if [ ! -x "$TUNCTL" ]; then
  51. echo "Error: Unable to find tunctl binary in '$NATIVE_SYSROOT_DIR/usr/bin'"
  52. exit 1
  53. fi
  54. TAP=`$TUNCTL -b $GROUP 2>&1`
  55. STATUS=$?
  56. if [ $STATUS -ne 0 ]; then
  57. # If tunctl -g fails, try using tunctl -u, for older host kernels
  58. # which do not support the TUNSETGROUP ioctl
  59. TAP=`$TUNCTL -b $USERID 2>&1`
  60. STATUS=$?
  61. if [ $STATUS -ne 0 ]; then
  62. echo "tunctl failed:"
  63. exit 1
  64. fi
  65. fi
  66. IFCONFIG=`which ifconfig 2> /dev/null`
  67. if [ "x$IFCONFIG" = "x" ]; then
  68. # better than nothing...
  69. IFCONFIG=/sbin/ifconfig
  70. fi
  71. if [ ! -x "$IFCONFIG" ]; then
  72. echo "$IFCONFIG cannot be executed"
  73. exit 1
  74. fi
  75. ROUTE=`which route`
  76. if [ "x$ROUTE" = "x" ]; then
  77. # better than nothing...
  78. ROUTE=/sbin/route
  79. fi
  80. if [ ! -x "$ROUTE" ]; then
  81. echo "$ROUTE cannot be executed"
  82. exit 1
  83. fi
  84. IPTABLES=`which iptables 2> /dev/null`
  85. if [ "x$IPTABLES" = "x" ]; then
  86. IPTABLES=/sbin/iptables
  87. fi
  88. if [ ! -x "$IPTABLES" ]; then
  89. echo "$IPTABLES cannot be executed"
  90. exit 1
  91. fi
  92. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  93. $IFCONFIG $TAP 192.168.7.$n netmask 255.255.255.255
  94. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  95. $ROUTE add -host 192.168.7.$dest $TAP
  96. # setup NAT for tap0 interface to have internet access in QEMU
  97. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  98. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
  99. echo 1 > /proc/sys/net/ipv4/ip_forward
  100. echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
  101. $IPTABLES -P FORWARD ACCEPT
  102. echo $TAP