blob: 11de318c1a2d4cbd0f598f8f91e238e980fbf21e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
25usage() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026 echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 echo "Where <uid> is the numeric user id the tap devices will be owned by"
28 echo "Where <gid> is the numeric group id the tap devices will be owned by"
29 echo "<num> is the number of tap devices to create (0 to remove all)"
30 echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 echo "e.g. $ bitbake qemu-helper-native"
32 echo "$ sudo $0 1000 1000 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 exit 1
34}
35
36if [ $EUID -ne 0 ]; then
37 echo "Error: This script must be run with root privileges"
38 exit
39fi
40
41if [ $# -ne 4 ]; then
42 echo "Error: Incorrect number of arguments"
43 usage
44fi
45
46TUID=$1
47GID=$2
48COUNT=$3
Brad Bishop6e60e8b2018-02-01 10:27:11 -050049STAGING_BINDIR_NATIVE=$4
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
53 echo "Error: $TUNCTL is not an executable"
54 usage
55fi
56
57SCRIPT_DIR=`dirname $0`
58RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
59if [ ! -x "$RUNQEMU_IFUP" ]; then
60 echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
61 exit 1
62fi
63
64IFCONFIG=`which ip 2> /dev/null`
65if [ -z "$IFCONFIG" ]; then
66 # Is it ever anywhere else?
67 IFCONFIG=/sbin/ip
68fi
69if [ ! -x "$IFCONFIG" ]; then
70 echo "$IFCONFIG cannot be executed"
71 exit 1
72fi
73
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074if [ $COUNT -ge 0 ]; then
75 # Ensure we start with a clean slate
76 for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do
77 echo "Note: Destroying pre-existing tap interface $tap..."
78 $TUNCTL -d $tap
79 done
80 rm -f /etc/runqemu-nosudo
81else
82 echo "Error: Incorrect count: $COUNT"
83 exit 1
84fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050086if [ $COUNT -gt 0 ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -060087 echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
88 for ((index=0; index < $COUNT; index++)); do
89 echo "Creating tap$index"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1`
Patrick Williamsc0f7c042017-02-23 20:41:17 -060091 if [ $? -ne 0 ]; then
92 echo "Error running tunctl: $ifup"
93 exit 1
94 fi
95 done
96
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 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:tap*"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 # 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
108fi