b9e861
#!/bin/bash --norc
b9e861
# New mkdumprd
b9e861
#
b9e861
# Copyright 2011 Red Hat, Inc.
b9e861
#
b9e861
# Written by Cong Wang <amwang@redhat.com>
b9e861
#
b9e861
fe2ad6
if [ -f /etc/sysconfig/kdump ]; then
fe2ad6
	. /etc/sysconfig/kdump
fe2ad6
fi
fe2ad6
b9e861
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
b9e861
. $dracutbasedir/dracut-functions.sh
b9e861
. /lib/kdump/kdump-lib.sh
fe2ad6
. /lib/kdump/kdump-logger.sh
b9e861
export IN_KDUMP=1
b9e861
fe2ad6
#initiate the kdump logger
fe2ad6
dlog_init
fe2ad6
if [ $? -ne 0 ]; then
fe2ad6
	echo "failed to initiate the kdump logger."
fe2ad6
	exit 1
fe2ad6
fi
fe2ad6
b9e861
conf_file="/etc/kdump.conf"
b9e861
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
73ea9d
SAVE_PATH=$(get_save_path)
73ea9d
OVERRIDE_RESETTABLE=0
73ea9d
73ea9d
extra_modules=""
fe2ad6
dracut_args="--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o \"plymouth dash resume ifcfg earlykdump\""
73ea9d
73ea9d
readonly MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
73ea9d
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
73ea9d
readonly MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"
73ea9d
73ea9d
trap '
73ea9d
    ret=$?;
73ea9d
    is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
