runqemu-ifup 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) <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 2 ]; then
  43. usage
  44. exit 1
  45. fi
  46. GROUP="-g $1"
  47. NATIVE_SYSROOT_DIR=$2
  48. TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl
  49. if [ ! -x "$TUNCTL" ]; then
  50. echo "Error: Unable to find tunctl binary in '$NATIVE_SYSROOT_DIR/usr/bin'"
  51. exit 1
  52. fi
  53. TAP=`$TUNCTL -b $GROUP 2>&1`
  54. STATUS=$?
  55. if [ $STATUS -ne 0 ]; then
  56. echo "tunctl failed:"
  57. echo $TAP
  58. exit 1
  59. fi
  60. IFCONFIG=`which ifconfig`
  61. if [ "x$IFCONFIG" = "x" ]; then
  62. # better than nothing...
  63. IFCONFIG=/sbin/ifconfig
  64. fi
  65. ROUTE=`which route`
  66. if [ "x$ROUTE" = "x" ]; then
  67. # better than nothing...
  68. ROUTE=/sbin/route
  69. fi
  70. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  71. $IFCONFIG $TAP 192.168.7.$n
  72. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  73. $ROUTE add -host 192.168.7.$dest $TAP
  74. # setup NAT for tap0 interface to have internet access in QEMU
  75. IPTABLES=`which iptables`
  76. if [ "x$IPTABLES" = "x" ]; then
  77. IPTABLES=/sbin/iptables
  78. fi
  79. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.0/24
  80. echo 1 > /proc/sys/net/ipv4/ip_forward
  81. $IPTABLES -P FORWARD ACCEPT
  82. echo $TAP