runqemu-gen-tapdevs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  5. #
  6. # Copyright (C) 2010 Intel Corp.
  7. #
  8. # SPDX-License-Identifier: GPL-2.0-only
  9. #
  10. gid=`id -g`
  11. if [ -n "$SUDO_GID" ]; then
  12. gid=$SUDO_GID
  13. fi
  14. usage() {
  15. echo "Usage: sudo $0 <gid> <num>"
  16. echo "Where <gid> is the numeric group id the tap devices will be owned by"
  17. echo "<num> is the number of tap devices to create (0 to remove all)"
  18. echo "For example:"
  19. echo "$ bitbake qemu-helper-native"
  20. echo "$ sudo $0 $gid 4"
  21. echo ""
  22. exit 1
  23. }
  24. # Allow passing 4 arguments for backward compatibility with warning
  25. if [ $# -gt 4 ]; then
  26. echo "Error: Incorrect number of arguments"
  27. usage
  28. fi
  29. if [ $# -gt 3 ]; then
  30. echo "Warning: Ignoring the <native-sysroot-basedir> parameter. It is no longer needed."
  31. fi
  32. if [ $# -gt 2 ]; then
  33. echo "Warning: Ignoring the <uid> parameter. It is no longer needed."
  34. GID=$2
  35. COUNT=$3
  36. elif [ $# -eq 2 ]; then
  37. GID=$1
  38. COUNT=$2
  39. else
  40. echo "Error: Incorrect number of arguments"
  41. usage
  42. fi
  43. if [ -z "$OE_TAP_NAME" ]; then
  44. OE_TAP_NAME=tap
  45. fi
  46. # check if COUNT is a number and >= 0
  47. if ! [ $COUNT -ge 0 ]; then
  48. echo "Error: Incorrect count: $COUNT"
  49. exit 1
  50. fi
  51. if [ $EUID -ne 0 ]; then
  52. echo "Error: This script must be run with root privileges"
  53. exit
  54. fi
  55. SCRIPT_DIR=`dirname $0`
  56. RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
  57. if [ ! -x "$RUNQEMU_IFUP" ]; then
  58. echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
  59. exit 1
  60. fi
  61. if interfaces=`ip tuntap list` 2>/dev/null; then
  62. interfaces=`echo "$interfaces" |cut -f1 -d: |grep -E "^$OE_TAP_NAME.*"`
  63. else
  64. echo "Failed to call 'ip tuntap list'" >&2
  65. exit 1
  66. fi
  67. # Ensure we start with a clean slate
  68. for tap in $interfaces; do
  69. echo "Note: Destroying pre-existing tap interface $tap..."
  70. ip tuntap del $tap mode tap
  71. done
  72. rm -f /etc/runqemu-nosudo
  73. if [ $COUNT -eq 0 ]; then
  74. exit 0
  75. fi
  76. echo "Creating $COUNT tap devices for GID: $GID..."
  77. for ((index=0; index < $COUNT; index++)); do
  78. echo "Creating $OE_TAP_NAME$index"
  79. if ! ifup=`$RUNQEMU_IFUP $GID 2>&1`; then
  80. echo "Error bringing up interface: $ifup"
  81. exit 1
  82. fi
  83. done
  84. echo "Note: For systems running NetworkManager, it's recommended"
  85. echo "Note: that the tap devices be set as unmanaged in the"
  86. echo "Note: NetworkManager.conf file. Add the following lines to"
  87. echo "Note: /etc/NetworkManager/NetworkManager.conf"
  88. echo "[keyfile]"
  89. echo "unmanaged-devices=interface-name:$OE_TAP_NAME*"
  90. # The runqemu script will check for this file, and if it exists,
  91. # will use the existing bank of tap devices without creating
  92. # additional ones via sudo.
  93. touch /etc/runqemu-nosudo