893e0b
#!/bin/bash --norc
893e0b
# New mkdumprd
893e0b
#
893e0b
# Copyright 2011 Red Hat, Inc.
893e0b
#
893e0b
# Written by Cong Wang <amwang@redhat.com>
893e0b
#
893e0b
8f4abc
if [ -f /etc/sysconfig/kdump ]; then
8f4abc
	. /etc/sysconfig/kdump
8f4abc
fi
8f4abc
893e0b
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
893e0b
. $dracutbasedir/dracut-functions.sh
893e0b
. /lib/kdump/kdump-lib.sh
8f4abc
. /lib/kdump/kdump-logger.sh
893e0b
export IN_KDUMP=1
893e0b
8f4abc
#initiate the kdump logger
8f4abc
dlog_init
8f4abc
if [ $? -ne 0 ]; then
8f4abc
	echo "failed to initiate the kdump logger."
8f4abc
	exit 1
8f4abc
fi
8f4abc
893e0b
conf_file="/etc/kdump.conf"
893e0b
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
f8bec6
SAVE_PATH=$(get_save_path)
f8bec6
OVERRIDE_RESETTABLE=0
f8bec6
f8bec6
extra_modules=""
fc48f3
dracut_args="--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o \"plymouth dash resume ifcfg earlykdump\" --compress=xz"
f8bec6
f8bec6
readonly MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
f8bec6
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
f8bec6
readonly MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"
f8bec6
f8bec6
trap '
f8bec6
    ret=$?;
f8bec6
    is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
