#!/bin/bash
#Net Reinstall Centos System
#bash <(curl -s -S -L https://file.iso.st/CentosNetReInstall.sh)
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
nicCard=$(ls /sys/class/net)
nicCard=(${nicCard})
d0Str='$0'
nicCardName=""
vnc_password=""
repo_url=""
ipaddr=""
gateway=""
netmask=""
bootRoot=""
centos_version=""
current_centos_version=$(cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/')
run_script7() {
if [ -f "/boot/initrd77.img" ]; then
  rm -rf /boot/initrd77.img
fi
wget -P /boot/ ${repo_url}/images/pxeboot/initrd.img -O /boot/initrd77.img > /dev/null
if [ -f "/boot/vmlinuz77" ]; then
  rm -rf /boot/vmlinuz77
fi
wget -P /boot/ ${repo_url}/images/pxeboot/vmlinuz -O /boot/vmlinuz77 > /dev/null
if [ -f "/boot/efi/EFI/centos/grub.cfg" ]; then
  startDiskPart=$(cat /boot/efi/EFI/centos/grub.cfg |grep "set root" | head -n 1 |awk -F \' '{print $2}')
fi
if [ -f "/boot/efi/EFI/redhat/grub.cfg" ]; then
  startDiskPart=$(cat /boot/efi/EFI/redhat/grub.cfg |grep "set root" | head -n 1 |awk -F \' '{print $2}')
fi
if [ -f "/boot/grub2/grub.cfg" ]; then
  startDiskPart=$(cat /boot/grub2/grub.cfg |grep "set root" | head -n 1 |awk -F \' '{print $2}')
fi
if [ -d "/boot/efi" ]; then
  startDiskType=$(df -T /boot/efi | awk '{print $2}' | sed -n '2p')
fi
if [ ! -d "/boot/efi" ]; then
  startDiskType=$(df -T /boot | awk '{print $2}' | sed -n '2p')
fi
if [ "ext4" = "${startDiskType}" -o "ext3" = "${startDiskType}" -o "vfat" = "${startDiskType}" ]
then
startDiskType="ext2"
fi

kernel_param="inst.vnc inst.vncpassword=${vnc_password} inst.headless ip=${ipaddr}::${gateway}:${netmask}::${nicCardName}:none nameserver=8.8.8.8 inst.repo=${repo_url} inst.lang=en_US inst.keymap=us"
if [ "$centos_version" -eq 6 ]; then
    kernel_param="vnc vncpassword=${vnc_password} headless ks=file:/dev/null ip=${ipaddr} noipv6 netmask=${netmask} gateway=${gateway} dns=8.8.8.8 ksdevice=${nicCardName} method=${repo_url} lang=en_US keymap=us"
fi
isfC8="16"
if [ "$current_centos_version" -eq 8 ]; then
    isfC8=""
fi
cat << EOF > /etc/grub.d/40_custom
#!/bin/sh
exec tail -n +3 ${d0Str}
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry "NetInstall" {
	load_video
	set gfxpayload=keep
	insmod gzio
	insmod part_msdos
	insmod ${startDiskType}
	set root='${startDiskPart}'

	linux${isfC8} ${bootRoot}/vmlinuz77 ${kernel_param}
  initrd${isfC8} ${bootRoot}/initrd77.img
}
EOF
if [ -f "/boot/efi/EFI/centos/grub.cfg" ]; then
  grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
fi
if [ -f "/boot/efi/EFI/redhat/grub.cfg" ]; then
  grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
fi
if [ -f "/boot/grub2/grub.cfg" ]; then
  grub2-mkconfig -o /boot/grub2/grub.cfg
fi
grub2-reboot NetInstall
reboot

}

run_script6() {
if [ -f "/boot/initrd77.img" ]; then
  rm -rf /boot/initrd77.img
fi
wget -P /boot/ ${repo_url}/images/pxeboot/initrd.img -O /boot/initrd77.img > /dev/null
if [ -f "/boot/vmlinuz77" ]; then
  rm -rf /boot/vmlinuz77
fi
wget -P /boot/ ${repo_url}/images/pxeboot/vmlinuz -O /boot/vmlinuz77 > /dev/null
bootdisk=""
if [ -f "/boot/efi/EFI/redhat/grub.conf" ]; then
  startDiskPart=$(cat /boot/efi/EFI/redhat/grub.conf |grep "root (" | head -n 1 |awk -F \' '{print $1}')
  startDiskPart=${startDiskPart/\#/}
  startDiskPart=${startDiskPart/root/}
  startDiskPart=$(echo ${startDiskPart} | sed 's/ //g')
  bootdisk=$(cat /boot/efi/EFI/redhat/grub.conf |grep "boot=" | head -n 1 |awk -F \' '{print $1}')
fi
if [ -f "/boot/grub/grub.conf" ]; then
  startDiskPart=$(cat /boot/grub/grub.conf |grep "root (" | head -n 1 |awk -F \' '{print $1}')
  startDiskPart=${startDiskPart/\#/}
  startDiskPart=${startDiskPart/root/}
  startDiskPart=$(echo ${startDiskPart} | sed 's/ //g')
  bootdisk=$(cat /boot/grub/grub.conf |grep "boot=" | head -n 1 |awk -F \' '{print $1}')
fi
kernel_param="inst.vnc inst.vncpassword=${vnc_password} inst.headless ip=${ipaddr}::${gateway}:${netmask}::${nicCardName}:none nameserver=8.8.8.8 inst.repo=${repo_url} inst.lang=en_US inst.keymap=us"
if [ "$centos_version" -eq 6 ]; then
    kernel_param="vnc vncpassword=${vnc_password} headless ks=file:/dev/null ip=${ipaddr} noipv6 netmask=${netmask} gateway=${gateway} dns=8.8.8.8 ksdevice=${nicCardName} method=${repo_url} lang=en_US keymap=us"
fi
if [ -f "/boot/efi/EFI/redhat/grub.conf" ]; then
  cat << EOF > /boot/efi/EFI/redhat/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root ${startDiskPart}
#          kernel /vmlinuz-version
#          initrd /initrd-[generic-]version.img
${bootdisk}
default=0
timeout=5
splashimage=${startDiskPart}/grub/splash.xpm.gz
hiddenmenu
title NetInstall
    root ${startDiskPart}
    kernel ${bootRoot}/vmlinuz77 ${kernel_param}
    initrd ${bootRoot}/initrd77.img
EOF
fi
if [ -f "/boot/grub/grub.conf" ]; then
  cat << EOF > /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root ${startDiskPart}
#          kernel /vmlinuz-version
#          initrd /initrd-[generic-]version.img
${bootdisk}
default=0
timeout=5
splashimage=${startDiskPart}/grub/splash.xpm.gz
hiddenmenu
title NetInstall
    root ${startDiskPart}
    kernel ${bootRoot}/vmlinuz77 ${kernel_param}
    initrd ${bootRoot}/initrd77.img
EOF
fi
reboot

}

input_nic() {
while true
do
echo -e "   ${yellow}请选择要安装的CentOS版本${plain}"
echo -e "   ${green}6：${plain}CentOS 6 [内存必须>=500M]"
echo -e "   ${green}7：${plain}CentOS 7 [内存必须>=768M]"
echo -e "   ${green}8：${plain}CentOS 8 [内存必须>=1.5G]"
read -p "请输入序号(版本):" input_centos_version
if [ "$input_centos_version" -eq 6 ]; then
    centos_version=6
    repo_url="https://linux.iso.st/centos/last6source"
    break
fi
if [ "$input_centos_version" -eq 7 ]; then
    centos_version=7
    repo_url="https://linux.iso.st/centos/source"
    break
fi
if [ "$input_centos_version" -eq 8 ]; then
    centos_version=8
    repo_url="https://linux.iso.st/centos/last8source"
    break
fi
echo -e "[${red}输入有误，请重新输入${plain}]"
done

while true
do
for ((i=1;i<=${#nicCard[@]};i++ )); do
    local_p="${nicCard[$i-1]}"
    echo -e "${green}${i}${plain}) ${local_p}"
done
read -p "请输入主网卡序号[默认 1]:" input_nic_no
[ -z "$input_nic_no" ] && input_nic_no=1
expr ${input_nic_no} + 1 &>/dev/null
if [ $? -ne 0 ]; then
    echo -e "[${red}错误:${plain}] 请输入一个数字"
    continue
fi
if [[ "$input_nic_no" -lt 1 || "$input_nic_no" -gt ${#nicCard[@]} ]]; then
    echo -e "[${red}错误：${plain}] 请输入一个数字 1 - ${#nicCard[@]}"
    continue
fi
nicCardName=${nicCard[$input_nic_no-1]}
break;
done
read -p "请输入VNC密码[6位+][默认密码 jockerli]:" input_passwd
[ -z "${input_passwd}" ] && input_passwd="jockerli"
vnc_password=${input_passwd}
read -p "请输入repo地址[建议使用默认直接回车]:" input_repo
[ -z "${input_repo}" ] && input_repo=$repo_url
repo_url=${input_repo}
gateway=$(route -n | grep ${nicCardName} | grep UG | awk '{print $2}')
ipaddr=$(ifconfig ${nicCardName} | grep "inet" | awk -F " " '{print $2}' | head -n 1)
netmask=$(ifconfig ${nicCardName} | grep "inet" | awk -F " " '{print $4}' | head -n 1)
if [ "$current_centos_version" -eq 6 ]; then
ipaddr=${ipaddr/addr:/}
netmask=${netmask/Mask:/}
fi
echo
echo "---------------------------"
echo -e "${green}将要开始安装的操作系统是：${plain} ${red}CentOS ${centos_version} ${plain}"
if [ "$centos_version" -eq 7 ]; then
echo -e "${yellow}CentOS 7 为了兼容小内存机器，引导内核为7.0${plain}"
echo -e "${yellow}如需安装最新内核，请在VNC界面更改安装源${plain}"
fi
echo "---------------------------"
echo -e "${green}网    卡 ${plain}= ${green}${nicCardName}${plain}"
echo -e "${green}IP  地址 ${plain}= ${green}${ipaddr}${plain}"
echo -e "${green}网    关 ${plain}= ${green}${gateway}${plain}"
echo -e "${green}掩    码 ${plain}= ${green}${netmask}${plain}"
echo "---------------------------"
echo -e "${green}VNC URL ${plain}= ${green}${ipaddr}:1${plain}"
echo "---------------------------"
echo -e "${green}VNC 密码 ${plain}= ${green}${vnc_password}${plain}"
echo -e "${yellow}VNC密码确保在6-8位之间！并且不包含特殊字符！${plain}"
echo "---------------------------"
echo -e "${green}repo地址 ${plain}= ${green}${repo_url}${plain}"
echo "---------------------------"
echo -e "注意：${yellow}VNC只能在脚本运行完等待数分钟可用${plain}"
echo -e "     ${yellow}请确保服务商防火墙端口[5901]为开放状态${plain}"
echo
read -p "回车开始运行脚本；ctrl + c 取消本次操作" input_read
}
init_boot_menu() {
if [ "$current_centos_version" -eq "6" ]; then
  if [ -f "/boot/efi/EFI/redhat/grub.conf" ]; then
  bootRoot=$(cat /boot/efi/EFI/redhat/grub.conf |grep "initrd /" | head -n 1)
  fi
  if [ -f "/boot/grub/grub.conf" ]; then
  bootRoot=$(cat /boot/grub/grub.conf |grep "initrd /" | head -n 1)
  fi
  bootRoot=${bootRoot#*/}
  bootRoot=${bootRoot:0:4}
  if [ "boot" = "${bootRoot}" ]
  then
  bootRoot="/boot"
  else
  bootRoot=""
  fi
  run_script6
fi
if [ "$current_centos_version" -eq "7" ]; then
  if [ -f "/boot/efi/EFI/centos/grub.cfg" ]; then
    bootRoot=$(cat /boot/efi/EFI/centos/grub.cfg |grep "linux16" | head -n 1)
  fi
  if [ -f "/boot/efi/EFI/redhat/grub.cfg" ]; then
    bootRoot=$(cat /boot/efi/EFI/redhat/grub.cfg |grep "linux16" | head -n 1)
  fi
  if [ -f "/boot/grub2/grub.cfg" ]; then
    bootRoot=$(cat /boot/grub2/grub.cfg |grep "linux16" | head -n 1)
  fi
  bootRoot=${bootRoot#*/}
  bootRoot=${bootRoot:0:4}
  if [ "boot" = "${bootRoot}" ]
  then
  bootRoot="/boot"
  else
  bootRoot=""
  fi
  run_script7
fi
if [ "$current_centos_version" -eq "8" ]; then
  bootRoot=$(cat /boot/loader/entries/*.conf |grep "linux" | head -n 1)
  bootRoot=${bootRoot#*/}
  bootRoot=${bootRoot:0:4}
  if [ "boot" = "${bootRoot}" ]
  then
  bootRoot="/boot"
  else
  bootRoot=""
  fi
  run_script7
fi
}
init() {
if [ ! -f "/usr/bin/wget" ]; then
  yum -y install wget
fi
if [ ! -f "/usr/sbin/ifconfig" ]; then
  yum -y install net-tools
fi
input_nic
init_boot_menu

}
init