diff --git a/dockers/docker-orchagent/start.sh b/dockers/docker-orchagent/start.sh index 80ccac913f..e26be9286a 100755 --- a/dockers/docker-orchagent/start.sh +++ b/dockers/docker-orchagent/start.sh @@ -6,7 +6,7 @@ sonic-cfggen -d -t /usr/share/sonic/templates/switch.json.j2 > /etc/swss/config. sonic-cfggen -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json sonic-cfggen -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json -export platform=`sonic-cfggen -v platform` +export platform=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` rm -f /var/run/rsyslogd.pid diff --git a/dockers/docker-snmp-sv2/snmpd-config-updater b/dockers/docker-snmp-sv2/snmpd-config-updater index 15d7dc9227..10c2a2b57e 100755 --- a/dockers/docker-snmp-sv2/snmpd-config-updater +++ b/dockers/docker-snmp-sv2/snmpd-config-updater @@ -172,7 +172,7 @@ def log_error(msg): # Determine whether we are running on an Arista platform def is_platform_arista(): - proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"], + proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = proc.communicate() diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 240a7514d6..c1a9b6ad41 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -20,7 +20,7 @@ function postStartAction() } # Obtain our platform as we will mount directories with these names in each docker -PLATFORM=`sonic-cfggen -v platform` +PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` {%- if docker_container_name == "database" %} # Don't mount HWSKU in {{docker_container_name}} container. diff --git a/files/build_templates/swss.service.j2 b/files/build_templates/swss.service.j2 index bcda050e3c..9e2d860cf0 100644 --- a/files/build_templates/swss.service.j2 +++ b/files/build_templates/swss.service.j2 @@ -30,7 +30,7 @@ ExecStartPre=/usr/bin/mst start ExecStartPre=/usr/bin/mlnx-fw-upgrade.sh ExecStartPre=/etc/init.d/sxdkernel start ExecStartPre=/sbin/modprobe i2c-dev -ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management start" +ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management start" {% elif sonic_asic_platform == 'cavium' %} ExecStartPre=/etc/init.d/xpnet.sh start {% endif %} @@ -43,7 +43,7 @@ ExecStop=/usr/bin/{{docker_container_name}}.sh stop ExecStopPost=/usr/bin/syncd.sh stop {% if sonic_asic_platform == 'mellanox' %} -ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management stop" +ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management stop" ExecStopPost=/etc/init.d/sxdkernel stop ExecStopPost=/usr/bin/mst stop {% elif sonic_asic_platform == 'cavium' %} diff --git a/files/image_config/caclmgrd/caclmgrd-start.sh b/files/image_config/caclmgrd/caclmgrd-start.sh index 3c7a2afbdf..6bca9a8126 100755 --- a/files/image_config/caclmgrd/caclmgrd-start.sh +++ b/files/image_config/caclmgrd/caclmgrd-start.sh @@ -2,7 +2,7 @@ # Only start control plance ACL manager daemon if not an Arista platform. # Arista devices will use their own service ACL manager daemon(s) instead. -if [ "$(sonic-cfggen -v "platform" | grep -c "arista")" -gt 0 ]; then +if [ "$(sonic-cfggen -H -v "DEVICE_METADATA.localhost.platform" | grep -c "arista")" -gt 0 ]; then echo "Not starting caclmgrd - unsupported platform" exit 0 fi diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index 563ba4cee7..aa44cadf52 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -79,22 +79,9 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then firsttime_exit fi - # setup initial switch mac - SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type) - SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}') - - # Align last byte of MAC if necessary - if [ "$SONIC_ASIC_TYPE" = "mellanox" ] || [ "$SONIC_ASIC_TYPE" = "centec" ]; then - last_byte=$(python -c "print '$SYSTEM_MAC_ADDRESS'[-2:]") - aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix - SYSTEM_MAC_ADDRESS=$(python -c "print '$SYSTEM_MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC - fi - - if [ -f /etc/sonic/init_cfg.json ]; then - sonic-cfggen -j /etc/sonic/init_cfg.json -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /tmp/init_cfg.json - mv /tmp/init_cfg.json /etc/sonic/init_cfg.json - else - sonic-cfggen -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /etc/sonic/init_cfg.json + if [ ! -f /etc/sonic/init_cfg.json ]; then + # Generate an empty init_cfg.json + echo '{}' > /etc/sonic/init_cfg.json fi # Try to take old configuration saved during installation @@ -102,20 +89,20 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then rm -f /host/old_config/sonic_version.yml mv -f /host/old_config/* /etc/sonic/ if [ ! -f /etc/sonic/config_db.json ]; then - sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json + sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json fi elif [ -f /host/minigraph.xml ]; then mv /host/minigraph.xml /etc/sonic/ # Combine information in minigraph and init_cfg.json to form initiate config DB dump file. # TODO: After moving all information from minigraph to DB, sample config DB dump should be provide - sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json + sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json else # Use default minigraph.xml cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/ - sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json + sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json fi - HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v "DEVICE_METADATA['localhost']['hwsku']"` + HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v DEVICE_METADATA.localhost.hwsku` if [ -f /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 ]; then # generate and merge buffers configuration into config file sonic-cfggen -m -t /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 > /tmp/buffers.json diff --git a/files/image_config/ssh/sshd-config-updater b/files/image_config/ssh/sshd-config-updater index 000ead9bfb..ad1ee000fe 100755 --- a/files/image_config/ssh/sshd-config-updater +++ b/files/image_config/ssh/sshd-config-updater @@ -153,7 +153,7 @@ def log_error(msg): # Determine whether we are running on an Arista platform def is_platform_arista(): - proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"], + proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = proc.communicate() diff --git a/files/image_config/updategraph/updategraph b/files/image_config/updategraph/updategraph index b72bc6e2c1..421bbbe539 100755 --- a/files/image_config/updategraph/updategraph +++ b/files/image_config/updategraph/updategraph @@ -31,7 +31,7 @@ if [ "$src" = "dhcp" ]; then if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then echo "'N/A' found in DHCP response. Skipping graph update and generating an empty configuration." echo '{"DEVICE_METADATA":' > /tmp/device_meta.json - sonic-cfggen -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json + sonic-cfggen -H -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json echo '}' >> /tmp/device_meta.json if [ -f /etc/sonic/init_cfg.json ]; then 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 echo "Regenerating config DB from minigraph..." if [ -f /etc/sonic/init_cfg.json ]; then - sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json + sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json else - sonic-cfggen -m --print-data > /etc/sonic/config_db.json + sonic-cfggen -H -m --print-data > /etc/sonic/config_db.json fi # Mark as disabled after graph is successfully downloaded diff --git a/platform/p4/docker-sonic-p4/start.sh b/platform/p4/docker-sonic-p4/start.sh index ae7bf4e5cf..7c8f9ce151 100755 --- a/platform/p4/docker-sonic-p4/start.sh +++ b/platform/p4/docker-sonic-p4/start.sh @@ -19,12 +19,6 @@ fi mkdir -p /etc/swss/config.d/ -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json - -# export platform=`sonic-cfggen -v platform` - rm -f /var/run/rsyslogd.pid echo "Start rsyslogd" @@ -94,4 +88,4 @@ VLAN=`sonic-cfggen -d -v 'VLAN.keys() | join(" ") if VLAN'` if [ "$VLAN" != "" ]; then echo "Start arp_update" supervisorctl start arp_update -fi \ No newline at end of file +fi diff --git a/platform/vs/docker-sonic-vs/start.sh b/platform/vs/docker-sonic-vs/start.sh index 9df6f9f5ad..960cc242b8 100755 --- a/platform/vs/docker-sonic-vs/start.sh +++ b/platform/vs/docker-sonic-vs/start.sh @@ -18,12 +18,6 @@ fi mkdir -p /etc/swss/config.d/ -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json -# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json - -# export platform=`sonic-cfggen -v platform` - rm -f /var/run/rsyslogd.pid supervisorctl start rsyslogd diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index a5727d38b9..f2c0c257a3 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -28,6 +28,7 @@ from minigraph import parse_xml from minigraph import parse_device_desc_xml from sonic_platform import get_machine_info from sonic_platform import get_platform_info +from sonic_platform import get_system_mac from swsssdk import ConfigDBConnector def is_ipv4(value): @@ -135,6 +136,7 @@ def main(): parser.add_argument("-j", "--json", help="json file that contains additional variables", action='append', default=[]) parser.add_argument("-a", "--additional-data", help="addition data, in json string") parser.add_argument("-d", "--from-db", help="read config from configdb", action='store_true') + parser.add_argument("-H", "--platform-info", help="read platform and hardware info", action='store_true') parser.add_argument("-s", "--redis-unix-sock-file", help="unix sock file for redis connection") group = parser.add_mutually_exclusive_group() group.add_argument("-t", "--template", help="render the data with the template file") @@ -144,25 +146,22 @@ def main(): group.add_argument("--print-data", help="print all data", action='store_true') args = parser.parse_args() - data = {} - machine_info = get_machine_info() - if machine_info != None: - deep_update(data, machine_info) - platform_info = get_platform_info(machine_info) - if platform_info != None: - data['platform'] = platform_info + platform = get_platform_info(get_machine_info()) db_kwargs = {} if args.redis_unix_sock_file != None: db_kwargs['unix_socket_path'] = args.redis_unix_sock_file + data = {} + + if args.minigraph != None: minigraph = args.minigraph - if data.has_key('platform'): + if platform: if args.port_config != None: - deep_update(data, parse_xml(minigraph, data['platform'], args.port_config)) + deep_update(data, parse_xml(minigraph, platform, args.port_config)) else: - deep_update(data, parse_xml(minigraph, data['platform'])) + deep_update(data, parse_xml(minigraph, platform)) else: if args.port_config != None: deep_update(data, parse_xml(minigraph, port_config_file=args.port_config)) @@ -189,6 +188,13 @@ def main(): configdb.connect() deep_update(data, FormatConverter.db_to_output(configdb.get_config())) + if args.platform_info: + hardware_data = {'DEVICE_METADATA': {'localhost': { + 'platform': platform, + 'mac': get_system_mac() + }}} + deep_update(data, hardware_data) + if args.template != None: template_file = os.path.abspath(args.template) env = jinja2.Environment(loader=jinja2.FileSystemLoader('/'), trim_blocks=True) diff --git a/src/sonic-config-engine/sonic_platform.py b/src/sonic-config-engine/sonic_platform.py index 1eca1487a7..feff5be5f5 100644 --- a/src/sonic-config-engine/sonic_platform.py +++ b/src/sonic-config-engine/sonic_platform.py @@ -1,5 +1,7 @@ #!/usr/bin/env python import os +import yaml +import subprocess DOCUMENTATION = ''' --- @@ -29,3 +31,25 @@ def get_platform_info(machine_info): elif machine_info.has_key('aboot_platform'): return machine_info['aboot_platform'] return None + +def get_sonic_version_info(): + if not os.path.isfile('/etc/sonic/version.yml'): + return None + data = {} + with open('/etc/sonic/version.yml') as stream: + data = yaml.load(stream) + return data + +def get_system_mac(): + proc = subprocess.Popen("ip link show eth0 | grep ether | awk '{print $2}'", shell=True, stdout=subprocess.PIPE) + (mac, err) = proc.communicate() + mac = mac.strip() + + # Align last byte of MAC if necessary + version_info = get_sonic_version_info() + if version_info and (version_info['asic_type'] == 'mellanox' or version_info['asic_type'] == 'centec'): + last_byte = mac[-2:] + aligned_last_byte = format(int(int(last_byte, 16) & 0b11000000), '02x') + mac = mac[:-2] + aligned_last_byte + return mac + diff --git a/src/sonic-utilities b/src/sonic-utilities index 63309c5388..35967e545a 160000 --- a/src/sonic-utilities +++ b/src/sonic-utilities @@ -1 +1 @@ -Subproject commit 63309c5388dd7d208d88b48d5b090791d314e62b +Subproject commit 35967e545af98f4d064e079d1057cc41590f079e