Skip to content

Commit 04b6944

Browse files
authored
[sonic-cfggen] Remove machine.conf info and add get_system_mac support (#1397)
[sonic-cfggen] Remove machine.conf info and add get_system_mac support
1 parent f657df7 commit 04b6944

File tree

13 files changed

+59
-54
lines changed

13 files changed

+59
-54
lines changed

dockers/docker-orchagent/start.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sonic-cfggen -d -t /usr/share/sonic/templates/switch.json.j2 > /etc/swss/config.
66
sonic-cfggen -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
77
sonic-cfggen -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json
88

9-
export platform=`sonic-cfggen -v platform`
9+
export platform=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
1010

1111
rm -f /var/run/rsyslogd.pid
1212

dockers/docker-snmp-sv2/snmpd-config-updater

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def log_error(msg):
172172

173173
# Determine whether we are running on an Arista platform
174174
def is_platform_arista():
175-
proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"],
175+
proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"],
176176
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
177177

178178
(stdout, stderr) = proc.communicate()

files/build_templates/docker_image_ctl.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function postStartAction()
2020
}
2121

2222
# Obtain our platform as we will mount directories with these names in each docker
23-
PLATFORM=`sonic-cfggen -v platform`
23+
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
2424

2525
{%- if docker_container_name == "database" %}
2626
# Don't mount HWSKU in {{docker_container_name}} container.

files/build_templates/swss.service.j2

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ExecStartPre=/usr/bin/mst start
3030
ExecStartPre=/usr/bin/mlnx-fw-upgrade.sh
3131
ExecStartPre=/etc/init.d/sxdkernel start
3232
ExecStartPre=/sbin/modprobe i2c-dev
33-
ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management start"
33+
ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management start"
3434
{% elif sonic_asic_platform == 'cavium' %}
3535
ExecStartPre=/etc/init.d/xpnet.sh start
3636
{% endif %}
@@ -43,7 +43,7 @@ ExecStop=/usr/bin/{{docker_container_name}}.sh stop
4343
ExecStopPost=/usr/bin/syncd.sh stop
4444

4545
{% if sonic_asic_platform == 'mellanox' %}
46-
ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management stop"
46+
ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management stop"
4747
ExecStopPost=/etc/init.d/sxdkernel stop
4848
ExecStopPost=/usr/bin/mst stop
4949
{% elif sonic_asic_platform == 'cavium' %}

files/image_config/caclmgrd/caclmgrd-start.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Only start control plance ACL manager daemon if not an Arista platform.
44
# Arista devices will use their own service ACL manager daemon(s) instead.
5-
if [ "$(sonic-cfggen -v "platform" | grep -c "arista")" -gt 0 ]; then
5+
if [ "$(sonic-cfggen -H -v "DEVICE_METADATA.localhost.platform" | grep -c "arista")" -gt 0 ]; then
66
echo "Not starting caclmgrd - unsupported platform"
77
exit 0
88
fi

files/image_config/platform/rc.local

+7-20
Original file line numberDiff line numberDiff line change
@@ -79,43 +79,30 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
7979
firsttime_exit
8080
fi
8181

