什么是PXE

PXE is Pro-Boot Execution Environment

  • 进行 PXE 安装的必要条件是在要安装的计算机中必须包含一个 PXE 支持的网卡(NIC),即网卡中必须要有 PXE Client。PXE 协议可以使计算机通过网络启动。
  • 此协议分为 Client端和 Server 端,而PXE Client则在网卡的 ROM 中。当计算机引导时,BIOS 把 PXE Client 调入内存中执行,然后由 PXE Client 将放置在远端的文件通过网络下载到本地运行。
  • 运行 PXE 协议需要设置 DHCP 服务器和 TFTP 服务器。
  • DHCP 服务器会给 PXE Client(将要安装系统的主机)分配一个 IP 地址,由于是给 PXE Client 分配 IP 地址,所以在配置 DHCP 服务器时需要增加相应的 PXE 设置。此外,在 PXE Client 的 ROM 中,已经存在了 TFTP Client,那么它就可以通过 TFTP 协议到 TFTP Server 上下载所需的文件了。

PXE的工作过程

  1. PXE Client 从自己的PXE网卡启动,向本网络中的DHCP服务器索取IP;
  2. DHCP 服务器返回分配给客户机的IP 以及PXE文件的放置位置(该文件一般是放在一台TFTP服务器上) ;
  3. PXE Client 向本网络中的TFTP服务器索取pxelinux.0 文件;
  4. PXE Client 取得pxelinux.0 文件后之执行该文件;
  5. 根据pxelinux.0 的执行结果,通过TFTP服务器加载内核和文件系统;
  6. 进入安装画面, 此时可以通过选择HTTP、FTP、NFS 方式之一进行安装;
    详细工作流程,请参考下面这幅图:

什么是Kickstart

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

安装DHCP + tftp

YUM 安装信息

1
2
yum install -y dhcp tftp-server vim lrzsz xinetd net-tools syslinux rsync system-config-kickstart httpd

其他操作

关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭selinux
sed -i “s/enforcing/disabled/g” /etc/selinux/config
setenforce 0

配置DHCP

1
2
3
4
5
6
[root@localhost dhcp]# cat dhcpd.conf 
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#

配置

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost dhcp]# cat dhcpd.conf 
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.35.0 netmask 255.255.255.0 {
range 192.168.35.10 192.168.35.200;
option routers 192.168.35.1;
next-server 192.168.35.177;
filename="pxelinux.0";
}

[root@localhost dhcp]#

systemctl enable dhcpd
systemctl start dhcpd

监听端口
netstat -tulnp |grep :67

配置tftp,// tftp服务提供压缩内核和系统引导程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root@localhost ~]# cat /etc/xinetd.d/tftp 
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = yes
per_source = 11
cps = 100 2
flags = IPv4
}
[root@localhost ~]#

配置

修改这一项为”disable =no”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# systemctl start tftp
[root@localhost ~]# systemctl status tftp
● tftp.service - Tftp Server
Loaded: loaded (/usr/lib/systemd/system/tftp.service; indirect; vendor preset: disabled)
Active: active (running) since 五 2021-09-24 16:37:24 CST; 1s ago
Docs: man:in.tftpd
Main PID: 1610 (in.tftpd)
CGroup: /system.slice/tftp.service
└─1610 /usr/sbin/in.tftpd -s /var/lib/tftpboot

9月 24 16:37:24 localhost.localdomain systemd[1]: Started Tftp Server.
[root@localhost ~]# systemctl enable tftp
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
[root@localhost ~]# systemctl restart xinetd
[root@localhost ~]# systemctl enable xinetd

监听端口
netstat -tulnp |grep :69

pxelinux.0 网络引导程序

1
2
3
4
[root@localhost tftpboot]# pwd
/var/lib/tftpboot
[root@localhost tftpboot]# ls
[root@localhost tftpboot]#

查看下pxelinux.0文件是否存在
yum install mlocate -y yum源处已经添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost tftpboot]# locate pxelinux.0
[root@localhost tftpboot]#
#以上查询不到,换种方式
[root@localhost tftpboot]# yum provides "*/pxelinux.0"
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.bupt.edu.cn
* updates: mirrors.cn99.com
base/7/x86_64/filelists_db | 7.2 MB 00:00:01
extras/7/x86_64/filelists_db | 259 kB 00:00:00
updates/7/x86_64/filelists_db | 6.1 MB 00:00:00
syslinux-4.05-15.el7.x86_64 : Simple kernel loader which boots from a FAT filesystem
源 :base
匹配来源:
文件名 :/usr/share/syslinux/pxelinux.0



