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
893e0b
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
893e0b
. $dracutbasedir/dracut-functions.sh
893e0b
. /lib/kdump/kdump-lib.sh
893e0b
export IN_KDUMP=1
893e0b
893e0b
conf_file="/etc/kdump.conf"
893e0b
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
603de6
SAVE_PATH=$(awk '/^path/ {print $2}' $conf_file)
893e0b
[ -z "$SAVE_PATH" ] && SAVE_PATH=$DEFAULT_PATH
893e0b
# strip the duplicated "/"
893e0b
SAVE_PATH=$(echo $SAVE_PATH | tr -s /)
893e0b
893e0b
is_wdt_addition_needed() {
893e0b
	local active
893e0b
893e0b
	is_wdt_mod_omitted
893e0b
	[[ $? -eq 0 ]] && return 1
893e0b
	[[ -d /sys/class/watchdog/ ]] || return 1
893e0b
	for dir in /sys/class/watchdog/*; do
893e0b
		[[ -f "$dir/state" ]] || continue
893e0b
		active=$(< "$dir/state")
893e0b
		[[ "$active" =  "active" ]] && return 0
893e0b
	done
893e0b
	return 1
893e0b
}
893e0b
893e0b
WDTCFG=""
893e0b
is_wdt_addition_needed
893e0b
[[ $? -eq 0 ]] && WDTCFG="-a watchdog"
893e0b
893e0b
extra_modules=""
603de6
dracut_args=("--quiet" "--hostonly" "--hostonly-cmdline" "--hostonly-i18n" "--hostonly-mode" "strict" "-o" "plymouth dash resume ifcfg earlykdump" $WDTCFG)
893e0b
OVERRIDE_RESETTABLE=0
893e0b
893e0b
add_dracut_arg() {
893e0b
    local arg qarg is_quoted=0
893e0b
    while [ $# -gt 0 ];
893e0b
    do
893e0b
        arg="${1//\'/\"}"
893e0b
        #Handle quoted substring properly for passing it to dracut_args array.
893e0b
        if [ $is_quoted -eq 0 ]; then
893e0b
            if [[ "$arg" == "\"" ]] || [[ $arg != ${arg#\"} ]]; then
893e0b
                is_quoted=1
893e0b
                arg=${arg#\"}
893e0b
            fi
893e0b
        fi
893e0b
        if [ $is_quoted -eq 1 ]; then
893e0b
            qarg="$qarg $arg"
893e0b
            if [[ "$arg" == "\"" ]] || [[ $arg != ${arg%\"} ]]; then
893e0b
                is_quoted=0
893e0b
                arg=${qarg%\"}
893e0b
                qarg=""
893e0b
            else
893e0b
                shift
893e0b
                continue
893e0b
            fi
893e0b
        fi
893e0b
        dracut_args+=("$arg")
893e0b
        shift
893e0b
    done
893e0b
}
893e0b
893e0b
add_dracut_module() {
893e0b
    add_dracut_arg "--add" "$1"
893e0b
}
893e0b
893e0b
add_dracut_mount() {
893e0b
    add_dracut_arg "--mount" "$1"
893e0b
}
893e0b
893e0b
add_dracut_sshkey() {
893e0b
    add_dracut_arg "--sshkey" "$1"
893e0b
}
893e0b
893e0b
# caller should ensure $1 is valid and mounted in 1st kernel
893e0b
to_mount() {
893e0b
    local _dev=$1 _source _target _fstype _options _mntopts _pdev
893e0b
893e0b
    _source=$(findmnt -k -f -n -r -o SOURCE $_dev)
893e0b
    _target=$(get_mntpoint_from_target $_dev)
893e0b
    # mount under /sysroot if dump to root disk or mount under
893e0b
    #/kdumproot/$_target in other cases in 2nd kernel. systemd
893e0b
    #will be in charge to umount it.
893e0b
893e0b
    if [ "$_target" = "/" ];then
893e0b
        _target="/sysroot"
893e0b
    else
893e0b
        _target="/kdumproot/$_target"
893e0b
    fi
893e0b
893e0b
    _fstype=$(findmnt -k -f -n -r -o FSTYPE $_dev)
893e0b
    [[ -e /etc/fstab ]] && _options=$(findmnt --fstab -f -n -r -o OPTIONS $_dev)
893e0b
    if [ -z "$_options" ]; then
893e0b
        _options=$(findmnt -k -f -n -r -o OPTIONS $_dev)
893e0b
        if [[ $_fstype == "nfs"* ]]; then
893e0b
            _options=$(echo $_options | sed 's/,addr=[^,]*//')
893e0b
            _options=$(echo $_options | sed 's/,proto=[^,]*//')
893e0b
            _options=$(echo $_options | sed 's/,clientaddr=[^,]*//')
893e0b
        fi
893e0b
    fi
893e0b
    #mount fs target as rw in 2nd kernel
893e0b
    _options=$(echo $_options | sed 's/\(^\|,\)ro\($\|,\)/\1rw\2/g')
8c3ceb
    # filter out 'noauto' here, it will be force appended later, avoid duplication
893e0b
    _options=$(echo $_options | sed 's/\(^\|,\)noauto\($\|,\)/\1/g')
8c3ceb
    # only mount the dump target when needed.
8c3ceb
    _options="$_options,noauto"
893e0b
893e0b
    _mntopts="$_target $_fstype $_options"
893e0b
    #for non-nfs _dev converting to use udev persistent name
893e0b
    if [ -b "$_source" ]; then
893e0b
        _pdev="$(get_persistent_dev $_source)"
893e0b
        if [ -z "$_pdev" ]; then
893e0b
            return 1
893e0b
        fi
893e0b
893e0b
    else
893e0b
        _pdev=$_dev
893e0b
    fi
893e0b
893e0b
    echo "$_pdev $_mntopts"
893e0b
}
893e0b
893e0b
is_readonly_mount() {
893e0b
    local _mnt
893e0b
    _mnt=$(findmnt -k -f -n -r -o OPTIONS $1)
893e0b
893e0b
    #fs/proc_namespace.c: show_mountinfo():
893e0b
    #seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
893e0b
    [[ "$_mnt" =~ ^ro ]]
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
893e0b
#caller should ensure write permission on $DUMP_TARGET:$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
893e0b
        perror_exit "mkdir failed on $DUMP_TARGET:$SAVE_PATH"
893e0b
    fi
893e0b
893e0b
    #check whether user has write permission on $SAVE_PATH/$DUMP_TARGET
893e0b
    _dir=$(ssh -qn $_opt $1 mktemp -dqp $SAVE_PATH 2>/dev/null)
893e0b
    _ret=$?
893e0b
    if [ $_ret -ne 0 ]; then
893e0b
        perror_exit "Could not create temporary directory on $DUMP_TARGET:$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
893e0b
        echo "Warning: There might not be enough space to save a vmcore."
893e0b
        echo "         The size of $2 should be greater than $memtotal kilo bytes."
893e0b
    fi
893e0b
}
893e0b
893e0b
# $1: core_collector config value
893e0b
verify_core_collector() {
893e0b
    if grep -q "^raw" $conf_file && [ "${1%% *}" != "makedumpfile" ]; then
893e0b
        echo "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
893e0b
    fi
893e0b
    if is_ssh_dump_target || is_raw_dump_target; then
893e0b
        if [ "${1%% *}" = "makedumpfile" ]; then
893e0b
            ! strstr "$1" "-F" && {
893e0b
                perror_exit "The specified dump target needs makedumpfile \"-F\" option."
893e0b
            }
893e0b
        fi
893e0b
    fi
893e0b
}
893e0b
893e0b
add_mount() {
893e0b
    local _mnt=$(to_mount "$1")
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
893e0b
    _mntpoint=$(get_mntpoint_from_path $SAVE_PATH)
893e0b
    _target=$(get_target_from_path $SAVE_PATH)
893e0b
893e0b
    if is_atomic && is_bind_mount $_mntpoint; then
893e0b
        SAVE_PATH=${SAVE_PATH##"$_mntpoint"}
893e0b
        # the real dump path in the 2nd kernel, if the mount point is bind mounted.
893e0b
        SAVE_PATH=$(get_bind_mount_directory $_mntpoint)/$SAVE_PATH
893e0b
        _mntpoint=$(get_mntpoint_from_target $_target)
893e0b
893e0b
        # the absolute path in the 1st kernel
893e0b
        SAVE_PATH=$_mntpoint/$SAVE_PATH
893e0b
    fi
893e0b
893e0b
    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
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
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}')
893e0b
            echo "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
# $1: maj:min
893e0b
is_crypt()
893e0b
{
893e0b
    local majmin=$1 dev line ID_FS_TYPE=""
893e0b
893e0b
    line=$(udevadm info --query=property --path=/sys/dev/block/$majmin \
893e0b
            | grep "^ID_FS_TYPE")
893e0b
    eval "$line"
893e0b
    [[ "$ID_FS_TYPE" = "crypto_LUKS" ]] && {
893e0b
        dev=$(udevadm info --query=all --path=/sys/dev/block/$majmin | awk -F= '/DEVNAME/{print $2}')
893e0b
        echo "Device $dev is encrypted."
893e0b
        return 0
893e0b
    }
893e0b
    return 1
893e0b
}
893e0b
893e0b
check_crypt()
893e0b
{
893e0b
    local _ret _target
893e0b
893e0b
    for_each_block_target is_crypt
893e0b
    _ret=$?
893e0b
893e0b
    [ $_ret -eq 0 ] && return
893e0b
893e0b
    return 1
893e0b
}
893e0b
893e0b
if ! check_resettable; then
893e0b
    exit 1
893e0b
fi
893e0b
893e0b
if ! check_crypt; then
893e0b
    echo "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
if [ "$(uname -m)" = "s390x" ]; then
893e0b
    add_dracut_module "znet"
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)
893e0b
        if ! findmnt $config_val >/dev/null; then
