#!/bin/bash

_install() {
    root_fs=`findmnt -n /|awk '{ print $3 }'`
    root_path=`findmnt -n /|awk '{ print $2 }'`
    root_uuid=`blkid -s UUID -o value $root_path`
    cat > /etc/kdump.conf << EOF
$root_fs UUID=$root_uuid
path /var/crash
core_collector makedumpfile -l --message-level 1 -d 31
default reboot
EOF

    if [[ $ARCH == 'x86_64' ]];then
        sed -i 's/.*MKDUMPRD_ARGS=.*/MKDUMPRD_ARGS="--allow-missing"/g' /etc/sysconfig/kdump > /dev/null 2>&1
        grep -q 'modprobe.blacklist=mlx5_core,mlxfw' /etc/sysconfig/kdump
        if [[ $? -ne 0 ]];then
            sed -i 's/^KDUMP_COMMANDLINE_APPEND="\(.*\)"/KDUMP_COMMANDLINE_APPEND="\1 modprobe.blacklist=mlx5_core,mlxfw"/' /etc/sysconfig/kdump
        fi
    fi

    # set kernel args
    grubby --update-kernel=ALL --args="crashkernel=512M-2G:64M,2G-4G:128M,4G-8G:192M,8G-256G:256M,256G-:512M"
    if [[ $OS = CentOS7 ]];then
        grep -q "crashkernel" /etc/default/grub
        if [[ $? -eq 0 ]];then
            grep -q "crashkernel=512M-2G:64M,2G-4G:128M,4G-8G:192M,8G-256G:256M,256G-:512M[\" ]" /etc/default/grub
            if [[ $? -ne 0 ]];then
                sed -i 's/crashkernel=\S*\([\" ]\)/crashkernel=512M-2G:64M,2G-4G:128M,4G-8G:192M,8G-256G:256M,256G-:512M\1/' /etc/default/grub
            fi
        else
            sed -i 's/^GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="\1 crashkernel=512M-2G:64M,2G-4G:128M,4G-8G:192M,8G-256G:256M,256G-:512M"/' /etc/default/grub
        fi
    fi
    if [[ $ARCH == 'x86_64' ]];then
        grubby --update-kernel=ALL --args="spectre_v2=off l1tf=off"
        if [[ $OS = CentOS7 ]];then
            grep -q "spectre_v2=off l1tf=off" /etc/default/grub
            if [[ $? -ne 0 ]];then
                sed -i 's/^GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="\1 spectre_v2=off l1tf=off"/' /etc/default/grub
            fi
        fi
    fi
    if [[ $ARCH == 'aarch64' ]];then
        yum -y update kexec-tools > /dev/null 2>&1
        sed -i 's/^KDUMP_COMMANDLINE_APPEND=.*/KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 swiotlb=noforce cma=0 reset_devices cgroup_disable=memory udev.children-max=2 panic=10 rootflags=nofail novmcoredd modprobe.blacklist=mlx5_core,mlxfw"/' /etc/sysconfig/kdump

    fi

    if [[ -f /boot/initramfs-$(uname -r)kdump.img ]];then
        local version=`lsinitrd /boot/initramfs-$(uname -r)kdump.img |head -n20|grep Version|awk '{print $2}'`
        if [[ ! `rpm -q dracut` =~ $version ]];then
            rm -f /boot/initramfs-$(uname -r)kdump.img
        fi
    fi

    # RaidChip=H460-M1 RaidType=PMC_HBA 增加内核参数 使ld逻辑盘放在最前面
    PMC_HBA_H460_M1=$(lspci -nn -d 9005:028f:0107)
    if [[ $PMC_HBA_H460_M1 ]] ;then
        grubby --update-kernel=ALL --args="smartpqi.expose_ld_first=1"
        if ! grep -q "smartpqi.expose_ld_first=1" /etc/default/grub
        then
            sed -i 's/^GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="\1 smartpqi.expose_ld_first=1"/' /etc/default/grub
        fi
    fi

    if [[ $BFFLAG = 1 ]];then
        sed -i '/mlx5_core num_of_groups/d' /etc/modprobe.d/mlnx.conf
        echo "options mlx5_core num_of_groups=127" >> /etc/modprobe.d/mlnx.conf
    fi

    # make grub.cfg
    if [[ $BOOTMODE == "Legacy" ]];then
        grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
    elif [[ $BOOTMODE == "UEFI" && $ARCH == 'x86_64' && $OS = CentOS7 ]];then
        grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
        grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg &> /dev/null
    elif [[ $ARCH == 'aarch64' ]];then
        grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg &> /dev/null
    else
        PRINT_ERR "grub2-mkconfig ERROR"
        return 1
    fi
    if [[ $? != 0 ]] ;then
        PRINT_ERR "grub2-mkconfig ERROR"
        return 1
    fi

    service kdump restart > /dev/null 2>&1
    return 0

}

_check() {
    _check="PASS"
    if [[ $ARCH == 'x86_64' ]];then
        if [[ $OS = CentOS6 ]];then
            grep -q "spectre_v2=off l1tf=off" /boot/grub/grub.conf
            result=$?
        elif [[ $OS = CentOS7 ]];then
            if [[ $BOOTMODE == "Legacy" ]];then 
                grep -q "spectre_v2=off l1tf=off" /boot/grub2/grub.cfg
            elif [[ $BOOTMODE == "UEFI" ]];then
                grep -q "spectre_v2=off l1tf=off" /boot/grub2/grub.cfg
                grep -q "spectre_v2=off l1tf=off" /boot/efi/EFI/centos/grub.cfg
            fi
            result=$?
            if [[ $VMFLAG == 0 ]];then
                grep -q "spectre_v2=off l1tf=off" /proc/cmdline || result=1
            fi
        fi
        if [[ $result -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "spectre_v2 and l1tf not set"
        fi
        grep -q 'modprobe.blacklist=mlx5_core,mlxfw' /etc/sysconfig/kdump
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "modprobe.blacklist=mlx5_core,mlxfw not set"
        fi
    fi
    if [[ $VMFLAG = 0 ]];then
        return 0
    fi

    if [[ $BFFLAG = 1 ]];then
	grep -q 'options mlx5_core num_of_groups=127' /etc/modprobe.d/mlnx.conf
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "options mlx5_core num_of_groups=127 not set"
        fi
    fi

    service kdump status >/dev/null 2>&1
    if [[ $? -ne 0 ]];then
        _check="FAIL"
        PRINT_ERR "kdump is not active"
    fi

    # 检查kdump.img
    if [[ -f /boot/initramfs-$(uname -r)kdump.img ]];then
        local version=`lsinitrd /boot/initramfs-$(uname -r)kdump.img |head -n20|grep Version|awk '{print $2}'`
        if [[ ! `rpm -q dracut` =~ $version ]];then
            _check="FAIL"
            PRINT_ERR "/boot/initramfs-$(uname -r)kdump.img dracut version $version is not newest"
        fi
    else
        check="FAIL"
        PRINT_ERR "/boot/initramfs-$(uname -r)kdump.img is not exist"
    fi
}
