dnsmasq部署pxe服務器腳本
dnsmasq中文參考資料(必看):https://wiki.archlinux.org/index.php/Dnsmasq_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
目標:dnsmasq部署pxe服務器腳本
網絡環境:
kvm+vsftpd服務器:192.168.10.7
dnsmasq簡介
Dnsmasq 提供 DNS 緩存和 DHCP 服務功能。作為域名解析服務器(DNS),dnsmasq可以通過緩存 DNS 請求來提高對訪問過的網址的連接速度。作為DHCP 服務器,dnsmasq 可以用於為局域網電腦分配內網ip地址和提供路由。
DNS和DHCP兩個功能可以同時或分別單獨實現。dnsmasq輕量且易配置,適用於個人用戶或少於50臺主機的網絡。由於dnsmasq可以管理tftp-server服務。因此可以用來搭建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/
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolist
yum install -y vsftpd
mkdir -pv /var/ftp/ks/
yum install -y dnsmasq curl tftp-server xinetd syslinux mlocate
mkdir -v /var/lib/tftpboot/pxelinux.cfg /var/lib/tftpboot/centos7/ /var/ftp/dvd/
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 <<EOF
default 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 with http://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 drive
EOF
cat /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/config
systemctl stop firewalld
systemctl disable firewalld
systemctl stop libvirtd
systemctl disable libvirtd
read -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 <<EOF
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolist
mkdir -pv /var/ftp/ks/
yum install -y dnsmasq vsftpd curl tftp-server xinetd
cp -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/tftpboot
EOF
grep -Ev ‘^#|^$‘ /etc/dnsmasq.d/dhcp.conf
yum 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/default
cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF
default 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 with http://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 drive
EOF
cat /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
#腳本編寫完畢
dnsmasq部署pxe服務器腳本