summaryrefslogtreecommitdiffstats
path: root/Scripts/setup_tap.sh
blob: 1c71344baab3c3e4d7857843cae09eed50ba1214 (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
#!/bin/bash

if [[ $EUID > 0 ]]; then
  echo "Run this script as root"
  exit
fi

BRIDGE="br0"
TAP="tap0"

echo "Available network interfaces:"
interfaces=$(ip link | awk -F ': ' '{print $2}')
index=0

# Array to store interface names
declare -a interface_names

# Array to store interface types
declare -a interface_types

# Loop through each interface and display its type
for interface in $interfaces; do
  type=$(ip link show $interface | grep -o 'type .*' | awk '{print $2}')
  echo "$index: $interface - $type"
  
  # Store interface name and type in arrays
  interface_names[$index]=$interface
  interface_types[$index]=$type
  
  ((index++))
done

# Prompt the user to select an interface
read -p "Enter the number of the interface you want to use: " selection

# Validate the user's input
if [[ ! $selection =~ ^[0-9]+$ || $selection -lt 0 || $selection -ge $index ]]; then
  echo "Invalid selection. Exiting."
  exit
fi

INTERFACE=${interface_names[$selection]}
INTERFACE_TYPE=${interface_types[$selection]}

echo "Selected interface: $INTERFACE - $INTERFACE_TYPE"

echo "Adding bridge $BRIDGE"
ip link add name $BRIDGE type bridge

echo "Flushing interface $INTERFACE"
ip addr flush dev $INTERFACE

echo "Setting $BRIDGE as master of $INTERFACE"
ip link set $INTERFACE master $BRIDGE

echo "Adding tap $TAP"
ip tuntap add $TAP mode tap

echo "Setting $BRIDGE as master of $TAP"
ip link set $TAP master $BRIDGE

echo "Setting $INTERFACE, $BRIDGE, and $TAP up"
ip link set up dev $INTERFACE
ip link set up dev $TAP
ip link set up dev $BRIDGE

echo "Stopping NetworkManager"
systemctl stop NetworkManager

echo "Requesting IP for $BRIDGE"
dhclient -1 -v $BRIDGE

if [ $? -eq 0 ]; then
    echo "Requesting IP for $INTERFACE"
    dhclient $INTERFACE
    echo "Killing dhclient and starting NetworkManager"
    pkill -9 dhclient
    systemctl start NetworkManager
fi

# run qemu with the below arguments
#
# qemu-system-x86_64 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no -device virtio-net-pci,netdev=net0