1、PXE简介


1)PXE协议

PXE(Preboot Excution Environment): 预启动执行环境,Intel公司研发,基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统

PXE工作原理:

网卡上的PXE芯片有512字节,存放了DHCP和TFTP的客户端

启动计算机并选择网卡启动

PXE上的DHCP客户端会向DHCP服务器,申请IP地址

DHCP服务器分配一个IP址地给它,同时DHCP配置文件还告诉PXE客户端TFTP服务器的地址,并去下载一个pxelinux.0的文件

  next-server 192.168.128.140;

  filename "pxelinux.0";

pxelinux.0告诉PXE要下载的配置文件是pxelinux.cfg目录下面的default

PXE下载并依据配置文件的内容下载启动必须的文件,并通过kickstart.cfg配置文件开始安装系统

2)kickstart

Kickstart是一种无人值守的安装方式。它的工作原理是在安装过程中记录典型的需要人工干预填写的各种参数,并生成一个名为ks.cfg的文件。如果在安装过程中(不只局限于生成Kickstart安装文件的机器)出现要填写参数的情况,安装程序首先会去查找Kickstart生成的文件,如果找到合适的参数,就采用所找到的参数;如果没有找到合适的参数,便需要安装者手工干预了。所以,如果Kickstart文件涵盖了安装过程中可能出现的所有需要填写的参数,那么安装者完全可以只告诉安装程序从何处取ks.cfg文件,然后就去忙自己的事情。等安装完毕,安装程序会根据ks.cfg中的设置重启系统,并结束安装。


2、基础网络建设与搭建DHCP服务

1)系统环境说明:

需关闭防火墙以及selinux

# cat /etc/redhat-release   
CentOS Linux release 7.6.1810 (Core) 
# uname -r
3.10.0-862.9.1.el7.x86_64
# systemctl stop firewalld  #暂时关闭防火墙
# setenforce 0  #暂时关闭selinux

2)配置静态IP地址并重起网卡

# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.146.141
NETMASK=255.255.255.0
GATEWAY=192.168.146.2
DNS1=202.106.0.20
# systemctl restart network

2)安装配置DHCP服务

dhcp服务用来为PXE客户机提供IP地址,并告诉客户机启动文件名和启动文件所在的服务器地址

如果使用vmware虚拟机实验需要关闭虚拟机的DHCP服务,否则会有冲突

# yum install dhcp -y

配置DHCP服务:

vim /etc/dhcp/dhcpd.conf

default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
option domain-name-servers 202.106.0.20;   #DNS地址可配置2个用逗号分隔
ignore client-updates;
subnet 192.168.146.0 netmask 255.255.255.0 {
  range 192.168.146.100 192.168.146.200;    #宣告获取地址段
  option subnet-mask 255.255.255.0;         #子网
  option routers 192.168.146.2;             #获取网关地址
  next-server 192.168.146.141;              #启动文件服务器地址,也就是本机地址
  filename "pxelinux.0";                    #启动文件名
}

#配置开机自启动并启动服务

# systemctl enable dhcpd
# systemctl start dhcpd

#查看DHCP监听端口UDP 67

netstat -lntup|grep 67

3、搭建TFTP服务

yum -y install tftp tftp-server

#配置自启动并启动服务

# systemctl enable tftp.socket
# systemctl start tftp.socket

#查看服务

#TFTP端口号为UDP的69

netstat -lntup|grep 69

如果加载到地址后停留在TFTP状态,有可能是TFTP服务出现问题,在centos6中tftp服务使用xinetd代理启动,需要安装xinetd,而在centos7中只安装tftp即可,可通过systemctl来管理服务,查看或管理服务时使用tftp.socket,systemctl restart tftp.socket

4、搭建HTTP服务

yum -y install httpd

#配置自启动&&启动http服务

# systemctl enable httpd
# systemctl start httpd

#查看服务状态

netstat -lntup|grep 80

