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