自动部署PXE脚本(仅测试过CentOS 7系列)

自动部署PXE脚本

#! /bin/bash
#Author:liy
#Blog:https://www.cnblogs.com/liy36/
#Time:2020-01-13 13:04:34
#Name:install_pxe.sh
#Version:V1.0

init () {
    setenforce 0
    systemctl stop firewalld 
}


vars () {
read -e -p "Enter IPAddress segment: (such as: 172.20.1.0) " segment 
test ! -z ${segment} || exit 7
read -e -p "Enter IPAddress range: (such as: 172.20.1.101 172.20.1.200) " startip stopip
test ! -z ${startip} || exit 8
test ! -z ${stopip} || exit 8
read -e -p "Enter NetMask: (such as: 255.255.255.0) " netmask
test ! -z ${netmask} || exit 9
read -e -p "Enter LocalHost IP (such as: 172.20.1.11) " localip
test ! -z ${localip} || exit 10
read -e -p "Enter system version. (such as: 7.6) " version
test ! -z ${version} || exit 11
read -e -p "Enter iso file Absolute path  (such as: /tmp/CentOS-7-x86_64-DVD-1810.iso) PS: ctrl+alt+f1-7 Can change terminal: "  isofile
test ! -z ${isofile} || exit 12
read -e -p "Enter kickstarts file Absolute path  (such as: /root/anaconda-ks.cfg) PS: ctrl+alt+f1-7 Can change terminal: "  ksfile
test ! -z ${isofile} || exit 13
}


system () {
    if [ -f "/etc/redhat-release" ]
    then
        grep -i "redhat" /etc/redhat-release
        if [ $? -eq 0 ]
        then
            OS="redhat"
        else
            OS="centos"
        fi
    else
        echo -e "\033[31mOnly for RedHat or CentOS. \033[0m"
        exit 1
    fi
    echo -e "\033[32mSystem is ${OS}."
}


yum_repository () {
    echo -e "\033[33mTest yum repository.\033[0m"
    yum repolist &>/dev/null 
    if [ $? -eq 0 ]
    then
        yum install dhcp httpd tftp-server syslinux
        rpm -q dhcp        || echo -e "\033[31mdhcp package install failed, Please check yum repository.\033[0m" || exit 3
        rpm -q httpd       || echo -e "\033[31mhttpd package install failed, Please check yum repository.\033[0m" || exit 3
        rpm -q tftp-server || echo -e "\033[31mtftp-server package install failed, Please check yum repository.\033[0m" || exit 3
        rpm -q syslinux    || echo -e "\033[31msyslinux package install failed, Please check yum repository.\033[0m" || exit 3
    else
        echo -e "\033[3nmYum repository unavailable.\033[0m"
        exit 2
    fi
}


conf_dhcp () {
cat >/etc/dhcp/dhcpd.conf<<-EOF
default-lease-time 86400;
max-lease-time 172800;
subnet ${segment} netmask ${netmask} {
range ${startip} ${stopip};
filename "pxelinux.0";
next-server ${localip};
}
EOF
echo -e "\033[32mChange dhcp configure file complete. \033[0m"
systemctl restart dhcpd && echo -e "\033[32mdhcpd service start success.\033[0m" || echo -e "\033[31mdhcp start failed.  \033[0m" || exit 4
}


conf_tftp () {
    sed -i "/disable/s/yes/no/" /etc/xinetd.d/tftp
    echo -e "\033[32mChange tftp-server configure file complete. \033[0m"
    systemctl restart tftp.socket && echo -e "\033[32mtftp-server start success. \033[0m" || echo -e "\033[31mtftp-server start failed.  \033[0m" || exit 5
}

conf_httpd () {
    mkdir -p /var/www/html/${OS}/${version}/
    mount -r ${isofile} /var/www/html/${OS}/${version}/
    mkdir -p /var/www/html/kickstarts/${OS}/${version}/
    cp ${ksfile} /var/www/html/kickstarts/${OS}/${version}/anaconda-ks.cfg
    chmod 777 /var/www/html/kickstarts/${OS}/${version}/anaconda-ks.cfg
    systemctl restart httpd && echo -e "\033[32mhttpd service start success. \033[0m" || echo -e "\033[31mhttpd start failed.  \033[0m" || exit 6
}


conf_pxe () {
    cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
    cp /var/www/html/${OS}/${version}/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/ 
    cp /usr/share/syslinux/{chain.c32,menu.c32,memdisk} /var/lib/tftpboot/
    mkdir -p /var/lib/tftpboot/pxelinux.cfg/
    touch /var/lib/tftpboot/pxelinux.cfg/default
cat >/var/lib/tftpboot/pxelinux.cfg/default<<-EOF 
default menu.c32
prompt 5
timeout 30
MENU TITLE ${OS} ${version}

LABEL linux
MENU LABEL Install ${OS} ${version} x86_64
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://${localip}/${OS}/${version}/ ks=http://${localip}/kickstarts/${OS}/${version}/anaconda-ks.cfg
EOF
}


vars
init
system
yum_repository
conf_dhcp
conf_tftp
conf_httpd
echo
echo 
echo
echo -e "\033[32m+++++++++++++Configuration complete+++++++++++++++ \033[0m"

返回值及使用方法

使用方法
    服务器安装好操作系统之后把iso格式镜像文件复制到服务器上,使用root用户执行bash ./install_pxe.sh

返回值说明
    1 = 操作系统版本不匹配
    2 = yum仓库不可用,需配置可用yum仓库
    3 = 软件包安装失败,需根据提示检查yum仓库内是否有指定软件包
    4 = dhcp配置服务启动失败
    5 = tftp-server服务启动失败
    6 = httpd服务启动失败
    7 = 地址段未指定
    8 = dhcp分配的地址范围为指定
    9 = 未输入掩码
    10 = 未输入本机IP
    11 = 操作系统版本未指定
    12 = 未输入iso镜像文件路径
    13 = 未输入ks应答文件路径

使用环境适用内网新上架服务器,未购买服务的情况。会关闭selinux及防火墙,文件路径未进行判断,使用时请自己检查,或者修改脚本添加test -f "${var}"判断条件。

造成影响本人不承担任何责任。

你可能感兴趣的:(自动部署PXE脚本(仅测试过CentOS 7系列))