From c188dc8acb2a5518a9c206fb5dc94584dbc440d5 Mon Sep 17 00:00:00 2001 From: Zach Dykstra Date: Sun, 17 Dec 2023 22:09:26 -0600 Subject: [PATCH] recovery shell: print zerrors since the last command was executed --- zfsbootmenu/bin/logs | 1 + zfsbootmenu/install-helpers.sh | 2 +- zfsbootmenu/lib/kmsg-log-lib.sh | 12 ++++++++---- zfsbootmenu/libexec/recovery-error-printer | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 120000 zfsbootmenu/bin/logs create mode 100755 zfsbootmenu/libexec/recovery-error-printer diff --git a/zfsbootmenu/bin/logs b/zfsbootmenu/bin/logs new file mode 120000 index 000000000..6a8efeda6 --- /dev/null +++ b/zfsbootmenu/bin/logs @@ -0,0 +1 @@ +zlogtail \ No newline at end of file diff --git a/zfsbootmenu/install-helpers.sh b/zfsbootmenu/install-helpers.sh index 2178e3147..15d805b8b 100644 --- a/zfsbootmenu/install-helpers.sh +++ b/zfsbootmenu/install-helpers.sh @@ -151,7 +151,7 @@ create_zbm_profiles() { [ -f /etc/profile ] && source /etc/profile [ -f /lib/zfsbootmenu-completions.sh ] && source /lib/zfsbootmenu-completions.sh - export PS1="\[\033[0;33m\]zfsbootmenu\[\033[0m\] \w > " + export PS1='\$( /libexec/recovery-error-printer )\[\033[0;33m\]zfsbootmenu\[\033[0m\] \w > ' alias clear="tput clear" alias reset="tput reset" diff --git a/zfsbootmenu/lib/kmsg-log-lib.sh b/zfsbootmenu/lib/kmsg-log-lib.sh index d953cf425..ba6dafe85 100755 --- a/zfsbootmenu/lib/kmsg-log-lib.sh +++ b/zfsbootmenu/lib/kmsg-log-lib.sh @@ -86,6 +86,7 @@ zerror() { } # arg1: comma-separated log levels to print +# arg2: optional date that messages must be timestamped after # prints: all logs at that level # returns: nothing @@ -98,14 +99,17 @@ print_kmsg_logs() { return fi - # Try to use feature flags found in dmesg from util-linux - if output="$( dmesg --notime -f user --color=always -l "${levels}" 2>/dev/null )" ; then - echo -e "${output}" + since="${2}" + + # dmesg from dmesg-util can helpfully do --since, but only if it's also allowed to print the time out + # so always print the time, optionally set --since, and filter the timestamp after + if output="$( dmesg -f user -l "${levels}" ${since:+--since ${since}} 2>/dev/null )" ; then + echo -e "${output}" | sed 's/^\[.*\]\ //' else # Both util-linux and Busybox dmesg support the -r flag. However, the log level that is # reported by Busybox dmesg is larger than that reported by util-linux dmesg. Busybox dmesg # is too bare-bones to do much of anything, so we just need to grep for both integers at - # a given log level, then refly on matching ZFSBootMenu for info and lower, and ZBM for debug. + # a given log level, then rely on matching ZFSBootMenu for info and lower, and ZBM for debug. IFS=',' read -r -a levels_array <<<"${levels}" for level in "${levels_array[@]}"; do diff --git a/zfsbootmenu/libexec/recovery-error-printer b/zfsbootmenu/libexec/recovery-error-printer new file mode 100755 index 000000000..f9301e486 --- /dev/null +++ b/zfsbootmenu/libexec/recovery-error-printer @@ -0,0 +1,18 @@ +#!/bin/bash + +PROMPT_TS_FILE="${BASE}/errors_since" + +[ -f "${PROMPT_TS_FILE}" ] && read -r prompt_ts < "${PROMPT_TS_FILE}" + +# shellcheck disable=SC1091 +source /lib/kmsg-log-lib.sh || exit 0 + +if [ -f "${BASE}/have_errors" ]; then + echo -e -n "\n" + print_kmsg_logs "err" ${prompt_ts:+${prompt_ts}} + # trailing newlines are eaten by the prompt, so add a no-op carriage return + echo -n -e "\n\r" + rm "/zfsbootmenu/have_errors" +fi + +printf '%(%Y-%m-%dT%H:%M:%S)T' > "${PROMPT_TS_FILE}"