#使用浏览器测试
http://192.168.146.141

5、组建PXE服务环境

1)安装syslinux包

#如不安装syslinux包就没有pxelinux.0文件

yum -y install syslinux
find / -type f -name "pxelinux.0" 
/usr/share/syslinux/pxelinux.0

#将pxelinux.0拷贝到tftp服务目录

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot

2)挂载linux系统盘到http服务根目录,拷贝linux内核和初始化文件

# mkdir -p /var/www/html/centos7
# mount /dev/cdrom /var/www/html/centos7
# cp -a /var/www/html/centos7/isolinux/* /var/lib/tftpboot/

#在tftp服务目录下创建pxelinux.cfg目录

#mkdir -p /var/lib/tftpboot/pxelinux.cfg

#配置启动菜单文件,将系统自带的启动引导菜单文件拷贝到TFTP目录下改名为default

cp /var/www/html/centos7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

#修改启动菜单文件

#cat /var/lib/tftpboot/pxelinux.cfg/default

default vesamenu.c32
timeout 600

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# 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 CentOS 7.6
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.146.141/ks.cfg  #修改指定kickstart文件的位置 

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
	If the system will not boot, this lets you access files
	and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
	If your system is having issues, a problem with your
	system's memory may be the cause. Use this utility to
	see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end


6、创建kickstart文件

1)安装system-config-kickstart
yum -y install system-config-kickstart
#注意安装桌面服务才能启动KICKSTART,创建应答文件:
/usr/bin/system-config-kickstart

我们新安装的系统root目录下的anaconda-ks.cfg文件就是我们安装这个系统的kickstart文件,可以拿来做简单修改即可使用,如修改安装介质为http等

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use HTTP installation media
url --url="http://192.168.146.141/centos7/"
# Use graphical install可以使用文本模式安装text
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=PJYC7

# Root password
rootpw --iscrypted $6$ZTOD6McFmRzF4KUv$oAHIEiZbPjlJH1K/G4XC0fhYxNr5d/EMgzqCEw/lCZhwMPnSBpIqnaqgZaDAKc5BWe.LpBRgOuRxd1HImxXPo.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="ext4" --ondisk=sda --grow --size=1
reboot
%packages
@^minimal
@compat-libraries
@core
@debugging
@development
@system-admin-tools
chrony
tree
net-tools
vim
%end

%addon com_redhat_kdump --disable --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

参数说明:

install:这是一次全新安装,而升级为upgrade

url --url= :通过FTP或HTTP服务器上的安装数安装,如:url --url=http://192.168.146.141/contos7/  ftp:url --url ftp://<username>:<password>@<server>/<dir>

text:使用文本模式安装

lang:设置安装语言以及系统的缺省语言,如:lang en_US.UTF-8

keyboard:设置键盘类型

zerombr:清除mbr引导信息

bootloader:系统引导配置

network:配置网络信息

timezone:设置系统时区

authconfig:设置系统认证信息

rootpw:设置root密码

clearpart:清空分区

part:磁盘分区

reboot:设定安装完成后重启系统

%packages:指定需要安装的软件包或组,@表示选择的包组

%post:安装完后执行的操作

#将应答脚本拷贝到http目录,并赋予权限

cp ks.cfg /var/www/html/
chown apache.apache /var/www/html/ks.cfg

6、启动客户机安装

注意:

客户机要和服务器在同一个局域网内;

如安装centOS7需要2G内存以上,否知会报错

如果使用vmware做实验,需要关闭VMware自带的DHCP功能

排除思路:
1)客户机是否获取IP地址,可查看网络和DHCP服务
2)查看链接TFTP是否正常
如链接正常在获取IP地址后链接TFTP加载配置成功会显示OK:
TFTP prefix:
trying to load: pxelinux.cfg/default ok
然后加载内核:
loading vmlinuz........
loading initrd.img.........
3)加载完内核后会链接下载安装系统文件,如tfp,http获取等