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