PXE Server 部署
一、簡要描述
PXE(Preboot eXecution Environment) 支持客戶端通過網絡從服務器端下載系統鏡像,並進行安裝。在安裝過程中,可以通過 Kickstart 配置文件實現無人值守安裝,並定制操作系統。
二、PXE服務器組件
DHCP : 負責分配網絡 IP 地址,並通過 DHCP 包來指定系統啟動文件的。
syslinux: 預啟動程序
TFTP: PXE 客戶端通過 TFTP 獲取啟動文件。
FTP: PXE 客戶端通過 FTP 下載系統鏡像內容。
三、初始化服務器
1、為 PXE Server 配置固定 IP 地址
[it@pxesvr ~]$ cat /etc/sysconfig/network-scripts/ifcfg-ens192
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens192
DEVICE=ens192
ONBOOT=yes
IPADDR=10.10.10.53
PREFIX=24
GATEWAY=10.10.10.1
DNS1=10.10.10.1
2、關閉SELinux、防火牆
sed -i 's/=enforcing/=disabled/i' /etc/selinux/config
四、搭建配置DHCP服務器
1、安裝DHCP服務器
[it@pxesvr ~]$ sudo yum install dhcp-server -y
2、配置DHCP
[it@pxesvr ~]$ sudo cat /etc/dhcp/dhcpd.conf | grep -Ev '^#|^$'
allow bootp;
allow booting;
default-lease-time 600;
max-lease-time 7200;
option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;
subnet 10.10.10.0 netmask 255.255.255.0 {
option routers 10.10.10.1;
range 10.10.10.100 10.10.10.199;
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server 10.10.10.53; #pxe server IP
if option architecture-type = 00:07 {
filename "uefi/BOOTX64.EFI"; # UEFI boot
} else {
filename "/pxelinux.0"; # BIOS boot
}
}
}
3、啟動 DHCP 服務,並配置開機啟動
[it@pxesvr ~]$ sudo systemctl enable dhcpd.service
[it@pxesvr ~]$ sudo systemctl start dhcpd.service
五、配置syslinux
1、安裝syslinux-tftpboot
[it@pxesvr ~]$ sudo yum install syslinux-tftpboot -y
2、配置啟動菜單
只有兩個菜單,一個 Install Red Hat Enterprise Linux 8.0,一個是 Boot from local drive(默認選擇),並在第一個菜單配置啟動系統鏡像所需的文件,以及下載系統鏡像的目錄。
* 啟動菜單文件 default 你可以通過示例文件修改獲得,也可以自己新建獲得
[it@pxesvr ~]$ sudo mkdir /tftpboot/pxelinux.cfg
[it@pxesvr ~]$ sudo cp /os/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default
[it@pxesvr ~]$ sudo vim /tftpboot/pxelinux.cfg/default
[it@pxesvr ~]$ sudo cat /tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 600
display boot.msg
menu title Red Hat Enterprise Linux 8.0
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
menu label ^Install Red Hat Enterprise Linux 8.0
kernel vmlinuz
append initrd=initrd.img inst.stage2=ftp://10.10.10.53/dvd quiet
label local
menu label ^Boot from local drive
menu default
localboot 0xffff
[it@pxesvr ~]$
3、復制系統鏡像啟動文件到對應位置
[it@pxesvr ~]$ sudo cp /os/isolinux/{boot.msg,vesamenu.c32} /tftpboot/
[it@pxesvr ~]$ sudo cp /os/images/pxeboot/{vmlinuz,initrd.img} /tftpboot/
六、搭建配置TFTP服務器
1、安裝TFTP服務器
pxe 客戶端通過 TFTP 獲取啟動文件
[it@pxesvr ~]$ sudo yum install tftp-server -y
2、配置TFTP服務器
修改 TFTP 的路徑為 /tftpboot
[it@pxesvr ~]$ sudo vim /usr/lib/systemd/system/tftp.service
[it@pxesvr ~]$ sudo cat /usr/lib/systemd/system/tftp.service
[Unit]
Description=Tftp Server
Requires=tftp.socket
Documentation=man:in.tftpd
[Service]
ExecStart=/usr/sbin/in.tftpd -s /tftpboot
StandardInput=socket
[Install]
Also=tftp.socket
3、啟動TFTP服務,並配置開機啟動
[it@pxesvr ~]$ sudo systemctl enable tftp
[it@pxesvr ~]$ sudo systemctl start tftp
七、搭建配置FTP服務器
1、安裝FTP服務器
用於 pxe client 下載系統鏡像文件
[it@pxesvr ~]$ sudo yum install vsftpd -y
2、配置 ftp 服務器
開啟匿名訪問,指定匿名訪問的目錄
[it@pxesvr ~]$ sudo vim /etc/vsftpd/vsftpd.conf
[sudo] password for it:
[it@pxesvr ~]$ sudo grep -e anonymous -e anon_root /etc/vsftpd/vsftpd.conf | grep -v ^#
anonymous_enable=YES
anon_root=/var/ftp
3、啟動 ftp 服務,並配置開機啟動
[it@pxesvr ~]$ sudo systemctl enable vsftpd.service
[it@pxesvr ~]$ sudo systemctl start vsftpd.service
4、創建 ftp 匿名訪問的目錄目錄,然后復制系統鏡像內容到該目錄
[it@pxesvr ~]$ sudo mkdir /var/ftp/dvd
[it@pxesvr ~]$ sudo cp -r /run/media/it/RHEL-8-0-0-BaseOS-x86_64/* /var/ftp/dvd/
八、測試啟動
1、通過 BIOS(Legacy ) 的方式啟動客戶端主機進行測試

2、配置支持UEFI啟動
①、配置UEFI啟動菜單
it@pxesvr ~]$ sudo mkdir /tftpboot/uefi
[sudo] password for it:
[it@pxesvr ~]$ sudo cp -r /os/EFI/BOOT/* /tftpboot/uefi/
[it@pxesvr ~]$ vim /tftpboot/uefi/grub.cfg
[it@pxesvr ~]$ sudo vim /tftpboot/uefi/grub.cfg
[it@pxesvr ~]$ sudo cat /tftpboot/uefi/grub.cfg
[sudo] password for it:
set default="1"
function load_video {
insmod efi_gop
insmod efi_uga
insmod video_bochs
insmod video_cirrus
insmod all_video
}
load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2
set timeout=60
### END /etc/grub.d/00_header ###
search --no-floppy --set=root -l 'RHEL-8-0-0-BaseOS-x86_64'
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Install Red Hat Enterprise Linux 8.0' --class fedora --class gnu-linux --class gnu --class os {
linuxefi vmlinuz inst.stage2=ftp://10.10.10.53/dvd quiet
initrdefi initrd.img
}
menuentry 'Test this media & install Red Hat Enterprise Linux 8.0' --class fedora --class gnu-linux --class gnu --class os {
linuxefi vmlinuz inst.stage2=ftp://10.10.10.53/dvd quiet
initrdefi initrd.img
}
②、重啟服務
[it@pxesvr ~]$ sudo systemctl restart dhcpd.service
[it@pxesvr ~]$ sudo systemctl restart tftp.socket
③、測試UEFI方式啟動客戶端主機進行測試

九、Kickstart自動應答
Kickstart 文件是實現系統自動安裝的應答文件。
1、示例文件
#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Use graphical install
graphical
# Reboot after installation
reboot
repo --name="AppStream" --baseurl=ftp://10.10.10.53/dvd/AppStream
# Use network installation
url --url="ftp://10.10.10.53/dvd"
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# Run the Setup Agent on first boot
firstboot --disable
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=ens192 --ipv6=auto --activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted $6$VrOUsKRz8dywq5D5$1SbE62Vu/YPtzqd.4Y0U/M6fqILDluHAPFU5Kt9GkAS8eZVC7.yA/aBCzkWbNx0MJicFBR2MmipOItFBdqW6X.
# X Window System configuration information
xconfig --startxonboot
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
user --groups=wheel --name=it --password=$6$fI8GAHa5Q6JG1BB3$6/Dv5ndCa4KMIkVFBkmVzvnV.SvwSO5.h0ppaaI75f53G9dH4.l6yUR.IUYGTKX/ctKwyzliKJQtOpV7TAZqI. --iscrypted --gecos="it"
%packages
@^graphical-server-environment
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
2、Kickstart文件說明
ignoredisk: 在安裝過程中控制對磁盤的連接,如果你使用的是自動分區,那么在安裝過程中可以通過 ignoredisk --driver=sdc 指定自動分區忽略的磁盤,如果你有多個磁盤,並且只想使用其中一個進行自動分區,並安裝操作系統,你可以使用 ignoredisk --only-use=sda 指定只使用 sda 這一個磁盤。
autopart: 自動創建根分區( / ),交換分區(swap)和適當的啟動分區(/boot),在足夠大的驅動器上,還會創建 /home 分區。通過 --type= 指定分區類型,如示例中使用的lvm。你還可以通過 --fstype= 指定系統文件類型,如 xfs,ext4 等等,默認是 xfs。你還可以通過 --encrypted 選項,對所有分區進行加密。
clearpart: 在分區前,刪除現有分區。可以通過 --drives= 指定要刪除分區的磁盤,也可以通過 --all 清除所有設備的分區。
graphical: 安裝過程以圖形化的方式進行,就和手動安裝一樣。也可以使用 text 命令,以文本的方式顯示安裝過程。
reboot: 安裝完成后重啟。
repo: 指定額外的軟件包安裝源。
url: 通過網絡安裝時,安裝文件的位置。
keyboard: 指定鍵盤布局。通過 --vckeymap= 指定應使用哪個 VConsole 鍵盤模式。通過 --xlayouts= 指定鍵盤的布局。
firstboot: 第一次啟動系統時,是否顯示 Initial Setup。如果啟用,則必須安裝 initial-setup 軟件包。如果未指定,則默認禁用這個選項。--enable 表示啟動 Initial Setup,--disable 表示禁用 Initial Setup。如果你使用 --enable ,你還可以使用 --reconfig 選項在引導系統時啟動重配置(reconfiguration)模式。在這個模式下,你可以從新配置系統語言,鍵盤鼠標,root 密碼,系統安全級別,時區以及一些網絡配置。
lang: 配置系統語言。
network: 網絡配置。使用 --bootproto= 指定計算機獲取 IP 的方式,是 dchp 還是 static。使用 --device= 指定要配置的設備名稱。使用 --activate 激活該設備。如果你的 --bootproto= 使用的是 static,那么你可以使用 ip= 指定 IP 地址,--netmask= 指定子網掩碼,--gateway= 指定網關,--nameserver= 指定 DNS。使用 ipv6= 指定 IPv6 配置,使用auto 表示自動配置。使用 --hostname 配置主機名。
rootpw: 指定 root 用戶的密碼。--iscrypted 表示密碼以密文的方式顯示。--plaintext 表示以明文的方式顯示密碼。你還可以使用 --lock 鎖定 root 用戶。
* 當你使用 --iscrypted 時,可以在一台已經安裝好系統的機器上,通過 python 生成密碼的密文(如果是 Python 3,則對應的命令應該是 python3.)。
$ python -c 'import crypt; print(crypt.crypt("My Password"))'
xconfig: 配置 X Windows 系統。--startxonboot 表示在安裝的系統中使用圖形界面登錄。
services: 配置服務的默認狀態。--disabled= 指定默認禁用的服務,使用 --enabled= 指定默認啟用的服務。
timezone: 指定時區。
user: 添加額外的用戶。通過 --name= 指定用戶名,通過 --groups= 指定用戶要添加的組(除默認組),通過 --password= 指定該用戶的密碼,--iscrypted 表示以密文的方式,通過--gecos= 定義用戶的 GECOS信息,如,用戶的全名等等。
%packages 表示要安裝的軟件包。
通過 @ 指定軟件包組,如:
%packages
@core
@X Window System
@Desktop
%end
通過 @^ 指定環境組,如:
%packages
@^graphical-server-environment
%end
直接輸入軟件包名,指定獨立的軟件包,如:
%packages
@core
@desktop
sqlite
curl
%end
通過 - 號排錯軟件包組中的某個軟件包
%packages
@Development Tools
-pkgconf
-pkgconf-m4
-pkgconf-pkg-config
-redhat-rpm-config
%end
* %package 要以 %end 結尾
%addon 配置 kdump。--enable 表示開啟,並通過 --reserve-mb= 指定為 kdump 預留的內存大小,auto 表示自動。要以 %end結束。
%anaconda 部分指定了 password 策略,同樣以 %end 結束。
你還可以通過 %pre 指定在磁盤分區前要執行的腳本,通過 %post 指定系統安裝完成后要執行的腳本。這些模塊都需要 %end 結束。
更多關於 kickstart 應答文件的語法可以參考官方網站的系統安裝指南。到目前為止,紅帽官網還沒有關於 RHEL 8 的安裝指南,你可以參考 RHEL 7 的安裝指南進行編寫。
3、在啟動菜單文件中添加 kickstart 應答路徑
編輯好 kickstart 應答文件后,將其保存成 ks.cfg (文件名可以自定義),上傳到 ftp 根目錄中(路徑也是自定義的),如該示例的文件路徑是:ftp://10.10.10.53/ks.cfg
①、BISO(Legacy)方式啟動:
[it@pxesvr ~]$ sudo vim /tftpboot/pxelinux.cfg/default
[it@pxesvr ~]$ sudo cat /tftpboot/pxelinux.cfg/default
... ... ... ...
... ... ... ...
label linux
menu label ^Install Red Hat Enterprise Linux 8.0
kernel vmlinuz
append initrd=initrd.img inst.stage2=ftp://10.10.10.53/dvd inst.ks=ftp://10.10.10.53/ks.cfg quiet
... ... ... ...
... ... ... ...
②、UEFI 方式啟動:
[it@pxesvr ~]$ sudo vim /tftpboot/uefi/grub.cfg
[it@pxesvr ~]$ sudo cat /tftpboot/uefi/grub.cfg
[sudo] password for it:
... ... ... ...
... ... ... ...
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Install Red Hat Enterprise Linux 8.0' --class fedora --class gnu-linux --class gnu --class os {
linuxefi vmlinuz inst.stage2=ftp://10.10.10.53/dvd inst.ks=ftp://10.10.10.53/ks.cfg quiet
initrdefi initrd.img
}
... ... ... ...
... ... ... ...
至此,你就可以通過 PXE Server 進行自動安裝 RHEL 8 操作系統了。
