blob: a00c79c442036a7cf1a4bd07ca1dc89b3c8c5182 [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
Patrick Williams520786c2023-06-25 16:20:36 -05004# runqemu script. This script needs to be run as root
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
6# Copyright (C) 2010 Intel Corp.
7#
Brad Bishopc342db32019-05-15 21:57:59 -04008# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
Brad Bishop977dc1a2019-02-06 16:01:43 -050011gid=`id -g`
Brad Bishop977dc1a2019-02-06 16:01:43 -050012if [ -n "$SUDO_GID" ]; then
13 gid=$SUDO_GID
14fi
15
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016usage() {
Patrick Williams520786c2023-06-25 16:20:36 -050017 echo "Usage: sudo $0 <gid> <num>"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 echo "Where <gid> is the numeric group id the tap devices will be owned by"
19 echo "<num> is the number of tap devices to create (0 to remove all)"
Brad Bishop977dc1a2019-02-06 16:01:43 -050020 echo "For example:"
21 echo "$ bitbake qemu-helper-native"
Patrick Williams520786c2023-06-25 16:20:36 -050022 echo "$ sudo $0 $gid 4"
Brad Bishop977dc1a2019-02-06 16:01:43 -050023 echo ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 exit 1
25}
26
Patrick Williams520786c2023-06-25 16:20:36 -050027# Allow passing 4 arguments for backward compatibility with warning
28if [ $# -gt 4 ]; then
29 echo "Error: Incorrect number of arguments"
30 usage
31fi
32if [ $# -gt 3 ]; then
33 echo "Warning: Ignoring the <native-sysroot-basedir> parameter. It is no longer needed."
34fi
35if [ $# -gt 2 ]; then
36 echo "Warning: Ignoring the <uid> parameter. It is no longer needed."
37 GID=$2
38 COUNT=$3
39elif [ $# -eq 2 ]; then
40 GID=$1
41 COUNT=$2
42else
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043 echo "Error: Incorrect number of arguments"
44 usage
45fi
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047
Patrick Williams520786c2023-06-25 16:20:36 -050048if [ -z "$OE_TAP_NAME" ]; then
49 OE_TAP_NAME=tap
50fi
51
52# check if COUNT is a number and >= 0
53if ! [ $COUNT -ge 0 ]; then
54 echo "Error: Incorrect count: $COUNT"
55 exit 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056fi
57
Brad Bishop977dc1a2019-02-06 16:01:43 -050058if [ $EUID -ne 0 ]; then
59 echo "Error: This script must be run with root privileges"
60 exit
61fi
62
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063SCRIPT_DIR=`dirname $0`
64RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
65if [ ! -x "$RUNQEMU_IFUP" ]; then
66 echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
67 exit 1
68fi
69
Patrick Williams520786c2023-06-25 16:20:36 -050070if interfaces=`ip tuntap list` 2>/dev/null; then
71 interfaces=`echo "$interfaces" |cut -f1 -d: |grep -E "^$OE_TAP_NAME.*"`
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072else
Patrick Williams520786c2023-06-25 16:20:36 -050073 echo "Failed to call 'ip tuntap list'" >&2
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 exit 1
75fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
Patrick Williams520786c2023-06-25 16:20:36 -050077# Ensure we start with a clean slate
78for tap in $interfaces; do
79 echo "Note: Destroying pre-existing tap interface $tap..."
80 ip tuntap del $tap mode tap
81done
82rm -f /etc/runqemu-nosudo
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083
Patrick Williams520786c2023-06-25 16:20:36 -050084if [ $COUNT -eq 0 ]; then
85 exit 0
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086fi
Patrick Williams520786c2023-06-25 16:20:36 -050087
88echo "Creating $COUNT tap devices for GID: $GID..."
89for ((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
95done
96
97echo "Note: For systems running NetworkManager, it's recommended"
98echo "Note: that the tap devices be set as unmanaged in the"
99echo "Note: NetworkManager.conf file. Add the following lines to"
100echo "Note: /etc/NetworkManager/NetworkManager.conf"
101echo "[keyfile]"
102echo "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.
107touch /etc/runqemu-nosudo