runqemu-gen-tapdevs 2.5 KB

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