From 8836992db145ea27620a8f2ba43adf01db16655f Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 6 Jul 2016 17:39:38 +0200 Subject: scripts/restore-factory: update the flash's journal Now we update the flash partition's journal when the config is restored to factory defaults. The line indicates if the operation was performed in interactive mode (implying the user responded Yes), and from what terminal, or if it was called with "-f" in which case the parent process name is also reported if possible, helping to know if it's from init or any web interface. Example : 2016/07/07 02:05:15 aloha alb3100 8.0.3 pts/0 restore-factory: confirmed in interactive mode 2016/07/07 02:18:04 aloha alb3100 8.0.3 none restore-factory: called with -f from process cmdwrapper --- scripts/restore-factory | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/scripts/restore-factory b/scripts/restore-factory index 2ae37d2..c377d4f 100755 --- a/scripts/restore-factory +++ b/scripts/restore-factory @@ -14,6 +14,7 @@ STDOUT=0 QUIET=0 LIST=0 LOCKFILE="/tmp/lock-save-dyn" +PROG="${0##*/}" # Mounts the flash reaw/write in $FLASHDIR. # This checks /proc/cmdline for the LAST 'flash=' statement, and @@ -76,6 +77,28 @@ umount_flash() { return 0 } +# Usage: $0 | +write_journal() +{ + local date product model version tty + local message="$1" + + date=$(date +"%Y/%m/%d %H:%M:%S") + if /bin/tty -s; then + tty=$(/bin/tty 2>/dev/null) + tty=${tty#/dev/} + else + tty="none" + fi + + set -- $(grep "^Product:" /usr/share/factory/version); product="$2" + set -- $(grep "^Model:" /usr/share/factory/version); model="$2" + set -- $(grep "^Version:" /usr/share/factory/version); version="$2" + + echo "$date $product $model $version $tty $PROG: $message" >> "$FLASHDIR/.journal" 2>/dev/null + chattr +a "$FLASHDIR/.journal" >/dev/null 2>&1 +} + function lock_save_dyn { local now local lockdate @@ -129,21 +152,23 @@ fi mount_flash_rw || exit 1 -if [ -e $FLASHCFG/config.cur ] ; then - rm -f $FLASHCFG/config.cur -fi - -if [ -e $FLASHCFG/config.bak ] ; then - rm -f $FLASHCFG/config.bak +if [ "$1" != "-f" ]; then + write_journal "operation requested in interactive mode" +else + set -- $(grep "^PPid:" /proc/$$/status 2>/dev/null); ppid="$2" + set -- $(grep "^Name:" /proc/$ppid/status 2>/dev/null); name="$2" + write_journal "called with -f from process $name" fi -if [ -e $FLASHCFG/config.val ] ; then - rm -f $FLASHCFG/config.val -fi +for file in config.cur config.bak config.val config.adm; do + if [ -e "$FLASHCFG/$file" ] ; then + msg=$(rm -f "$FLASHCFG/$file" 2>&1) + ret=$? + msg=$(echo -n "$msg" | tr '[\000- ]' ' ') + [ $ret = 0 ] || write_journal "rm $file failed: |$msg| status: $ret" + fi +done -if [ -e $FLASHCFG/config.adm ] ; then - rm -f $FLASHCFG/config.adm -fi umount_flash sync -- 1.7.12.1