基于linux下的PXE预启动执行环境

什么是PXE?

PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,
工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支
持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP或MTFTP
协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件
设置,从而引导预先安装在服务器中的终端操作系统。PXE可以引导多种操作系统。
PXE最直接的表现是,在网络环境下工作站可以省去硬盘,但又不是通常所说的无盘站的概念,
因为使用该PXE网卡-WY574T,PXE网卡-WY574T(2张)技术的PC在网络方式下的运行速度要比
有盘PC快3倍以上。当然使用PXE的PC也不是传统意义上的TERMINAL终端,因为使用了PXE的PC
并不消耗服务器的CPU,RAM等资源,故服务器的硬件要求极低。

PXE启动原理:

当计算机引导时,BIOS把 PXE Client 调入内存中执行,然后由 PXE Client 将放置在远端
的文件通过网络下载到本地运行。 
PXE是RPL的升级品,它是Preboot Execution Environment的缩写。
它们的不同之处在于RPL是静态路由,而PXE是动态路由。其通信协议采用TCP/IP,
与Internet连接高效而可靠,PXE无盘工作站的启动过程分析如下:
¨ 客户端个人电脑开机后, 在 TCP/IP Bootrom 获得控制权之前先做自我测试。
¨ Bootprom 送出 BOOTP/DHCP 要求以取得 IP。
¨ 如果服务器收到个人电脑所送出的要求, 就会送回 BOOTP/DHCP 回应,内容包括
客户端的 IP 地址, 预设网关, 及开机映像文件。否则,服务器会忽略这个要求。
¨ Bootprom 由 TFTP 通讯协议从服务器下载开机映像文件。
¨ 个人电脑通过这个开机映像文件开机, 这个开机文件可以只是单纯的开机程式也可
以是操作系统。
¨ 开机映像文件将包含 kernel loader 及压缩过的 kernel,此 kernel 将支持NTFS root
系统。
¨ 远程客户端根据下载的文件启动机器。

将desktop虚拟机进行配置(ip,yum源),server虚拟机进行测试:

在desktop:

[root@localhost ~]# yum install syslinux dhcp httpd tftp-server -y  安装所需要服务
httpd       ##共享安装资源
dhcp        ##分配ip以及让客户主机获取pxelinux.0文件
pxelinux.0  ##预安装环境头文件,用来指定读取后续文件的程序
tftp-server ##提供tftp协议,令客户主机可以读取到预安装环境
syslinux    ##提供pxelinux.0文件

这里写图片描述

确保关闭防火墙开启所需服务进行更改配置文件断网测试:

[root@localhost ~]# systemctl stop firewalld  关闭防火墙
[root@localhost ~]# systemctl start httpd 开启httpd服务
[root@localhost ~]# systemctl enable httpd 开机自启动
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@localhost ~]# vim /etc/xinetd.d/tftp 启用tftp服务

这里写图片描述

[root@localhost ~]# systemctl start xinetd.service  开启服务
[root@localhost ~]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# ls
[root@localhost tftpboot]# scp [email protected]:/home/kiosk/Desktop/isolinux/* . 将主机isolinux里面的东西复制过来
The authenticity of host '172.25.254.84 (172.25.254.84)' can't be established.
ECDSA key fingerprint is c8:a1:05:9a:0f:ca:ad:4c:61:d1:df:f2:53:e1:b4:a1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.84' (ECDSA) to the list of known hosts.
root@172.25.254.84's password: 
Permission denied, please try again.
root@172.25.254.84's password: 
boot.cat                                      100% 2048     2.0KB/s   00:00     传输成功
boot.msg                                      100%   84     0.1KB/s   00:00    
grub.conf                                     100%  321     0.3KB/s   00:00    
initrd.img                                    100%   34MB  33.9MB/s   00:01    
isolinux.bin                                  100%   24KB  24.0KB/s   00:00    
isolinux.cfg                                  100% 3166     3.1KB/s   00:00    
memtest                                       100%  172KB 172.4KB/s   00:00    
splash.png                                    100%  186     0.2KB/s   00:00    
TRANS.TBL                                     100% 2438     2.4KB/s   00:00    
upgrade.img                                   100%   32MB  32.2MB/s   00:00    
vesamenu.c32                                  100%  152KB 152.1KB/s   00:00    
vmlinuz                                       100% 4787KB   4.7MB/s   00:00    
[root@localhost tftpboot]# ls查看
boot.cat  grub.conf   isolinux.bin  memtest     TRANS.TBL    vesamenu.c32
boot.msg  initrd.img  isolinux.cfg  splash.png  upgrade.img  vmlinuz
[root@localhost tftpboot]# mkdir pxelinux.cfg 制作预安装环境
[root@localhost tftpboot]# cp isolinux.cfg pxelinux.cfg/default
[root@localhost tftpboot]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf  配置dhcp
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y

这里写图片描述

制作预安装环境:

这里写图片描述

[root@localhost tftpboot]# vim /etc/dhcp/dhcpd.conf  编辑dhcp配置文件
option domain-name "example.com";   ##客户主机域名
option domain-name-servers 172.25.254.254; ##客户主机获得到的dns
default-lease-time 600;         ##默认租约期
max-lease-time 7200;            ##最长租约期
log-facility local7;            ##日志级别
subnet 172.25.254.0 netmask 255.255.255.0 { ##网段,子网掩码
  range 172.25.254.60 172.25.254.70;    ##ip地址池
  option routers 172.25.254.250;    ##客户主机获得到的网关
  filename "pxelinux.0";        ##客户主机获得到的预安装环境头文件
  next-server 172.25.254.100;       ##指定头文件所在主机的ip
}

这里写图片描述

