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
54b5ae
if [ -f /etc/sysconfig/kdump ]; then
54b5ae
	. /etc/sysconfig/kdump
54b5ae
fi
54b5ae
b9e861
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
b9e861
. $dracutbasedir/dracut-functions.sh
b9e861
. /lib/kdump/kdump-lib.sh
fd10de
. /lib/kdump/kdump-logger.sh
b9e861
export IN_KDUMP=1
b9e861
54b5ae
#initiate the kdump logger
54b5ae
dlog_init
54b5ae
if [ $? -ne 0 ]; then
54b5ae
	echo "failed to initiate the kdump logger."
54b5ae
	exit 1
54b5ae
fi
54b5ae
b9e861
conf_file="/etc/kdump.conf"
b9e861
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
399b37
SAVE_PATH=$(get_save_path)
399b37
OVERRIDE_RESETTABLE=0
399b37
399b37
extra_modules=""
bb7919
dracut_args="--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o \"plymouth dash resume ifcfg earlykdump\""
399b37
399b37
readonly MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
399b37
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
399b37
readonly MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"
399b37
399b37
trap '
399b37
    ret=$?;
399b37
    is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
399b37
    [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKDUMPRD_TMPDIR";
399b37
    exit $ret;
399b37
    ' EXIT
399b37
399b37
# clean up after ourselves no matter how we die.
399b37
trap 'exit 1;' SIGINT
b9e861
b9e861
add_dracut_arg() {
399b37
    dracut_args="$dracut_args $@"
b9e861
}
b9e861
b9e861
add_dracut_mount() {
399b37
    add_dracut_arg "--mount" "\"$1\""
b9e861
}
b9e861
b9e861
add_dracut_sshkey() {
399b37
    add_dracut_arg "--sshkey" "\"$1\""
b9e861
}
b9e861
b9e861
# caller should ensure $1 is valid and mounted in 1st kernel
b9e861
to_mount() {
a6b1d2
    local _target=$1 _fstype=$2 _options=$3 _new_mntpoint _pdev
b9e861
399b37
    _new_mntpoint=$(get_kdump_mntpoint_from_target $_target)
399b37
    _fstype="${_fstype:-$(get_fs_type_from_target $_target)}"
399b37
    _options="${_options:-$(get_mntopt_from_target $_target)}"
399b37
    _options="${_options:-defaults}"
b9e861
399b37
    if [[ "$_fstype" == "nfs"* ]]; then
a6b1d2
        _pdev=$_target
399b37
        _options=$(echo $_options | sed 's/,addr=[^,]*//')
399b37
        _options=$(echo $_options | sed 's/,proto=[^,]*//')
399b37
        _options=$(echo $_options | sed 's/,clientaddr=[^,]*//')
a6b1d2
    else
a6b1d2
        # for non-nfs _target converting to use udev persistent name
a6b1d2
        _pdev="$(kdump_get_persistent_dev $_target)"
a6b1d2
        if [ -z "$_pdev" ]; then
a6b1d2
            return 1
a6b1d2
        fi
b9e861
    fi
b9e861
b9e861
    #mount fs target as rw in 2nd kernel
b9e861
    _options=$(echo $_options | sed 's/\(^\|,\)ro\($\|,\)/\1rw\2/g')
399b37
    # with 'noauto' in fstab nfs and non-root disk mount will fail in 2nd
399b37
    # kernel, filter it out here.
b9e861
    _options=$(echo $_options | sed 's/\(^\|,\)noauto\($\|,\)/\1/g')
fd10de
    # use both nofail and x-systemd.before to ensure systemd will try best to
fd10de
    # mount it before kdump starts, this is an attempt to improve robustness
fd10de
    _options="$_options,nofail,x-systemd.before=initrd-fs.target"
399b37
a6b1d2
    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
399b37
#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
399b37
        perror_exit "mkdir failed on $1:$SAVE_PATH"
b9e861
    fi
b9e861
399b37
    #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
399b37
        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
54b5ae
        dwarn "Warning: There might not be enough space to save a vmcore."
54b5ae
        dwarn "         The size of $2 should be greater than $memtotal kilo bytes."
b9e861
    fi
b9e861
}
b9e861
399b37
check_save_path_fs()
399b37
{
399b37
    local _path=$1
399b37
399b37
    if [ ! -d $_path ]; then
399b37
        perror_exit "Dump path $_path does not exist."
399b37
    fi
399b37
}
399b37
399b37
check_user_configured_target()
399b37
{
399b37
    local _target=$1 _cfg_fs_type=$2 _mounted
399b37
    local _mnt=$(get_mntpoint_from_target $_target)
399b37
    local _opt=$(get_mntopt_from_target $_target)
399b37
    local _fstype=$(get_fs_type_from_target $_target)
399b37
399b37
    if [ -n "$_fstype" ]; then
399b37
        # In case of nfs4, nfs should be used instead, nfs* options is deprecated in kdump.conf
399b37
        [[ $_fstype = "nfs"* ]] && _fstype=nfs
399b37
399b37
        if [ -n "$_cfg_fs_type" ] && [ "$_fstype" != "$_cfg_fs_type" ]; then
399b37
            perror_exit "\"$_target\" have a wrong type config \"$_cfg_fs_type\", expected \"$_fstype\""
399b37
        fi
399b37
    else
399b37
        _fstype="$_cfg_fs_type"
399b37
        _fstype="$_cfg_fs_type"
399b37
    fi
399b37
399b37
    # For noauto mount, mount it inplace with default value.
399b37
    # Else use the temporary target directory
399b37
    if [ -n "$_mnt" ]; then
399b37
        if ! is_mounted "$_mnt"; then
399b37
            if [[ $_opt  = *",noauto"* ]]; then
399b37
                mount $_mnt
399b37
                [ $? -ne 0 ] && perror_exit "Failed to mount $_target on $_mnt for kdump preflight check."
399b37
                _mounted=$_mnt
399b37
            else
a6b1d2
                perror_exit "Dump target \"$_target\" is neither mounted nor configured as \"noauto\""
399b37
            fi
399b37
        fi
399b37
    else
399b37
        _mnt=$MKDUMPRD_TMPMNT
399b37
        mkdir -p $_mnt
399b37
        mount $_target $_mnt -t $_fstype -o defaults
399b37
        [ $? -ne 0 ] && perror_exit "Failed to mount $_target for kdump preflight check."
399b37
        _mounted=$_mnt
399b37
    fi
399b37
399b37
    # For user configured target, use $SAVE_PATH as the dump path within the target
399b37
    if [ ! -d "$_mnt/$SAVE_PATH" ]; then
aa8f05
        perror_exit "Dump path \"$_mnt/$SAVE_PATH\" does not exist in dump target \"$_target\""
399b37
    fi
399b37
399b37
    check_size fs "$_target"
399b37
399b37
    # Unmount it early, if function is interrupted and didn't reach here, the shell trap will clear it up anyway
399b37
    if [ -n "$_mounted" ]; then
399b37
        umount -f -- $_mounted
399b37
    fi
399b37
}
399b37
b9e861
# $1: core_collector config value
b9e861
verify_core_collector() {
1a067e
    local _cmd="${1%% *}"
1a067e
    local _params="${1#* }"
1a067e
1a067e
    if [ "$_cmd" != "makedumpfile" ]; then
1a067e
        if is_raw_dump_target; then
54b5ae
            dwarn "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
1a067e
        fi
1a067e
        return
b9e861
    fi
1a067e
b9e861
    if is_ssh_dump_target || is_raw_dump_target; then
1a067e
        if ! strstr "$_params" "-F"; then
1a067e
            perror_exit "The specified dump target needs makedumpfile \"-F\" option."
b9e861
        fi
1a067e
        _params="$_params vmcore"
1a067e
    else
1a067e
        _params="$_params vmcore dumpfile"
1a067e
    fi
1a067e
1a067e
    if ! $_cmd --check-params $_params; then
1a067e
        perror_exit "makedumpfile parameter check failed."
b9e861
    fi
b9e861
}
b9e861
b9e861
add_mount() {
399b37
    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
399b37
    _save_path=$(get_bind_mount_source $SAVE_PATH)
399b37
    _target=$(get_target_from_path $_save_path)
399b37
    _mntpoint=$(get_mntpoint_from_target $_target)
b9e861
399b37
    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
# $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
#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}')
54b5ae
            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
check_crypt()
b9e861
{
e8a34c
    local _dev
b9e861
e8a34c
    for _dev in $(get_kdump_targets); do
e8a34c
        if [[ -n $(get_luks_crypt_dev "$(get_maj_min "$_dev")") ]]; then
e8a34c
            derror "Device $_dev is encrypted." && return 1
e8a34c
        fi
e8a34c
    done
b9e861
}
b9e861
b9e861
if ! check_resettable; then
b9e861
    exit 1
b9e861
fi
b9e861
b9e861
if ! check_crypt; then
54b5ae
    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)
399b37
        check_user_configured_target "$config_val" "$config_opt"
399b37
        add_mount "$config_val" "$config_opt"
b9e861
        ;;
b9e861
    raw)
399b37
        # 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
        }
a6b1d2
        _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
a6b1d2
    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
399b37
echo "$dracut_args $@" | xargs dracut
399b37
b9e861
_rc=$?
b9e861
sync
b9e861
exit $_rc