一、PXE介紹
PXE: Preboot Excution Environment,由Intel公司研發,可以使沒有任何操作系統的主機能夠基于網絡完成系統的安裝工作,實現服務器的自動化安裝系統
二、PXE工作原理
- Client向PXE Server上的DHCP發送IP地址請求消息,DHCP檢測Client是否合法(主要是檢測Client的網卡MAC地址),如果合法則返回Client的IP地址,同時將啟動文件pxelinux.0的位置信息一并傳送給Client
- Client向PXE Server上的TFTP發送獲取pxelinux.0請求消息, TFTP接收到消息之后再向Client發送pxelinux.0大小信息,試探Client是否滿意,當TFTP收到Client發回的同意大小信息之后,正式向Client發送pxelinux.0
- Client執行接收到的pxelinux.0文件
- Client向TFTP Server發送針對本機的配置信息文件(在TFTP 服務的pxelinux.cfg目錄下), TFTP將配置文件發回Client,繼而Client根據配置文件執行后續操作
- Client向TFTP發送Linux內核請求信息, TFTP接收到消息之后將內核文件發送給Client
- Client向TFTP發送根文件請求信息, TFTP接收到消息之后返回Linux根文件系統
- Client啟動Linux內核
- Client下載安裝源文件,讀取自動化安裝腳本
三、PXE Server 的配置
圖1 PXE Server 的依賴服務
1. DHCP服務的配置
安裝包:dhcp-4.2.5-47.el7.centos.x86_64
[root@centos7 ~]#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf --> 使用模本配置文件 [root@centos7 ~]#vi /etc/dhcp/dhcpd.conf subnet 192.168.196.0 netmask 255.255.255.0 { --> 定義網段 range 192.168.196.10 192.168.196.40; --> 定義分配IP范圍 option routers 192.168.196.1; --> 配置路由 filename "pxelinux.0"; --> 啟動文件名稱 next-server 192.168.196.188; --> dhcp服務器地址 } [root@centos7 ~]#systemctl start dhcpd [root@centos7 ~]#systemctl enable dhcpd
配置完成,并設置DHCP靜態IP192.168.196.188。目標主機開機獲取IP后,可以訪問PXE Sever
2. tftp、httpd服務的配置
安裝包:tftp-5.2-13.el7.x86_64 httpd-2.4.6-45.el7.centos.x86_64
[root@Centos7 ~]#yum -q -y tftp [root@Centos7 ~]#yum -q -y httpd [root@Centos7 ~]#systemctl start tftpd.socket httpd [root@Centos7 ~]#systemctl enable tftp.socket [root@Centos7 ~]#systemctl enable httpd
注意:centos6里tftp是由xinetd服務代理的,需要先安裝xinetd服務,再chkconfig tftp on 命令開啟tftp服務
3.關閉SElinux、防火墻
[root@Centos7 ~]#setenforce 0 [root@Centos7 ~]#vi /etc/selinux/config --> 修改配置文件 SELINUX=permissive [root@Centos7 ~]#systemctl stop firewalld [root@Centos7 ~]#systemctl disable firewalld
4.kickstart應答文件準備
[root@centos7 ~]#mkdir /var/www/html/{centos7,ks} --> PXE Sever http服務會提供網絡源于ks文件 [root@centos7 ~]#cp ~/anaconda-ks.cfg /var/www/html/ks/centos7-m.cfg --> 以系統安裝ks文件為范本 [root@centos7 ks]#vi centos7-m.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 url --url=http://192.168.196.188/centos7/ --> 網絡源由PXE Sever http服務提供 text --> 文本界面安裝 # Run the Setup Agent on first boot firstboot --disable reboot --> 安裝完成后重啟 ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate -->網卡配置 network --hostname=centos7.ffu.com # Root password rootpw --iscrypted $6$8sIxlzOyC1RQ4g.X$UXgct3LHH67rVNH5StXAall/82K/5OkK6.QijHagr.DB4zw8IbnI0CYIUUAhTSDa.dfVfnZabRVZ8nFq5Cc1c1 # System services services --disabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc --nontp # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda # Partition clearing information clearpart --none --initlabel # Disk partitioning information part swap --fstype="swap" --ondisk=sda --size=2048 part /app --fstype="ext4" --ondisk=sda --size=50000 part / --fstype="ext4" --ondisk=sda --size=100000 part /boot --fstype="ext4" --ondisk=sda --size=1000 %packages --> 安裝包選擇 @^minimal @core kexec-tools %end -->各個模塊要以%end結束 %addon com_redhat_kdump --enable --reserve-mb='auto' %end %post -->安裝完成后運行自定義腳本 useradd ffu echo 123456|passwd --stdin ffu rm -rf /etc/yum.repos.d/* cat > /etc/yum.repos.d/base.repo << EOF [base] name=base baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/ gpgcheck=0 EOF %end %anaconda pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty %end [root@centos7 ks]#chmod a+r centos7-m.cfg --> 為other加讀權限,以便能通過http訪問 [root@centos7 ks]#ll -rw-r--r--. 1 root root 2042 Jul 24 15:00 centos7-m.cfg
5. 安裝源準備
[root@centos7 ks]# mount /dev/sr0 /var/www/html/centos7/
6. 啟動菜單、內核等相關文件準備
a.相關文件準備
[root@centos7 tftpboot]#mkdir /var/lib/tftpboot/pxelinux.cfg [root@centos7 tftpboot]#cp /var/www/html/centos7/isolinux/isolinux.cfg pxelinux.cfg/default -->復制安裝光盤啟動菜單并重命名 [root@centos7 tftpboot]#cp /usr/share/syslinux/menu.c32 /usr/share/syslinux/pxelinux.0 ./ -->復制syslinux的菜單及啟動文件 [root@centos7 tftpboot]#cp /var/www/html/centos7/isolinux/vmlinuz /var/www/html/centos7/isolinux/initrd.img ./ [root@centos7 tftpboot]#tree -->目錄結構 . ├── initrd.img ├── menu.c32 ├── pxelinux.0 ├── pxelinux.cfg │ └── default └── vmlinuz
b.啟動菜單定制
[root@centos7 tftpboot]#vi pxelinux.cfg/default default menu.c32 -->所用菜單文件 timeout 600 menu title PXE CentOS Linux 7 Install Menu label linux-mini menu label ^Auto-install CentOS Linux 7 Mini kernel vmlinuz append initrd=initrd.img ks=http://192.168.196.188/ks/centos7-m.cfg -->ks文件由PXE Sever http服務提供 label linux-desktop menu label Test Auto-install CentOS Linux 7 ^Desktop kernel vmlinuz append initrd=initrd.img ks=http://192.168.196.188/ks/centos7.cfg -->可根據不同目的定制不同應答文件 label linux-manual menu label Test ^Manual-Install CentOS Linux 7 kernel vmlinuz append initrd=initrd.img label local menu label Boot from ^local drive menu default localboot 0xffff
四、主機自動化安裝系統實現
a.使用VMware實驗,根據以上步驟配置PXE Server,然后新建虛擬機
b.開機進入啟動菜單,選擇最小化安裝
c.安裝完成
原創文章,作者:ffu,如若轉載,請注明出處:http://www.www58058.com/82613