From 50c3bba44ffff5c4105648275f36aa64e6a96e02 Mon Sep 17 00:00:00 2001 From: Cem Keylan Date: Wed, 20 May 2020 14:57:02 +0300 Subject: add option to log boot and shutdown to a file --- rc.boot | 4 ++-- rc.conf | 5 +++++ rc.lib | 19 ++++++++++++++++--- rc.shutdown | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/rc.boot b/rc.boot index 24f3a4d..3a59a79 100755 --- a/rc.boot +++ b/rc.boot @@ -12,7 +12,7 @@ mnt() { done < /proc/mounts mnt="$1"; shift - mount "$@" "$mnt" + mount "$@" "$mnt" 2>&1 | log } # Display a pretty welcome message @@ -61,7 +61,7 @@ out "Remounting rootfs as read-only..."; { [ "$FASTBOOT" = 1 ] || { out "Checking filesystems..." - fsck "-ATat${FORCEFSCK}" noopts=_netdev + fsck "-ATat${FORCEFSCK}" noopts=_netdev 2>&1 | log [ $? -gt 1 ] && shell } diff --git a/rc.conf b/rc.conf index 7a8ce97..c201f45 100644 --- a/rc.conf +++ b/rc.conf @@ -8,3 +8,8 @@ keymap= # Set dmesg level dmesg_level= +# Enable logging +# log=1 + +# Set log directory (default /var/log/init) +# log_dir= \ No newline at end of file diff --git a/rc.lib b/rc.lib index 7d5068d..57f66d4 100644 --- a/rc.lib +++ b/rc.lib @@ -1,10 +1,22 @@ # rc.lib -- common functions for rc.boot and rc.shutdown -# shellcheck disable=1090 +# shellcheck disable=1090,2034 export PATH=$PATH:/usr/local/bin:/usr/bin -out() { printf '\033[1;36m-> \033[39m%s\033[m\n' "$@" ;} -err() { printf '\033[1;31m!> \033[39m%s\033[m\n' "$@" ;} +log_file="${log_dir:=/var/log/init}/$(date +%Y%m%d-%H%M)-${0##*.}" + +log() { + [ "$log" = 1 ] || { + cat + return 0 + } + mkdir -p "$log_dir" + tee -a "$log_file" +} + + +out() { printf '\033[1;36m-> \033[39m%s\033[m\n' "$@" | log ;} +err() { printf '\033[1;31m!> \033[39m%s\033[m\n' "$@" | log ;} shell() { err "Dropping to shell, type 'exit' to continue the boot process." @@ -58,6 +70,7 @@ parse_cmdline() { ro) RO=1 ;; forcefsck) FORCEFSCK="-f" ;; fastboot) FASTBOOT=1 ;; + log) log=1 ;; esac done } diff --git a/rc.shutdown b/rc.shutdown index 292284f..7f99c4c 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -36,7 +36,7 @@ out "Remounting rootfs as readonly"; { mount -o remount,ro / sync -} +} 2>&1 | log run_hook post.shutdown -- cgit v1.2.3