From 1455c45e577476f1ca070b34d795f23a4afea86e Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Tue, 22 Oct 2024 13:25:38 +0200 Subject: [PATCH 1/7] kdumpbase/kdump.sh: simplify opalcore handling Checking for the Opalcore currently requires duplicate code. Simplify it by always checking if an Opalcore exists at the beginning of the script. Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index fe50a5d1..17ad4540 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -35,7 +35,7 @@ FINAL_ACTION="systemctl reboot -f" KDUMP_PRE="" KDUMP_POST="" NEWROOT="/sysroot" -OPALCORE="/sys/firmware/opal/mpipl/core" +OPALCORE="" KDUMP_CONF_PARSED="/tmp/kdump.conf.$$" # POSIX doesn't have pipefail, only apply when using bash @@ -226,14 +226,7 @@ save_vmcore_dmesg_fs() { # $1: dump path save_opalcore_fs() { - if [ ! -f $OPALCORE ]; then - # Check if we are on an old kernel that uses a different path - if [ -f /sys/firmware/opal/core ]; then - OPALCORE="/sys/firmware/opal/core" - else - return 0 - fi - fi + [ -n "$OPALCORE" ] || return 0 dinfo "saving opalcore:$OPALCORE to $1/opalcore" if ! cp $OPALCORE "$1/opalcore"; then @@ -468,14 +461,7 @@ dump_ssh() { # $3: ssh address in @ format # $4: scp address, similar with ssh address but IPv6 addresses are quoted save_opalcore_ssh() { - if [ ! -f $OPALCORE ]; then - # Check if we are on an old kernel that uses a different path - if [ -f /sys/firmware/opal/core ]; then - OPALCORE="/sys/firmware/opal/core" - else - return 0 - fi - fi + [ -n "$OPALCORE" ] || return 0 dinfo "saving opalcore:$OPALCORE to $3:$1" @@ -642,6 +628,12 @@ kdump_test_init() { kdump_test_set_status 'fail' } +for _core in "/sys/firmware/opal/mpipl/core" "/sys/firmware/opal/core"; do + [ -f "$_core" ] || continue + OPALCORE="$_core" + break +done + if [ "$1" = "--error-handler" ]; then get_kdump_confs do_failure_action From 559a711c702ce44881dcfa2a1997236cfe46c522 Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 27 Nov 2024 10:50:32 +0100 Subject: [PATCH 2/7] kdumpbase/kdump.sh: move ssh host to global variable Move the ssh host to a global variable to make it available for new functions introduced in following commits. While at it rename $SSH_KEY_LOCATION to $SSH_KEY to shorten the name and be consistent with the naming schema of the other variables. Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 54 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index 17ad4540..1b0b5aba 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -29,7 +29,7 @@ FAILURE_ACTION="systemctl reboot -f" DATEDIR=$(date +%Y-%m-%d-%T) HOST_IP='127.0.0.1' DUMP_INSTRUCTION="" -SSH_KEY_LOCATION=$DEFAULT_SSHKEY +SSH_KEY=$DEFAULT_SSHKEY DD_BLKSIZE=512 FINAL_ACTION="systemctl reboot -f" KDUMP_PRE="" @@ -58,7 +58,7 @@ get_kdump_confs() { ;; sshkey) if [ -f "$config_val" ]; then - SSH_KEY_LOCATION=$config_val + SSH_KEY=$config_val fi ;; kdump_pre) @@ -392,37 +392,35 @@ dump_raw() { return 0 } -# $1: ssh key file -# $2: ssh address in @ format dump_ssh() { _ret=0 - _ssh_opts="-i $1 -o BatchMode=yes -o StrictHostKeyChecking=yes" + _ssh_opts="-i $SSH_KEY -o BatchMode=yes -o StrictHostKeyChecking=yes" if [ -z "$KDUMP_TEST_ID" ]; then _ssh_dir="$KDUMP_PATH/$HOST_IP-$DATEDIR" else _ssh_dir="$KDUMP_PATH" fi - if is_ipv6_address "$2"; then - _scp_address=${2%@*}@"[${2#*@}]" + if is_ipv6_address "$SSH_HOST"; then + _scp_address=${SSH_HOST%@*}@"[${SSH_HOST#*@}]" else - _scp_address=$2 + _scp_address=$SSH_HOST fi - dinfo "saving to $2:$_ssh_dir" + dinfo "saving to $SSH_HOST:$_ssh_dir" cat /var/lib/random-seed > /dev/urandom # shellcheck disable=SC2086 # ssh_opts needs to be split - ssh -q $_ssh_opts "$2" mkdir -p "$_ssh_dir" || return 1 + ssh -q $_ssh_opts "$SSH_HOST" mkdir -p "$_ssh_dir" || return 1 - save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$_ssh_dir" "$_ssh_opts" "$2" + save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$_ssh_dir" "$_ssh_opts" dinfo "saving vmcore" - KDUMP_LOG_DEST=$2:$_ssh_dir/ + KDUMP_LOG_DEST=$SSH_HOST:$_ssh_dir/ KDUMP_LOG_OP="scp -q $_ssh_opts '$KDUMP_LOG_FILE' '$_scp_address:$_ssh_dir/'" - save_opalcore_ssh "$_ssh_dir" "$_ssh_opts" "$2" "$_scp_address" + save_opalcore_ssh "$_ssh_dir" "$_ssh_opts" "$_scp_address" if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then # shellcheck disable=SC2086 # ssh_opts needs to be split @@ -433,7 +431,7 @@ dump_ssh() { # shellcheck disable=SC2029,SC2086 # - _ssh_opts needs to be split # - _ssh_dir needs to be expanded - $CORE_COLLECTOR /proc/vmcore | ssh $_ssh_opts "$2" "umask 0077 && dd bs=512 of='$_ssh_dir/vmcore-incomplete'" + $CORE_COLLECTOR /proc/vmcore | ssh $_ssh_opts "$SSH_HOST" "umask 0077 && dd bs=512 of='$_ssh_dir/vmcore-incomplete'" _ret=$? _vmcore="vmcore.flat" fi @@ -442,7 +440,7 @@ dump_ssh() { # shellcheck disable=SC2029,SC2086 # - _ssh_opts needs to be split # - _ssh_dir needs to be expanded - ssh $_ssh_opts "$2" "mv '$_ssh_dir/vmcore-incomplete' '$_ssh_dir/$_vmcore'" + ssh $_ssh_opts "$SSH_HOST" "mv '$_ssh_dir/vmcore-incomplete' '$_ssh_dir/$_vmcore'" _ret=$? if [ $_ret -ne 0 ]; then derror "moving vmcore failed, exitcode:$_ret" @@ -458,15 +456,14 @@ dump_ssh() { # $1: dump path # $2: ssh opts -# $3: ssh address in @ format -# $4: scp address, similar with ssh address but IPv6 addresses are quoted +# $3: scp address, similar with ssh address but IPv6 addresses are quoted save_opalcore_ssh() { [ -n "$OPALCORE" ] || return 0 - dinfo "saving opalcore:$OPALCORE to $3:$1" + dinfo "saving opalcore:$OPALCORE to $SSH_HOST:$1" # shellcheck disable=SC2086 # $2 (_ssh_opts) needs to be split - if ! scp $2 "$OPALCORE" "$4:$1/opalcore-incomplete"; then + if ! scp $2 "$OPALCORE" "$3:$1/opalcore-incomplete"; then derror "saving opalcore failed" return 1 fi @@ -474,7 +471,7 @@ save_opalcore_ssh() { # shellcheck disable=SC2029,SC2086 # - $1 (dump path) needs to be expanded # - $2 (_ssh_opts) needs to be split - ssh $2 "$3" mv "$1/opalcore-incomplete" "$1/opalcore" + ssh $2 "$SSH_HOST" mv "$1/opalcore-incomplete" "$1/opalcore" dinfo "saving opalcore complete" return 0 } @@ -482,14 +479,13 @@ save_opalcore_ssh() { # $1: dmesg collector # $2: dump path # $3: ssh opts -# $4: ssh address in @ format save_vmcore_dmesg_ssh() { - dinfo "saving vmcore-dmesg.txt to $4:$2" + dinfo "saving vmcore-dmesg.txt to $SSH_HOST:$2" # shellcheck disable=SC2029,SC2086 # - $2 (_ssh_dir) needs to be expanded # - $3 (_ssh_opts) needs to be split - if "$1" /proc/vmcore | ssh $3 "$4" "umask 0077 && dd of='$2/vmcore-dmesg-incomplete.txt'"; then - ssh -q $3 "$4" mv "$2/vmcore-dmesg-incomplete.txt" "$2/vmcore-dmesg.txt" + if "$1" /proc/vmcore | ssh $3 "$SSH_HOST" "umask 0077 && dd of='$2/vmcore-dmesg-incomplete.txt'"; then + ssh -q $3 "$SSH_HOST" mv "$2/vmcore-dmesg-incomplete.txt" "$2/vmcore-dmesg.txt" dinfo "saving vmcore-dmesg.txt complete" else derror "saving vmcore-dmesg.txt failed" @@ -573,7 +569,8 @@ read_kdump_confs() { DUMP_INSTRUCTION="dump_raw $config_val" ;; ssh) - DUMP_INSTRUCTION="dump_ssh $SSH_KEY_LOCATION $config_val" + SSH_HOST="$config_val" + DUMP_INSTRUCTION="dump_ssh" ;; esac done < "$KDUMP_CONF_PARSED" @@ -600,12 +597,11 @@ kdump_test_set_status() { esac if is_ssh_dump_target; then - _ssh_opts="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes" - _ssh_host=$(echo "$DUMP_INSTRUCTION" | awk '{print $3}') + _ssh_opts="-i $SSH_KEY -o BatchMode=yes -o StrictHostKeyChecking=yes" - ssh -q $_ssh_opts "$_ssh_host" "mkdir -p ${KDUMP_TEST_STATUS%/*}" \ + ssh -q $_ssh_opts "$SSH_HOST" "mkdir -p ${KDUMP_TEST_STATUS%/*}" \ || return 1 - ssh -q $_ssh_opts "$_ssh_host" "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" \ + ssh -q $_ssh_opts "$SSH_HOST" "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" \ || return 1 else _target=$(echo "$DUMP_INSTRUCTION" | awk '{print $2}') From 3950a8bbfcb03b946f22ed5aa920bdc6cbdb30f4 Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 27 Nov 2024 11:26:04 +0100 Subject: [PATCH 3/7] kdumpbase/kdump.sh: move ssh/scp calls to separate function In dump_ssh the _ssh_opts need to be word split, which triggers ShellChecks SC2086 warning. This warning is currently disabled for all calls to ssh/scp. However in the ShellCheck wiki [1] the suggested solution for POSIX compatible code is to make the call in a separate function. This also makes disabling of SC2029 obsolete. One big benefit of this solution is that now calls to ssh/scp can also be made outside of dump_ssh without redefining _ssh_opts. One small downside is that the options passed to ssh/scp must now be maintained in each new function separately. Compared to the benefit described above this is a small price to pay. Note: Currently ssh/scp option -q,--quiet is used inconsistently. With this commit it will always be set. [1] https://www.shellcheck.net/wiki/SC2086 Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 65 ++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index 1b0b5aba..183e9e9f 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -392,9 +392,21 @@ dump_raw() { return 0 } +_ssh() { + ssh -q -i "$SSH_KEY" \ + -o BatchMode=yes \ + -o StrictHostKeyChecking=yes \ + "$SSH_HOST" "$@" +} + +_scp() { + scp -q -i "$SSH_KEY" \ + -o BatchMode=yes \ + -o StrictHostKeyChecking=yes \ + "$@" +} + dump_ssh() { - _ret=0 - _ssh_opts="-i $SSH_KEY -o BatchMode=yes -o StrictHostKeyChecking=yes" if [ -z "$KDUMP_TEST_ID" ]; then _ssh_dir="$KDUMP_PATH/$HOST_IP-$DATEDIR" else @@ -410,37 +422,29 @@ dump_ssh() { dinfo "saving to $SSH_HOST:$_ssh_dir" cat /var/lib/random-seed > /dev/urandom - # shellcheck disable=SC2086 # ssh_opts needs to be split - ssh -q $_ssh_opts "$SSH_HOST" mkdir -p "$_ssh_dir" || return 1 + _ssh mkdir -p "$_ssh_dir" || return 1 - save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$_ssh_dir" "$_ssh_opts" + save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$_ssh_dir" dinfo "saving vmcore" KDUMP_LOG_DEST=$SSH_HOST:$_ssh_dir/ - KDUMP_LOG_OP="scp -q $_ssh_opts '$KDUMP_LOG_FILE' '$_scp_address:$_ssh_dir/'" + KDUMP_LOG_OP="_scp '$KDUMP_LOG_FILE' '$_scp_address:$_ssh_dir/'" - save_opalcore_ssh "$_ssh_dir" "$_ssh_opts" "$_scp_address" + save_opalcore_ssh "$_ssh_dir" "$_scp_address" if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then - # shellcheck disable=SC2086 # ssh_opts needs to be split - scp -q $_ssh_opts /proc/vmcore "$_scp_address:$_ssh_dir/vmcore-incomplete" + _scp /proc/vmcore "$_scp_address:$_ssh_dir/vmcore-incomplete" _ret=$? _vmcore="vmcore" else - # shellcheck disable=SC2029,SC2086 - # - _ssh_opts needs to be split - # - _ssh_dir needs to be expanded - $CORE_COLLECTOR /proc/vmcore | ssh $_ssh_opts "$SSH_HOST" "umask 0077 && dd bs=512 of='$_ssh_dir/vmcore-incomplete'" + $CORE_COLLECTOR /proc/vmcore | _ssh "umask 0077 && dd bs=512 of='$_ssh_dir/vmcore-incomplete'" _ret=$? _vmcore="vmcore.flat" fi if [ $_ret -eq 0 ]; then - # shellcheck disable=SC2029,SC2086 - # - _ssh_opts needs to be split - # - _ssh_dir needs to be expanded - ssh $_ssh_opts "$SSH_HOST" "mv '$_ssh_dir/vmcore-incomplete' '$_ssh_dir/$_vmcore'" + _ssh mv "$_ssh_dir/vmcore-incomplete" "$_ssh_dir/$_vmcore" _ret=$? if [ $_ret -ne 0 ]; then derror "moving vmcore failed, exitcode:$_ret" @@ -455,37 +459,28 @@ dump_ssh() { } # $1: dump path -# $2: ssh opts -# $3: scp address, similar with ssh address but IPv6 addresses are quoted +# $2: scp address, similar with ssh address but IPv6 addresses are quoted save_opalcore_ssh() { [ -n "$OPALCORE" ] || return 0 dinfo "saving opalcore:$OPALCORE to $SSH_HOST:$1" - # shellcheck disable=SC2086 # $2 (_ssh_opts) needs to be split - if ! scp $2 "$OPALCORE" "$3:$1/opalcore-incomplete"; then + if ! _scp "$OPALCORE" "$2:$1/opalcore-incomplete"; then derror "saving opalcore failed" return 1 fi - # shellcheck disable=SC2029,SC2086 - # - $1 (dump path) needs to be expanded - # - $2 (_ssh_opts) needs to be split - ssh $2 "$SSH_HOST" mv "$1/opalcore-incomplete" "$1/opalcore" + _ssh mv "$1/opalcore-incomplete" "$1/opalcore" dinfo "saving opalcore complete" return 0 } # $1: dmesg collector # $2: dump path -# $3: ssh opts save_vmcore_dmesg_ssh() { dinfo "saving vmcore-dmesg.txt to $SSH_HOST:$2" - # shellcheck disable=SC2029,SC2086 - # - $2 (_ssh_dir) needs to be expanded - # - $3 (_ssh_opts) needs to be split - if "$1" /proc/vmcore | ssh $3 "$SSH_HOST" "umask 0077 && dd of='$2/vmcore-dmesg-incomplete.txt'"; then - ssh -q $3 "$SSH_HOST" mv "$2/vmcore-dmesg-incomplete.txt" "$2/vmcore-dmesg.txt" + if "$1" /proc/vmcore | _ssh "umask 0077 && dd of='$2/vmcore-dmesg-incomplete.txt'"; then + _ssh mv "$2/vmcore-dmesg-incomplete.txt" "$2/vmcore-dmesg.txt" dinfo "saving vmcore-dmesg.txt complete" else derror "saving vmcore-dmesg.txt failed" @@ -597,12 +592,8 @@ kdump_test_set_status() { esac if is_ssh_dump_target; then - _ssh_opts="-i $SSH_KEY -o BatchMode=yes -o StrictHostKeyChecking=yes" - - ssh -q $_ssh_opts "$SSH_HOST" "mkdir -p ${KDUMP_TEST_STATUS%/*}" \ - || return 1 - ssh -q $_ssh_opts "$SSH_HOST" "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" \ - || return 1 + _ssh "mkdir -p ${KDUMP_TEST_STATUS%/*}" || return 1 + _ssh "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" || return 1 else _target=$(echo "$DUMP_INSTRUCTION" | awk '{print $2}') From 29bd542adc44ee4a9d767d190354f3651228bacd Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 27 Nov 2024 17:34:48 +0100 Subject: [PATCH 4/7] kdumpbase/kdump.sh: make use of global NEWROOT The mount point for file system dumps is currently passed as an argument to dump_fs. But there are only two possible values for the mount point. Its either $NEWROOT for failure_action dump_to_rootfs or whatever the user provided in kdump.conf. Thus instead of passing the mount point as argument use a global variable and update it when parsing the config and use that in dump_fs. Reuse $NEWROOT for this. Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index 183e9e9f..c26b3f96 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -133,14 +133,13 @@ save_log() { eval "$KDUMP_LOG_OP" } -# $1: dump path, must be a mount point dump_fs() { - ddebug "dump_fs _mp=$1" + ddebug "dump_fs _mp=$NEWROOT" - if ! is_mounted "$1"; then - dinfo "dump path '$1' is not mounted, trying to mount..." - if ! mount --target "$1"; then - derror "failed to dump to '$1', it's not a mount point!" + if ! is_mounted "$NEWROOT"; then + dinfo "dump path '$NEWROOT' is not mounted, trying to mount..." + if ! mount --target "$NEWROOT"; then + derror "failed to dump to '$NEWROOT', it's not a mount point!" return 1 fi fi @@ -165,11 +164,11 @@ dump_fs() { dinfo "saving to $_dump_fs_path" # Only remount to read-write mode if the dump target is mounted read-only. - _dump_mnt_op=$(get_mount_info OPTIONS target "$1" -f) + _dump_mnt_op=$(get_mount_info OPTIONS target "$NEWROOT" -f) case $_dump_mnt_op in ro*) dinfo "Remounting the dump target in rw mode." - mount -o remount,rw "$1" || return 1 + mount -o remount,rw "$NEWROOT" || return 1 ;; esac @@ -263,7 +262,7 @@ dump_to_rootfs() { fi ddebug "NEWROOT=$NEWROOT" - dump_fs $NEWROOT + dump_fs } kdump_emergency_shell() { @@ -552,13 +551,13 @@ read_kdump_confs() { dracut_args) config_val=$(get_dracut_args_target "$config_val") if [ -n "$config_val" ]; then - config_val=$(get_mntpoint_from_target "$config_val") - DUMP_INSTRUCTION="dump_fs $config_val" + NEWROOT=$(get_mntpoint_from_target "$config_val") + DUMP_INSTRUCTION="dump_fs" fi ;; ext[234] | xfs | btrfs | minix | nfs | virtiofs) - config_val=$(get_mntpoint_from_target "$config_val") - DUMP_INSTRUCTION="dump_fs $config_val" + NEWROOT=$(get_mntpoint_from_target "$config_val") + DUMP_INSTRUCTION="dump_fs" ;; raw) DUMP_INSTRUCTION="dump_raw $config_val" @@ -595,11 +594,9 @@ kdump_test_set_status() { _ssh "mkdir -p ${KDUMP_TEST_STATUS%/*}" || return 1 _ssh "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" || return 1 else - _target=$(echo "$DUMP_INSTRUCTION" | awk '{print $2}') - - mkdir -p "$_target/$KDUMP_PATH" || return 1 - echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$_target/$KDUMP_TEST_STATUS" - sync -f "$_target/$KDUMP_TEST_STATUS" + mkdir -p "$NEWROOT/$KDUMP_PATH" || return 1 + echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$NEWROOT/$KDUMP_TEST_STATUS" + sync -f "$NEWROOT/$KDUMP_TEST_STATUS" fi } @@ -644,7 +641,7 @@ if ! get_host_ip; then fi if [ -z "$DUMP_INSTRUCTION" ]; then - DUMP_INSTRUCTION="dump_fs $NEWROOT" + DUMP_INSTRUCTION="dump_fs" fi kdump_test_init From 2f309a0c46620f9dacb0bcab946de6a6ef0e5462 Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 27 Nov 2024 17:39:00 +0100 Subject: [PATCH 5/7] kdumpbase/kdump.sh: make use of global KDUMP_PATH Both dump_fs and dump_ssh currently define their own, almost identical, directories they use to write the dump to. This not only adds duplicate code but also prevents any code outside of dump_{fs,ssh} to access the dump directory. Thus use a global definition for said directory. Reuse the already existing $KDUMP_PATH for that. Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 58 ++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index c26b3f96..60243c0a 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -26,7 +26,6 @@ CORE_COLLECTOR="" DEFAULT_CORE_COLLECTOR="makedumpfile -l --message-level 7 -d 31" DMESG_COLLECTOR="/sbin/vmcore-dmesg" FAILURE_ACTION="systemctl reboot -f" -DATEDIR=$(date +%Y-%m-%d-%T) HOST_IP='127.0.0.1' DUMP_INSTRUCTION="" SSH_KEY=$DEFAULT_SSHKEY @@ -155,13 +154,7 @@ dump_fs() { ;; esac - if [ -z "$KDUMP_TEST_ID" ]; then - _dump_fs_path=$(echo "$1/$KDUMP_PATH/$HOST_IP-$DATEDIR/" | tr -s /) - else - _dump_fs_path=$(echo "$1/$KDUMP_PATH/" | tr -s /) - fi - - dinfo "saving to $_dump_fs_path" + dinfo "saving to $KDUMP_PATH" # Only remount to read-write mode if the dump target is mounted read-only. _dump_mnt_op=$(get_mount_info OPTIONS target "$NEWROOT" -f) @@ -172,22 +165,22 @@ dump_fs() { ;; esac - mkdir -p "$_dump_fs_path" || return 1 + mkdir -p "$KDUMP_PATH" || return 1 - save_vmcore_dmesg_fs ${DMESG_COLLECTOR} "$_dump_fs_path" - save_opalcore_fs "$_dump_fs_path" + save_vmcore_dmesg_fs ${DMESG_COLLECTOR} "$KDUMP_PATH" + save_opalcore_fs "$KDUMP_PATH" dinfo "saving vmcore" - KDUMP_LOG_DEST=$_dump_fs_path/ + KDUMP_LOG_DEST=$KDUMP_PATH/ KDUMP_LOG_OP="mv '$KDUMP_LOG_FILE' '$KDUMP_LOG_DEST/'" - $CORE_COLLECTOR /proc/vmcore "$_dump_fs_path/vmcore-incomplete" + $CORE_COLLECTOR /proc/vmcore "$KDUMP_PATH/vmcore-incomplete" _dump_exitcode=$? if [ $_dump_exitcode -eq 0 ]; then - sync -f "$_dump_fs_path/vmcore-incomplete" + sync -f "$KDUMP_PATH/vmcore-incomplete" _sync_exitcode=$? if [ $_sync_exitcode -eq 0 ]; then - mv "$_dump_fs_path/vmcore-incomplete" "$_dump_fs_path/vmcore" + mv "$KDUMP_PATH/vmcore-incomplete" "$KDUMP_PATH/vmcore" dinfo "saving vmcore complete" else derror "sync vmcore failed, exitcode:$_sync_exitcode" @@ -261,6 +254,7 @@ dump_to_rootfs() { return fi + KDUMP_PATH="$NEWROOT/$KDUMP_PATH/$HOST_IP-$(date +%Y-%m-%d-%T)" ddebug "NEWROOT=$NEWROOT" dump_fs } @@ -406,38 +400,31 @@ _scp() { } dump_ssh() { - if [ -z "$KDUMP_TEST_ID" ]; then - _ssh_dir="$KDUMP_PATH/$HOST_IP-$DATEDIR" - else - _ssh_dir="$KDUMP_PATH" - fi - if is_ipv6_address "$SSH_HOST"; then _scp_address=${SSH_HOST%@*}@"[${SSH_HOST#*@}]" else _scp_address=$SSH_HOST fi - - dinfo "saving to $SSH_HOST:$_ssh_dir" + dinfo "saving to $SSH_HOST:$KDUMP_PATH" cat /var/lib/random-seed > /dev/urandom - _ssh mkdir -p "$_ssh_dir" || return 1 + _ssh mkdir -p "$KDUMP_PATH" || return 1 - save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$_ssh_dir" + save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$KDUMP_PATH" dinfo "saving vmcore" - KDUMP_LOG_DEST=$SSH_HOST:$_ssh_dir/ - KDUMP_LOG_OP="_scp '$KDUMP_LOG_FILE' '$_scp_address:$_ssh_dir/'" + KDUMP_LOG_DEST=$SSH_HOST:$KDUMP_PATH/ + KDUMP_LOG_OP="_scp '$KDUMP_LOG_FILE' '$_scp_address:$KDUMP_PATH/'" - save_opalcore_ssh "$_ssh_dir" "$_scp_address" + save_opalcore_ssh "$KDUMP_PATH" "$_scp_address" if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then - _scp /proc/vmcore "$_scp_address:$_ssh_dir/vmcore-incomplete" + _scp /proc/vmcore "$_scp_address:$KDUMP_PATH/vmcore-incomplete" _ret=$? _vmcore="vmcore" else - $CORE_COLLECTOR /proc/vmcore | _ssh "umask 0077 && dd bs=512 of='$_ssh_dir/vmcore-incomplete'" + $CORE_COLLECTOR /proc/vmcore | _ssh "umask 0077 && dd bs=512 of='$KDUMP_PATH/vmcore-incomplete'" _ret=$? _vmcore="vmcore.flat" fi @@ -594,9 +581,9 @@ kdump_test_set_status() { _ssh "mkdir -p ${KDUMP_TEST_STATUS%/*}" || return 1 _ssh "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" || return 1 else - mkdir -p "$NEWROOT/$KDUMP_PATH" || return 1 - echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$NEWROOT/$KDUMP_TEST_STATUS" - sync -f "$NEWROOT/$KDUMP_TEST_STATUS" + mkdir -p "$KDUMP_PATH" || return 1 + echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$KDUMP_TEST_STATUS" + sync -f "$KDUMP_TEST_STATUS" fi } @@ -606,6 +593,7 @@ kdump_test_init() { KDUMP_TEST_ID=$(getarg kdump_test_id=) [ -z "$KDUMP_TEST_ID" ] && return + KDUMP_PATH="${KDUMP_PATH%/*}" KDUMP_PATH="$KDUMP_PATH/kdump-test-$KDUMP_TEST_ID" KDUMP_TEST_STATUS="$KDUMP_PATH/vmcore-creation.status" @@ -644,6 +632,10 @@ if [ -z "$DUMP_INSTRUCTION" ]; then DUMP_INSTRUCTION="dump_fs" fi +KDUMP_PATH="$KDUMP_PATH/$HOST_IP-$(date +%Y-%m-%d-%T)" +[ "$DUMP_INSTRUCTION" = "dump_fs" ] && KDUMP_PATH="$NEWROOT/$KDUMP_PATH" +KDUMP_PATH="$(echo "$KDUMP_PATH" | tr -s /)" + kdump_test_init if ! do_kdump_pre; then derror "kdump_pre script exited with non-zero status!" From ac0e7188d299710119365f11f69509f9bcbfb00f Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 27 Nov 2024 13:14:43 +0100 Subject: [PATCH 6/7] kdumpbase/kdump.sh: use curl for ssh dumps This commit contains two major changes for dumps via ssh. One is the switch from scp to sftp as protocol to transfer files. Using sftp has two big advantages 1. It allows a wide variety of file operations, e.g. renaming files, on the remote host. 2. It can work with files of unknown size, i.e. read from a pipe. sftp is fully supported by OpenSSH, in fact since OpenSSH 9.0 (released April 2022) the 'scp' command uses sftp internally by default. The other big change is to make use of curl rather than ssh/scp/sftp directly. This is mainly because curl provides a cleaner user interface for now. But curl also supports a big variety of different protocols and thus could be used to extend support to dump via e.g. http in the future. Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 104 ++++++++++------------------- dracut/99kdumpbase/module-setup.sh | 1 + 2 files changed, 38 insertions(+), 67 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index 60243c0a..d9335cad 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -385,94 +385,61 @@ dump_raw() { return 0 } -_ssh() { - ssh -q -i "$SSH_KEY" \ - -o BatchMode=yes \ - -o StrictHostKeyChecking=yes \ - "$SSH_HOST" "$@" +_curl() { + curl --silent \ + --fail-early \ + --create-file-mode 0600 \ + --ftp-create-dirs \ + -u "$SSH_USER:" \ + --key "$SSH_KEY" \ + "$@" } -_scp() { - scp -q -i "$SSH_KEY" \ - -o BatchMode=yes \ - -o StrictHostKeyChecking=yes \ - "$@" +# copy a file to remote host using curl +# $1: file to be copied or - (dash) when read from stdin +# $2: destination path on remote host +copy_to_remote() { + _src="$1"; shift + _dst="$1"; shift + + _url="sftp://$SSH_HOST" + + dinfo "saving ${_dst##*/} to $SSH_HOST:${_dst%/*}" + _curl -T "$_src" -Q "-rename $_dst-incomplete $_dst" "$_url/$_dst-incomplete" + _ret=$? + if [ $_ret -ne 0 ]; then + derror "failed to save ${_dst##*/}, exitcode $_ret" + return $_ret + fi + + dinfo "saving ${_dst##*/} complete" } dump_ssh() { - if is_ipv6_address "$SSH_HOST"; then - _scp_address=${SSH_HOST%@*}@"[${SSH_HOST#*@}]" - else - _scp_address=$SSH_HOST - fi dinfo "saving to $SSH_HOST:$KDUMP_PATH" cat /var/lib/random-seed > /dev/urandom - _ssh mkdir -p "$KDUMP_PATH" || return 1 - save_vmcore_dmesg_ssh "$DMESG_COLLECTOR" "$KDUMP_PATH" + $DMESG_COLLECTOR /proc/vmcore | copy_to_remote "-" "$KDUMP_PATH/vmcore-dmesg.txt" dinfo "saving vmcore" KDUMP_LOG_DEST=$SSH_HOST:$KDUMP_PATH/ - KDUMP_LOG_OP="_scp '$KDUMP_LOG_FILE' '$_scp_address:$KDUMP_PATH/'" + KDUMP_LOG_OP="copy_to_remote '$KDUMP_LOG_FILE' '$KDUMP_PATH/kexec-dmesg.log'" - save_opalcore_ssh "$KDUMP_PATH" "$_scp_address" + [ -n "$OPALCORE" ] && copy_to_remote "$OPALCORE" "$KDUMP_PATH/opalcore" if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then - _scp /proc/vmcore "$_scp_address:$KDUMP_PATH/vmcore-incomplete" + copy_to_remote /proc/vmcore "$KDUMP_PATH/vmcore" _ret=$? - _vmcore="vmcore" else - $CORE_COLLECTOR /proc/vmcore | _ssh "umask 0077 && dd bs=512 of='$KDUMP_PATH/vmcore-incomplete'" + $CORE_COLLECTOR /proc/vmcore | copy_to_remote "-" "$KDUMP_PATH/vmcore.flat" _ret=$? - _vmcore="vmcore.flat" - fi - - if [ $_ret -eq 0 ]; then - _ssh mv "$_ssh_dir/vmcore-incomplete" "$_ssh_dir/$_vmcore" - _ret=$? - if [ $_ret -ne 0 ]; then - derror "moving vmcore failed, exitcode:$_ret" - else - dinfo "saving vmcore complete" - fi - else - derror "saving vmcore failed, exitcode:$_ret" fi return $_ret } -# $1: dump path -# $2: scp address, similar with ssh address but IPv6 addresses are quoted -save_opalcore_ssh() { - [ -n "$OPALCORE" ] || return 0 - - dinfo "saving opalcore:$OPALCORE to $SSH_HOST:$1" - - if ! _scp "$OPALCORE" "$2:$1/opalcore-incomplete"; then - derror "saving opalcore failed" - return 1 - fi - - _ssh mv "$1/opalcore-incomplete" "$1/opalcore" - dinfo "saving opalcore complete" - return 0 -} - -# $1: dmesg collector -# $2: dump path -save_vmcore_dmesg_ssh() { - dinfo "saving vmcore-dmesg.txt to $SSH_HOST:$2" - if "$1" /proc/vmcore | _ssh "umask 0077 && dd of='$2/vmcore-dmesg-incomplete.txt'"; then - _ssh mv "$2/vmcore-dmesg-incomplete.txt" "$2/vmcore-dmesg.txt" - dinfo "saving vmcore-dmesg.txt complete" - else - derror "saving vmcore-dmesg.txt failed" - fi -} - wait_online_network() { # In some cases, network may still not be ready because nm-online is called # with "-s" which means to wait for NetworkManager startup to complete, rather @@ -550,7 +517,11 @@ read_kdump_confs() { DUMP_INSTRUCTION="dump_raw $config_val" ;; ssh) - SSH_HOST="$config_val" + SSH_USER="${config_val%@*}" + SSH_HOST="${config_val#*@}" + if is_ipv6_address "$SSH_HOST"; then + SSH_HOST="[$SSH_HOST]" + fi DUMP_INSTRUCTION="dump_ssh" ;; esac @@ -578,8 +549,7 @@ kdump_test_set_status() { esac if is_ssh_dump_target; then - _ssh "mkdir -p ${KDUMP_TEST_STATUS%/*}" || return 1 - _ssh "echo $_status kdump_test_id=$KDUMP_TEST_ID > $KDUMP_TEST_STATUS" || return 1 + echo $_status kdump_test_id=$KDUMP_TEST_ID | _curl -T "-" "sftp://$SSH_HOST/$KDUMP_TEST_STATUS" else mkdir -p "$KDUMP_PATH" || return 1 echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$KDUMP_TEST_STATUS" diff --git a/dracut/99kdumpbase/module-setup.sh b/dracut/99kdumpbase/module-setup.sh index 9eb5f50c..2a61a4ce 100755 --- a/dracut/99kdumpbase/module-setup.sh +++ b/dracut/99kdumpbase/module-setup.sh @@ -1097,6 +1097,7 @@ install() { inst "/usr/bin/chmod" "/sbin/chmod" inst "/usr/bin/nproc" "/sbin/nproc" inst "/usr/bin/dirname" "/sbin/dirname" + inst "/usr/bin/curl" "/sbin/curl" inst "/lib/kdump/kdump-lib-initramfs.sh" "/lib/kdump-lib-initramfs.sh" inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh" inst "$moddir/kdump.sh" "/usr/bin/kdump.sh" From e6e421ad6898927e8d5d50e4f3174f8df774c7e6 Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Wed, 4 Dec 2024 16:11:52 +0100 Subject: [PATCH 7/7] kdumpbase/kdump.sh: drop setting test status to 'failure' initially When fetching the test status from the target a missing status file is always considered a 'failure'. So there is no need to explicitly setting the status to 'failure' in the initrd. This allows simplifying the code a bit. For example we can now assume that the directory for $KDUMP_PATH always exists (otherwise dump_{fs,ssh} would have returned with an error). Signed-off-by: Philipp Rudo --- dracut/99kdumpbase/kdump.sh | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/dracut/99kdumpbase/kdump.sh b/dracut/99kdumpbase/kdump.sh index d9335cad..006b5714 100755 --- a/dracut/99kdumpbase/kdump.sh +++ b/dracut/99kdumpbase/kdump.sh @@ -535,24 +535,15 @@ fence_kdump_notify() { fi } -kdump_test_set_status() { - _status="$1" - +kdump_test_mark_success() { [ -n "$KDUMP_TEST_STATUS" ] || return - case "$_status" in - success|fail) ;; - *) - derror "Unknown test status $_status" - return 1 - ;; - esac + _status="success kdump_test_id=$KDUMP_TEST_ID" if is_ssh_dump_target; then - echo $_status kdump_test_id=$KDUMP_TEST_ID | _curl -T "-" "sftp://$SSH_HOST/$KDUMP_TEST_STATUS" + echo "$_status" | _curl -T "-" "sftp://$SSH_HOST/$KDUMP_TEST_STATUS" else - mkdir -p "$KDUMP_PATH" || return 1 - echo "$_status kdump_test_id=$KDUMP_TEST_ID" > "$KDUMP_TEST_STATUS" + echo "$_status" > "$KDUMP_TEST_STATUS" sync -f "$KDUMP_TEST_STATUS" fi } @@ -566,8 +557,6 @@ kdump_test_init() { KDUMP_PATH="${KDUMP_PATH%/*}" KDUMP_PATH="$KDUMP_PATH/kdump-test-$KDUMP_TEST_ID" KDUMP_TEST_STATUS="$KDUMP_PATH/vmcore-creation.status" - - kdump_test_set_status 'fail' } for _core in "/sys/firmware/opal/mpipl/core" "/sys/firmware/opal/core"; do @@ -627,5 +616,5 @@ if [ $DUMP_RETVAL -ne 0 ]; then exit 1 fi -kdump_test_set_status "success" +kdump_test_mark_success do_final_action