syslinux-tftpboot-4.05-15.el7.noarch : SYSLINUX modules in /var/lib/tftpboot, available for network booting
源 :base
匹配来源:
文件名 :/var/lib/tftpboot/pxelinux.0



[root@localhost tftpboot]# rpm -aq |grep syslinux
[root@localhost tftpboot]#

#可知syslinux包没有安装
#yum install syslinux -y

[root@localhost tftpboot]# rpm -ql syslinux |grep pxe
/usr/share/doc/syslinux-4.05/pxelinux.txt
/usr/share/syslinux/gpxecmd.c32
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/gpxelinuxk.0
/usr/share/syslinux/pxechain.com
/usr/share/syslinux/pxelinux.0

pxelinux.0路径/usr/share/syslinux/pxelinux.0
拷贝pxelinux.0到/var/lib/tftpboot

1
2
3
4
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost tftpboot]# ls
pxelinux.0
[root@localhost tftpboot]#

传统linux操作系统启动
1.BIOS自检
2.读取引导程序
2.1 GRUb(一段固定的可执行代码)
2.2 BIOS执行引导程序
2.3 引导程序读取配置文件(/boot/grub/grub.conf)

PXE
1.客户端启动系统,选择网卡启动
2.从DHCP服务器中获取到了IP地址等信息
3.还获取到了tftp server及网络引导程序pxelinux.0
4.通过网卡读取到tftp server(/var/lib/tftpboot)上的pxelinux.0,读取到内存中
5.在内存中执行引导程序
6.读取引导程序的配置文件(var/lib/tftpboot/pxelinux.0/default)
7.读取default内容

挂载ios镜像

mount -o loop CentOS-7-x86_64-Minimal-1908.iso /mnt/

配置pxelinux.cfg 文件

mkdir /var/lib/tftpboot/pxelinux.cfg

1
2
3
4
5
6
[root@localhost isolinux]# cp isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default 
cp:是否覆盖"/var/lib/tftpboot/pxelinux.cfg/default"? y
[root@localhost isolinux]# ls
boot.cat boot.msg grub.conf initrd.img isolinux.bin isolinux.cfg memtest splash.png TRANS.TBL vesamenu.c32 vmlinuz
[root@localhost isolinux]# cp * /var/lib/tftpboot/
[root@localhost isolinux]#

已经可以看到安装界面

扩展多个安装系统

这里面最重要的文件,vmlinuz initd.img

vmlinuz 内核
initd.img 驱动程序,一定要匹配OS

ios镜像挂载后,可以看到一个image文件夹,里面有个pxeboot

1
2
3
4
5
6
7
8
9
10
[root@localhost mnt]# ls
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
[root@localhost mnt]# cd images/
[root@localhost images]# ls
efiboot.img pxeboot TRANS.TBL
[root@localhost images]# cd pxeboot/
[root@localhost pxeboot]# ls
initrd.img TRANS.TBL vmlinuz
[root@localhost pxeboot]#

在/var/lib/tftpboot下创建一个kilig的文件夹
把vmlinuz initd.img拷贝到这个文件夹下

仿写安装界面

1
2
#cp /media/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/
#cp /media/isolinux/{boot.msg,vesamenu.c32,splash.jpg} /var/lib/tftpboot/

system-config-kickstart配置和httpd配置

安装system-config-kickstart httpd

1
2
3
4
[root@localhost mnt]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost mnt]# systemctl start httpd
[root@localhost mnt]#

安装桌面

  • tftp客户端测试:tftp

    1
    2
    3
    4
    5
    6
    7
     yum -y install tftp
    cd /var/lib/tftpboot
    touch tftp.test
    cd
    tftp 10.1.255.166
    tftp> get tftp.test
    tftp> quit
1
2
3
4
systemctl  start xinetd httpd dhcpd  tftp
systemctl enable xinetd httpd dhcpd tftp