forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharista-net
More file actions
127 lines (112 loc) · 3.04 KB
/
Copy patharista-net
File metadata and controls
127 lines (112 loc) · 3.04 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
case $1 in
prereqs)
exit 0
;;
esac
set -e
# Extract kernel parameters
set -- $(cat /proc/cmdline)
items=""
for x in "$@"; do
case "$x" in
Aboot=*)
aboot_flag="${x#Aboot=}"
;;
net_*)
item="${x#net_}"
items="$items $item"
;;
platform=*)
platform_flag="${x#platform=}"
;;
esac
done
random() {
echo $(od -vAn -N1 -tu1 < /dev/urandom)
}
arista_net_devname() {
local pciaddr="$1"
local devname_prefix="$2"
for path in $(ls -d /sys/class/net/${devname_prefix}* 2>/dev/null); do
local devid="$(realpath "$path/device")"
if echo "$devid" | grep -q "$pciaddr"; then
echo "${path##*/}"
return
fi
done
}
arista_net_rename() {
local device_path="$1"
local new_name="$2"
local from_name="$3"
devname=$(arista_net_devname "$device_path" "$from_name")
[ -n "$devname" ] && ip link set "$devname" name "$new_name"
}
# Sets the MAC address to the value passed by Aboot through /proc/cmdline
tg3fixhwaddr()
{
local default_tg3_hwaddr="00:10:18:00:00:00"
local pciaddr="$1"
local hwaddr="$2"
devname=$(arista_net_devname "$pciaddr")
if [ -z "$devname" ]; then
return
fi
driver=$(basename $(readlink "/sys/class/net/$devname/device/driver"))
if [ "$driver" != "tg3" ]; then
return 0
fi
if [ "$hwaddr" = "$default_tg3_hwaddr" ]; then
hwaddr=$(cat /sys/class/net/$devname/address)
fi
if [ "$hwaddr" = "$default_tg3_hwaddr" ]; then
hwaddr=$(printf "%02x" "$(($(random) & 0xfe | 0x02))")
for i in 1 2 3 4 5; do
hwaddr=$(printf "$hwaddr:%02x" "$(($(random) & 0xfe | 0x02))")
done
fi
ip link set dev "$devname" addr "$hwaddr"
}
if [ -n "$aboot_flag" ]; then
for item in $items; do
key="${item%=*}"
value="${item#*=}"
hwaddr=$(eval echo \${hwaddr_${key}})
if [ -n "$hwaddr" ]; then
tg3fixhwaddr "$value" "$hwaddr"
fi
done
fi
# Iterate over all the net_maX items found in the cmdline two times.
# First time renaming the interfaces to maX.
# The second time renaming them to their final name ethX.
if [ -n "$aboot_flag" ]; then
if [ "$platform_flag" = 'rook' -o "$platform_flag" = 'lorikeet' ]; then
# Rename existing ethX interfaces to tmpX
for x in $(ls /sys/class/net/); do
case $x in
eth*)
value="${x#*eth}"
newname="tmp$value"
ip link set $x down
ip link set $x name "$newname"
;;
*)
esac
done
for item in $items; do
key="${item%=*}"
value="${item#*=}"
arista_net_rename "$value" "$key" tmp
done
for item in $items; do
key="${item%=*}"
value="${item#*=}"
index="${key#ma}"
index="$(( $index - 1 ))"
newKey="eth$index"
arista_net_rename "$value" "$newKey" ma
done
fi
fi