f8bec6
    [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKDUMPRD_TMPDIR";
f8bec6
    exit $ret;
f8bec6
    ' EXIT
f8bec6
f8bec6
# clean up after ourselves no matter how we die.
f8bec6
trap 'exit 1;' SIGINT
893e0b
893e0b
add_dracut_arg() {
f8bec6
    dracut_args="$dracut_args $@"
893e0b
}
893e0b
893e0b
add_dracut_mount() {
f8bec6
    add_dracut_arg "--mount" "\"$1\""
893e0b
}
893e0b
893e0b
add_dracut_sshkey() {
f8bec6
    add_dracut_arg "--sshkey" "\"$1\""
893e0b
}
893e0b
893e0b
# caller should ensure $1 is valid and mounted in 1st kernel
893e0b
to_mount() {
f8bec6
    local _target=$1 _fstype=$2 _options=$3 _new_mntpoint _pdev
f8bec6
f8bec6
    _new_mntpoint=$(get_kdump_mntpoint_from_target $_target)
f8bec6
    _fstype="${_fstype:-$(get_fs_type_from_target $_target)}"
f8bec6
    _options="${_options:-$(get_mntopt_from_target $_target)}"
f8bec6
    _options="${_options:-defaults}"
f8bec6
f8bec6
    if [[ "$_fstype" == "nfs"* ]]; then
f8bec6
        _pdev=$_target
10c66c
        _options=$(echo $_options | sed 's/,\(mount\)\?addr=[^,]*//g')
10c66c
        _options=$(echo $_options | sed 's/,\(mount\)\?proto=[^,]*//g')
f8bec6
        _options=$(echo $_options | sed 's/,clientaddr=[^,]*//')
893e0b
    else
f8bec6
        # for non-nfs _target converting to use udev persistent name
f8bec6
        _pdev="$(kdump_get_persistent_dev $_target)"
893e0b
        if [ -z "$_pdev" ]; then
893e0b
            return 1
893e0b
        fi
893e0b
    fi
893e0b
f8bec6
    #mount fs target as rw in 2nd kernel
f8bec6
    _options=$(echo $_options | sed 's/\(^\|,\)ro\($\|,\)/\1rw\2/g')
f8bec6
    # with 'noauto' in fstab nfs and non-root disk mount will fail in 2nd
f8bec6
    # kernel, filter it out here.
f8bec6
    _options=$(echo $_options | sed 's/\(^\|,\)noauto\($\|,\)/\1/g')
f8bec6
    # use both nofail and x-systemd.before to ensure systemd will try best to
f8bec6
    # mount it before kdump starts, this is an attempt to improve robustness
f8bec6
    _options="$_options,nofail,x-systemd.before=initrd-fs.target"
893e0b
f8bec6
    echo "$_pdev $_new_mntpoint $_fstype $_options"
893e0b
}
893e0b
893e0b
#Function: get_ssh_size
893e0b
#$1=dump target
893e0b
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
893e0b
get_ssh_size() {
893e0b
    local _opt _out _size
893e0b
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
893e0b
    _out=$(ssh -q -n $_opt $1 "df -P $SAVE_PATH")
893e0b
    [ $? -ne 0 ] && {
893e0b
        perror_exit "checking remote ssh server available size failed."
893e0b
    }
893e0b
893e0b
    #ssh output removed the line break, so print field NF-2
893e0b
    _size=$(echo -n $_out| awk '{avail=NF-2; print $avail}')
893e0b
    echo -n $_size
893e0b
}
893e0b
893e0b
#mkdir if save path does not exist on ssh dump target
893e0b
#$1=ssh dump target
f8bec6
#caller should ensure write permission on $1:$SAVE_PATH
893e0b
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
893e0b
mkdir_save_path_ssh()
893e0b
{
893e0b
    local _opt _dir
893e0b
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
893e0b
    ssh -qn $_opt $1 mkdir -p $SAVE_PATH 2>&1 > /dev/null
893e0b
    _ret=$?
893e0b
    if [ $_ret -ne 0 ]; then
f8bec6
        perror_exit "mkdir failed on $1:$SAVE_PATH"
893e0b
    fi
893e0b
f8bec6
    #check whether user has write permission on $1:$SAVE_PATH
893e0b
    _dir=$(ssh -qn $_opt $1 mktemp -dqp $SAVE_PATH 2>/dev/null)
893e0b
    _ret=$?
893e0b
    if [ $_ret -ne 0 ]; then
f8bec6
        perror_exit "Could not create temporary directory on $1:$SAVE_PATH. Make sure user has write permission on destination"
893e0b
    fi
893e0b
    ssh -qn $_opt $1 rmdir $_dir
893e0b
893e0b
    return 0
893e0b
}
893e0b
893e0b
#Function: get_fs_size
893e0b
#$1=dump target
893e0b
get_fs_size() {
893e0b
    local _mnt=$(get_mntpoint_from_target $1)
893e0b
    echo -n $(df -P "${_mnt}/$SAVE_PATH"|tail -1|awk '{print $4}')
893e0b
}
893e0b
893e0b
#Function: get_raw_size
893e0b
#$1=dump target
893e0b
get_raw_size() {
893e0b
        echo -n $(fdisk -s "$1")
893e0b
}
893e0b
893e0b
#Function: check_size
893e0b
#$1: dump type string ('raw', 'fs', 'ssh')
893e0b
#$2: dump target
893e0b
check_size() {
893e0b
    local avail memtotal
893e0b
893e0b
    memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
893e0b
    case "$1" in
893e0b
        raw)
893e0b
            avail=$(get_raw_size "$2")
893e0b
            ;;
893e0b
        ssh)
893e0b
            avail=$(get_ssh_size "$2")
893e0b
            ;;
893e0b
        fs)
893e0b
            avail=$(get_fs_size "$2")
893e0b
            ;;
893e0b
        *)
893e0b
            return
893e0b
    esac
893e0b
893e0b
    if [ $? -ne 0 ]; then
893e0b
            perror_exit "Check dump target size failed"
893e0b
    fi
893e0b
893e0b
    if [ $avail -lt $memtotal ]; then
8f4abc
        dwarn "Warning: There might not be enough space to save a vmcore."
8f4abc
        dwarn "         The size of $2 should be greater than $memtotal kilo bytes."
893e0b
    fi
