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 |
| 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 | # This program is free software; you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License version 2 as |
| 14 | # published by the Free Software Foundation. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License along |
| 22 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 25 | uid=`id -u` |
| 26 | gid=`id -g` |
| 27 | if [ -n "$SUDO_UID" ]; then |
| 28 | uid=$SUDO_UID |
| 29 | fi |
| 30 | if [ -n "$SUDO_GID" ]; then |
| 31 | gid=$SUDO_GID |
| 32 | fi |
| 33 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | usage() { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 35 | echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>" |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 36 | echo "Where <uid> is the numeric user id the tap devices will be owned by" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | echo "Where <gid> is the numeric group id the tap devices will be owned by" |
| 38 | echo "<num> is the number of tap devices to create (0 to remove all)" |
| 39 | echo "<native-sysroot-basedir> is the path to the build system's native sysroot" |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 40 | echo "For example:" |
| 41 | echo "$ bitbake qemu-helper-native" |
| 42 | echo "$ sudo $0 $uid $gid 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin" |
| 43 | echo "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | exit 1 |
| 45 | } |
| 46 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | if [ $# -ne 4 ]; then |
| 48 | echo "Error: Incorrect number of arguments" |
| 49 | usage |
| 50 | fi |
| 51 | |
| 52 | TUID=$1 |
| 53 | GID=$2 |
| 54 | COUNT=$3 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | STAGING_BINDIR_NATIVE=$4 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 57 | TUNCTL=$STAGING_BINDIR_NATIVE/tunctl |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then |
| 59 | echo "Error: $TUNCTL is not an executable" |
| 60 | usage |
| 61 | fi |
| 62 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 63 | if [ $EUID -ne 0 ]; then |
| 64 | echo "Error: This script must be run with root privileges" |
| 65 | exit |
| 66 | fi |
| 67 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | SCRIPT_DIR=`dirname $0` |
| 69 | RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup" |
| 70 | if [ ! -x "$RUNQEMU_IFUP" ]; then |
| 71 | echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR" |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | IFCONFIG=`which ip 2> /dev/null` |
| 76 | if [ -z "$IFCONFIG" ]; then |
| 77 | # Is it ever anywhere else? |
| 78 | IFCONFIG=/sbin/ip |
| 79 | fi |
| 80 | if [ ! -x "$IFCONFIG" ]; then |
| 81 | echo "$IFCONFIG cannot be executed" |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 85 | if [ $COUNT -ge 0 ]; then |
| 86 | # Ensure we start with a clean slate |
| 87 | for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do |
| 88 | echo "Note: Destroying pre-existing tap interface $tap..." |
| 89 | $TUNCTL -d $tap |
| 90 | done |
| 91 | rm -f /etc/runqemu-nosudo |
| 92 | else |
| 93 | echo "Error: Incorrect count: $COUNT" |
| 94 | exit 1 |
| 95 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 | if [ $COUNT -gt 0 ]; then |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..." |
| 99 | for ((index=0; index < $COUNT; index++)); do |
| 100 | echo "Creating tap$index" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1` |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | if [ $? -ne 0 ]; then |
| 103 | echo "Error running tunctl: $ifup" |
| 104 | exit 1 |
| 105 | fi |
| 106 | done |
| 107 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | echo "Note: For systems running NetworkManager, it's recommended" |
| 109 | echo "Note: that the tap devices be set as unmanaged in the" |
| 110 | echo "Note: NetworkManager.conf file. Add the following lines to" |
| 111 | echo "Note: /etc/NetworkManager/NetworkManager.conf" |
| 112 | echo "[keyfile]" |
| 113 | echo "unmanaged-devices=interface-name:tap*" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 114 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | # The runqemu script will check for this file, and if it exists, |
| 116 | # will use the existing bank of tap devices without creating |
| 117 | # additional ones via sudo. |
| 118 | touch /etc/runqemu-nosudo |
| 119 | fi |