dnsmasq部署pxe服务器脚本
#临时测试:systemctl stop firewalld
systemctl stop libvirtd
setenforce 0#准备本地yum源mkdir -pv /dvd
mount /dev/cdrom /dvd
chmod +x /etc/rc.d/rc.local
grep dvd /etc/rc.d/rc.local || echo ‘mount /dev/cdrom /dvd‘ > /etc/rc.d/rc.local
source /etc/rc.d/rc.local
cd /etc/yum.repos.d
mkdir -pv bak/
mv -v *.repo bak/
cat > dvd.repo <
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolistyum install -y vsftpdmkdir -pv /var/ftp/ks/yum install -y dnsmasq curl tftp-server xinetd syslinux mlocatemkdir -v /var/lib/tftpboot/pxelinux.cfg /var/lib/tftpboot/centos7/ /var/ftp/dvd/
systemctl restart vsftpd
mount /dev/cdrom /var/ftp/dvd
updatedb
locate pxelinux.0
cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp -v /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
cp -v /dvd/isolinux/* /var/lib/tftpboot/centos7/
touch /var/lib/tftpboot/pxelinux.cfg/default
#创建pxe启动菜单default文件
cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOFdefault menu.c32
prompt 0
timeout 300
ONTIMEOUT local
menu title ########## PXE Boot Menu ##########
label 1
menu label ^1)InstallCentOS7 x64 withLocalRepo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd
devfs=nomount
#ks=ftp://192.168.10.7/ks/7text.ks
label 2
menu label ^2)InstallCentOS7 x64 withhttp://mirror.centos.org
Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/
devfs=nomount ip=dhcp
label 3
menu label ^3)InstallCentOS7 x64 withLocalRepousing VNC
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/pub
devfs=nomount inst.vnc inst.vncpassword=password
label 4
menu label ^4)Bootfromlocal driveEOFcat /var/lib/tftpboot/pxelinux.cfg/default
测试dnsmasq手动启动(已测OK):
netstat -atunlp|grep dnsmasq
systemctl stop libvirtd
systemctl stop dnsmasq
pkill -9 dnsmasq
dnsmasq --interface=ens33 --dhcp-range=192.168.10.100,192.168.10.150 --dhcp-boot=pxelinux.0 --enable-tftp --tftp-root=/var/lib/tftpboot/
dnsmasq -i ens33 -F 192.168.10.100,192.168.10.150 -M pxelinux.0 --enable-tftp --tftp-root=/var/lib/tftpboot/
#以上的临时测试完毕(已测OK)
-----------------------------------------------------------------------
#正式编写dnsmasq自动部署pxe服务器的脚本vi dnsmasq.sh 脚本代码如下
#!/bin/bash
setenforce 0
getenforce
sed -i ‘/^SELINUX=/c\SELINUX=disable‘ /etc/selinux/config
grep ^SELINUX /etc/selinux/configsystemctl stop firewalld
systemctl disable firewalldsystemctl stop libvirtd
systemctl disable libvirtdread -p ‘selinux和firewalld已关闭,按回车键继续配置本地yum源...‘
#准备本地yum源
mkdir -pv /dvd
mount /dev/cdrom /dvd
chmod +x /etc/rc.d/rc.local
grep dvd /etc/rc.d/rc.local || echo ‘mount /dev/cdrom /dvd‘ > /etc/rc.d/rc.local
source /etc/rc.d/rc.local
cd /etc/yum.repos.d
mkdir -pv bak/
mv -v *.repo bak/
cat > dvd.repo <
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolistmkdir -pv /var/ftp/ks/yum install -y dnsmasq vsftpd curl tftp-server xinetdcp -v /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
grep -Ev ‘^#|^$‘ /etc/dnsmasq.conf
cat >/etc/dnsmasq.d/dhcp.conf
<<EOF#接口设置,在94行interface=ens33,lo
#监听IP地址,在99行
#listen-address=192.168.10.7
#绑定接口,在112行
#bind-interfaces
#域名,#在132行
domain=sky.com
# DHCP range-leases,dhcp租约信息,在145行
dhcp-range= ens33,192.168.10.100,192.168.10.200,255.255.255.0,1h
#ip和mac地址绑定
#dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50# PXE启动文件和tftp服务器ip地址,在418行
dhcp-boot=pxelinux.0,pxeserver,192.168.10.7 # Gateway网关,在312行
dhcp-option=3,192.168.10.1
# DNS服务器IP地址
dhcp-option=6,114.114.114.114,8.8.8.8
server=8.8.4.4
# Broadcast Address广播地址
dhcp-option=28,192.168.10.255
# NTP Server网络时间服务器IP地址,
dhcp-option=42,0.0.0.0
#pxe启动时的命令提示,在448行
pxe-prompt="Press F8 for menu.", 5
#pxe服务,在458行
pxe-service=x86PC,"Install CentOS 7 from network server 192.168.10.7", pxelinux
#启用tftp服务,在473行
enable-tftp
#tftp文件共享的根目录,在476行
tftp-root=/var/lib/tftpbootEOFgrep -Ev ‘^#|^$‘ /etc/dnsmasq.d/dhcp.confyum install -y syslinux mlocate
yum install -y tftp-server xinetd
ls /usr/share/syslinux
updatedb
locate pxelinux.0
cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/defaultcat > /var/lib/tftpboot/pxelinux.cfg/default <<EOFdefault menu.c32
prompt 0
timeout 15
ONTIMEOUT local
menu title ########## PXE Boot Menu ##########
label 1
menu label ^1)InstallCentOS7 x64 withLocalRepo
menu default
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd
devfs=nomount
#ks=ftp://192.168.10.7/ks/7text.ks
label 2
menu label ^2)InstallCentOS7 x64 withhttp://mirror.centos.org
Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/
devfs=nomount ip=dhcp
label 3
menu label ^3)InstallCentOS7 x64 withLocalRepousing VNC
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd
devfs=nomount inst.vnc inst.vncpassword=password
label 4
menu label ^4)Bootfromlocal driveEOFcat /var/lib/tftpboot/pxelinux.cfg/default
mkdir -v /var/lib/tftpboot/centos7/
cp -v /dvd/isolinux/* /var/lib/tftpboot/centos7/
pkill -9 dnsmasq
systemctl restart dnsmasq
systemctl restart vsftpd
systemctl status dnsmasq
systemctl status vsftpd
systemctl enable dnsmasq
systemctl enable vsftpd
netstat -atunlp|grep dnsmasq
#脚本编写完毕
来源: http://www.bubuko.com/infodetail-2310609.html