summaryrefslogtreecommitdiffstats
path: root/scripts/runqemu-gen-tapdevs
blob: a00c79c442036a7cf1a4bd07ca1dc89b3c8c5182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# Create a "bank" of tap network devices that can be used by the
# runqemu script. This script needs to be run as root
#
# Copyright (C) 2010 Intel Corp.
#
# SPDX-License-Identifier: GPL-2.0-only
#

gid=`id -g`
if [ -n "$SUDO_GID" ]; then
    gid=$SUDO_GID
fi

usage() {
	echo "Usage: sudo $0 <gid> <num>"
	echo "Where <gid> is the numeric group id the tap devices will be owned by"
	echo "<num> is the number of tap devices to create (0 to remove all)"
	echo "For example:"
	echo "$ bitbake qemu-helper-native"
	echo "$ sudo $0 $gid 4"
	echo ""
	exit 1
}

# Allow passing 4 arguments for backward compatibility with warning
if [ $# -gt 4 ]; then
	echo "Error: Incorrect number of arguments"
	usage
fi
if [ $# -gt 3 ]; then
	echo "Warning: Ignoring the <native-sysroot-basedir> parameter. It is no longer needed."
fi
if [ $# -gt 2 ]; then
	echo "Warning: Ignoring the <uid> parameter. It is no longer needed."
	GID=$2
	COUNT=$3
elif [ $# -eq 2 ]; then
	GID=$1
	COUNT=$2
else
	echo "Error: Incorrect number of arguments"
	usage
fi


if [ -z "$OE_TAP_NAME" ]; then
	OE_TAP_NAME=tap
fi

# check if COUNT is a number and >= 0
if ! [ $COUNT -ge 0 ]; then
	echo "Error: Incorrect count: $COUNT"
	exit 1
fi

if [ $EUID -ne 0 ]; then
	echo "Error: This script must be run with root privileges"
	exit
fi

SCRIPT_DIR=`dirname $0`
RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
if [ ! -x "$RUNQEMU_IFUP" ]; then
	echo "Error: Unable to find the runqemu-ifup script in $SCRIPT_DIR"
	exit 1
fi

if interfaces=`ip tuntap list` 2>/dev/null; then
	interfaces=`echo "$interfaces" |cut -f1 -d: |grep -E "^$OE_TAP_NAME.*"`
else
	echo "Failed to call 'ip tuntap list'" >&2
	exit 1
fi

# Ensure we start with a clean slate
for tap in $interfaces; do
	echo "Note: Destroying pre-existing tap interface $tap..."
	ip tuntap del $tap mode tap
done
rm -f /etc/runqemu-nosudo

if [ $COUNT -eq 0 ]; then
	exit 0
fi

echo "Creating $COUNT tap devices for GID: $GID..."
for ((index=0; index < $COUNT; index++)); do
	echo "Creating $OE_TAP_NAME$index"
	if ! ifup=`$RUNQEMU_IFUP $GID 2>&1`; then
		echo "Error bringing up interface: $ifup"
		exit 1
	fi
done

echo "Note: For systems running NetworkManager, it's recommended"
echo "Note: that the tap devices be set as unmanaged in the"
echo "Note: NetworkManager.conf file. Add the following lines to"
echo "Note: /etc/NetworkManager/NetworkManager.conf"
echo "[keyfile]"
echo "unmanaged-devices=interface-name:$OE_TAP_NAME*"

# The runqemu script will check for this file, and if it exists,
# will use the existing bank of tap devices without creating
# additional ones via sudo.
touch /etc/runqemu-nosudo