runqemu-ifup 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. STAGING_BINDIR_NATIVE=$3
  49. TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
  50. if [ ! -x "$TUNCTL" ]; then
  51. echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
  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 ip 2> /dev/null`
  67. if [ "x$IFCONFIG" = "x" ]; then
  68. # better than nothing...
  69. IFCONFIG=/sbin/ip
  70. fi
  71. if [ ! -x "$IFCONFIG" ]; then
  72. echo "$IFCONFIG cannot be executed"
  73. exit 1
  74. fi
  75. IPTABLES=`which iptables 2> /dev/null`
  76. if [ "x$IPTABLES" = "x" ]; then
  77. IPTABLES=/sbin/iptables
  78. fi
  79. if [ ! -x "$IPTABLES" ]; then
  80. echo "$IPTABLES cannot be executed"
  81. exit 1
  82. fi
  83. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  84. $IFCONFIG addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP
  85. STATUS=$?
  86. if [ $STATUS -ne 0 ]; then
  87. echo "Failed to set up IP addressing on $TAP"
  88. exit 1
  89. fi
  90. $IFCONFIG link set dev $TAP up
  91. STATUS=$?
  92. if [ $STATUS -ne 0 ]; then
  93. echo "Failed to bring up $TAP"
  94. exit 1
  95. fi
  96. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  97. $IFCONFIG route add to 192.168.7.$dest dev $TAP
  98. STATUS=$?
  99. if [ $STATUS -ne 0 ]; then
  100. echo "Failed to add route to 192.168.7.$dest using $TAP"
  101. exit 1
  102. fi
  103. # setup NAT for tap0 interface to have internet access in QEMU
  104. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  105. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
  106. echo 1 > /proc/sys/net/ipv4/ip_forward
  107. echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
  108. $IPTABLES -P FORWARD ACCEPT
  109. echo $TAP