893e0b
            perror_exit "Dump target $config_val is probably not mounted."
893e0b
        fi
893e0b
893e0b
        _absolute_save_path=$(make_absolute_save_path $config_val)
893e0b
        _mntpoint=$(get_mntpoint_from_path $_absolute_save_path)
893e0b
        if is_atomic && is_bind_mount $_mntpoint; then
893e0b
            SAVE_PATH=${_absolute_save_path##"$_mntpoint"}
893e0b
            # the real dump path in the 2nd kernel, if the mount point is bind mounted.
893e0b
            SAVE_PATH=$(get_bind_mount_directory $_mntpoint)/$SAVE_PATH
893e0b
        fi
893e0b
893e0b
        add_mount "$config_val"
893e0b
        check_save_path_fs $_absolute_save_path
893e0b
        check_size fs $config_val
893e0b
        ;;
893e0b
    raw)
893e0b
        #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
        }
893e0b
        _praw=$(persistent_policy="by-id" 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
            check_size ssh $config_val
893e0b
            mkdir_save_path_ssh $config_val
893e0b
            add_dracut_module "ssh-client"
893e0b
		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
893e0b
    add_dracut_arg "--add-drivers" "$extra_modules"
893e0b
fi
893e0b
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
893e0b
dracut "${dracut_args[@]}" "$@"
893e0b
_rc=$?
893e0b
sync
893e0b
exit $_rc