runqemu-gen-tapdevs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # SPDX-License-Identifier: GPL-2.0-only
  13. #
  14. uid=`id -u`
  15. gid=`id -g`
  16. if [ -n "$SUDO_UID" ]; then
  17. uid=$SUDO_UID
  18. fi
  19. if [ -n "$SUDO_GID" ]; then
  20. gid=$SUDO_GID
  21. fi
  22. usage() {
  23. echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>"
  24. echo "Where <uid> is the numeric user id the tap devices will be owned by"
  25. echo "Where <gid> is the numeric group id the tap devices will be owned by"
  26. echo "<num> is the number of tap devices to create (0 to remove all)"
  27. echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
  28. echo "For example:"
  29. echo "$ bitbake qemu-helper-native"
  30. echo "$ sudo $0 $uid $gid 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin"
  31. echo ""
  32. exit 1
  33. }
  34. if [ $# -ne 4 ]; then
  35. echo "Error: Incorrect number of arguments"
  36. usage
  37. fi
  38. TUID=$1
  39. GID=$2
  40. COUNT=$3
  41. STAGING_BINDIR_NATIVE=$4
  42. TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
  43. if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
  44. echo "Error: $TUNCTL is not an executable"
  45. usage
  46. fi
  47. if [ $EUID -ne 0 ]; then
  48. echo "Error: This script must be run with root privileges"
  49. exit
  50. fi
  51. SCRIPT_DIR=`dirname $0`
  52. RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
  53. if [ ! -x "$RUNQEMU_IFUP" ]; then
  54. echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
  55. exit 1
  56. fi
  57. IFCONFIG=`which ip 2> /dev/null`
  58. if [ -z "$IFCONFIG" ]; then
  59. # Is it ever anywhere else?
  60. IFCONFIG=/sbin/ip
  61. fi
  62. if [ ! -x "$IFCONFIG" ]; then
  63. echo "$IFCONFIG cannot be executed"
  64. exit 1
  65. fi
  66. if [ $COUNT -ge 0 ]; then
  67. # Ensure we start with a clean slate
  68. for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do
  69. echo "Note: Destroying pre-existing tap interface $tap..."
  70. $TUNCTL -d $tap
  71. done
  72. rm -f /etc/runqemu-nosudo
  73. else
  74. echo "Error: Incorrect count: $COUNT"
  75. exit 1
  76. fi
  77. if [ $COUNT -gt 0 ]; then
  78. echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
  79. for ((index=0; index < $COUNT; index++)); do
  80. echo "Creating tap$index"
  81. ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1`
  82. if [ $? -ne 0 ]; then
  83. echo "Error running tunctl: $ifup"
  84. exit 1
  85. fi
  86. done
  87. echo "Note: For systems running NetworkManager, it's recommended"
  88. echo "Note: that the tap devices be set as unmanaged in the"
  89. echo "Note: NetworkManager.conf file. Add the following lines to"
  90. echo "Note: /etc/NetworkManager/NetworkManager.conf"
  91. echo "[keyfile]"
  92. echo "unmanaged-devices=interface-name:tap*"
  93. # The runqemu script will check for this file, and if it exists,
  94. # will use the existing bank of tap devices without creating
  95. # additional ones via sudo.
  96. touch /etc/runqemu-nosudo
  97. fi