893e0b
}
893e0b
f8bec6
check_save_path_fs()
f8bec6
{
f8bec6
    local _path=$1
f8bec6
f8bec6
    if [ ! -d $_path ]; then
f8bec6
        perror_exit "Dump path $_path does not exist."
f8bec6
    fi
f8bec6
}
f8bec6
f8bec6
check_user_configured_target()
f8bec6
{
f8bec6
    local _target=$1 _cfg_fs_type=$2 _mounted
f8bec6
    local _mnt=$(get_mntpoint_from_target $_target)
f8bec6
    local _opt=$(get_mntopt_from_target $_target)
f8bec6
    local _fstype=$(get_fs_type_from_target $_target)
f8bec6
f8bec6
    if [ -n "$_fstype" ]; then
f8bec6
        # In case of nfs4, nfs should be used instead, nfs* options is deprecated in kdump.conf
f8bec6
        [[ $_fstype = "nfs"* ]] && _fstype=nfs
f8bec6
f8bec6
        if [ -n "$_cfg_fs_type" ] && [ "$_fstype" != "$_cfg_fs_type" ]; then
f8bec6
            perror_exit "\"$_target\" have a wrong type config \"$_cfg_fs_type\", expected \"$_fstype\""
f8bec6
        fi
f8bec6
    else
f8bec6
        _fstype="$_cfg_fs_type"
f8bec6
        _fstype="$_cfg_fs_type"
f8bec6
    fi
f8bec6
f8bec6
    # For noauto mount, mount it inplace with default value.
f8bec6
    # Else use the temporary target directory
f8bec6
    if [ -n "$_mnt" ]; then
f8bec6
        if ! is_mounted "$_mnt"; then
f8bec6
            if [[ $_opt  = *",noauto"* ]]; then
f8bec6
                mount $_mnt
f8bec6
                [ $? -ne 0 ] && perror_exit "Failed to mount $_target on $_mnt for kdump preflight check."
f8bec6
                _mounted=$_mnt
f8bec6
            else
f8bec6
                perror_exit "Dump target \"$_target\" is neither mounted nor configured as \"noauto\""
f8bec6
            fi
f8bec6
        fi
f8bec6
    else
f8bec6
        _mnt=$MKDUMPRD_TMPMNT
f8bec6
        mkdir -p $_mnt
f8bec6
        mount $_target $_mnt -t $_fstype -o defaults
f8bec6
        [ $? -ne 0 ] && perror_exit "Failed to mount $_target for kdump preflight check."
f8bec6
        _mounted=$_mnt
f8bec6
    fi
f8bec6
f8bec6
    # For user configured target, use $SAVE_PATH as the dump path within the target
f8bec6
    if [ ! -d "$_mnt/$SAVE_PATH" ]; then
063c48
        perror_exit "Dump path \"$SAVE_PATH\" does not exist in dump target \"$_target\""
f8bec6
    fi
f8bec6
f8bec6
    check_size fs "$_target"
f8bec6
f8bec6
    # Unmount it early, if function is interrupted and didn't reach here, the shell trap will clear it up anyway
f8bec6
    if [ -n "$_mounted" ]; then
f8bec6
        umount -f -- $_mounted
f8bec6
    fi
f8bec6
}
f8bec6
893e0b
# $1: core_collector config value
893e0b
verify_core_collector() {
f8bec6
    local _cmd="${1%% *}"
f8bec6
    local _params="${1#* }"
f8bec6
f8bec6
    if [ "$_cmd" != "makedumpfile" ]; then
f8bec6
        if is_raw_dump_target; then
8f4abc
            dwarn "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
f8bec6
        fi
f8bec6
        return
893e0b
    fi
f8bec6
893e0b
    if is_ssh_dump_target || is_raw_dump_target; then
f8bec6
        if ! strstr "$_params" "-F"; then
f8bec6
            perror_exit "The specified dump target needs makedumpfile \"-F\" option."
893e0b
        fi
f8bec6
        _params="$_params vmcore"
f8bec6
    else
f8bec6
        _params="$_params vmcore dumpfile"
f8bec6
    fi
f8bec6
f8bec6
    if ! $_cmd --check-params $_params; then
f8bec6
        perror_exit "makedumpfile parameter check failed."
893e0b
    fi
893e0b
}
893e0b
893e0b
add_mount() {
f8bec6
    local _mnt=$(to_mount $@)
893e0b
893e0b
    if [ $? -ne 0 ]; then
893e0b
        exit 1
893e0b
    fi
893e0b
893e0b
    add_dracut_mount "$_mnt"
893e0b
}
893e0b
893e0b
#handle the case user does not specify the dump target explicitly
893e0b
handle_default_dump_target()
893e0b
{
893e0b
    local _target
893e0b
    local _mntpoint
893e0b
893e0b
    is_user_configured_dump_target && return
893e0b
893e0b
    check_save_path_fs $SAVE_PATH
893e0b
f8bec6
    _save_path=$(get_bind_mount_source $SAVE_PATH)
f8bec6
    _target=$(get_target_from_path $_save_path)
f8bec6
    _mntpoint=$(get_mntpoint_from_target $_target)
893e0b
f8bec6
    SAVE_PATH=${_save_path##"$_mntpoint"}
893e0b
    add_mount "$_target"
893e0b
    check_size fs $_target
893e0b
}
893e0b
893e0b
get_override_resettable()
893e0b
{
893e0b
    local override_resettable
893e0b
893e0b
    override_resettable=$(grep "^override_resettable" $conf_file)
893e0b
    if [ -n "$override_resettable" ]; then
893e0b
        OVERRIDE_RESETTABLE=$(echo $override_resettable | cut -d' '  -f2)
893e0b
        if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ];then
893e0b
            perror_exit "override_resettable value $OVERRIDE_RESETTABLE is invalid"
893e0b
        fi
893e0b
    fi
893e0b
}
893e0b
893e0b
# $1: function name
893e0b
for_each_block_target()
893e0b
{
893e0b
    local dev majmin
893e0b
893e0b
    for dev in $(get_kdump_targets); do
893e0b
        [ -b "$dev" ] || continue
893e0b
        majmin=$(get_maj_min $dev)
893e0b
        check_block_and_slaves $1 $majmin && return 1
893e0b
    done
893e0b
893e0b
    return 0
893e0b
}
893e0b
893e0b
#judge if a specific device with $1 is unresettable
893e0b
#return false if unresettable.
893e0b
is_unresettable()
893e0b
{
893e0b
    local path="/sys/$(udevadm info --query=all --path=/sys/dev/block/$1 | awk '/^P:/ {print $2}' | sed -e 's/\(cciss[0-9]\+\/\).*/\1/g' -e 's/\/block\/.*$//')/resettable"
893e0b
    local resettable=1
893e0b
893e0b
    if [ -f "$path" ]
893e0b
    then
893e0b
        resettable="$(cat $path)"
893e0b
        [ $resettable -eq 0 -a "$OVERRIDE_RESETTABLE" -eq 0 ] && {
893e0b
            local device=$(udevadm info --query=all --path=/sys/dev/block/$1 | awk -F= '/DEVNAME/{print $2}')
8f4abc
            derror "Error: Can not save vmcore because device $device is unresettable"
893e0b
            return 0
893e0b
        }
893e0b
    fi
893e0b
893e0b
    return 1
893e0b
}
893e0b
893e0b
#check if machine is resettable.
893e0b
#return true if resettable
893e0b
check_resettable()
893e0b
{
893e0b
    local _ret _target
893e0b
893e0b
    get_override_resettable
893e0b
893e0b
    for_each_block_target is_unresettable
893e0b
    _ret=$?
893e0b
893e0b
    [ $_ret -eq 0 ] && return
893e0b
893e0b
    return 1
893e0b
}
893e0b
893e0b
check_crypt()
893e0b
{
d43fe6
    local _dev
893e0b
d43fe6
    for _dev in $(get_kdump_targets); do
d43fe6
        if [[ -n $(get_luks_crypt_dev "$(get_maj_min "$_dev")") ]]; then
d43fe6
            derror "Device $_dev is encrypted." && return 1
d43fe6
        fi
d43fe6
    done
893e0b
}
893e0b
893e0b
if ! check_resettable; then
893e0b
    exit 1
