There are some functions which are common for mkdumprd and kdumpctl
test/test-check. For example, mkdumprd will check the target fs size
and warn if size is less than total memory. This involves remote fs mount,
same as test-check which will also mount remote fs to check if the test
timestamp id exist.
Also, this patch makes modifications on functions such as
"handle_default_dump_target" "check_user_configured_target", which
receives a
function as argument, so they can be used for both mkdumprd and kdumpctl
test/test-check.
Signed-off-by: Tao Liu <ltao(a)redhat.com>
---
kdump-lib.sh | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++
mkdumprd | 127 ++++-------------------------------------------
2 files changed, 149 insertions(+), 116 deletions(-)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 042ac87..0eb5f03 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -1089,3 +1089,141 @@ get_all_kdump_crypt_dev()
get_luks_crypt_dev "$(kdump_get_maj_min "$_dev")"
done
}
+
+SAVE_PATH=$(get_save_path)
+
+handle_tmp_dir()
+{
+ TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
+ [ -d "$TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX
failed."
+ TMPMNT="$TMPDIR/target"
+
+ trap '
+ ret=$?;
+ is_mounted $TMPMNT && umount -f $TMPMNT;
+ [[ -d $TMPDIR ]] && rm --one-file-system -rf -- "$TMPDIR";
+ exit $ret;
+ ' EXIT
+}
+
+check_save_path_fs()
+{
+ local _path=$1
+
+ if [[ ! -d $_path ]]; then
+ perror_exit "Dump path $_path does not exist."
+ fi
+}
+
+#handle the case user does not specify the dump target explicitly
+# $1: function
+# ${2..}: arguments for the function
+handle_default_dump_target()
+{
+ local _target
+ local _mntpoint
+ local _func=$1
+
+ shift 1
+ is_user_configured_dump_target && return
+
+ check_save_path_fs "$SAVE_PATH"
+
+ _save_path=$(get_bind_mount_source "$SAVE_PATH")
+ _target=$(get_target_from_path "$_save_path")
+ _mntpoint=$(get_mntpoint_from_target "$_target")
+
+ SAVE_PATH=${_save_path##"$_mntpoint"}
+ $_func "$_target" "$_save_path" $@
+}
+
+mount_failure()
+{
+ local _target=$1
+ local _mnt=$2
+ local _fstype=$3
+ local msg="Failed to mount $_target"
+
+ if [[ -n $_mnt ]]; then
+ msg="$msg on $_mnt"
+ fi
+
+ msg="$msg for kdump preflight check."
+
+ if [[ $_fstype == "nfs" ]]; then
+ msg="$msg Please make sure nfs-utils has been installed."
+ fi
+
+ perror_exit "$msg"
+}
+
+# $1: fs target
+# $2: fs type
+# $3: function
+# ${4..}: arguments for the function
+check_user_configured_target()
+{
+ local _target=$1 _cfg_fs_type=$2 _mounted _func=$3
+ local _mnt _opt _fstype _ret
+
+ _mnt=$(get_mntpoint_from_target "$_target")
+ _opt=$(get_mntopt_from_target "$_target")
+ _fstype=$(get_fs_type_from_target "$_target")
+
+ if [[ -n $_fstype ]]; then
+ # In case of nfs4, nfs should be used instead, nfs* options is deprecated in
kdump.conf
+ [[ $_fstype == "nfs"* ]] && _fstype=nfs
+
+ if [[ -n $_cfg_fs_type ]] && [[ $_fstype != "$_cfg_fs_type" ]]; then
+ perror_exit "\"$_target\" have a wrong type config
\"$_cfg_fs_type\", expected \"$_fstype\""
+ fi
+ else
+ _fstype="$_cfg_fs_type"
+ _fstype="$_cfg_fs_type"
+ fi
+
+ # For noauto mount, mount it inplace with default value.
+ # Else use the temporary target directory
+ if [[ -n $_mnt ]]; then
+ if ! is_mounted "$_mnt"; then
+ if [[ $_opt == *",noauto"* ]]; then
+ mount "$_mnt" || mount_failure "$_target" "$_mnt"
"$_fstype"
+ _mounted=$_mnt
+ else
+ perror_exit "Dump target \"$_target\" is neither mounted nor
configured as \"noauto\""
+ fi
+ fi
+ else
+ handle_tmp_dir
+ _mnt=$TMPMNT
+ mkdir -p "$_mnt"
+ mount "$_target" "$_mnt" -t "$_fstype" -o defaults ||
mount_failure "$_target" "" "$_fstype"
+ _mounted=$_mnt
+ fi
+
+ # For user configured target, use $SAVE_PATH as the dump path within the target
+ if [[ ! -d "$_mnt/$SAVE_PATH" ]]; then
+ perror_exit "Dump path \"$SAVE_PATH\" does not exist in dump target
\"$_target\""
+ fi
+
+ shift 3
+ $_func "$_target" "$_mnt/$SAVE_PATH" $@
+ _ret=$?
+
+ # Unmount it early, if function is interrupted and didn't reach here, the shell trap
will clear it up anyway
+ if [[ -n $_mounted ]]; then
+ umount -f -- "$_mounted"
+ fi
+
+ return $_ret
+}
+
+get_ssh_key_location()
+{
+ SSH_KEY_LOCATION=$DEFAULT_SSHKEY
+ keyfile=$(kdump_get_conf_val sshkey)
+ if [[ -f $keyfile ]]; then
+ # canonicalize the path
+ SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$keyfile")
+ fi
+}
\ No newline at end of file
diff --git a/mkdumprd b/mkdumprd
index 31c4b76..74abf74 100644
--- a/mkdumprd
+++ b/mkdumprd
@@ -22,23 +22,11 @@ if ! dlog_init; then
exit 1
fi
-SSH_KEY_LOCATION=$DEFAULT_SSHKEY
-SAVE_PATH=$(get_save_path)
+get_ssh_key_location
extra_modules=""
dracut_args=(--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n
--hostonly-mode strict --hostonly-nics '' --aggressive-strip -o "plymouth
resume ifcfg earlykdump")
-MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
-[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t
dracut.XXXXXX failed."
-MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"
-
-trap '
- ret=$?;
- is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
- [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf --
"$MKDUMPRD_TMPDIR";
- exit $ret;
- ' EXIT
-
# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT
@@ -171,87 +159,6 @@ check_size()
fi
}
-check_save_path_fs()
-{
- local _path=$1
-
- if [[ ! -d $_path ]]; then
- perror_exit "Dump path $_path does not exist."
- fi
-}
-
-mount_failure()
-{
- local _target=$1
- local _mnt=$2
- local _fstype=$3
- local msg="Failed to mount $_target"
-
- if [[ -n $_mnt ]]; then
- msg="$msg on $_mnt"
- fi
-
- msg="$msg for kdump preflight check."
-
- if [[ $_fstype == "nfs" ]]; then
- msg="$msg Please make sure nfs-utils has been installed."
- fi
-
- perror_exit "$msg"
-}
-
-check_user_configured_target()
-{
- local _target=$1 _cfg_fs_type=$2 _mounted
- local _mnt _opt _fstype
-
- _mnt=$(get_mntpoint_from_target "$_target")
- _opt=$(get_mntopt_from_target "$_target")
- _fstype=$(get_fs_type_from_target "$_target")
-
- if [[ -n $_fstype ]]; then
- # In case of nfs4, nfs should be used instead, nfs* options is deprecated in
kdump.conf
- [[ $_fstype == "nfs"* ]] && _fstype=nfs
-
- if [[ -n $_cfg_fs_type ]] && [[ $_fstype != "$_cfg_fs_type" ]]; then
- perror_exit "\"$_target\" have a wrong type config
\"$_cfg_fs_type\", expected \"$_fstype\""
- fi
- else
- _fstype="$_cfg_fs_type"
- _fstype="$_cfg_fs_type"
- fi
-
- # For noauto mount, mount it inplace with default value.
- # Else use the temporary target directory
- if [[ -n $_mnt ]]; then
- if ! is_mounted "$_mnt"; then
- if [[ $_opt == *",noauto"* ]]; then
- mount "$_mnt" || mount_failure "$_target" "$_mnt"
"$_fstype"
- _mounted=$_mnt
- else
- perror_exit "Dump target \"$_target\" is neither mounted nor
configured as \"noauto\""
- fi
- fi
- else
- _mnt=$MKDUMPRD_TMPMNT
- mkdir -p "$_mnt"
- mount "$_target" "$_mnt" -t "$_fstype" -o defaults ||
mount_failure "$_target" "" "$_fstype"
- _mounted=$_mnt
- fi
-
- # For user configured target, use $SAVE_PATH as the dump path within the target
- if [[ ! -d "$_mnt/$SAVE_PATH" ]]; then
- perror_exit "Dump path \"$SAVE_PATH\" does not exist in dump target
\"$_target\""
- fi
-
- check_size fs "$_target"
-
- # Unmount it early, if function is interrupted and didn't reach here, the shell trap
will clear it up anyway
- if [[ -n $_mounted ]]; then
- umount -f -- "$_mounted"
- fi
-}
-
# $1: core_collector config value
verify_core_collector()
{
@@ -289,25 +196,6 @@ add_mount()
add_dracut_mount "$_mnt"
}
-#handle the case user does not specify the dump target explicitly
-handle_default_dump_target()
-{
- local _target
- local _mntpoint
-
- is_user_configured_dump_target && return
-
- check_save_path_fs "$SAVE_PATH"
-
- _save_path=$(get_bind_mount_source "$SAVE_PATH")
- _target=$(get_target_from_path "$_save_path")
- _mntpoint=$(get_mntpoint_from_target "$_target")
-
- SAVE_PATH=${_save_path##"$_mntpoint"}
- add_mount "$_target"
- check_size fs "$_target"
-}
-
check_crypt()
{
local _dev
@@ -323,6 +211,14 @@ if ! check_crypt; then
dwarn "Warning: Encrypted device is in dump path, which is not recommended, see
kexec-kdump-howto.txt for more details."
fi
+mount_and_check_fs_size()
+{
+ local _target=$1
+
+ add_mount "$_target"
+ check_size fs "$_target"
+}
+
# firstly get right SSH_KEY_LOCATION
keyfile=$(kdump_get_conf_val sshkey)
if [[ -f $keyfile ]]; then
@@ -337,8 +233,7 @@ while read -r config_opt config_val; do
extra_modules="$extra_modules $config_val"
;;
ext[234] | xfs | btrfs | minix | nfs | virtiofs)
- check_user_configured_target "$config_val" "$config_opt"
- add_mount "$config_val" "$config_opt"
+ check_user_configured_target "$config_val" "$config_opt"
mount_and_check_fs_size
;;
raw)
# checking raw disk writable
@@ -383,7 +278,7 @@ while read -r config_opt config_val; do
esac
done <<< "$(kdump_read_conf)"
-handle_default_dump_target
+handle_default_dump_target mount_and_check_fs_size
if ! have_compression_in_dracut_args; then
if is_squash_available && dracut_have_option "--squash-compressor";
then
--
2.40.1