runqemu-ifup 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. #
  3. # QEMU network interface configuration script. This utility needs to
  4. # be run as root, and will use the ip utility
  5. #
  6. # If you find yourself calling this script a lot, you can add the
  7. # the following to your /etc/sudoers file to be able to run this
  8. # command without entering your password each time:
  9. #
  10. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup
  11. # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown
  12. #
  13. # If you'd like to create a bank of tap devices at once, you should use
  14. # the runqemu-gen-tapdevs script instead. If tap devices are set up using
  15. # that script, the runqemu script will never end up calling this
  16. # script.
  17. #
  18. # Copyright (c) 2006-2011 Linux Foundation
  19. #
  20. # SPDX-License-Identifier: GPL-2.0-only
  21. #
  22. usage() {
  23. echo "sudo $(basename $0) <gid>"
  24. }
  25. if [ $EUID -ne 0 ]; then
  26. echo "Error: This script (runqemu-ifup) must be run with root privileges"
  27. exit 1
  28. fi
  29. if [ $# -eq 2 ]; then
  30. echo "Warning: uid parameter is ignored. It is no longer needed." >&2
  31. GROUP="$2"
  32. elif [ $# -eq 1 ]; then
  33. GROUP="$1"
  34. else
  35. usage
  36. exit 1
  37. fi
  38. if [ -z "$OE_TAP_NAME" ]; then
  39. OE_TAP_NAME=tap
  40. fi
  41. if taps=$(ip tuntap list 2>/dev/null); then
  42. tap_no_last=$(echo "$taps" |cut -f 1 -d ":" |grep -E "^$OE_TAP_NAME.*" |sed "s/$OE_TAP_NAME//g" | sort -rn | head -n 1)
  43. if [ -z "$tap_no_last" ]; then
  44. tap_no=0
  45. else
  46. tap_no=$(("$tap_no_last" + 1))
  47. fi
  48. ip tuntap add "$OE_TAP_NAME$tap_no" mode tap group "$GROUP" && TAP=$OE_TAP_NAME$tap_no
  49. fi
  50. if [ -z "$TAP" ]; then
  51. echo "Error: Unable to find a tap device to use"
  52. exit 1
  53. fi
  54. IPTOOL=`which ip 2> /dev/null`
  55. if [ "x$IPTOOL" = "x" ]; then
  56. # better than nothing...
  57. IPTOOL=/sbin/ip
  58. fi
  59. if [ ! -x "$IPTOOL" ]; then
  60. echo "$IPTOOL cannot be executed"
  61. exit 1
  62. fi
  63. IPTABLES=`which iptables 2> /dev/null`
  64. if [ "x$IPTABLES" = "x" ]; then
  65. IPTABLES=/sbin/iptables
  66. fi
  67. if [ ! -x "$IPTABLES" ]; then
  68. echo "$IPTABLES cannot be executed"
  69. exit 1
  70. fi
  71. n=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 1 ]
  72. $IPTOOL addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP
  73. STATUS=$?
  74. if [ $STATUS -ne 0 ]; then
  75. echo "Failed to set up IP addressing on $TAP"
  76. exit 1
  77. fi
  78. $IPTOOL link set dev $TAP up
  79. STATUS=$?
  80. if [ $STATUS -ne 0 ]; then
  81. echo "Failed to bring up $TAP"
  82. exit 1
  83. fi
  84. dest=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 2 ]
  85. $IPTOOL route add to 192.168.7.$dest dev $TAP
  86. STATUS=$?
  87. if [ $STATUS -ne 0 ]; then
  88. echo "Failed to add route to 192.168.7.$dest using $TAP"
  89. exit 1
  90. fi
  91. # setup NAT for tap0 interface to have internet access in QEMU
  92. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
  93. $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
  94. echo 1 > /proc/sys/net/ipv4/ip_forward
  95. echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
  96. $IPTABLES -P FORWARD ACCEPT
  97. echo $TAP