Centos7|8自动安装PXE

改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决 改进质量解决,改进质量解决,改进质量解决,改进质量解决
#!/bin/bash
#PS:Centos7|8自动安装PXE相关服务
. /etc/init.d/functions
set -u
#DHCP网段
Segment="192.168.182.0"
#提供引导文件的服务器ip
Next_server="192.168.182.170"
ks_path="/root/ks.cnf"
color_red="echo -e \033[1;31m"
color_end="\033[0m"
color_green="echo -e \033[1;32m"
ip_range=`echo $Segment |sed -rn 's/(.*.)0/\1/p'`

check() {
    #查看版本
    Version=`cat /etc/redhat-release |sed -nr  's/.*release ([0-9]).*/\1/p'`
    #检查是否安装yum
    yum makecache || { echo "yum不可用,请配置本地源和epel源"; exit 1; }
    #检查是否安装PXE相关包,并安装
    if [[ $Version =~ 7 ]];then
        rpm -q dhcp || yum -y Install dhcp-*
        rpm -q tftp-server || yum -y install tftp-server
        rpm -q httpd || yum -y install httpd
        rpm -q syslinux || yum -y install syslinuxsystem-configkickstart
        rpm -q system-configkickstart || yum -y install 
        install_path=`rpm -q dhcp |sed -nr 's/(.*)-.*/\1/p'`
        return 1
    elif  [[ $Version =~ 8 ]];then
        rpm -q dhcp-server || yum -y install dhcp-server
        rpm -q tftp-server || yum -y install tftp-server
        rpm -q httpd || yum -y install httpd
        rpm -qa |grep -E 'syslinux-tftpboot|syslinux-nonlinux' || yum -y install syslinux-tftpboot
        install_path=`dhcp-server`
        return 2
    fi
}

install_httpd(){
    systemctl enable --now httpd || { $color_red"httpd服务启动失败"$color_end;exit 1;}
    mkdir -p /var/www/html/centos/7/os/x86_64
    mount /dev/sr0 /var/www/html/centos/7/os/x86_64
    mkdir -p /var/www/html/Centos/7/ks/
    
}

install_dhcp() {
    #准备配置文件
    mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
    cp -p /usr/share/doc/$install_path/dhcpd.conf.example /etc/dhcp/dhcpd.conf
    echo " " > /etc/dhcp/dhcpd.conf
    cat >/etc/dhcp/dhcpd.conf<<EOF
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 86400;
max-lease-time 164000;
log-facility local7;

subnet $Segment netmask 255.255.255.0 {
	range ${ip_range}100 ${ip_range}254;
	option routers ${ip_range}1;
	next-server $Next_server;
	filename "pxelinux.0"; 
}
EOF

systemctl enable --now dhcpd && action "DHCP启动成功" || action "DHCP启动出错" flase

}

install_tftp() {
    systemctl enable --now tftpd
    ss -nulp |grep  tftp  && $color_green"tftp启动成功"$color_end || $color_red"DHCP启动出错"$color_end
}

install_pxe() {
#准备pxe路径与文件
    mkdir -p /mnt/cdrom;mount /dev/sr0 /mnt/cdrom
    [ -f /mnt/cdrom/isolinux/vmlinuz ] || { $Color_red"请将光盘文件挂载至/mnt/cdrom"$color_end;exit 1; }
    mkdir /var/lib/tftpboot/pxelinux.cfg/
    cp -p /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
    cp -p /mnt/cdrom/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/
    cp -p /mnt/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
 #准备启动菜单   
    echo " " > /var/lib/tftpboot/pxelinux.cfg/default
    cat /var/lib/tftpboot/pxelinux.cfg/default << EOF 
default menu.c32
timeout 600
men title Install Centos linux-xiangdeming
#auto install
label auto linux7
	menu laber ^Install Centos 7
	kernel Centos7/vmlinuz
	append initrd=Centos7/initrd.img ks=http://$Next_server/Centos/7/ks/ks.cfg

#manual install
label manual linux7
	menu laber ^Install Centos 7
	kernel Centos7/vmlinuz
        append initrd=Centos7/initrd.img ks=http://$Next_server/centos/7/os/x86_64/
#rescue
label rescue
	menu label ^Install Centos 7
	kernel Centos7/vmlinuz
	append initrd=Centos7/initrd.img ks=http://$Next_server/centos/7/os/x86_64/ rescue
#default
label loacl
	menu default
	menu label Boot from ^local drive
	localboot 0xffff

menu end
EOF
#制作kickstart文件(建议使用图形化工具:system-config-kickstart自行准备)
touch $ks_path
cat $ks_path << EOF
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext redhat
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=static --device=eth0 --gateway=192.168.182.1 --ip=192.168.182.109 --netmask=255.255.255.0
# Reboot after installation
reboot
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="http://$Next_server/centos/7/os/x86_64/"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="xfs" --size=20480
part /boot --fstype="xfs" --size=1024
part /home --fstype="xfs" --size=10240
part swap --fstype="swap" --size=2048

%packages
@web-server

%end
#脚本配置
%post
install -d /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
install -d /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
mkdir -m 700 /root/.ssh
chmod -600 /root/.ssh/authorized_keys
useradd xiangdeming 
echo "redhat" |passwd --stdin xiangdeming
echo "export PS1="[\[\e[33;1m\]\u@\h\[\e[0m\] \[\e[36;1m\]\W\A\[\e[0m\]]\[\e[33;1m\]\\$\[\e[0m\]"" >> /root/.bashrc


%end
EOF
[ -f $ks_path ] && chmod 755 $ks_path || $color_red "ks文件创建失败" $color_end
cp -p $ks_path /var/www/html/Centos/7/ks/
[ -f  /var/www/html/Centos/7/ks/ks.cnf ]  ||  $color_red "请手动复制ks.cnf文件到:/var/www/html/Centos/7/ks/" $color_end


}

PS3="请选择:(1-6)"
select MENU in 环境检查与包安装 dhcp配置 httpd环境配置 pxe配置 全部执行 退出;do
case $REPLY in
1)
    echo 执行$MENU
    check
    ;;
2)
    echo 执行$MENU
    install_dhcp
    ;;
3) 
    echo 执行$MENU
    install_httpd
    ;;
4)
    echo 执行$MENU
    install_pxe
    ;;
5)
    echo 执行$MENU
    check
    install_dhcp
    install_httpd
    install_pxe
    ;;
6)
    break
    ;;
esac 
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值