6

shell脚本之一键部署PXE

 3 years ago
source link: https://blog.csdn.net/weixin_51432789/article/details/111829790
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

shell脚本之一键部署PXE

huangzhihuan0813 2020-12-28 08:01:12 2441
#!/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命令)将会自动通过网络下载安装文件,并进入默认的图形安装入口,然后就等着就可以了。。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK