diff options
Diffstat (limited to 'scripts/runqemu-gen-tapdevs')
-rwxr-xr-x | scripts/runqemu-gen-tapdevs | 120 |
1 files changed, 59 insertions, 61 deletions
diff --git a/scripts/runqemu-gen-tapdevs b/scripts/runqemu-gen-tapdevs index a6ee4517da..a00c79c442 100755 --- a/scripts/runqemu-gen-tapdevs +++ b/scripts/runqemu-gen-tapdevs | |||
@@ -1,53 +1,58 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | # | 2 | # |
3 | # Create a "bank" of tap network devices that can be used by the | 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 | 4 | # runqemu script. This script needs to be run as root |
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 | # | 5 | # |
10 | # Copyright (C) 2010 Intel Corp. | 6 | # Copyright (C) 2010 Intel Corp. |
11 | # | 7 | # |
12 | # SPDX-License-Identifier: GPL-2.0-only | 8 | # SPDX-License-Identifier: GPL-2.0-only |
13 | # | 9 | # |
14 | 10 | ||
15 | uid=`id -u` | ||
16 | gid=`id -g` | 11 | gid=`id -g` |
17 | if [ -n "$SUDO_UID" ]; then | ||
18 | uid=$SUDO_UID | ||
19 | fi | ||
20 | if [ -n "$SUDO_GID" ]; then | 12 | if [ -n "$SUDO_GID" ]; then |
21 | gid=$SUDO_GID | 13 | gid=$SUDO_GID |
22 | fi | 14 | fi |
23 | 15 | ||
24 | usage() { | 16 | usage() { |
25 | echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>" | 17 | echo "Usage: sudo $0 <gid> <num>" |
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" | 18 | 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)" | 19 | 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 | echo "For example:" | 20 | echo "For example:" |
31 | echo "$ bitbake qemu-helper-native" | 21 | echo "$ bitbake qemu-helper-native" |
32 | echo "$ sudo $0 $uid $gid 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin" | 22 | echo "$ sudo $0 $gid 4" |
33 | echo "" | 23 | echo "" |
34 | exit 1 | 24 | exit 1 |
35 | } | 25 | } |
36 | 26 | ||
37 | if [ $# -ne 4 ]; then | 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 | ||
38 | echo "Error: Incorrect number of arguments" | 43 | echo "Error: Incorrect number of arguments" |
39 | usage | 44 | usage |
40 | fi | 45 | fi |
41 | 46 | ||
42 | TUID=$1 | ||
43 | GID=$2 | ||
44 | COUNT=$3 | ||
45 | STAGING_BINDIR_NATIVE=$4 | ||
46 | 47 | ||
47 | TUNCTL=$STAGING_BINDIR_NATIVE/tunctl | 48 | if [ -z "$OE_TAP_NAME" ]; then |
48 | if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then | 49 | OE_TAP_NAME=tap |
49 | echo "Error: $TUNCTL is not an executable" | 50 | fi |
50 | usage | 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 | ||
51 | fi | 56 | fi |
52 | 57 | ||
53 | if [ $EUID -ne 0 ]; then | 58 | if [ $EUID -ne 0 ]; then |
@@ -62,48 +67,41 @@ if [ ! -x "$RUNQEMU_IFUP" ]; then | |||
62 | exit 1 | 67 | exit 1 |
63 | fi | 68 | fi |
64 | 69 | ||
65 | IFCONFIG=`which ip 2> /dev/null` | 70 | if interfaces=`ip tuntap list` 2>/dev/null; then |
66 | if [ -z "$IFCONFIG" ]; then | 71 | interfaces=`echo "$interfaces" |cut -f1 -d: |grep -E "^$OE_TAP_NAME.*"` |
67 | # Is it ever anywhere else? | ||
68 | IFCONFIG=/sbin/ip | ||
69 | fi | ||
70 | if [ ! -x "$IFCONFIG" ]; then | ||
71 | echo "$IFCONFIG cannot be executed" | ||
72 | exit 1 | ||
73 | fi | ||
74 | |||
75 | if [ $COUNT -ge 0 ]; then | ||
76 | # Ensure we start with a clean slate | ||
77 | for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do | ||
78 | echo "Note: Destroying pre-existing tap interface $tap..." | ||
79 | $TUNCTL -d $tap | ||
80 | done | ||
81 | rm -f /etc/runqemu-nosudo | ||
82 | else | 72 | else |
83 | echo "Error: Incorrect count: $COUNT" | 73 | echo "Failed to call 'ip tuntap list'" >&2 |
84 | exit 1 | 74 | exit 1 |
85 | fi | 75 | fi |
86 | 76 | ||
87 | if [ $COUNT -gt 0 ]; then | 77 | # Ensure we start with a clean slate |
88 | echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..." | 78 | for tap in $interfaces; do |
89 | for ((index=0; index < $COUNT; index++)); do | 79 | echo "Note: Destroying pre-existing tap interface $tap..." |
90 | echo "Creating tap$index" | 80 | ip tuntap del $tap mode tap |
91 | ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1` | 81 | done |
92 | if [ $? -ne 0 ]; then | 82 | rm -f /etc/runqemu-nosudo |
93 | echo "Error running tunctl: $ifup" | ||
94 | exit 1 | ||
95 | fi | ||
96 | done | ||
97 | 83 | ||
98 | echo "Note: For systems running NetworkManager, it's recommended" | 84 | if [ $COUNT -eq 0 ]; then |
99 | echo "Note: that the tap devices be set as unmanaged in the" | 85 | exit 0 |
100 | echo "Note: NetworkManager.conf file. Add the following lines to" | ||
101 | echo "Note: /etc/NetworkManager/NetworkManager.conf" | ||
102 | echo "[keyfile]" | ||
103 | echo "unmanaged-devices=interface-name:tap*" | ||
104 | |||
105 | # The runqemu script will check for this file, and if it exists, | ||
106 | # will use the existing bank of tap devices without creating | ||
107 | # additional ones via sudo. | ||
108 | touch /etc/runqemu-nosudo | ||
109 | fi | 86 | fi |
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 | ||