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