Skip to content

Commit 37dc7bd

Browse files
authored
[interfaces]: Set hwaddr of VLAN interfaces to system MAC upon creation (#1042)
1 parent 463c5e4 commit 37dc7bd

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#!/bin/bash
22

3-
sonic-cfggen -d -t /usr/share/sonic/templates/interfaces.j2 >/etc/network/interfaces
3+
SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)
4+
SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}')
5+
6+
# Align last byte of MAC if necessary
7+
if [ "$SONIC_ASIC_TYPE" == "mellanox" -o "$SONIC_ASIC_TYPE" == "centec" ]; then
8+
last_byte=$(python -c "print '$SYSTEM_MAC_ADDRESS'[-2:]")
9+
aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix
10+
SYSTEM_MAC_ADDRESS=$(python -c "print '$SYSTEM_MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC
11+
fi
12+
13+
sonic-cfggen -d -a '{"hwaddr":"'$SYSTEM_MAC_ADDRESS'"}' -t /usr/share/sonic/templates/interfaces.j2 > /etc/network/interfaces
14+
415
[ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid
5-
service networking restart
16+
17+
systemctl restart networking
18+
619
ifdown lo && ifup lo

files/image_config/interfaces/interfaces.j2

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ iface {{ member }} inet manual
9090
auto {{ name }}
9191
iface {{ name }} {{ 'inet' if prefix | ipv4 else 'inet6' }} static
9292
bridge_ports none
93+
hwaddress ether {{ hwaddr }}
9394
address {{ prefix | ip }}
9495
netmask {{ prefix | netmask if prefix | ipv4 else prefix | prefixlen }}
9596
{% endfor %}

src/sonic-config-engine/tests/sample_output/interfaces

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ iface fortyGigE0/124 inet manual
214214
auto Vlan1000
215215
iface Vlan1000 inet static
216216
bridge_ports none
217+
hwaddress ether e4:1d:2d:a5:f3:ad
217218
address 192.168.0.1
218219
netmask 255.255.255.224
219220
#

src/sonic-config-engine/tests/test_j2files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def run_script(self, argument):
2020

2121
def test_interfaces(self):
2222
interfaces_template = os.path.join(self.test_dir, '..', '..', '..', 'files', 'image_config', 'interfaces', 'interfaces.j2')
23-
argument = '-m ' + self.t0_minigraph + ' -t ' + interfaces_template + ' > ' + self.output_file
23+
argument = '-m ' + self.t0_minigraph + ' -a \'{\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + interfaces_template + ' > ' + self.output_file
2424
self.run_script(argument)
2525
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', 'interfaces'), self.output_file))
2626

0 commit comments

Comments
 (0)