73ea9d
    [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKDUMPRD_TMPDIR";
73ea9d
    exit $ret;
73ea9d
    ' EXIT
73ea9d
73ea9d
# clean up after ourselves no matter how we die.
73ea9d
trap 'exit 1;' SIGINT
b9e861
b9e861
add_dracut_arg() {
73ea9d
    dracut_args="$dracut_args $@"
b9e861
}
b9e861
b9e861
add_dracut_mount() {
73ea9d
    add_dracut_arg "--mount" "\"$1\""
b9e861
}
b9e861
b9e861
add_dracut_sshkey() {
73ea9d
    add_dracut_arg "--sshkey" "\"$1\""
b9e861
}
b9e861
b9e861
# caller should ensure $1 is valid and mounted in 1st kernel
b9e861
to_mount() {
73ea9d
    local _target=$1 _fstype=$2 _options=$3 _new_mntpoint _pdev
73ea9d
73ea9d
    _new_mntpoint=$(get_kdump_mntpoint_from_target $_target)
73ea9d
    _fstype="${_fstype:-$(get_fs_type_from_target $_target)}"
73ea9d
    _options="${_options:-$(get_mntopt_from_target $_target)}"
73ea9d
    _options="${_options:-defaults}"
73ea9d
73ea9d
    if [[ "$_fstype" == "nfs"* ]]; then
73ea9d
        _pdev=$_target
73ea9d
        _options=$(echo $_options | sed 's/,addr=[^,]*//')
73ea9d
        _options=$(echo $_options | sed 's/,proto=[^,]*//')
73ea9d
        _options=$(echo $_options | sed 's/,clientaddr=[^,]*//')
b9e861
    else
73ea9d
        # for non-nfs _target converting to use udev persistent name
73ea9d
        _pdev="$(kdump_get_persistent_dev $_target)"
b9e861
        if [ -z "$_pdev" ]; then
b9e861
            return 1
b9e861
        fi
b9e861
    fi
b9e861
73ea9d
    #mount fs target as rw in 2nd kernel
73ea9d
    _options=$(echo $_options | sed 's/\(^\|,\)ro\($\|,\)/\1rw\2/g')
73ea9d
    # with 'noauto' in fstab nfs and non-root disk mount will fail in 2nd
73ea9d
    # kernel, filter it out here.
73ea9d
    _options=$(echo $_options | sed 's/\(^\|,\)noauto\($\|,\)/\1/g')
73ea9d
    # use both nofail and x-systemd.before to ensure systemd will try best to
73ea9d
    # mount it before kdump starts, this is an attempt to improve robustness
73ea9d
    _options="$_options,nofail,x-systemd.before=initrd-fs.target"
b9e861
73ea9d
    echo "$_pdev $_new_mntpoint $_fstype $_options"
b9e861
}
b9e861
b9e861
#Function: get_ssh_size
b9e861
#$1=dump target
b9e861
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
b9e861
get_ssh_size() {
b9e861
    local _opt _out _size
b9e861
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
b9e861
    _out=$(ssh -q -n $_opt $1 "df -P $SAVE_PATH")
b9e861
    [ $? -ne 0 ] && {
b9e861
        perror_exit "checking remote ssh server available size failed."
b9e861
    }
b9e861
b9e861
    #ssh output removed the line break, so print field NF-2
b9e861
    _size=$(echo -n $_out| awk '{avail=NF-2; print $avail}')
b9e861
    echo -n $_size
b9e861
}
b9e861
b9e861
#mkdir if save path does not exist on ssh dump target
b9e861
#$1=ssh dump target
73ea9d
#caller should ensure write permission on $1:$SAVE_PATH
b9e861
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
b9e861
mkdir_save_path_ssh()
b9e861
{
b9e861
    local _opt _dir
b9e861
    _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
b9e861
    ssh -qn $_opt $1 mkdir -p $SAVE_PATH 2>&1 > /dev/null
b9e861
    _ret=$?
b9e861
    if [ $_ret -ne 0 ]; then
73ea9d
        perror_exit "mkdir failed on $1:$SAVE_PATH"
b9e861
    fi
b9e861
73ea9d
    #check whether user has write permission on $1:$SAVE_PATH
b9e861
    _dir=$(ssh -qn $_opt $1 mktemp -dqp $SAVE_PATH 2>/dev/null)
b9e861
    _ret=$?
b9e861
    if [ $_ret -ne 0 ]; then
73ea9d
        perror_exit "Could not create temporary directory on $1:$SAVE_PATH. Make sure user has write permission on destination"
b9e861
    fi
b9e861
    ssh -qn $_opt $1 rmdir $_dir
b9e861
b9e861
    return 0
b9e861
}
b9e861
b9e861
#Function: get_fs_size
b9e861
#$1=dump target
b9e861
get_fs_size() {
b9e861
    local _mnt=$(get_mntpoint_from_target $1)
b9e861
    echo -n $(df -P "${_mnt}/$SAVE_PATH"|tail -1|awk '{print $4}')
b9e861
}
b9e861
b9e861
#Function: get_raw_size
b9e861
#$1=dump target
b9e861
get_raw_size() {
b9e861
        echo -n $(fdisk -s "$1")
b9e861
}
b9e861
b9e861
#Function: check_size
b9e861
#$1: dump type string ('raw', 'fs', 'ssh')
b9e861
#$2: dump target
b9e861
check_size() {
b9e861
    local avail memtotal
b9e861
b9e861
    memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
b9e861
    case "$1" in
b9e861
        raw)
b9e861
            avail=$(get_raw_size "$2")
b9e861
            ;;
b9e861
        ssh)
b9e861
            avail=$(get_ssh_size "$2")
b9e861
            ;;
b9e861
        fs)
b9e861
            avail=$(get_fs_size "$2")
b9e861
            ;;
b9e861
        *)
b9e861
            return
b9e861
    esac
b9e861
b9e861
    if [ $? -ne 0 ]; then
b9e861
            perror_exit "Check dump target size failed"
b9e861
    fi
b9e861
b9e861
    if [ $avail -lt $memtotal ]; then
fe2ad6
        dwarn "Warning: There might not be enough space to save a vmcore."
fe2ad6
        dwarn "         The size of $2 should be greater than $memtotal kilo bytes."
b9e861
    fi
