b9e861
#!/bin/bash --norc
b9e861
# New mkdumprd
b9e861
#
b9e861
# Copyright 2011 Red Hat, Inc.
b9e861
#
b9e861
# Written by Cong Wang <amwang@redhat.com>
b9e861
#
b9e861
b9e861
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
b9e861
. $dracutbasedir/dracut-functions.sh
b9e861
. /lib/kdump/kdump-lib.sh
b9e861
export IN_KDUMP=1
b9e861
b9e861
conf_file="/etc/kdump.conf"
b9e861
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
b9e861
SAVE_PATH=$(awk '/^path/ {print $2}' $conf_file)
b9e861
[ -z "$SAVE_PATH" ] && SAVE_PATH=$DEFAULT_PATH
b9e861
# strip the duplicated "/"
b9e861
SAVE_PATH=$(echo $SAVE_PATH | tr -s /)
b9e861
b9e861
is_wdt_addition_needed() {
b9e861
	local active
b9e861
b9e861
	is_wdt_mod_omitted
b9e861
	[[ $? -eq 0 ]] && return 1
b9e861
	[[ -d /sys/class/watchdog/ ]] || return 1
b9e861
	for dir in /sys/class/watchdog/*; do
b9e861
		[[ -f "$dir/state" ]] || continue
b9e861
		active=$(< "$dir/state")
b9e861
		[[ "$active" =  "active" ]] && return 0
b9e861
	done
b9e861
	return 1
b9e861
}
b9e861
b9e861
WDTCFG=""
b9e861
is_wdt_addition_needed
b9e861
[[ $? -eq 0 ]] && WDTCFG="-a watchdog"
b9e861
b9e861
extra_modules=""
b9e861
dracut_args=("--quiet" "--hostonly" "--hostonly-cmdline" "--hostonly-i18n" "--hostonly-mode" "strict" "-o" "plymouth dash resume ifcfg earlykdump" $WDTCFG)
b9e861
OVERRIDE_RESETTABLE=0
b9e861
b9e861
add_dracut_arg() {
b9e861
    local arg qarg is_quoted=0
b9e861
    while [ $# -gt 0 ];
b9e861
    do
b9e861
        arg="${1//\'/\"}"
b9e861
        #Handle quoted substring properly for passing it to dracut_args array.
b9e861
        if [ $is_quoted -eq 0 ]; then
b9e861
            if [[ "$arg" == "\"" ]] || [[ $arg != ${arg#\"} ]]; then
b9e861
                is_quoted=1
b9e861
                arg=${arg#\"}
b9e861
            fi
b9e861
        fi
b9e861
        if [ $is_quoted -eq 1 ]; then
b9e861
            qarg="$qarg $arg"
b9e861
            if [[ "$arg" == "\"" ]] || [[ $arg != ${arg%\"} ]]; then
b9e861
                is_quoted=0
b9e861
                arg=${qarg%\"}
b9e861
                qarg=""
b9e861
            else
b9e861
                shift
b9e861
                continue
b9e861
            fi
b9e861
        fi
b9e861
        dracut_args+=("$arg")
b9e861
        shift
b9e861
    done
b9e861
}
b9e861
b9e861
add_dracut_module() {
b9e861
    add_dracut_arg "--add" "$1"
b9e861
}
b9e861
b9e861
add_dracut_mount() {
b9e861
    add_dracut_arg "--mount" "$1"
b9e861
}
b9e861
b9e861
add_dracut_sshkey() {
b9e861
    add_dracut_arg "--sshkey" "$1"
b9e861
}
b9e861
b9e861
# caller should ensure $1 is valid and mounted in 1st kernel
b9e861
to_mount() {
b9e861
    local _dev=$1 _source _target _fstype _options _mntopts _pdev
b9e861
b9e861
    _source=$(findmnt -k -f -n -r -o SOURCE $_dev)
b9e861
    _target=$(get_mntpoint_from_target $_dev)
b9e861
    # mount under /sysroot if dump to root disk or mount under
b9e861
    #/kdumproot/$_target in other cases in 2nd kernel. systemd
b9e861
    #will be in charge to umount it.
b9e861
b9e861
    if [ "$_target" = "/" ];then
b9e861
        _target="/sysroot"
b9e861
    else
b9e861
        _target="/kdumproot/$_target"
b9e861
    fi
b9e861
b9e861
    _fstype=$(findmnt -k -f -n -r -o FSTYPE $_dev)
b9e861
    [[ -e /etc/fstab ]] && _options=$(findmnt --fstab -f -n -r -o OPTIONS $_dev)
b9e861
    if [ -z "$_options" ]; then
b9e861
        _options=$(findmnt -k -f -n -r -o OPTIONS $_dev)
b9e861
        if [[ $_fstype == "nfs"* ]]; then
b9e861
            _options=$(echo $_options | sed 's/,addr=[^,]*//')
b9e861
            _options=$(echo $_options | sed 's/,proto=[^,]*//')
b9e861
            _options=$(echo $_options | sed 's/,clientaddr=[^,]*//')
b9e861
        fi
b9e861
    fi
b9e861
    #mount fs target as rw in 2nd kernel
b9e861
    _options=$(echo $_options | sed 's/\(^\|,\)ro\($\|,\)/\1rw\2/g')
bda30f
    # filter out 'noauto' here, it will be force appended later, avoid duplication
b9e861
    _options=$(echo $_options | sed 's/\(^\|,\)noauto\($\|,\)/\1/g')
bda30f
    # only mount the dump target when needed.
bda30f
    _options="$_options,noauto"
b9e861
b9e861
    _mntopts="$_target $_fstype $_options"
b9e861
    #for non-nfs _dev converting to use udev persistent name
b9e861
    if [ -b "$_source" ]; then
b9e861
        _pdev="$(get_persistent_dev $_source)"
b9e861
        if [ -z "$_pdev" ]; then
b9e861
            return 1
b9e861
        fi
b9e861
b9e861
    else
b9e861
        _pdev=$_dev
b9e861
    fi
b9e861
b9e861
    echo "$_pdev $_mntopts"
b9e861
}
b9e861
b9e861
is_readonly_mount() {
b9e861
    local _mnt
b9e861
    _mnt=$(findmnt -k -f -n -r -o OPTIONS $1)
b9e861
b9e861
    #fs/proc_namespace.c: show_mountinfo():
b9e861
    #seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
b9e861
    [[ "$_mnt" =~ ^ro ]]
b9e861
}
b9e861
b9e861
#Function: get_ssh_size
b9e861
#$1=dump target
b9e861
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
b9e861
get_ssh_size() {
b9e861
    local _opt _out _size
b9e861
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
b9e861
    _out=$(ssh -q -n $_opt $1 "df -P $SAVE_PATH")
b9e861
    [ $? -ne 0 ] && {
b9e861
        perror_exit "checking remote ssh server available size failed."
b9e861
    }
b9e861
b9e861
    #ssh output removed the line break, so print field NF-2
b9e861
    _size=$(echo -n $_out| awk '{avail=NF-2; print $avail}')
b9e861
    echo -n $_size
b9e861
}
b9e861
b9e861
#mkdir if save path does not exist on ssh dump target
b9e861
#$1=ssh dump target
b9e861
#caller should ensure write permission on $DUMP_TARGET:$SAVE_PATH
b9e861
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
b9e861
mkdir_save_path_ssh()
b9e861
{
b9e861
    local _opt _dir
b9e861
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
b9e861
    ssh -qn $_opt $1 mkdir -p $SAVE_PATH 2>&1 > /dev/null
b9e861
    _ret=$?
b9e861
    if [ $_ret -ne 0 ]; then
b9e861
        perror_exit "mkdir failed on $DUMP_TARGET:$SAVE_PATH"
b9e861
    fi
b9e861
b9e861
    #check whether user has write permission on $SAVE_PATH/$DUMP_TARGET
b9e861
    _dir=$(ssh -qn $_opt $1 mktemp -dqp $SAVE_PATH 2>/dev/null)
b9e861
    _ret=$?
b9e861
    if [ $_ret -ne 0 ]; then
b9e861
        perror_exit "Could not create temporary directory on $DUMP_TARGET:$SAVE_PATH. Make sure user has write permission on destination"
b9e861
    fi
b9e861
    ssh -qn $_opt $1 rmdir $_dir
b9e861
b9e861
    return 0
b9e861
}
b9e861
b9e861
#Function: get_fs_size
b9e861
#$1=dump target
b9e861
get_fs_size() {
b9e861
    local _mnt=$(get_mntpoint_from_target $1)
b9e861
    echo -n $(df -P "${_mnt}/$SAVE_PATH"|tail -1|awk '{print $4}')
b9e861
}
b9e861
b9e861
#Function: get_raw_size
b9e861
#$1=dump target
b9e861
get_raw_size() {
b9e861
        echo -n $(fdisk -s "$1")
b9e861
}
b9e861
b9e861
#Function: check_size
b9e861
#$1: dump type string ('raw', 'fs', 'ssh')
b9e861
#$2: dump target
b9e861
check_size() {
b9e861
    local avail memtotal
b9e861
b9e861
    memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
b9e861
    case "$1" in
b9e861
        raw)
