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