blob: a6ee4517dadd62030696c6b6e0dbc657bf78545b [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#
Brad Bishopc342db32019-05-15 21:57:59 -040012# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Brad Bishop977dc1a2019-02-06 16:01:43 -050015uid=`id -u`
16gid=`id -g`
17if [ -n "$SUDO_UID" ]; then
18 uid=$SUDO_UID
19fi
20if [ -n "$SUDO_GID" ]; then
21 gid=$SUDO_GID
22fi
23
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024usage() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 echo "Usage: sudo $0 <uid> <gid> <num> <staging_bindir_native>"
Brad Bishop977dc1a2019-02-06 16:01:43 -050026 echo "Where <uid> is the numeric user id the tap devices will be owned by"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 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)"
29 echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
Brad Bishop977dc1a2019-02-06 16:01:43 -050030 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 ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 exit 1
35}
36
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037if [ $# -ne 4 ]; then
38 echo "Error: Incorrect number of arguments"
39 usage
40fi
41
42TUID=$1
43GID=$2
44COUNT=$3
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045STAGING_BINDIR_NATIVE=$4
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
49 echo "Error: $TUNCTL is not an executable"
50 usage
51fi
52
Brad Bishop977dc1a2019-02-06 16:01:43 -050053if [ $EUID -ne 0 ]; then
54 echo "Error: This script must be run with root privileges"
55 exit
56fi
57
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058SCRIPT_DIR=`dirname $0`
59RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
60if [ ! -x "$RUNQEMU_IFUP" ]; then
61 echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
62 exit 1
63fi
64
65IFCONFIG=`which ip 2> /dev/null`
66if [ -z "$IFCONFIG" ]; then
67 # Is it ever anywhere else?
68 IFCONFIG=/sbin/ip
69fi
70if [ ! -x "$IFCONFIG" ]; then
71 echo "$IFCONFIG cannot be executed"
72 exit 1
73fi
74
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075if [ $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
82else
83 echo "Error: Incorrect count: $COUNT"
84 exit 1
85fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087if [ $COUNT -gt 0 ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -060088 echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
89 for ((index=0; index < $COUNT; index++)); do
90 echo "Creating tap$index"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 ifup=`$RUNQEMU_IFUP $TUID $GID $STAGING_BINDIR_NATIVE 2>&1`
Patrick Williamsc0f7c042017-02-23 20:41:17 -060092 if [ $? -ne 0 ]; then
93 echo "Error running tunctl: $ifup"
94 exit 1
95 fi
96 done
97
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 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*"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 # 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
109fi