b9e861
            avail=$(get_raw_size "$2")
b9e861
            ;;
b9e861
        ssh)
b9e861
            avail=$(get_ssh_size "$2")
b9e861
            ;;
b9e861
        fs)
b9e861
            avail=$(get_fs_size "$2")
b9e861
            ;;
b9e861
        *)
b9e861
            return
b9e861
    esac
b9e861
b9e861
    if [ $? -ne 0 ]; then
b9e861
            perror_exit "Check dump target size failed"
b9e861
    fi
b9e861
b9e861
    if [ $avail -lt $memtotal ]; then
b9e861
        echo "Warning: There might not be enough space to save a vmcore."
b9e861
        echo "         The size of $2 should be greater than $memtotal kilo bytes."
b9e861
    fi
b9e861
}
b9e861
b9e861
# $1: core_collector config value
b9e861
verify_core_collector() {
b9e861
    if grep -q "^raw" $conf_file && [ "${1%% *}" != "makedumpfile" ]; then
b9e861
        echo "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
b9e861
    fi
b9e861
    if is_ssh_dump_target || is_raw_dump_target; then
b9e861
        if [ "${1%% *}" = "makedumpfile" ]; then
b9e861
            ! strstr "$1" "-F" && {
b9e861
                perror_exit "The specified dump target needs makedumpfile \"-F\" option."
b9e861
            }
b9e861
        fi
b9e861
    fi
b9e861
}
b9e861
b9e861
add_mount() {
b9e861
    local _mnt=$(to_mount "$1")
b9e861
b9e861
    if [ $? -ne 0 ]; then
b9e861
        exit 1
b9e861
    fi
b9e861
b9e861
    add_dracut_mount "$_mnt"
b9e861
}
b9e861
b9e861
#handle the case user does not specify the dump target explicitly
b9e861
handle_default_dump_target()
b9e861
{
b9e861
    local _target
b9e861
    local _mntpoint
b9e861
b9e861
    is_user_configured_dump_target && return
b9e861
b9e861
    check_save_path_fs $SAVE_PATH
b9e861
b9e861
    _mntpoint=$(get_mntpoint_from_path $SAVE_PATH)
b9e861
    _target=$(get_target_from_path $SAVE_PATH)
b9e861
b9e861
    if is_atomic && is_bind_mount $_mntpoint; then
b9e861
        SAVE_PATH=${SAVE_PATH##"$_mntpoint"}
b9e861
        # the real dump path in the 2nd kernel, if the mount point is bind mounted.
b9e861
        SAVE_PATH=$(get_bind_mount_directory $_mntpoint)/$SAVE_PATH
b9e861
        _mntpoint=$(get_mntpoint_from_target $_target)
b9e861
b9e861
        # the absolute path in the 1st kernel
b9e861
        SAVE_PATH=$_mntpoint/$SAVE_PATH
b9e861
    fi
b9e861
b9e861
    SAVE_PATH=${SAVE_PATH##"$_mntpoint"}
b9e861
    add_mount "$_target"
b9e861
    check_size fs $_target
b9e861
}
b9e861
b9e861
get_override_resettable()
b9e861
{
b9e861
    local override_resettable
b9e861
b9e861
    override_resettable=$(grep "^override_resettable" $conf_file)
b9e861
    if [ -n "$override_resettable" ]; then
b9e861
        OVERRIDE_RESETTABLE=$(echo $override_resettable | cut -d' '  -f2)
b9e861
        if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ];then
b9e861
            perror_exit "override_resettable value $OVERRIDE_RESETTABLE is invalid"
b9e861
        fi
b9e861
    fi
b9e861
}
b9e861
b9e861
b9e861
# $1: function name
b9e861
for_each_block_target()
b9e861
{
b9e861
    local dev majmin
b9e861
b9e861
    for dev in $(get_kdump_targets); do
b9e861
        [ -b "$dev" ] || continue
b9e861
        majmin=$(get_maj_min $dev)
b9e861
        check_block_and_slaves $1 $majmin && return 1
b9e861
    done
b9e861
b9e861
    return 0
b9e861
}
b9e861
b9e861
b9e861
b9e861
#judge if a specific device with $1 is unresettable
b9e861
#return false if unresettable.
b9e861
is_unresettable()
b9e861
{
b9e861
    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"
b9e861
    local resettable=1
b9e861
b9e861
    if [ -f "$path" ]
b9e861
    then
b9e861
        resettable="$(cat $path)"
b9e861
        [ $resettable -eq 0 -a "$OVERRIDE_RESETTABLE" -eq 0 ] && {
b9e861
            local device=$(udevadm info --query=all --path=/sys/dev/block/$1 | awk -F= '/DEVNAME/{print $2}')
b9e861
            echo "Error: Can not save vmcore because device $device is unresettable"
b9e861
            return 0
b9e861
        }
b9e861
    fi
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
#check if machine is resettable.
b9e861
#return true if resettable
b9e861
check_resettable()
b9e861
{
b9e861
    local _ret _target
b9e861
b9e861
    get_override_resettable
b9e861
b9e861
    for_each_block_target is_unresettable
b9e861
    _ret=$?
b9e861
b9e861
    [ $_ret -eq 0 ] && return
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
# $1: maj:min
b9e861
is_crypt()
b9e861
{
b9e861
    local majmin=$1 dev line ID_FS_TYPE=""
b9e861
b9e861
    line=$(udevadm info --query=property --path=/sys/dev/block/$majmin \
b9e861
            | grep "^ID_FS_TYPE")
b9e861
    eval "$line"
b9e861
    [[ "$ID_FS_TYPE" = "crypto_LUKS" ]] && {
b9e861
        dev=$(udevadm info --query=all --path=/sys/dev/block/$majmin | awk -F= '/DEVNAME/{print $2}')
b9e861
        echo "Device $dev is encrypted."
b9e861
        return 0
b9e861
    }
b9e861
    return 1
b9e861
}
b9e861
b9e861
check_crypt()
b9e861
{
b9e861
    local _ret _target
b9e861
b9e861
    for_each_block_target is_crypt
b9e861
    _ret=$?
b9e861
b9e861
    [ $_ret -eq 0 ] && return
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
if ! check_resettable; then
b9e861
    exit 1
b9e861
fi
b9e861
b9e861
if ! check_crypt; then
b9e861
    echo "Warning: Encrypted device is in dump path. User will prompted for password during second kernel boot." 
b9e861
fi
b9e861
b9e861
# firstly get right SSH_KEY_LOCATION
b9e861
keyfile=$(awk '/^sshkey/ {print $2}' $conf_file)
b9e861
if [ -f "$keyfile" ]; then
b9e861
    # canonicalize the path
b9e861
    SSH_KEY_LOCATION=$(/usr/bin/readlink -m $keyfile)
b9e861
fi
b9e861
b9e861
if [ "$(uname -m)" = "s390x" ]; then
b9e861
    add_dracut_module "znet"
b9e861
fi
b9e861
b9e861
while read config_opt config_val;
b9e861
do
b9e861
    # remove inline comments after the end of a directive.
b9e861
    case "$config_opt" in
b9e861
    extra_modules)
b9e861
        extra_modules="$extra_modules $config_val"
b9e861
        ;;
b9e861
    ext[234]|xfs|btrfs|minix|nfs)
b9e861
        if ! findmnt $config_val >/dev/null; then
b9e861
            perror_exit "Dump target $config_val is probably not mounted."
b9e861
        fi
b9e861
b9e861
        _absolute_save_path=$(make_absolute_save_path $config_val)
b9e861
        _mntpoint=$(get_mntpoint_from_path $_absolute_save_path)
b9e861
        if is_atomic && is_bind_mount $_mntpoint; then
b9e861
            SAVE_PATH=${_absolute_save_path##"$_mntpoint"}
b9e861
            # the real dump path in the 2nd kernel, if the mount point is bind mounted.
b9e861
            SAVE_PATH=$(get_bind_mount_directory $_mntpoint)/$SAVE_PATH
b9e861
        fi
b9e861
b9e861
        add_mount "$config_val"
b9e861
        check_save_path_fs $_absolute_save_path
b9e861
        check_size fs $config_val
b9e861
        ;;
b9e861
    raw)
b9e861
        #checking raw disk writable
b9e861
        dd if=$config_val count=1 of=/dev/null > /dev/null 2>&1 || {
b9e861
            perror_exit "Bad raw disk $config_val"
b9e861
        }
b9e861
        _praw=$(persistent_policy="by-id" get_persistent_dev $config_val)
b9e861
        if [ -z "$_praw" ]; then
b9e861
            exit 1
b9e861
        fi
b9e861
        add_dracut_arg "--device" "$_praw"
b9e861
        check_size raw $config_val
b9e861
        ;;
b9e861
    ssh)
b9e861
        if strstr "$config_val" "@";
b9e861
        then
b9e861
            mkdir_save_path_ssh $config_val
bda30f
            check_size ssh $config_val
b9e861
            add_dracut_module "ssh-client"
bda30f
            add_dracut_sshkey "$SSH_KEY_LOCATION"
b9e861
        else
b9e861
            perror_exit "Bad ssh dump target $config_val"
b9e861
        fi
b9e861
        ;;
b9e861
    core_collector)
b9e861
        verify_core_collector "$config_val"
b9e861
        ;;
b9e861
    dracut_args)
b9e861
        add_dracut_arg $config_val
b9e861
        ;;
b9e861
    *)
b9e861
        ;;
b9e861
    esac
b9e861
done <<< "$(read_strip_comments $conf_file)"
b9e861
b9e861
handle_default_dump_target
b9e861
b9e861
if [ -n "$extra_modules" ]
b9e861
then
b9e861
    add_dracut_arg "--add-drivers" "$extra_modules"
b9e861
fi
b9e861
b9e861
if ! is_fadump_capable; then
b9e861
    # The 2nd rootfs mount stays behind the normal dump target mount,
b9e861
    # so it doesn't affect the logic of check_dump_fs_modified().
b9e861
    is_dump_to_rootfs && add_mount "$(to_dev_name $(get_root_fs_device))"
b9e861
b9e861
    add_dracut_arg "--no-hostonly-default-device"
b9e861
fi
b9e861
b9e861
dracut "${dracut_args[@]}" "$@"
b9e861
_rc=$?
b9e861
sync
b9e861
exit $_rc