runqemu-gen-tapdevs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. #
  3. # Create a "bank" of tap network devices that can be used by the
  4. # runqemu script. This script needs to be run as root, and will
  5. # use the tunctl binary from the build system sysroot. Note: many Linux
  6. # distros these days still use an older version of tunctl which does not
  7. # support the group permissions option, hence the need to use the build
  8. # system provided version.
  9. #
  10. # Copyright (C) 2010 Intel Corp.
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License version 2 as
  14. # published by the Free Software Foundation.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. uid=`id -u`
  25. gid=`id -g`
  26. if [ -n "$SUDO_UID" ]; then
  27. uid=$SUDO_UID
  28. fi
  29. if [ -n "$SUDO_GID" ]; then
  30. gid=$SUDO_GID
  31. fi
  32. usage() {
  33. echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>"
  34. echo "Where <uid> is the numeric user id the tap devices will be owned by"
  35. echo "Where <gid> is the numeric group id the tap devices will be owned by"
  36. echo "<num> is the number of tap devices to create (0 to remove all)"
  37. echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
  38. echo "For example:"
  39. echo "$ bitbake qemu-helper-native"
  40. echo "$ sudo $0 $uid $gid 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin"
  41. echo ""
  42. exit 1
  43. }
  44. if [ $# -ne 4 ]; then
  45. echo "Error: Incorrect number of arguments"
  46. usage
  47. fi
  48. TUID=$1
  49. GID=$2
  50. COUNT=$3
  51. STAGING_BINDIR_NATIVE=$4
  52. TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
  53. if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
  54. echo "Error: $TUNCTL is not an executable"
  55. usage
  56. fi
  57. if [ $EUID -ne 0 ]; then
  58. echo "Error: This script must be run with root privileges"
  59. exit
  60. fi
  61. SCRIPT_DIR=`dirname $0`
  62. RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
  63. if [ ! -x "$RUNQEMU_IFUP" ]; then
  64. echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
  65. exit 1
  66. fi
  67. IFCONFIG=`which ip 2> /dev/null`
  68. if [ -z "$IFCONFIG" ]; then
  69. # Is it ever anywhere else?
  70. IFCONFIG=/sbin/ip
  71. fi
  72. if [ ! -x "$IFCONFIG" ]; then
  73. echo "$IFCONFIG cannot be executed"
  74. exit 1
  75. fi
  76. if [ $COUNT -ge 0 ]; then
  77. # Ensure we start with a clean slate
  78. for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do
  79. echo "Note: Destroying pre-existing tap interface $tap..."
  80. $TUNCTL -d $tap
  81. done
  82. rm -f /etc/runqemu-nosudo
  83. else
  84. echo "Error: Incorrect count: $COUNT"
  85. exit 1
  86. fi
  87. if [ $COUNT -gt 0 ]; then
  88. echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
  89. for ((index=0; index < $COUNT; index++)); do
  90. echo "Creating tap$index"
  91. ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1`
  92. if [ $? -ne 0 ]; then
  93. echo "Error running tunctl: $ifup"
  94. exit 1
  95. fi
  96. done
  97. echo "Note: For systems running NetworkManager, it's recommended"
  98. echo "Note: that the tap devices be set as unmanaged in the"
  99. echo "Note: NetworkManager.conf file. Add the following lines to"
  100. echo "Note: /etc/NetworkManager/NetworkManager.conf"
  101. echo "[keyfile]"
  102. echo "unmanaged-devices=interface-name:tap*"
  103. # The runqemu script will check for this file, and if it exists,
  104. # will use the existing bank of tap devices without creating
  105. # additional ones via sudo.
  106. touch /etc/runqemu-nosudo
  107. fi