-
Notifications
You must be signed in to change notification settings - Fork 3
/
pci2nic.sh
executable file
·77 lines (61 loc) · 1.9 KB
/
pci2nic.sh
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
#!/usr/bin/env sh
## This script uses the following programs:
## * [
## * awk
## * basename
## * cut
## * cut
## * echo
## * grep
## * ip
## * readlink
## * sed
## * which
# Get the location of the ethtool command
ethtool=$(which "ethtool")
# Guess if the command is installed but it is not in the path
if ([ -z "${ethtool}" ] || ! [ -x "${ethtool}" ]) && [ -x /sbin/ethtool ]; then
ethtool=/sbin/ethtool
fi
# If not found or not executable, we can't continue
if [ -z "${ethtool}" ] || ! [ -x "${ethtool}" ]; then
echo '# The program "ethtool" could not be found or it is not executable'
exit 1
fi
# List of devices
ifaces=$(ip link | grep -E '^[[:digit:]]+: ' | awk '{print $2}' | sed 's/:\s*$//')
# Loop counter
i=1
v=1
# Iterate over all devices
for iface in ${ifaces}; do
# Check if the interface is the excluded one
[ "${iface}" = "${1}" ] && continue
# Get the driver module used by the interface and skip this iteration if it does not exist
drv=$(readlink /sys/class/net/${iface}/device/driver/module)
[ -z "${drv}" ] && continue
# Get the driver module name and act accordingly to the driver
drv=$(basename "${drv}")
# Virtio devices
if [ "${drv}" = 'virtio_net' ]; then
# Get the PCI bus device
addr=$(${ethtool} -i ${iface} | grep bus-info | cut -d' ' -f2)
# Output the exports
echo "export IXY_VIRTIO_ADDR_${v}='${addr}'"
echo "export IXY_VIRTIO_NAME_${v}='${iface}'"
# Loop counter increment
v=$((${v} + 1))
# Ixgbe devices
elif [ "${drv}" = 'ixgbe' ]; then
# Get the PCI bus device
addr=$(${ethtool} -i ${iface} | grep bus-info | cut -d' ' -f2)
# Output the exports
echo "export IXY_IXGBE_ADDR_${i}='${addr}'"
echo "export IXY_IXGBE_NAME_${i}='${iface}'"
# Loop counter increment
i=$((${i} + 1))
fi
done
# Export another variable that says how many variables were exported
echo "export IXY_IXGBE_COUNT=$((${i} - 1))"
echo "export IXY_VIRTIO_COUNT=$((${v} - 1))"