runqemu-gen-tapdevs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. usage() {
  25. echo "Usage: sudo $0 <uid> <gid> <num> <native-sysroot-basedir>"
  26. echo "Where <uid> is the numeric user id the tap devices will be owned by"
  27. echo "Where <gid> is the numeric group id the tap devices will be owned by"
  28. echo "<num> is the number of tap devices to create (0 to remove all)"
  29. echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
  30. exit 1
  31. }
  32. if [ $EUID -ne 0 ]; then
  33. echo "Error: This script must be run with root privileges"
  34. exit
  35. fi
  36. if [ $# -ne 4 ]; then
  37. echo "Error: Incorrect number of arguments"
  38. usage
  39. fi
  40. TUID=$1
  41. GID=$2
  42. COUNT=$3
  43. SYSROOT=$4
  44. TUNCTL=$SYSROOT/usr/bin/tunctl
  45. if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
  46. echo "Error: $TUNCTL is not an executable"
  47. usage
  48. fi
  49. SCRIPT_DIR=`dirname $0`
  50. RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
  51. if [ ! -x "$RUNQEMU_IFUP" ]; then
  52. echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
  53. exit 1
  54. fi
  55. IFCONFIG=`which ifconfig 2> /dev/null`
  56. if [ -z "$IFCONFIG" ]; then
  57. # Is it ever anywhere else?
  58. IFCONFIG=/sbin/ifconfig
  59. fi
  60. if [ ! -x "$IFCONFIG" ]; then
  61. echo "$IFCONFIG cannot be executed"
  62. exit 1
  63. fi
  64. # Ensure we start with a clean slate
  65. for tap in `$IFCONFIG | grep ^tap | awk '{ print \$1 }' | sed s/://`; do
  66. echo "Note: Destroying pre-existing tap interface $tap..."
  67. $TUNCTL -d $tap
  68. done
  69. echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
  70. for ((index=0; index < $COUNT; index++)); do
  71. echo "Creating tap$index"
  72. ifup=`$RUNQEMU_IFUP $TUID $GID $SYSROOT 2>&1`
  73. if [ $? -ne 0 ]; then
  74. echo "Error running tunctl: $ifup"
  75. exit 1
  76. fi
  77. done
  78. # The runqemu script will check for this file, and if it exists,
  79. # will use the existing bank of tap devices without creating
  80. # additional ones via sudo.
  81. touch /etc/runqemu-nosudo