b9e861
}
b9e861
73ea9d
check_save_path_fs()
73ea9d
{
73ea9d
    local _path=$1
73ea9d
73ea9d
    if [ ! -d $_path ]; then
73ea9d
        perror_exit "Dump path $_path does not exist."
73ea9d
    fi
73ea9d
}
73ea9d
73ea9d
check_user_configured_target()
73ea9d
{
73ea9d
    local _target=$1 _cfg_fs_type=$2 _mounted
73ea9d
    local _mnt=$(get_mntpoint_from_target $_target)
73ea9d
    local _opt=$(get_mntopt_from_target $_target)
73ea9d
    local _fstype=$(get_fs_type_from_target $_target)
73ea9d
73ea9d
    if [ -n "$_fstype" ]; then
73ea9d
        # In case of nfs4, nfs should be used instead, nfs* options is deprecated in kdump.conf
73ea9d
        [[ $_fstype = "nfs"* ]] && _fstype=nfs
73ea9d
73ea9d
        if [ -n "$_cfg_fs_type" ] && [ "$_fstype" != "$_cfg_fs_type" ]; then
73ea9d
            perror_exit "\"$_target\" have a wrong type config \"$_cfg_fs_type\", expected \"$_fstype\""
73ea9d
        fi
73ea9d
    else
73ea9d
        _fstype="$_cfg_fs_type"
73ea9d
        _fstype="$_cfg_fs_type"
73ea9d
    fi
73ea9d
73ea9d
    # For noauto mount, mount it inplace with default value.
73ea9d
    # Else use the temporary target directory
73ea9d
    if [ -n "$_mnt" ]; then
73ea9d
        if ! is_mounted "$_mnt"; then
73ea9d
            if [[ $_opt  = *",noauto"* ]]; then
73ea9d
                mount $_mnt
73ea9d
                [ $? -ne 0 ] && perror_exit "Failed to mount $_target on $_mnt for kdump preflight check."
73ea9d
                _mounted=$_mnt
73ea9d
            else
73ea9d
                perror_exit "Dump target \"$_target\" is neither mounted nor configured as \"noauto\""
73ea9d
            fi
73ea9d
        fi
73ea9d
    else
73ea9d
        _mnt=$MKDUMPRD_TMPMNT
73ea9d
        mkdir -p $_mnt
73ea9d
        mount $_target $_mnt -t $_fstype -o defaults
73ea9d
        [ $? -ne 0 ] && perror_exit "Failed to mount $_target for kdump preflight check."
73ea9d
        _mounted=$_mnt
73ea9d
    fi
73ea9d
73ea9d
    # For user configured target, use $SAVE_PATH as the dump path within the target
73ea9d
    if [ ! -d "$_mnt/$SAVE_PATH" ]; then
73ea9d
        perror_exit "Dump path \"$SAVE_PATH\" does not exist in dump target \"$_target\""
73ea9d
    fi
73ea9d
73ea9d
    check_size fs "$_target"
73ea9d
73ea9d
    # Unmount it early, if function is interrupted and didn't reach here, the shell trap will clear it up anyway
73ea9d
    if [ -n "$_mounted" ]; then
73ea9d
        umount -f -- $_mounted
73ea9d
    fi
73ea9d
}
73ea9d
b9e861
# $1: core_collector config value
b9e861
verify_core_collector() {
73ea9d
    local _cmd="${1%% *}"
73ea9d
    local _params="${1#* }"
73ea9d
73ea9d
    if [ "$_cmd" != "makedumpfile" ]; then
73ea9d
        if is_raw_dump_target; then
fe2ad6
            dwarn "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
73ea9d
        fi
73ea9d
        return
b9e861
    fi
73ea9d
b9e861
    if is_ssh_dump_target || is_raw_dump_target; then
73ea9d
        if ! strstr "$_params" "-F"; then
73ea9d
            perror_exit "The specified dump target needs makedumpfile \"-F\" option."
b9e861
        fi
73ea9d
        _params="$_params vmcore"
73ea9d
    else
73ea9d
        _params="$_params vmcore dumpfile"
73ea9d
    fi
73ea9d
73ea9d
    if ! $_cmd --check-params $_params; then
73ea9d
        perror_exit "makedumpfile parameter check failed."
b9e861
    fi
b9e861
}
b9e861
b9e861
add_mount() {
73ea9d
    local _mnt=$(to_mount $@)
b9e861
b9e861
    if [ $? -ne 0 ]; then
b9e861
        exit 1
b9e861
    fi
b9e861
b9e861
    add_dracut_mount "$_mnt"
b9e861
}
b9e861
b9e861
#handle the case user does not specify the dump target explicitly
b9e861
handle_default_dump_target()
b9e861
{
b9e861
    local _target
b9e861
    local _mntpoint
b9e861
b9e861
    is_user_configured_dump_target && return
b9e861
b9e861
    check_save_path_fs $SAVE_PATH
b9e861
73ea9d
    _save_path=$(get_bind_mount_source $SAVE_PATH)
73ea9d
    _target=$(get_target_from_path $_save_path)
73ea9d
    _mntpoint=$(get_mntpoint_from_target $_target)
b9e861
73ea9d
    SAVE_PATH=${_save_path##"$_mntpoint"}
b9e861
    add_mount "$_target"
b9e861
    check_size fs $_target
b9e861
}
b9e861
b9e861
get_override_resettable()
b9e861
{
b9e861
    local override_resettable
b9e861
b9e861
    override_resettable=$(grep "^override_resettable" $conf_file)
b9e861
    if [ -n "$override_resettable" ]; then
b9e861
        OVERRIDE_RESETTABLE=$(echo $override_resettable | cut -d' '  -f2)
b9e861
        if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ];then
b9e861
            perror_exit "override_resettable value $OVERRIDE_RESETTABLE is invalid"
b9e861
        fi
b9e861
    fi
b9e861
}
b9e861
b9e861
b9e861
# $1: function name
b9e861
for_each_block_target()
b9e861
{
b9e861
    local dev majmin
b9e861
b9e861
    for dev in $(get_kdump_targets); do
b9e861
        [ -b "$dev" ] || continue
b9e861
        majmin=$(get_maj_min $dev)
b9e861
        check_block_and_slaves $1 $majmin && return 1
b9e861
    done
b9e861
b9e861
    return 0
b9e861
}
b9e861
b9e861
b9e861
b9e861
#judge if a specific device with $1 is unresettable
b9e861
#return false if unresettable.
b9e861
is_unresettable()
b9e861
{
b9e861
    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"
b9e861
    local resettable=1
b9e861
b9e861
    if [ -f "$path" ]
b9e861
    then
b9e861
        resettable="$(cat $path)"
b9e861
        [ $resettable -eq 0 -a "$OVERRIDE_RESETTABLE" -eq 0 ] && {
b9e861
            local device=$(udevadm info --query=all --path=/sys/dev/block/$1 | awk -F= '/DEVNAME/{print $2}')
fe2ad6
            derror "Error: Can not save vmcore because device $device is unresettable"
b9e861
            return 0
b9e861
        }
b9e861
    fi
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
#check if machine is resettable.
b9e861
#return true if resettable
b9e861
check_resettable()
b9e861
{
b9e861
    local _ret _target
b9e861
b9e861
    get_override_resettable
b9e861
b9e861
    for_each_block_target is_unresettable
b9e861
    _ret=$?
b9e861
b9e861
    [ $_ret -eq 0 ] && return
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
# $1: maj:min
b9e861
is_crypt()
b9e861
{
b9e861
    local majmin=$1 dev line ID_FS_TYPE=""
b9e861
b9e861
    line=$(udevadm info --query=property --path=/sys/dev/block/$majmin \
b9e861
            | grep "^ID_FS_TYPE")
b9e861
    eval "$line"
b9e861
    [[ "$ID_FS_TYPE" = "crypto_LUKS" ]] && {
b9e861
        dev=$(udevadm info --query=all --path=/sys/dev/block/$majmin | awk -F= '/DEVNAME/{print $2}')
fe2ad6
        derror "Device $dev is encrypted."
b9e861
        return 0
b9e861
    }
b9e861
    return 1
b9e861
}
b9e861
b9e861
check_crypt()
b9e861
{
b9e861
    local _ret _target
b9e861
b9e861
    for_each_block_target is_crypt
b9e861
    _ret=$?
b9e861
b9e861
    [ $_ret -eq 0 ] && return
b9e861
b9e861
    return 1
b9e861
}
b9e861
b9e861
if ! check_resettable; then
b9e861
    exit 1
b9e861
fi
b9e861
b9e861
if ! check_crypt; then
fe2ad6
    dwarn "Warning: Encrypted device is in dump path. User will prompted for password during second kernel boot." 
b9e861
fi
b9e861
b9e861
# firstly get right SSH_KEY_LOCATION
b9e861
keyfile=$(awk '/^sshkey/ {print $2}' $conf_file)
b9e861
if [ -f "$keyfile" ]; then
b9e861
    # canonicalize the path
b9e861
    SSH_KEY_LOCATION=$(/usr/bin/readlink -m $keyfile)
b9e861
fi
b9e861
b9e861
while read config_opt config_val;
b9e861
do
b9e861
    # remove inline comments after the end of a directive.
b9e861
    case "$config_opt" in
b9e861
    extra_modules)
b9e861
        extra_modules="$extra_modules $config_val"
b9e861
        ;;
b9e861
    ext[234]|xfs|btrfs|minix|nfs)
