shell脚本之一键部署PXE

#!/bin/bash

#先判断是否挂载,并检查tftp-server xinetd软件包是否安装
yum -y install tftp-server xinetd &>/dev/null
if [ $? -ne 0 ];then
mount /dev/cdrom /mnt &>/dev/null
fi

#安装并启用TFTP服务
yum -y install tftp-server xinetd &>/dev/null
sed -i '14s/yes/no/g' /etc/xinetd.d/tftp
systemctl start tftp
systemctl enable tftp
systemctl start xinetd
systemctl enable xinetd

#安装并启用DHCP服务
yum -y install dhcp expect tcl &>/dev/null
 /usr/bin/expect &>/dev/null <<EOF
spawn cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
expect "是否覆盖" {send "y\n"}
EOF
cat >/etc/dhcp/dhcpd.conf << EOF
ddns-update-style none;
next-server 192.168.241.3;
filename "pxelinux.0";
subnet 192.168.241.0 netmask 255.255.255.0 {
range 192.168.241.100 192.168.241.200;
option routers 192.168.241.3;
}
EOF
systemctl start dhcpd
systemctl enable dhcpd

#准备Linux内核、初始化镜像文件
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot

#准备PXE引导程序
yum -y install syslinux &>/dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot

#安装FTP服务,准备CentOS7 安装源
yum -y install vsftpd &>/dev/null
mkdir /var/ftp/centos7
cp -rf /mnt/* /var/ftp/centos7
systemctl start vsftpd
systemctl enable vsftpd

#配置启动菜单文件
mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/default
cat >/var/lib/tftpboot/pxelinux.cfg/default <<-EOF
default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.241.3/centos7
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.241.3/centos7
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.241.3/centos7
EOF

#关闭防火墙,验证PXE网络安装
systemctl stop firewalld.service
setenforce 0 &>/dev/null

重新新建一台虚拟机,虚拟机内存要2G否则在安装时可能会出现错误。
开启虚拟机,在提示字符串“boot:”后直接回车键(或者执行auto命令)将会自动通过网络下载安装文件,并进入默认的图形安装入口,然后就等着就可以了。。

你可能感兴趣的:(shell脚本之一键部署PXE)