893e0b
fi
893e0b
893e0b
if ! check_crypt; then
8f4abc
    dwarn "Warning: Encrypted device is in dump path. User will prompted for password during second kernel boot." 
893e0b
fi
893e0b
893e0b
# firstly get right SSH_KEY_LOCATION
893e0b
keyfile=$(awk '/^sshkey/ {print $2}' $conf_file)
893e0b
if [ -f "$keyfile" ]; then
893e0b
    # canonicalize the path
893e0b
    SSH_KEY_LOCATION=$(/usr/bin/readlink -m $keyfile)
893e0b
fi
893e0b
893e0b
while read config_opt config_val;
893e0b
do
893e0b
    # remove inline comments after the end of a directive.
893e0b
    case "$config_opt" in
893e0b
    extra_modules)
893e0b
        extra_modules="$extra_modules $config_val"
893e0b
        ;;
893e0b
    ext[234]|xfs|btrfs|minix|nfs)
f8bec6
        check_user_configured_target "$config_val" "$config_opt"
f8bec6
        add_mount "$config_val" "$config_opt"
893e0b
        ;;
893e0b
    raw)
f8bec6
        # checking raw disk writable
893e0b
        dd if=$config_val count=1 of=/dev/null > /dev/null 2>&1 || {
893e0b
            perror_exit "Bad raw disk $config_val"
893e0b
        }
