From 54bdac18102b82cae739a5f8d4f4e720f76bbb64 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Fri, 22 Jun 2018 21:34:37 +0000 Subject: [PATCH 1/4] [rc.local] Move all constants and functions to top of file; Unify style; Reword messages --- files/image_config/platform/rc.local | 61 +++++++++++++++------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index de54d141ef7e..e20e0ba40ed4 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -11,6 +11,12 @@ # # By default this script does nothing. +eval SONIC_VERSION=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") +FIRST_BOOT_FILE="/host/image-${SONIC_VERSION}/platform/firsttime" + +echo $SONIC_VERSION +echo $FIRST_BOOT_FILE + # In case the unit is migrating from another NOS, save the logs log_migration() { echo $1 >> /host/migration/migration.log @@ -85,6 +91,25 @@ update_mgmt_interface_macaddr() { sed -i "/eth0/ s/ATTR{address}==\"$old_mac\"/ATTR{address}==\"$new_mac\"/g" /etc/udev/rules.d/70-persistent-net.rules } +firsttime_exit() { + rm -rf $FIRST_BOOT_FILE + exit 0 +} + +# Given a string of tuples of the form field=value, extract the value for a field +# In : $string, $field +# Out: $value +value_extract() { + set -- $string + for x in "$@"; do + case "$x" in + $field=*) + value="${x#$field=}" + esac + done +} + + # If the machine.conf is absent, it indicates that the unit booted # into SONiC from another NOS. Extract the machine.conf from ONIE. if [ ! -e /host/machine.conf ]; then @@ -161,38 +186,16 @@ fi . /host/machine.conf -echo "install platform dependent packages at the first boot time" - -firsttime_exit() -{ - rm /host/image-$sonic_version/platform/firsttime - exit 0 -} - -# Given a string of tuples of the form field=value, extract the value for a field -# In : $string, $field -# Out: $value -value_extract() -{ -set -- $string -for x in "$@"; do - case "$x" in - $field=*) - value="${x#$field=}" - esac -done -} - -eval sonic_version=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") +if [ -f $FIRST_BOOT_FILE ]; then -if [ -f /host/image-$sonic_version/platform/firsttime ]; then + echo "First boot detected. Performing first boot tasks..." if [ -n "$aboot_platform" ]; then platform=$aboot_platform elif [ -n "$onie_platform" ]; then platform=$onie_platform else - echo "Unknown sonic platform" + echo "Unknown SONiC platform" firsttime_exit fi @@ -216,15 +219,15 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then touch /tmp/pending_config_initialization fi - if [ -d /host/image-$sonic_version/platform/$platform ]; then - dpkg -i /host/image-$sonic_version/platform/$platform/*.deb + if [ -d /host/image-$SONIC_VERSION/platform/$platform ]; then + dpkg -i /host/image-$SONIC_VERSION/platform/$platform/*.deb fi # If the unit booted into SONiC from another NOS's grub, # we now install a grub for SONiC. if [ -n "$onie_platform" ] && [ -n "$migration" ]; then - grub_bin=$(ls /host/image-$sonic_version/platform/x86_64-grub/grub-pc-bin*.deb 2> /dev/null) + grub_bin=$(ls /host/image-$SONIC_VERSION/platform/x86_64-grub/grub-pc-bin*.deb 2> /dev/null) if [ -z "$grub_bin" ]; then log_migration "Unable to locate grub package !" firsttime_exit @@ -302,7 +305,7 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then mv /host/grub.cfg /host/grub/grub.cfg fi - rm /host/image-$sonic_version/platform/firsttime + firsttime_exit fi exit 0 From 184bd44e560f69c1dcf12681c1937f66f3a5b0ad Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 23 Jun 2018 02:00:24 +0000 Subject: [PATCH 2/4] Add function to process reboot cause upon boot --- files/image_config/platform/rc.local | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index e20e0ba40ed4..d11ac7dfb223 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -14,9 +14,6 @@ eval SONIC_VERSION=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") FIRST_BOOT_FILE="/host/image-${SONIC_VERSION}/platform/firsttime" -echo $SONIC_VERSION -echo $FIRST_BOOT_FILE - # In case the unit is migrating from another NOS, save the logs log_migration() { echo $1 >> /host/migration/migration.log @@ -109,6 +106,32 @@ value_extract() { done } +# Set up previous and next reboot cause files +process_reboot_cause() { + REBOOT_CAUSE_FILE="/var/cache/sonic/reboot-cause.txt" + PREVIOUS_REBOOT_CAUSE_FILE="/var/cache/sonic/previous-reboot-cause.txt" + + # Set the previous reboot cause accordingly + # If this is the first boot after an image upgrade, state that as the + # cause. Otherwise, move REBOOT_CAUSE_FILE to PREVIOUS_REBOOT_CAUSE_FILE. + # REBOOT_CAUSE_FILE should always exist, but we add the else case + # to ensure we always generate PREVIOUS_REBOOT_CAUSE_FILE here + if [ -f $FIRST_BOOT_FILE ]; then + echo "SONiC firmware upgrade" > $PREVIOUS_REBOOT_CAUSE_FILE + elif [ -f $REBOOT_CAUSE_FILE ]; then + mv -f $REBOOT_CAUSE_FILE $PREVIOUS_REBOOT_CAUSE_FILE + else + echo "Unknown reboot cause" > $PREVIOUS_REBOOT_CAUSE_FILE + fi + + # Set the default cause for the next reboot + echo "Unexpected reboot" > $REBOOT_CAUSE_FILE +} + +#### Begin Main Body #### + +# Set up previous and next reboot cause files +process_reboot_cause # If the machine.conf is absent, it indicates that the unit booted # into SONiC from another NOS. Extract the machine.conf from ONIE. From 7c11e84b4e5ecfda0cc942385ac94fea33941439 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 25 Jun 2018 23:15:53 +0000 Subject: [PATCH 3/4] Simplify retrieval of SONIC_VERSION per comments --- files/image_config/platform/rc.local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index d11ac7dfb223..9742916df518 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -11,7 +11,7 @@ # # By default this script does nothing. -eval SONIC_VERSION=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") +SONIC_VERSION=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version) FIRST_BOOT_FILE="/host/image-${SONIC_VERSION}/platform/firsttime" # In case the unit is migrating from another NOS, save the logs From 753174f507112df925419ffae0a2eafe926222b7 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Tue, 26 Jun 2018 00:12:14 +0000 Subject: [PATCH 4/4] Change wording --- files/image_config/platform/rc.local | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index 9742916df518..b99ecdbcee9d 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -112,12 +112,12 @@ process_reboot_cause() { PREVIOUS_REBOOT_CAUSE_FILE="/var/cache/sonic/previous-reboot-cause.txt" # Set the previous reboot cause accordingly - # If this is the first boot after an image upgrade, state that as the + # If this is the first boot after an image install, state that as the # cause. Otherwise, move REBOOT_CAUSE_FILE to PREVIOUS_REBOOT_CAUSE_FILE. # REBOOT_CAUSE_FILE should always exist, but we add the else case # to ensure we always generate PREVIOUS_REBOOT_CAUSE_FILE here if [ -f $FIRST_BOOT_FILE ]; then - echo "SONiC firmware upgrade" > $PREVIOUS_REBOOT_CAUSE_FILE + echo "SONiC image installation" > $PREVIOUS_REBOOT_CAUSE_FILE elif [ -f $REBOOT_CAUSE_FILE ]; then mv -f $REBOOT_CAUSE_FILE $PREVIOUS_REBOOT_CAUSE_FILE else