[root@localhost ~]# bash PXE_centos7_6.sh
前提能连接外网,仓库没有问题
!/bin/bash
echo "-----------------PXE批量部署CentOS7.6脚本-------------------"
echo -e "开始部署。。。。。\n"
#下载安装部署PXE必要服务与文件
systemctl status expect dhcp vsftpd syslinux tftp-server xinetd &> /dev/null
if [ $? -eq 0 ];then
echo -e "必要软件已经安装!!\n"
else
yum -y install expect dhcp vsftpd syslinux tftp-server xinetd &> /dev/null &> /dev/null
systemctl start vsftpd
systemctl enable vsftpd &> /dev/null
echo -e "软件已安装完毕\n"
fi
mkdir /var/ftp/centos7.6 &> /dev/null
if [ -e /var/ftp/centos7.6/repodata ];then
echo "镜像内容在ftp服务器中已存在!!"
else
echo -e "正在将镜像内容复制到ftp服务器中。。。\n"
cp -a /mnt/* /var/ftp/centos7.6/
echo -e "复制结束\n"
fi
systemctl start tftp
systemctl enable tftp &> /dev/null
systemctl start xinetd
systemctl enable xinetd &> /dev/null
if [ -e /var/lib/tftpboot/centos7.6 ]; then
echo -e "centos7.6文件在tftp服务器中已存在\n"
else
echo -e "正在将centos7.6引导文件复制到tftp服务器\n"
cp -a /mnt/isolinux/* /var/lib/tftpboot/
echo -e "复制完毕!\n"
fi
#开始部署PXE
echo -e "开始提取centos7.6共享内核与初始化镜像。。。。。。\n"
mkdir /var/lib/tftpboot/centos7.6 &> /dev/null
mv /var/lib/tftpboot/initrd.img /var/lib/tftpboot/centos7.6/ &> /dev/null
mv /var/lib/tftpboot/vmlinuz /var/lib/tftpboot/centos7.6/ &> /dev/null
mv /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default &> /dev/null
echo -e "提取完毕!!!\n"
cat > /var/lib/tftpboot/pxelinux.cfg/default << eof
# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none
# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
menu label Install CentOS 7
eof
echo -e "pxe菜单文件编写完成!\n"
sed -i '/disable/c \ disable = no' /etc/xinetd.d/tftp
systemctl restart xinetd
cp -a /root/anaconda-ks.cfg /var/ftp/centos76_ks.cfg
chmod 644 /var/ftp/centos76_ks.cfg
echo ""
#下边是CenTos7.6的ks文件
cat > /var/ftp/centos76_ks.cfg << eof
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$pHhwGQlp$FnSejX6/.MBUrHVJlIUTb/
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1
%packages
@core
vim-enhanced
net-tools
psmisc
lftp
wget
rsync
ntpdate
bash-completion
lvm2
%end
%post
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
%end
eof
#提供DHCP服务
cat > /etc/dhcp/dhcpd.conf << eof
subnet 192.168.152.0 netmask 255.255.255.0 {
range 192.168.152.100 192.168.152.150;
option routers 192.168.152.2;
option domain-name-servers 114.114.114.114;
next-server 192.168.152.15;
filename "pxelinux.0";
}
eof
systemctl restart dhcpd
systemctl restart xinetd
systemctl restart dhcpd
systemctl restart tftp
systemctl restart vsftpd
echo "PXE部署成功"