diff --git a/files/build_templates/dhcp_relay.service.j2 b/files/build_templates/dhcp_relay.service.j2 index c0e993eec06..49b5be2ecf7 100644 --- a/files/build_templates/dhcp_relay.service.j2 +++ b/files/build_templates/dhcp_relay.service.j2 @@ -1,7 +1,9 @@ [Unit] Description=DHCP relay container Requires=docker.service +After=role-config.service After=interfaces-config.service +ConditionPathExists=/etc/sonic/role/dhcp_relay [Service] User={{ sonicadmin_user }} diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 77fcd1f3865..340ef3a4ce6 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -125,6 +125,18 @@ sudo bash -c "echo enabled=false > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" # Copy SNMP configuration files sudo cp $IMAGE_CONFIGS/snmp/snmp.yml $FILESYSTEM_ROOT/etc/sonic/ +# Copy platform-init script and service unit file, enable service +sudo cp $IMAGE_CONFIGS/platform/platform-init.service $FILESYSTEM_ROOT/etc/systemd/system/ +sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable platform-init.service +sudo cp $IMAGE_CONFIGS/platform/platform-init.sh $FILESYSTEM_ROOT/usr/bin/ + +# Copy role-config script and service unit file, enable service and create dir to +# house files that will be used to conditionally start services based on device role +sudo cp $IMAGE_CONFIGS/role/role-config.service $FILESYSTEM_ROOT/etc/systemd/system/ +sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable role-config.service +sudo cp $IMAGE_CONFIGS/role/role-config.sh $FILESYSTEM_ROOT/usr/bin/ +sudo mkdir -p $FILESYSTEM_ROOT/etc/sonic/role/ + # Copy sudoers configuration file sudo cp $IMAGE_CONFIGS/sudoers/sudoers $FILESYSTEM_ROOT/etc/ @@ -173,8 +185,6 @@ sudo dpkg --root=$FILESYSTEM_ROOT -P {{ debname }} sudo rm -f $FILESYSTEM_ROOT/usr/sbin/policy-rc.d -## copy platform rc.local -sudo cp $IMAGE_CONFIGS/platform/rc.local $FILESYSTEM_ROOT/etc/ {% if installer_images.strip() -%} {% for image in installer_images.strip().split(' ') -%} diff --git a/files/build_templates/teamd.service.j2 b/files/build_templates/teamd.service.j2 index e291a39b387..fb4b6745e48 100644 --- a/files/build_templates/teamd.service.j2 +++ b/files/build_templates/teamd.service.j2 @@ -1,7 +1,9 @@ [Unit] Description=TEAMD container Requires=database.service +After=role-config.service After=database.service +ConditionPathExists=/etc/sonic/role/teamd [Service] User={{ sonicadmin_user }} diff --git a/files/image_config/platform/platform-init.service b/files/image_config/platform/platform-init.service new file mode 100644 index 00000000000..efe196eb0af --- /dev/null +++ b/files/image_config/platform/platform-init.service @@ -0,0 +1,13 @@ +[Unit] +Description=First boot platform initialization +Before=updategraph.service +ConditionPathExists=/host/platform/firsttime + +[Service] +Type=oneshot +ExecStartPre=/bin/echo "First boot detected. Initializing platform..." +ExecStart=/usr/bin/platform-init.sh +ExecStartPost=/bin/rm -f /host/platform/firsttime + +[Install] +WantedBy=multi-user.target diff --git a/files/image_config/platform/platform-init.sh b/files/image_config/platform/platform-init.sh new file mode 100755 index 00000000000..4bacaac6a77 --- /dev/null +++ b/files/image_config/platform/platform-init.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# platform-init.sh +# +# Script to perform tasks which configure platform upon first boot +# To be run by platform-init.service if it detects first boot +# + +PLATFORM=`sonic-cfggen -v platform` + +if [ -n $PLATFORM ]; then + echo "SONiC platform: $PLATFORM" + + echo "Copying default minigraph for $PLATFORM to /etc/sonic/" + cp /usr/share/sonic/device/$PLATFORM/minigraph.xml /etc/sonic/ + + if [ -d /host/platform/$PLATFORM ]; then + echo "Installing platform-dependent packages for $PLATFORM..." + dpkg -i /host/platform/$PLATFORM/*.deb + fi +else + echo "SONiC platform unknown. Could not install platform-dependent packages." +fi + diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local deleted file mode 100755 index eccba516e69..00000000000 --- a/files/image_config/platform/rc.local +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh -e -# -# rc.local -# -# This script is executed at the end of each multiuser runlevel. -# Make sure that the script will "exit 0" on success or any other -# value on error. -# -# In order to enable or disable this script just change the execution -# bits. -# -# By default this script does nothing. - -. /host/machine.conf - -echo "install platform dependent packages at the first boot time" - -if [ -f /host/platform/firsttime ]; then - - if [ -n "$aboot_platform" ]; then - platform=$aboot_platform - elif [ -n "$onie_platform" ]; then - platform=$onie_platform - else - echo "Unknown sonic platform" - rm /host/platform/firsttime - exit 0 - fi - - cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/ - - if [ -d /host/platform/$platform ]; then - dpkg -i /host/platform/$platform/*.deb - fi - - rm /host/platform/firsttime -fi - -exit 0 diff --git a/files/image_config/role/role-config.service b/files/image_config/role/role-config.service new file mode 100644 index 00000000000..21b337b9ee0 --- /dev/null +++ b/files/image_config/role/role-config.service @@ -0,0 +1,10 @@ +[Unit] +Description=Role-specific configuration +After=updategraph.service + +[Service] +Type=oneshot +ExecStart=/usr/bin/role-config.sh + +[Install] +WantedBy=multi-user.target diff --git a/files/image_config/role/role-config.sh b/files/image_config/role/role-config.sh new file mode 100755 index 00000000000..371cff8be4a --- /dev/null +++ b/files/image_config/role/role-config.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# role-config.sh +# +# Script to perform tasks which configure device based on its role as determined by its minigraph +# To be run by role-config.service +# + +# Disable DHCP relay service if device does not require it +DHCP_RELAY_SERVICE_START_FILE=/etc/sonic/role/dhcp_relay +DEVICE_ROLE=`sonic-cfggen -m /etc/sonic/minigraph.xml -v "minigraph_devices[minigraph_hostname]['type']"` + +if [ $DEVICE_ROLE == "ToRRouter" ]; then + echo "Device requires DHCP relay service. Ensuring start file exists..." + touch $DHCP_RELAY_SERVICE_START_FILE +else + echo "Device does not require DHCP relay service. Deleting start file..." + rm -f $DHCP_RELAY_SERVICE_START_FILE +fi + + +# Disable teamd service if device does not require it +TEAMD_SERVICE_START_FILE=/etc/sonic/role/teamd +NUM_PORTCHANNELS=`sonic-cfggen -m /etc/sonic/minigraph.xml -v "minigraph_portchannels.keys() | count"` + +if [ $NUM_PORTCHANNELS -gt 0 ]; then + echo "Device requires teamd service. Ensuring start file exists..." + touch $TEAMD_SERVICE_START_FILE +else + echo "Device does not require teamd service. Deleting start file..." + rm -f $TEAMD_SERVICE_START_FILE +fi + diff --git a/files/image_config/updategraph/updategraph.service b/files/image_config/updategraph/updategraph.service index ba0b5dcb85e..c8fc10c49b5 100644 --- a/files/image_config/updategraph/updategraph.service +++ b/files/image_config/updategraph/updategraph.service @@ -1,5 +1,6 @@ [Unit] Description=download minigraph from graph service +After=platform-init.service Before=ntp-config.service Before=rsyslog-config.service Before=interfaces-config.service