runqemu-ifup 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # SPDX-License-Identifier: GPL-2.0-only
  24. #
  25. usage() {
  26. echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir>"
  27. }
  28. if [ $EUID -ne 0 ]; then
  29. echo "Error: This script (runqemu-ifup) must be run with root privileges"
  30. exit 1
  31. fi
  32. if [ $# -ne 3 ]; then
  33. usage
  34. exit 1
  35. fi
  36. USERID="-u $1"
  37. GROUP="-g $2"
  38. STAGING_BINDIR_NATIVE=$3
  39. TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
  40. if [ ! -x "$TUNCTL" ]; then
  41. echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
  42. exit 1
  43. fi
  44. TAP=`$TUNCTL -b $GROUP 2>&1`
  45. STATUS=$?
  46. if [ $STATUS -ne 0 ]; then
  47. # If tunctl -g fails, try using tunctl -u, for older host kernels
  48. # which do not support the TUNSETGROUP ioctl
  49. TAP=`$TUNCTL -b $USERID 2>&1`
  50. STATUS=$?
  51. if [ $STATUS -ne 0 ]; then
  52. echo "tunctl failed:"
  53. exit 1
  54. fi
  55. fi
  56. IFCONFIG=`which ip 2> /dev/null`
  57. if [ "x$IFCONFIG" = "x" ]; then
  58. # better than nothing...
  59. IFCONFIG=/sbin/ip
  60. fi
  61. if [ ! -x "$IFCONFIG" ]; then
  62. echo "$IFCONFIG cannot be executed"
  63. exit 1
  64. fi
  65. IPTABLES=`which iptables 2> /dev/null`
  66. if [ "x$IPTABLES" = "x" ]; then
  67. IPTABLES=/sbin/iptables
  68. fi
  69. if [ ! -x "$IPTABLES" ]; then
  70. echo "$IPTABLES cannot be executed"
  71. exit 1
  72. fi
  73. n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  74. $IFCONFIG addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP
  75. STATUS=$?
  76. if [ $STATUS -ne 0 ]; then
  77. echo "Failed to set up IP addressing on $TAP"
  78. exit 1
  79. fi
  80. $IFCONFIG link set dev $TAP up
  81. STATUS=$?
  82. if [ $STATUS -ne 0 ]; then
  83. echo "Failed to bring up $TAP"
  84. exit 1
  85. fi
  86. dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  87. $IFCONFIG route add to 192.168.7.$dest dev $TAP
  88. STATUS=$?
  89. if [ $STATUS -ne 0 ]; then
  90. echo "Failed to add route to 192.168.7.$dest using $TAP"
  91. exit 1
  92. fi
  93. # setup NAT for tap0 interface to have internet access in QEMU
  94. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  95. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
  96. echo 1 > /proc/sys/net/ipv4/ip_forward
  97. echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
  98. $IPTABLES -P FORWARD ACCEPT
  99. echo $TAP