f8bec6
        _praw=$(persistent_policy="by-id" kdump_get_persistent_dev $config_val)
893e0b
        if [ -z "$_praw" ]; then
893e0b
            exit 1
893e0b
        fi
893e0b
        add_dracut_arg "--device" "$_praw"
893e0b
        check_size raw $config_val
893e0b
        ;;
893e0b
    ssh)
893e0b
        if strstr "$config_val" "@";
893e0b
        then
893e0b
            mkdir_save_path_ssh $config_val
7a865b
            check_size ssh $config_val
7a865b
            add_dracut_sshkey "$SSH_KEY_LOCATION"
893e0b
        else
893e0b
            perror_exit "Bad ssh dump target $config_val"
893e0b
        fi
893e0b
        ;;
893e0b
    core_collector)
893e0b
        verify_core_collector "$config_val"
893e0b
        ;;
893e0b
    dracut_args)
893e0b
        add_dracut_arg $config_val
893e0b
        ;;
893e0b
    *)
893e0b
        ;;
893e0b
    esac
603de6
done <<< "$(read_strip_comments $conf_file)"
893e0b
893e0b
handle_default_dump_target
893e0b
893e0b
if [ -n "$extra_modules" ]
893e0b
then
f8bec6
    add_dracut_arg "--add-drivers" \"$extra_modules\"
893e0b
fi
893e0b
10c66c
# TODO: The below check is not needed anymore with the introduction of
10c66c
#       'zz-fadumpinit' module, that isolates fadump's capture kernel initrd,
10c66c
#       but still sysroot.mount unit gets generated based on 'root=' kernel
10c66c
#       parameter available in fadump case. So, find a way to fix that first
10c66c
#       before removing this check.
893e0b
if ! is_fadump_capable; then
893e0b
    # The 2nd rootfs mount stays behind the normal dump target mount,
893e0b
    # so it doesn't affect the logic of check_dump_fs_modified().
893e0b
    is_dump_to_rootfs && add_mount "$(to_dev_name $(get_root_fs_device))"
893e0b
893e0b
    add_dracut_arg "--no-hostonly-default-device"
893e0b
fi
893e0b
f8bec6
echo "$dracut_args $@" | xargs dracut