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)
ab224c
[ -z "$SAVE_PATH" ] && SAVE_PATH="/var/crash"
ab224c
extra_modules=""
765b01
dracut_args=("--hostonly" "--hostonly-cmdline" "-o" "plymouth dash")
ab224c
OVERRIDE_RESETTABLE=0
ab224c
ab224c
perror_exit() {
ab224c
    echo $@ >&2
ab224c
    exit 1
ab224c
}
ab224c
ab224c
perror() {
ab224c
    echo $@ >&2
ab224c
}
ab224c
ab224c
get_persistent_dev() {
ab224c
    local i _tmp _dev
ab224c
ab224c
    _dev=$(udevadm info --query=name --name="$1" 2>/dev/null)
ab224c
    [ -z "$_dev" ] && {
ab224c
        perror_exit "Kernel dev name of $1 is not found."
ab224c
    }
ab224c
ab224c
    for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do
ab224c
        _tmp=$(udevadm info --query=name --name="$i" 2>/dev/null)
ab224c
        if [ "$_tmp" = "$_dev" ]; then
ab224c
            echo $i
ab224c
            return
ab224c
        fi
ab224c
    done
ab224c
ab224c
    perror "WARNING: Persistent device name of $1 not found. Using $1 as dump target name"
ab224c
    echo $1
ab224c
}
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
target_is_root() {
ab224c
    local _t
ab224c
    _t=$(findmnt -k -n -r -o TARGET $1|sort|head -1)
ab224c
    [ "$_t" = "/" ]
ab224c
}
ab224c
ab224c
# caller should ensure $1 is valid and mounted in 1st kernel
ab224c
to_mount() {
ab224c
    local _dev=$1 _s _t _o _mntopts _pdev
ab224c
ab224c
    _s=$(findmnt -k -f -n -r -o SOURCE $_dev)
ab224c
    _t=$(findmnt -k -f -n -r -o TARGET,FSTYPE $_dev)
ab224c
    _o=$(findmnt -k -f -n -r -o OPTIONS $_dev)
ab224c
    _o=${_o/#ro/rw} #mount fs target as rw in 2nd kernel
ab224c
    _o="${_o},nofail" #with nofail set, systemd won't block for mount failure
ab224c
    _mntopts="$_t $_o"
ab224c
    #for non-nfs _dev converting to use udev persistent name
ab224c
    if [ -b "$_s" ]; then
ab224c
        _pdev="$(get_persistent_dev $_s)"
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
to_mount_point() {
ab224c
    echo $(findmnt -k -f -n -r -o TARGET $1)
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
ab224c
#mkdir if save path does not exist on dump target filesystem
ab224c
#$1=dump target
ab224c
#caller should ensure $1 is mounted
ab224c
mkdir_save_path_fs() {
ab224c
    local _mnt=$(to_mount_point $1)
ab224c
    local _remount="no"
ab224c
    local _ret
ab224c
ab224c
    [ ! -d ${_mnt}/$SAVE_PATH ] && {
ab224c
        if is_readonly_mount $1; then
ab224c
            echo "Mounting $1 as read-write for creating dump directory.."
ab224c
            mount -o remount,rw $1 || {
ab224c
                perror_exit "Mounting $1 as read-write failed."
ab224c
            }
ab224c
            _remount="yes"
ab224c
        fi
ab224c
        mkdir -p ${_mnt}/$SAVE_PATH
ab224c
        _ret=$?
ab224c
        [ "$_remount" = "yes" ] && {
ab224c
            echo "Remounting $1 as read-only."
ab224c
            mount -o remount,ro $1 || {
ab224c
                perror_exit "Remounting $1 as read-only failed."
ab224c
            }
ab224c
        }
ab224c
        [ $_ret -ne 0 ] && {
ab224c
            perror_exit "Creating ${_mnt}/$SAVE_PATH failed."
ab224c
        }
ab224c
    }
ab224c
}
ab224c
ab224c
#Function: get_fs_size
ab224c
#$1=dump target
ab224c
get_fs_size() {
ab224c
    local _mnt=$(to_mount_point $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() {
ab224c
    if ! target_is_root "$1"; then
ab224c
        local _mnt=$(to_mount "$1")
ab224c
        if [ $? -ne 0 ]; then
ab224c
            exit 1
ab224c
        fi
ab224c
        add_dracut_mount "$_mnt"
ab224c
    fi
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
ab224c
to_dev_name() {
ab224c
    local dev="${1//\"/}"
ab224c
ab224c
    case "$dev" in
ab224c
    UUID=*)
ab224c
        dev=`blkid -U "${dev#UUID=}"`
ab224c
        ;;
ab224c
    LABEL=*)
ab224c
        dev=`blkid -L "${dev#LABEL=}"`
ab224c
        ;;
ab224c
    esac
ab224c
    echo $dev
ab224c
}
ab224c
ab224c
get_block_dump_target()
ab224c
{
ab224c
    local _target
ab224c
ab224c
765b01
    _target=$(get_user_configured_dump_disk)
ab224c
    [ -n "$_target" ] && echo $(to_dev_name $_target) && return
ab224c
ab224c
    #get rootfs device name
765b01
    _target=$(get_root_fs_device)
ab224c
    [ -b "$_target" ] && echo $(to_dev_name $_target)
ab224c
}
ab224c
765b01
# If no dump disk is specified make sure /var/crash is not mounted on a
765b01
# separate disk.
765b01
check_block_dump_target()
765b01
{
765b01
    local _target
765b01
    local _mntpoint
765b01
765b01
    is_user_configured_dump_target && return
765b01
765b01
    _target=$(get_root_fs_device)
765b01
    if [ -b "$_target" ]; then
765b01
        mkdir -p $SAVE_PATH
765b01
        _mntpoint=`df $SAVE_PATH | tail -1 |  awk '{print $NF}'`
765b01
        if [ "$_mntpoint" != "/" ]; then
765b01
            perror "No dump target specified. Default dump target is rootfs block device."
765b01
            perror "But dump path $SAVE_PATH is not backed by rootfs block device. "
765b01
            perror_exit "Either explicitly specify a dump target or specify a dump path backed by rootfs block device"
765b01
        fi
765b01
    fi
765b01
}
765b01
ab224c
get_default_action_target()
ab224c
{
ab224c
    local _target
ab224c
    local _action=$(grep "^default" /etc/kdump.conf 2>/dev/null | awk '{print $2}')
ab224c
    if [ -n "$_action" ] && [ "$_action" = "dump_to_rootfs" ]; then
ab224c
        #get rootfs device name
ab224c
        _target=$(findmnt -k -f -n -o SOURCE /)
ab224c
        [ -b "$_target" ] && echo $(to_dev_name $_target)
ab224c
    fi
ab224c
    return
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
ab224c
    #check dump target
ab224c
    dev=$(get_block_dump_target)
ab224c
ab224c
    if [ -n "$dev" ]; then
ab224c
        majmin=$(get_maj_min $dev)
ab224c
        check_block_and_slaves $1 $majmin && return 1
ab224c
    fi
ab224c
ab224c
    #check rootfs when default action dump_to_rootfs is set
ab224c
    dev=$(get_default_action_target)
ab224c
    if [ -n "$dev" ]; then
ab224c
        majmin=$(get_maj_min $dev)
ab224c
        check_block_and_slaves $1 $majmin && return 2
ab224c
    fi
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}')
ab224c
            echo "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
    if [ $_ret -eq 1 ]; then
ab224c
        _target=$(get_block_dump_target)
ab224c
        perror "Can not save vmcore to target device $_target . This device can not be initialized in kdump kernel as it is not resettable"
ab224c
    elif [ $_ret -eq 2 ]; then
ab224c
        _target=$(get_default_action_target)
ab224c
        perror "Rootfs device $_target is not resettable, can not be used as the default target, please specify a default action"
ab224c
    fi
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
check_block_dump_target
765b01
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
ab224c
        if [ "$config_opt" = "nfs" ]; then
ab224c
            add_dracut_module "nfs"
ab224c
        fi
ab224c
        add_mount "$config_val"
ab224c
        mkdir_save_path_fs $config_val
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
        }
ab224c
        _praw=$(get_persistent_dev $config_val)
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
ab224c
if [ -n "$extra_modules" ]
ab224c
then
ab224c
    add_dracut_arg "--add-drivers" "$extra_modules"
ab224c
fi
ab224c
ab224c
dracut "${dracut_args[@]}" "$@"
ab224c
_rc=$?
ab224c
sync
ab224c
exit $_rc