一、基本原理簡介
PXE 是一種系統引導的方式。進行 PXE 安裝的必要條件是要安裝的計算機中包含一個 PXE 支持的網卡(NIC Network Information Center),即網卡中必須要有 PXE Client。
PXE(Pre-boot Execution Environment)是由Intel設計的協議,它可以使計算機通過網絡啓動。協議分爲client和server兩端,PXE client在網卡的ROM中,當計算機引導時,BIOS把PXE client調入內存執行,並顯示出命令菜單,經用戶選擇後,PXE client將放置在遠端的操作系統通過網絡下載到本地運行。
既然是通過網絡傳輸,就需要IP地址;也就是說在其啓動過程中,客戶端請求服務器分配IP地址,之後 PXE Client 使用 TFTP Client 通過TFTP(Trivial File Transfer Protocol 簡單文件傳輸協議)協議下載啓動安裝程序所需的文件。
簡單地說PXE網絡安裝,客戶機通過支持PXE的網卡向網絡中發送請求DHCP信息的廣播請求IP地址等信息,DHCP服務器給客戶端提供IP地址和其它信息(TFTP服務器、啓動文件等),之後請求並下載安裝需要的文件。在這個過程中需要一臺服務器來提供啓動文件、安裝文件、以及安裝過程中的自動應答文件等。
二、自動安裝系統的詳細工作過程
第一步:PXE Client向DHCP發送請求
首先,將支持PXE的網絡接口卡(NIC)的客戶端的BIOS設置成爲網絡啓動,通過PXE BootROM(自啓動芯片)會以UDP(簡單用戶數據報協議)發送一個廣播請求,向網絡中的DHCP服務器索取IP地址等信息。
第二步:DHCP服務器提供信息
DHCP服務器收到客戶端的請求,驗證是否來至合法的PXE Client的請求,驗證通過它將給客戶端一個“提供”響應,這個“提供”響應中包含了爲客戶端分配的IP地址、pxelinux啓動程序(TFTP)位置,以及配置文件所在位置。
第三步:PXE客戶端請求下載啓動文件
客戶端收到服務器的“迴應”後,會迴應一個幀,以請求傳送啓動所需文件。這些啓動文件包括:pxelinux.0、pxelinux.cfg/default、vmlinuz、initrd.img等文件。
第四步:Boot Server響應客戶端請求並傳送文件
當服務器收到客戶端的請求後,他們之間之後將有更多的信息在客戶端與服務器之間作應答, 用以決定啓動參數。BootROM 由 TFTP 通訊協議從Boot Server下載啓動安裝程序所必須的文件(pxelinux.0、pxelinux.cfg/default)。default文件下載完成後,會根據該文件中定義的引導順序,啓動Linux安裝程序的引導內核。
第五步:請求下載自動應答文件
客戶端通過pxelinux.cfg/default文件成功的引導Linux安裝內核後,安裝程序首先必須確定你通過什麼安裝介質來安裝linux,如果是通過網絡安裝(NFS, FTP, HTTP),則會在這個時候初始化網絡,並定位安裝源位置。
或許你會說,剛纔PXE不是已經獲取過IP地址了嗎?爲什麼現在還需要一次?這是由於PXE獲取的是安裝用的內核以及安裝程序等,而安裝程序要獲取的是安裝系統所需的二進制包以及配置文件。
由於它們需要的內容不同造成PXE模塊和安裝程序是相對獨立的,PXE的網絡配置並不能傳遞給安裝程序。從而進行兩次獲取IP地址過程。
接着會讀取該文件中指定的自動應答文件ks.cfg所在位置,根據該位置請求下載該文件。
第六步:客戶端安裝操作系統
將ks.cfg文件下載回來後,通過該文件找到OS Server,並按照該文件的配置請求下載安裝過程需要的軟件包。
OS Server和客戶端建立連接後,將開始傳輸軟件包,客戶端將開始安裝操作系統。安裝完成後,將提示重新引導計算機。這個時候注意,在重新引導的過程中一定要將BIOS修改回從硬盤啓動,不然的話又會重複的自動安裝操作系統。
在上面介紹中PXE client是需要安裝Linux的計算機,TFTP Server、DHCP Server和NFS Server運行在另外一臺Linux Server上。Bootstrap文件(引導加載器)、配置文件、Linux內核都放置在Linux Server上TFTP服務器的根目錄下。而Linux根文件系統存放於NFS Server的共享目錄中。
PXE client在工作過程中,需要三個二進制文件:bootstrap、Linux 內核和Linux根文件系統。Bootstrap文件是可執行程序,它向用戶提供簡單的控制界面,並根據用戶的選擇,下載合適的Linux內核以及Linux根文件系統。
三、Kickstart簡介
KickStart的工作原理是通過記錄典型的安裝過程中所需人工干預填寫的各種參數,並生成一個名爲ks.cfg的文件;在其後的安裝過程中(不只侷限於生成KickStart安裝文件的機器)當出現要求填寫參數的情況時,安裝程序會首先去查找KickStart生成的文件,當找到合適的參數時,就採用找到的參數,當沒有找到合適的參數時,才需要安裝者手工干預。如果KickStart文件涵蓋了安裝過程中出現的所有需要填寫的參數時,安裝者完全可以只告訴安裝程序從何處取ks.cfg文件,實現在動安裝系統。
四、自動部署環境介紹
① 配置DHCP,用於給客戶端提供IP地址及其它信息
② 配置TFTP服務器,用於提供客戶端PXE引導所必須的文件
③ 配置NFS服務器,用於存放安裝系統和應答文件
④ 配置Kickstart,用於自動應答安裝
⑤ 使用PXE功能引導客戶機
1.部署前的準備
關閉系統的iptables防火牆和selinux
永久禁用selinux:
vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled #此項改爲disable # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are pro tected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
CentOS6.5:
[root@kickstart sys]# service iptables stop [root@kickstart sys]# setenforce 0 [root@kickstart sys]# chkconfig iptables off
CentOS7:
[root@kickstart sys]# service firewalld stop [root@kickstart sys]#systemctl disable firewalld.service [root@kickstart sys]# setenforce 0
2.部署dhcp服務
(1)安裝dhcpd服務
yum install dhcp -y
(2)修改dhcpd.conf配置文件
[root@kickstart sys]# cat /etc/dhcp/dhcpd.conf # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page ddns-update-style # 設置DNS的動態更新類型,style參數必須是:ad-hoc、interim 或者 none ignore client-updates; #不允許動態更新DNS,允許的話是allow allow booting; #定義能夠PXE啓動 allow bootp; #是否響應激活查詢,定義支持bootp default-lease-time 21600; #默認dhcp地址租期 max-lease-time 43200; #最大dhcp地址租期 option routers 192.168.5.1; #設定dhcp客戶端的默認路由 subnet 192.168.5.0 netmask 255.255.255.0 { #定義dhcp網絡 range dynamic-bootp 192.168.5.20 192.168.5.30; #定義dhcp地址範圍 next-server 192.168.5.100; #指定tftp服務器地址 filename "pxelinux.0"; #指定pxe引導文件 } #注意大括號結尾
(3)啓動dhcpd服務並檢查
[root@kickstart sys]# service dhcpd start [root@kickstart sys]# ps -ef|grep dhcp root 36482 1 0 04:14 ? 00:00:00 dhcpd -cf /etc/dhcp/dhcpd.conf [root@kickstart sys]#
注意:CenOS6.5部署dhcpd服務時,有時用service dhcpd start會失敗,可以直接用
dhcpd -cf /etc/dhcp/dhcpd.conf嘗試啓動,後面是的路徑是dhcpd的配置文件路徑
3.部署NFS服務
(1)安裝rpcbind和nfs服務
[root@kickstart sys]# yum install rpcbind nfs-utils -y
(2)創建nfs共享目錄,並更改目錄屬主
[root@kickstart sys]#mkdir -p /data/sys [root@kickstart sys]#chown nfsnobody.nfsnbody /data/sys
(3)修改nfs配置文件
[root@kickstart sys]#echo “/data/sys 192.168.5.0/24(rw,sync)” >/etc/exports
(4)啓動rpcbind和nfs服務(注意順序,先rpcbind後nfs)
[root@kickstart sys]#service rpcbind start [root@kickstart sys]#service nfs start
(5)檢查rpcbind和nfs服務
[root@kickstart sys]#/etc/init.d/nfs reload [root@kickstart sys]#rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100005 1 udp 58611 mountd 100005 1 tcp 47901 mountd 100005 2 udp 36752 mountd 100005 2 tcp 46315 mountd 100005 3 udp 43498 mountd 100005 3 tcp 34087 mountd 100003 2 tcp 2049 nfs 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 2 tcp 2049 nfs_acl 100227 3 tcp 2049 nfs_acl 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 2 udp 2049 nfs_acl 100227 3 udp 2049 nfs_acl 100021 1 udp 54387 nlockmgr 100021 3 udp 54387 nlockmgr 100021 4 udp 54387 nlockmgr 100021 1 tcp 59024 nlockmgr 100021 3 tcp 59024 nlockmgr 100021 4 tcp 59024 nlockmgr [root@kickstart sys]# showmount -e localhost Export list for localhost: /data 192.168.5.0/24
3.部署tftp服務
(1)安裝tftp服務
[root@kickstart sys]# yum install xinetd -y (centos7還要需要單獨安裝yum install tftp-server -y)
(2)修改tftp配置文件
[root@kickstart sys]# vim /etc/xinetd.d/tftp service tftp { socket_type = dgram protocol = udp wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /var/lib/tftpboot ##tftp根目錄,要記住 disable = no ###此項修改爲no per_source = 11 cps = 100 2 flags = IPv4 }
(3)啓動tftp服務並檢查
[root@kickstart sys]# service xinetd restart [root@kickstart sys]# netstat -lntup|grep 69 tcp 0 0 0.0.0.0:46955 0.0.0.0:* LISTEN 7532/rpc.mountd udp 0 0 0.0.0.0:69 0.0.0.0:* 7180/xinetd [root@kickstart sys]#
4.文件準備
(1)複製pxelinux.0到tftp根目錄
[root@kickstart yum.repos.d]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
如果沒有/usr/share/syslinux/pxelinux.0,請先安裝syslinux,安裝命令:yum install syslinux -y
(2)掛載系統光盤到/mnt,複製系統文件到nfs目錄
[root@kickstart dev]# mount /dev/cdrom /mnt/ mount: /dev/sr0 is write-protected, mounting read-only [root@kickstart dev]# cd /mnt/ [root@kickstart mnt]# ls CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7 EFI images Packages RPM-GPG-KEY-CentOS-Testing-7 EULA isolinux repodata TRANS.TBL [root@kickstart mnt]# cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/ [root@kickstart mnt]# cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
(3)在tftp根目錄創建pxelinux.cfg目錄,並將系統光盤中/isolinux/isolinux.cfg文件複製到pxelinux.cfg目錄下,並改變爲default
[root@kickstart tftpboot]# mkdir /var/lib/tftpboot/pxelinux.cfg [root@kickstart tftpboot]# cp /mnt/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default [root@kickstart tftpboot]# cat /var/lib/tftpboot/pxelinux.cfg/default default ks # 默認啓動的是“label linux”中標記的啓動內核 prompt 1#顯示“boot:”提示符。爲“0”時則不提示,將會直接啓動“default”參數中指定的內容。 timeout 600 # 在用戶輸入之前的超時時間,單位爲 1/10 秒,超時後會啓動“default”參數中指定的內容。 display boot.msg menu background splash.jpg menu title Welcome to CentOS 6.5! menu color border 0 #ffffffff #00000000 menu color sel 7 #ffffffff #ff000000 menu color title 0 #ffffffff #00000000 menu color tabmsg 0 #ffffffff #00000000 menu color unsel 0 #ffffffff #00000000 menu color hotsel 0 #ff000000 #ffffffff menu color hotkey 7 #ffffffff #ff000000 menu color scrollbar 0 #ffffffff #00000000 label ks #創建一個新的標籤,命名爲ks menu label ^Auto installed system #菜單提示 kernel vmlinuz append ks=nfs:192.168.5.100:/data/sys/ks.cfg initrd=initrd.img #指定 ks.cfg 配置文件的位置 label linux menu label ^Install or upgrade an existing system menu default kernel vmlinuz append initrd=initrd.img label vesa menu label Install system with ^basic video driver kernel vmlinuz append initrd=initrd.img xdriver=vesa nomodeset label rescue menu label ^Rescue installed system kernel vmlinuz append initrd=initrd.img rescue label local menu label Boot from ^local drive localboot 0xffff [root@kickstart sys]#
具體關於default配置說明,可以百度一下:pxelinux.cfg相關說明
(4)將ks.cfg文件,放到nfs共享目錄/data/sys,權限設爲755
ks.cfg自動應答文件生成有兩種方法:
1)安裝system-config-kickstart,命令:yum -y install system-config-kickstart, 在linux的GUI下,打開終端窗口輸入命令:system-config-kickstart,使用圖形窗口配置保存ks.cfg文件,如下圖:
2)直接修改root家目錄下的anaconda-ks.cfg,保存爲ks.cfg到nfs共享目錄/data/sys
ks.cfg內容如下:
# Kickstart file automatically generated by anaconda. #version=DEVEL install nfs --server=192.168.5.100 --dir=/data/sys lang en_US.UTF-8 keyboard us network --onboot yes --device eth0 --bootproto dhcp --noipv6 rootpw admin=12 firewall --disabled authconfig --enableshadow --passalgo=sha512 selinux --disabled timezone --utc Asia/Shanghai bootloader --location=mbr --driveorder=sda # The following is the partition information you requested # Note that any partitions you deleted are not expressed # here so unless you clear all partitions first, this is # not guaranteed to work #clearpart --none firstboot --disable logging --level=info zerombr clearpart --all part /boot --fstype=ext4 --size=200 part swap --size=1024 part / --fstype=ext4 --grow --size=200 reboot %packages @base @compat-libraries @core @debugging @development @dial-up @hardware-monitoring @network-tools @performance @server-policy @workstation-policy %post mkdir /scripts /tools useradd fenfa echo 123456|passwd --stdin fenfa echo "fenfa ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config %end [root@kickstart sys]# cat /data/sys/ks.cfg # Kickstart file automatically generated by anaconda. #version=DEVEL install nfs --server=192.168.5.100 --dir=/data/sys lang en_US.UTF-8 keyboard us network --onboot yes --device eth0 --bootproto dhcp --noipv6 rootpw admin=12 firewall --disabled authconfig --enableshadow --passalgo=sha512 selinux --disabled timezone --utc Asia/Shanghai bootloader --location=mbr --driveorder=sda # The following is the partition information you requested # Note that any partitions you deleted are not expressed # here so unless you clear all partitions first, this is # not guaranteed to work #clearpart --none firstboot --disable logging --level=info zerombr clearpart --all part /boot --fstype=ext4 --size=200 part swap --size=1024 part / --fstype=ext4 --grow --size=200 reboot %packages @base @compat-libraries @core @debugging @development @dial-up @hardware-monitoring @network-tools @performance @server-policy @workstation-policy %post mkdir /scripts /tools useradd fenfa echo 123456|passwd --stdin fenfa echo "fenfa ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config %end
kickstart自動應答文件ks.cfg的部分註釋見下圖
5.客戶機網卡設爲pxe啓動,開啓電源開始安裝
將需要安裝centos系統的電腦或服務器用網線設置爲pxe啓動,然後連入kickstart主機的網絡中,啓動即可