[root@localhost tftpboot]# systemctl restart dhcpd  重启服务
[root@localhost tftpboot]# systemctl status dhcpd  查看dhcp状态确保开启
dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled)
   Active: active (running) since Sat 2018-06-30 09:23:56 EDT; 7s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 2846 (dhcpd)
   CGroup: /system.slice/dhcpd.service
           └─2846 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -gr...

Jun 30 09:23:56 localhost systemd[1]: Starting DHCPv4 Server Daemon...
Jun 30 09:23:56 localhost systemd[1]: Started DHCPv4 Server Daemon.
Jun 30 09:23:56 localhost dhcpd[2846]: Internet Systems Consortium DHCP Ser....5
Jun 30 09:23:56 localhost dhcpd[2846]: Copyright 2004-2013 Internet Systems...m.
Jun 30 09:23:56 localhost dhcpd[2846]: All rights reserved.
Jun 30 09:23:56 localhost dhcpd[2846]: For info, please visit https://www.i...p/
Jun 30 09:23:56 localhost dhcpd[2846]: Not searching LDAP since ldap-server...le
Jun 30 09:23:56 localhost dhcpd[2846]: Internet Systems Consortium DHCP Ser....5
Jun 30 09:23:56 localhost dhcpd[2846]: Copyright 2004-2013 Internet Systems...m.
Jun 30 09:23:56 localhost dhcpd[2846]: All rights reserved.
Jun 30 09:23:56 localhost dhcpd[2846]: For info, please visit https://www.i...p/
Jun 30 09:23:56 localhost dhcpd[2846]: Wrote 0 leases to leases file.
Jun 30 09:23:56 localhost dhcpd[2846]: Listening on LPF/eth0/52:54:00:00:54...24
Jun 30 09:23:56 localhost dhcpd[2846]: Sending on   LPF/eth0/52:54:00:00:54...24
Jun 30 09:23:56 localhost dhcpd[2846]: Sending on   Socket/fallback/fallback-net
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost tftpboot]# getenforce 
Disabled
[root@localhost tftpboot]# cd pxelinux.cfg/
[root@localhost pxelinux.cfg]# ls
default

这里写图片描述

在真机进行配置server虚拟机从网络启动:(下列更改测试时必须断网测试)

[root@foundation84 ~]# virt-manager

这里写图片描述

然后可以看到默认启动界面:

这里写图片描述

1,更改开机界面时长:

[root@localhost pxelinux.cfg]# vim default 编辑配置文件

这里写图片描述

在server虚拟机测试已经变成5秒:

这里写图片描述

2,更改停留位置为第一行:

[root@localhost pxelinux.cfg]# vim default  更改配置文件

这里写图片描述

在server虚拟机测试已经停留在第一行:

这里写图片描述

3,更改开机界面:

需注意图片必须是.png结尾大小也必须是640*480否则替换不成功,均已经测试 我们可以使用convert命令裁剪图片,用法可以百度。

[root@localhost tftpboot]# ls    寻找一张照片640*480的放到/var/lib/tftpboot/路径下即可
1         boot.msg    isolinux.bin  pxelinux.0    TRANS.TBL     vmlinuz
2.png     grub.conf   isolinux.cfg  pxelinux.cfg  upgrade.img
boot.cat  initrd.img  memtest       splash.png    vesamenu.c32
[root@localhost tftpboot]# vim pxelinux.cfg/default  修改配置文件更改开机图片

这里写图片描述

查看效果开机已经变成了替换的文件:

这里写图片描述

4,从真机开始加载yum源:

[root@localhost pxelinux.cfg]# vim default  编辑配置文件

这里写图片描述

查看效果看不到加载过程:

这里写图片描述

5,开机安装时可以看到加载过程:

[root@localhost pxelinux.cfg]# vim default 

这里写图片描述

查看效果看得到加载过程:

这里写图片描述

设定自动化部署:

[root@localhost pxelinux.cfg]# yum install system-config-kickstart -y  安装kickstart
[root@localhost ~]# system-config-kickstart   配置kickstart
/usr/share/system-config-kickstart/kickstartGui.py:104: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page size is deprecated
  xml = gtk.glade.XML ("/usr/share/system-config-kickstart/system-config-kickstart.glade", domain="system-config-kickstart")
Loaded plugins: langpacks

之前kickstart有很详细的说明可翻看之前的博客,挨个配置即可:

常规密码设置:
这里写图片描述
yum源:
这里写图片描述
安装什么:
这里写图片描述
分区:
这里写图片描述
网络:
这里写图片描述
防火墙:
这里写图片描述
安装好了自动配置yum源:
这里写图片描述
保存:
这里写图片描述

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
ks.cfg
[root@localhost html]# vim ks.cfg  编辑文件

这里写图片描述

可以在网页看到我们设置的ks.cfg内容:

这里写图片描述

[root@localhost html]# ksvalidator ks.cfg  只可以检查语法是否正确
[root@localhost html]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# ls
boot.cat   initrd.img    memtest       splash.png   vesamenu.c32
boot.msg   isolinux.bin  pxelinux.0    TRANS.TBL    vmlinuz
grub.conf  isolinux.cfg  pxelinux.cfg  upgrade.img

这里写图片描述

自动安装:

[root@localhost tftpboot]# vim pxelinux.cfg/default  编辑配置文件读取ks.cfg文件实现自动安装

这里写图片描述

用server进行测试,不需要按什么等待其自动安装即可:

这里写图片描述

编辑配置文件添加自己想添加的标题:

[root@localhost tftpboot]# vim pxelinux.cfg/default 编辑配置文件

主标题:

这里写图片描述

副标题:

这里写图片描述

server进行测试为我们自己设置的标题:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/80834411