73ea9d
        check_user_configured_target "$config_val" "$config_opt"
73ea9d
        add_mount "$config_val" "$config_opt"
b9e861
        ;;
b9e861
    raw)
73ea9d
        # checking raw disk writable
b9e861
        dd if=$config_val count=1 of=/dev/null > /dev/null 2>&1 || {
b9e861
            perror_exit "Bad raw disk $config_val"
b9e861
        }
73ea9d
        _praw=$(persistent_policy="by-id" kdump_get_persistent_dev $config_val)
b9e861
        if [ -z "$_praw" ]; then
b9e861
            exit 1
b9e861
        fi
b9e861
        add_dracut_arg "--device" "$_praw"
b9e861
        check_size raw $config_val
b9e861
        ;;
b9e861
    ssh)
b9e861
        if strstr "$config_val" "@";
b9e861
        then
b9e861
            mkdir_save_path_ssh $config_val
bda30f
            check_size ssh $config_val
bda30f
            add_dracut_sshkey "$SSH_KEY_LOCATION"
b9e861
        else
b9e861
            perror_exit "Bad ssh dump target $config_val"
b9e861
        fi
b9e861
        ;;
b9e861
    core_collector)
b9e861
        verify_core_collector "$config_val"
b9e861
        ;;
b9e861
    dracut_args)
b9e861
        add_dracut_arg $config_val
b9e861
        ;;
b9e861
    *)
b9e861
        ;;
b9e861
    esac
b9e861
done <<< "$(read_strip_comments $conf_file)"
b9e861
b9e861
handle_default_dump_target
b9e861
b9e861
if [ -n "$extra_modules" ]
b9e861
then
73ea9d
    add_dracut_arg "--add-drivers" \"$extra_modules\"
b9e861
fi
b9e861
b9e861
if ! is_fadump_capable; then
b9e861
    # The 2nd rootfs mount stays behind the normal dump target mount,
b9e861
    # so it doesn't affect the logic of check_dump_fs_modified().
b9e861
    is_dump_to_rootfs && add_mount "$(to_dev_name $(get_root_fs_device))"
b9e861
b9e861
    add_dracut_arg "--no-hostonly-default-device"
b9e861
fi
b9e861
73ea9d
echo "$dracut_args $@" | xargs dracut
73ea9d
b9e861
_rc=$?
b9e861
sync
b9e861
exit $_rc