]>
Commit | Line | Data |
---|---|---|
8532405c SG |
1 | #!/bin/bash |
2 | # | |
3 | # Create a "bank" of tap network devices that can be used by the | |
7687d91f | 4 | # runqemu script. This script needs to be run as root, and will |
fe73ea8c RP |
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. | |
8532405c SG |
9 | # |
10 | # Copyright (C) 2010 Intel Corp. | |
11 | # | |
f8c9c511 | 12 | # SPDX-License-Identifier: GPL-2.0-only |
8532405c | 13 | # |
8532405c | 14 | |
446e7da7 RY |
15 | uid=`id -u` |
16 | gid=`id -g` | |
17 | if [ -n "$SUDO_UID" ]; then | |
18 | uid=$SUDO_UID | |
19 | fi | |
20 | if [ -n "$SUDO_GID" ]; then | |
21 | gid=$SUDO_GID | |
22 | fi | |
23 | ||
8532405c | 24 | usage() { |
a31b1434 | 25 | echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>" |
446e7da7 | 26 | echo "Where <uid> is the numeric user id the tap devices will be owned by" |
8532405c SG |
27 | 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)" | |
fe73ea8c | 29 | echo "<native-sysroot-basedir> is the path to the build system's native sysroot" |
446e7da7 RY |
30 | echo "For example:" |
31 | echo "$ bitbake qemu-helper-native" | |
32 | echo "$ sudo $0 $uid $gid 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin" | |
33 | echo "" | |
8532405c SG |
34 | exit 1 |
35 | } | |
36 | ||
a624ec02 | 37 | if [ $# -ne 4 ]; then |
8532405c SG |
38 | echo "Error: Incorrect number of arguments" |
39 | usage | |
40 | fi | |
41 | ||
a624ec02 EF |
42 | TUID=$1 |
43 | GID=$2 | |
44 | COUNT=$3 | |
a31b1434 | 45 | STAGING_BINDIR_NATIVE=$4 |
8532405c | 46 | |
a31b1434 | 47 | TUNCTL=$STAGING_BINDIR_NATIVE/tunctl |
8532405c SG |
48 | if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then |
49 | echo "Error: $TUNCTL is not an executable" | |
50 | usage | |
51 | fi | |
52 | ||
446e7da7 RY |
53 | if [ $EUID -ne 0 ]; then |
54 | echo "Error: This script must be run with root privileges" | |
55 | exit | |
56 | fi | |
57 | ||
8532405c | 58 | SCRIPT_DIR=`dirname $0` |
747fcc03 RP |
59 | RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup" |
60 | if [ ! -x "$RUNQEMU_IFUP" ]; then | |
7687d91f | 61 | echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR" |
8532405c SG |
62 | exit 1 |
63 | fi | |
64 | ||
c19e5d19 | 65 | IFCONFIG=`which ip 2> /dev/null` |
8532405c SG |
66 | if [ -z "$IFCONFIG" ]; then |
67 | # Is it ever anywhere else? | |
c19e5d19 | 68 | IFCONFIG=/sbin/ip |
8532405c | 69 | fi |
6b5706d1 RP |
70 | if [ ! -x "$IFCONFIG" ]; then |
71 | echo "$IFCONFIG cannot be executed" | |
72 | exit 1 | |
73 | fi | |
8532405c | 74 | |
3d4bf5b0 RY |
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 | |
83 | echo "Error: Incorrect count: $COUNT" | |
84 | exit 1 | |
85 | fi | |
8532405c | 86 | |
503a80de | 87 | if [ $COUNT -gt 0 ]; then |
3d4bf5b0 RY |
88 | echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..." |
89 | for ((index=0; index < $COUNT; index++)); do | |
90 | echo "Creating tap$index" | |
a31b1434 | 91 | ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1` |
3d4bf5b0 RY |
92 | if [ $? -ne 0 ]; then |
93 | echo "Error running tunctl: $ifup" | |
94 | exit 1 | |
95 | fi | |
96 | done | |
97 | ||
503a80de SW |
98 | echo "Note: For systems running NetworkManager, it's recommended" |
99 | echo "Note: that the tap devices be set as unmanaged in the" | |
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*" | |
503a80de | 104 | |
3d4bf5b0 RY |
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 |