82-
# setup initial switch mac
83-
SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)
84-
SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}')
85-
86-
# Align last byte of MAC if necessary
87-
if [ "$SONIC_ASIC_TYPE" = "mellanox" ] || [ "$SONIC_ASIC_TYPE" = "centec" ]; then
88-
last_byte=$(python -c "print '$SYSTEM_MAC_ADDRESS'[-2:]")
89-
aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix
90-
SYSTEM_MAC_ADDRESS=$(python -c "print '$SYSTEM_MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC
91-
fi
92-
93-
if [ -f /etc/sonic/init_cfg.json ]; then
94-
sonic-cfggen -j /etc/sonic/init_cfg.json -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /tmp/init_cfg.json
95-
mv /tmp/init_cfg.json /etc/sonic/init_cfg.json
96-
else
97-
sonic-cfggen -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /etc/sonic/init_cfg.json
82+
if [ ! -f /etc/sonic/init_cfg.json ]; then
83+
# Generate an empty init_cfg.json
84+
echo '{}' > /etc/sonic/init_cfg.json
9885
fi
9986

10087
# Try to take old configuration saved during installation
10188
if test_config; then
10289
rm -f /host/old_config/sonic_version.yml
10390
mv -f /host/old_config/* /etc/sonic/
10491
if [ ! -f /etc/sonic/config_db.json ]; then
105-
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
92+
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
10693
fi
10794
elif [ -f /host/minigraph.xml ]; then
10895
mv /host/minigraph.xml /etc/sonic/
10996
# Combine information in minigraph and init_cfg.json to form initiate config DB dump file.
11097
# TODO: After moving all information from minigraph to DB, sample config DB dump should be provide
111-
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
98+
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
11299
else
113100
# Use default minigraph.xml
114101
cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/
115-
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
102+
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
116103
fi
117104

118-
HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v "DEVICE_METADATA['localhost']['hwsku']"`
105+
HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v DEVICE_METADATA.localhost.hwsku`
119106
if [ -f /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 ]; then
120107
# generate and merge buffers configuration into config file
121108
sonic-cfggen -m -t /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 > /tmp/buffers.json

files/image_config/ssh/sshd-config-updater

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def log_error(msg):
153153

154154
# Determine whether we are running on an Arista platform
155155
def is_platform_arista():
156-
proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"],
156+
proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"],
157157
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
158158

159159
(stdout, stderr) = proc.communicate()

files/image_config/updategraph/updategraph

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if [ "$src" = "dhcp" ]; then
3131
if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then
3232
echo "'N/A' found in DHCP response. Skipping graph update and generating an empty configuration."
3333
echo '{"DEVICE_METADATA":' > /tmp/device_meta.json
34-
sonic-cfggen -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json
34+
sonic-cfggen -H -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json
3535
echo '}' >> /tmp/device_meta.json
3636
if [ -f /etc/sonic/init_cfg.json ]; then
3737
sonic-cfggen -j /tmp/device_meta.json -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
@@ -88,9 +88,9 @@ done
8888

8989
echo "Regenerating config DB from minigraph..."
9090
if [ -f /etc/sonic/init_cfg.json ]; then
91-
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
91+
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
9292
else
93-
sonic-cfggen -m --print-data > /etc/sonic/config_db.json
93+
sonic-cfggen -H -m --print-data > /etc/sonic/config_db.json
9494
fi
9595

9696
# Mark as disabled after graph is successfully downloaded

platform/p4/docker-sonic-p4/start.sh

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ fi
1919

2020
mkdir -p /etc/swss/config.d/
2121

22-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
23-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json
24-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json
25-
26-
# export platform=`sonic-cfggen -v platform`
27-
2822
rm -f /var/run/rsyslogd.pid
2923

3024
echo "Start rsyslogd"
@@ -94,4 +88,4 @@ VLAN=`sonic-cfggen -d -v 'VLAN.keys() | join(" ") if VLAN'`
9488
if [ "$VLAN" != "" ]; then
9589
echo "Start arp_update"
9690
supervisorctl start arp_update
97-
fi
91+
fi

platform/vs/docker-sonic-vs/start.sh

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ fi
1818

1919
mkdir -p /etc/swss/config.d/
2020

21-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
22-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json
23-
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json
24-
25-
# export platform=`sonic-cfggen -v platform`
26-
2721
rm -f /var/run/rsyslogd.pid
2822

2923
supervisorctl start rsyslogd

src/sonic-config-engine/sonic-cfggen

+16-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ from minigraph import parse_xml
2828
from minigraph import parse_device_desc_xml
2929
from sonic_platform import get_machine_info
3030
from sonic_platform import get_platform_info
31+
from sonic_platform import get_system_mac
3132
from swsssdk import ConfigDBConnector
3233

3334
def is_ipv4(value):
@@ -135,6 +136,7 @@ def main():
135136
parser.add_argument("-j", "--json", help="json file that contains additional variables", action='append', default=[])
136137
parser.add_argument("-a", "--additional-data", help="addition data, in json string")
137138
parser.add_argument("-d", "--from-db", help="read config from configdb", action='store_true')
139+
parser.add_argument("-H", "--platform-info", help="read platform and hardware info", action='store_true')
138140
parser.add_argument("-s", "--redis-unix-sock-file", help="unix sock file for redis connection")
139141
group = parser.add_mutually_exclusive_group()
140142
group.add_argument("-t", "--template", help="render the data with the template file")
@@ -144,25 +146,22 @@ def main():
144146
group.add_argument("--print-data", help="print all data", action='store_true')
145147
args = parser.parse_args()
146148

147-
data = {}
148-
machine_info = get_machine_info()
149-
if machine_info != None:
150-
deep_update(data, machine_info)
151-
platform_info = get_platform_info(machine_info)
152-
if platform_info != None:
153-
data['platform'] = platform_info
149+
platform = get_platform_info(get_machine_info())
154150

155151
db_kwargs = {}
156152
if args.redis_unix_sock_file != None:
157153
db_kwargs['unix_socket_path'] = args.redis_unix_sock_file
158154

155+
data = {}
156+
157+
159158
if args.minigraph != None:
160159
minigraph = args.minigraph
161-
if data.has_key('platform'):
160+
if platform:
162161
if args.port_config != None:
163-
deep_update(data, parse_xml(minigraph, data['platform'], args.port_config))
162+
deep_update(data, parse_xml(minigraph, platform, args.port_config))
164163
else:
165-
deep_update(data, parse_xml(minigraph, data['platform']))
164+
deep_update(data, parse_xml(minigraph, platform))
166165
else:
167166
if args.port_config != None:
168167
deep_update(data, parse_xml(minigraph, port_config_file=args.port_config))
@@ -189,6 +188,13 @@ def main():
189188
configdb.connect()
190189
deep_update(data, FormatConverter.db_to_output(configdb.get_config()))
191190

191+
if args.platform_info:
192+
hardware_data = {'DEVICE_METADATA': {'localhost': {
193+
'platform': platform,
194+
'mac': get_system_mac()
195+
}}}
196+
deep_update(data, hardware_data)
197+
192198
if args.template != None:
193199
template_file = os.path.abspath(args.template)
194200
env = jinja2.Environment(loader=jinja2.FileSystemLoader('/'), trim_blocks=True)

src/sonic-config-engine/sonic_platform.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
22
import os
3+
import yaml
4+
import subprocess
35

46
DOCUMENTATION = '''
57
---
@@ -29,3 +31,25 @@ def get_platform_info(machine_info):
2931
elif machine_info.has_key('aboot_platform'):
3032
return machine_info['aboot_platform']
3133
return None
34+
35+
def get_sonic_version_info():
36+
if not os.path.isfile('/etc/sonic/version.yml'):
37+
return None
38+
data = {}
39+
with open('/etc/sonic/version.yml') as stream:
40+
data = yaml.load(stream)
41+
return data
42+
43+
def get_system_mac():
44+
proc = subprocess.Popen("ip link show eth0 | grep ether | awk '{print $2}'", shell=True, stdout=subprocess.PIPE)
45+
(mac, err) = proc.communicate()
46+
mac = mac.strip()
47+
48+
# Align last byte of MAC if necessary
49+
version_info = get_sonic_version_info()
50+
if version_info and (version_info['asic_type'] == 'mellanox' or version_info['asic_type'] == 'centec'):
51+
last_byte = mac[-2:]
52+
aligned_last_byte = format(int(int(last_byte, 16) & 0b11000000), '02x')
53+
mac = mac[:-2] + aligned_last_byte
54+
return mac
55+

src/sonic-utilities

0 commit comments

Comments
 (0)