From 89c9a12db227ee87c5aa862c7c2f8b5ec024b985 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 8 May 2024 17:09:27 +0200 Subject: [PATCH] confd: on failure to load startup-config, reset and regroup With the sysrepo patch from the previous commit, we can now properly detect if a callback failed to apply its changes in SR_EV_DONE. When this occurs the system may be in an undefined state, so we must try to recover it before loading failure-config. This patch tries to perform a factory-default RPC, which is an Infix specifc RPC that does "copy factory-config running-config". We give sysrepocfg some time to clean up any stale SHM connections before we do a hard scratch of the db state and restart sysrepo-plugind. Fixes #429 Signed-off-by: Joachim Wiberg --- src/confd/bin/load | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/confd/bin/load b/src/confd/bin/load index 03649f82..1de60e88 100755 --- a/src/confd/bin/load +++ b/src/confd/bin/load @@ -19,6 +19,16 @@ banner_append() return 0 } +note() +{ + logger -s -I $$ -p user.notice -t load "$*" +} + +err() +{ + logger -s -I $$ -p user.error -t load "$*" +} + # shellcheck disable=SC1091 . /etc/confdrc @@ -35,17 +45,31 @@ else fi if [ ! -f "$fn" ]; then - logger -sik -p user.error "No such file, $fn, aborting!" + err "No such file, $fn, aborting!" exit 1 fi +note "Loading $config ..." if ! sysrepocfg -v3 -I"$fn" -f json; then case "$config" in startup-config) - logger -sik -p user.error "Failed loading $fn, reverting to Fail Secure mode!" + err "Failed loading $fn, reverting to Fail Secure mode!" + # On failure to load startup-config the system is in an undefined state + cat <<-EOF >/tmp/factory.json + { + "infix-factory-default:factory-default": {} + } + EOF + # Default timeout is 2 seconds, allow time to clean up shm + if ! sysrepocfg -f json -t 10 -R /tmp/factory.json; then + rm -f /etc/sysrepo/data/*startup* + rm -f /etc/sysrepo/data/*running* + rm -f /dev/shm/sr_* + killall sysrepo-plugind + fi ;; failure-config) - logger -sik -p user.error "Failed loading $fn, aborting!" + err "Failed loading $fn, aborting!" banner_append "CRITICAL ERROR: Logins are disabled, no credentials available" initctl -nbq runlevel 9 ;; @@ -53,7 +77,8 @@ if ! sysrepocfg -v3 -I"$fn" -f json; then exit 1 fi -logger -sik -p user.notice "Loaded $fn successfully." + +note "Loaded $fn successfully." if [ "$config" = "failure-config" ]; then banner_append "ERROR: Corrupt startup-config, system has reverted to default login credentials" else