PXE批量网络装机

发布时间 2023-08-08 18:57:47作者: Bacolate

PXE高效批量网络装机

系统装机的三种引导方式

1.硬盘

2.光驱(u盘)

3.网络启动 pxe

系统安装过程

  • 加载boot loader

Boot Loader 是在操作系统内核运行之前运行的一段小程序。通过这段小程序,我们可以初始化硬件设备、建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,以便为最终调用操作系统内核准备好正确的环境

  • 加载启动安装菜单

  • 加载内核和initrd系统(文件才能使用文件)

  • 加载根系统()

  • 运行anaconda的安装向导

Linux安装光盘的安装相关文件

在系统光盘的isolinux目录下有和安装相关的文件

  • boot.cat: 相当于grub的第一阶段

  • isolinux.bin:光盘引导程序,在mkisofs的选项中需要明确给出文件路径,这个文件属于SYSLINUX项目

  • isolinux.cfg:启动菜单的配置文件,当光盘启动后(即运行isolinux.bin),会自动去找isolinux.cfg文件

  • vesamenu.c32:是光盘启动后的启动菜单图形界面,也属于SYSLINUX项目,menu.c32提供纯文本的菜单

  • memtest:内存检测程序

  • splash.png:光盘启动菜单界面的背景图

  • vmlinuz:是内核映像

  • initrd.img:ramfs文件(精简版的linux系统,文件系统驱动等)

 

准备四大文件:内核vmlinuz、驱动initrd.img/:精简版

1、什么是PXE

PXE 的全称是 preboot execute environment它其实是Intel在很久以前提出来的一项用于通过网络来引导系统的标准。允许客户机通过网络从远程服务器上下载引导镜像,并加载安装文件或者整个操作系统。

pxe是通过网卡引导启动

1.1实现的硬件条件

1.BIOS支持

2.网卡支持(现在基本都已支持)

3.需要在bios中开启开关,服务器一般没打开(bmc界面里打开)

4.网络中有一台 DHCP 服务器以便为客户机自动分配地址、指定引导文件位置 网卡自带DHCP功能 dhcp 指明分配的ip地址

5.服务器通过 tftp服务(简单文件传输协议)来提供系统内核和引导镜像的下载 udp()协议 网卡自带这个功能(不支持其他的远程共享服务)

自带的站点(/var/lib/tftp?) initrd.img(驱动) vmlinuz(内核) pxelinux.cfg/default(引导文件) pxelinux.0(小型的grub程序) tftp 指明?

2、实现过程讲解

1.网卡需要查找相关的dhcp服务器(获取地址时间)

2.找到后dhcp服务器提供ip地址,和引导程序(boot loader)的地址 还提供给客户机TFTPserver地址(dhcp本身不提供tftp服务)

3.网卡使用tftp客户端吧引导程序加载到内存中来

4.bios执行引导程序

5.引导程序会去TFTP去查找配置文件

6.根据配置文件去引导安装系统

7.系统安装过程

image-20230808135144526

3、搭建过程中服务详解

PXE 远程安装服务器集成了 CentOS 7 安装源、TFTP 服务、DHCP 服务,能 够向客户机裸机发送 PXE 引导程序、Linux 内核、启动菜单等数据,以及提供安装文件。

3.1 TFTP服务

TFTP(简单文件传输协议):内核和引导文件

是一种基于UDP协议实现的用于在客户机和服务器之间进行简单文件传输的协议,适合于小型文件传输的应用场合。TFTP服务默认由xinetd服务进行管理,使用UDP69端口。

3.2 vsftp

可以使用http ftp等方式,ftp文件传输的一种。

3.3 syslinux

syslinux是一个功能强大的引导加载程序,而且兼容各种介质。它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。它的安装很简单,一旦安装syslinux好之后,sysLinux启动盘就可以引导各种基于DOS的工具,以及MS-DOS/Windows或者任何其它操作系统。

pxelinux.0用于启动pxe

3.4 DHCP服务

分配IP地址,让客户机有能力联网

pxe是通过网卡引导启动、

网卡自带:IP地址、tftp软件功能

  • 请求IP地址

  • 返回IP地址和PXE文件位置

4、pxe优点

1.规模化:同时装配多台服务器 2.自动化:安装系统、配置各种服务 3.远程实现:不需要光盘、U盘等安装介质

5、操作过程

大概过程

  1. 分配给同一局域网内新加机器的地址(配置文件) dhcp 分配地址 指明tftp 服务器的地址

  2. tftp服务端开启udp,配置成默认关闭

  3. 安装syslinux取得pxelinux.0 文件

  4. 安装vsftpd服务挂载软件安装源(mount /dev/sr0 /var/ftp/centos7)

  5. 将四大文件拷入(/var/lib/tftpboot )

  • initrd.img

  • pxelinux.0(类似grub)

  • pxelinux.cfg/default(手写) 

  • vmlinuz

6.将服务开启,关闭防火墙

具体操作步骤:

安装、关防火墙

 ##先全都安装一下
 [root@31yml ~]#yum install tftp-server   vsftpd   syslinux  dhcp   -y
 ##别忘了关闭防火墙
 [root@31yml ~]#systemctl stop firewalld
 [root@31yml ~]#setenforce 0

 

将配置文件模板复制到dhcp目录下

 ###
 [root@31yml ~]#cat /etc/dhcp/dhcpd.conf //提示你模板在哪
 #
 # DHCP Server Configuration file.
 #   see /usr/share/doc/dhcp*/dhcpd.conf.example
 #   see dhcpd.conf(5) man page
 #
 [root@31yml ~]#yum install dhcp -y
 [root@31yml ~]#cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/dhcpd.conf
 ​
 [root@31yml ~]#cd /etc/dhcp/
 [root@31yml dhcp]#ls
 dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
 [root@31yml dhcp]#vim dhcpd.conf
 ​
 subnet 192.168.177.0 netmask 255.255.255.0 {
   range 192.168.177.10 192.168.177.50;
   option routers 192.168.177.2;
   next-server 192.168.177.100;
   filename "pxelinux.0";
 }
 [root@31yml dhcp]#systemctl start dhcpd
 ###
 [root@31yml dhcp]#yum install tftp-server.x86_64 -y
 [root@31yml dhcp]#rpm -ql tftp-server
 /etc/xinetd.d/tftp
 /usr/lib/systemd/system/tftp.service
 /usr/lib/systemd/system/tftp.socket
 /usr/sbin/in.tftpd
 /usr/share/doc/tftp-server-5.2
 /usr/share/doc/tftp-server-5.2/CHANGES
 /usr/share/doc/tftp-server-5.2/README
 /usr/share/doc/tftp-server-5.2/README.security
 /usr/share/man/man8/in.tftpd.8.gz
 /usr/share/man/man8/tftpd.8.gz
 /var/lib/tftpboot

 

拷贝

 ##安装tftp,装过也无碍
 [root@localhost isolinux]# yum install tftp-server -y
 [root@localhost isolinux]# rpm -ql tftp-server 
 /etc/xinetd.d/tftp  ##配置文件
 /usr/lib/systemd/system/tftp.service
 /usr/lib/systemd/system/tftp.socket
 /usr/sbin/in.tftpd
 /usr/share/doc/tftp-server-5.2
 /usr/share/doc/tftp-server-5.2/CHANGES
 /usr/share/doc/tftp-server-5.2/README
 /usr/share/doc/tftp-server-5.2/README.security
 /usr/share/man/man8/in.tftpd.8.gz
 /usr/share/man/man8/tftpd.8.gz
 /var/lib/tftpboot   ##站点,四文件放置路径
 [root@31yml ~]#vim /etc/xinetd.d/tftp 
 ​
   1 # default: off
   2 # description: The tftp server serves files using the trivial file transfer \
   3 #       protocol.  The tftp protocol is often used to boot diskless \
   4 #       workstations, download configuration files to network-aware printers, \
   5 #       and to start the installation process for some operating systems.
   6 service tftp
   7 {
   8         socket_type             = dgram
   9         protocol                = udp
  10         wait                    = yes
  11         user                    = root
  12         server                  = /usr/sbin/in.tftpd
  13         server_args             = -s /var/lib/tftpboot
  14         disable                 = no
  15         per_source              = 11
  16         cps                     = 100 2
  17         flags                   = IPv4
  18 }
 ​
 #每项详解
 service tftp
 {
         socket_type             = dgram
         protocol                = udp
         wait                    = no    //wait no表示客户机可以多台一起连接,yes表示客户机只能一台一台连接,表示是否开启多线程一起工作,否则需要等待
         user                    = root
         server                    = /usr/sbin/in.tftpd
         server_args             = -s /var/lib/tftpboot    //指定TFTP根目录(引导文件的存储路径) -c允许上传
         disable                 = no   //disable no表示开启TFTP服务 
         per_source              = 11    //通过限制一个主机的最大连接数,从而防止某个主机独占某个服务,这里每个IP地址的连接数是11个
         cps                     = 100 2   //表示服务器最多启动100个连接,如果达到这个数目将停止启动新服务2秒。在此期间不接受任何请求
         flags                   = IPv4
 }

 

拷贝pxelinux.0

 ##先安装软件,安装过也无碍
 [root@31yml ~]#yum install syslinux -y      //引导程序文件需要先安装此服务
 [root@31yml ~]#rpm -ql syslinux |grep pxelinux.0        //安装完成后找到该文件的地址
 /usr/share/syslinux/gpxelinux.0
 /usr/share/syslinux/pxelinux.0
 [root@localhost isolinux]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/    //拷贝到站点文件里

 

拷贝initrd.img与vmlinuz

配置pxelinux.cfg/default

 ##先安装软件,安装过也无碍
 [root@31yml ~]#yum install vsftpd -y
 [root@31yml ~]#rpm -ql vsftpd  
 /etc/logrotate.d/vsftpd
 /etc/pam.d/vsftpd
 /etc/vsftpd
 /etc/vsftpd/ftpusers
 /etc/vsftpd/user_list
 /etc/vsftpd/vsftpd.conf
 /etc/vsftpd/vsftpd_conf_migrate.sh
 /usr/lib/systemd/system-generators/vsftpd-generator
 /usr/lib/systemd/system/vsftpd.service
 /usr/lib/systemd/system/vsftpd.target
 /usr/lib/systemd/system/vsftpd@.service
 /usr/sbin/vsftpd
 /usr/share/doc/vsftpd-3.0.2
 /usr/share/doc/vsftpd-3.0.2/AUDIT
 /usr/share/doc/vsftpd-3.0.2/BENCHMARKS
 /usr/share/doc/vsftpd-3.0.2/BUGS
 /usr/share/doc/vsftpd-3.0.2/COPYING
 /usr/share/doc/vsftpd-3.0.2/Changelog
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README.configuration
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README.configuration
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/hosts.allow
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README.configuration
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/logins.txt
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2
 /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2/README
 /usr/share/doc/vsftpd-3.0.2/FAQ
 /usr/share/doc/vsftpd-3.0.2/INSTALL
 /usr/share/doc/vsftpd-3.0.2/LICENSE
 /usr/share/doc/vsftpd-3.0.2/README
 /usr/share/doc/vsftpd-3.0.2/README.security
 /usr/share/doc/vsftpd-3.0.2/REWARD
 /usr/share/doc/vsftpd-3.0.2/SECURITY
 /usr/share/doc/vsftpd-3.0.2/SECURITY/DESIGN
 /usr/share/doc/vsftpd-3.0.2/SECURITY/IMPLEMENTATION
 /usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
 /usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
 /usr/share/doc/vsftpd-3.0.2/SIZE
 /usr/share/doc/vsftpd-3.0.2/SPEED
 /usr/share/doc/vsftpd-3.0.2/TODO
 /usr/share/doc/vsftpd-3.0.2/TUNING
 /usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
 /usr/share/man/man5/vsftpd.conf.5.gz
 /usr/share/man/man8/vsftpd.8.gz
 /var/ftp
 /var/ftp/pub##
 ​
 [root@31yml ~]#cd /var/ftp/
 [root@31yml ftp]#mkdir centos7
 [root@31yml ftp]#mount /dev/sr0 /var/ftp/centos7/
 mount: /dev/sr0 写保护,将以只读方式挂载
 [root@31yml ftp]#cd centos7/
 [root@31yml centos7]#ls
 CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
 EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
 [root@31yml centos7]#cd images/
 [root@31yml images]#ls
 efiboot.img  pxeboot  TRANS.TBL
 [root@31yml images]#cd pxeboot/
 [root@31yml pxeboot]#ls
 initrd.img  TRANS.TBL  vmlinuz
 [root@31yml pxeboot]#cp initrd.img vmlinuz /var/lib/tftpboot/
 [root@31yml pxeboot]#cd /var/lib/tftpboot/
 [root@31yml tftpboot]#mkdir pxelinux.cfg
 [root@31yml tftpboot]#cd pxelinux.cfg/
 [root@31yml pxelinux.cfg]#vim default
 ​
 default linux
 timeout 600
 label linux
   kernel vmlinuz
   append initrd=initrd.img method=ftp://192.168.177.100/centos7
 label text
   kernel vmlinuz
   append text initrd=initrd.img method=ftp://192.168.177.100/centos7
 ​
 label rescue
   kernel vmlinuz
   append rescue initrd=initrd.img method=ftp://192.168.177.100/centos7
 ​
 ##关闭防火墙#启动程序
 [root@31yml tftpboot]#systemctl stop firewalld
 [root@31yml tftpboot]#systemctl start dhcpd
 [root@31yml tftpboot]#systemctl start tftp
 [root@31yml tftpboot]#systemctl start vsftpd

 


 
问题:

如果客户机卡在这样的提示

img

表明,它没有获取到IP地址,找不到你配置的文件,我就是如此,发现是网关配错了,修改对后有后续了

 

如果出现过这样的信息,表明服务器防火墙没关,拒绝提供下载,一定要关。

image-20230808164850513

 

6、kickstart无人值守安装

kickstart无人值守安装(脚本、自动化)

注意:CentOS 8,7,6 不同版本的kickstart文件格式不尽相同,不可混用

 [root@localhost tftpboot]#yum install system-config-kickstart -y
 #安装图形化工具

 

基本配置:

image-20230808172425139

安装方法:

image-20230808173938828

引导文件:

image-20230808172642809

分区:

image-20230808173045175

image-20230808174054585

image-20230808174118346

image-20230808174140845

添加网卡

image-20230808183337058

验证配置可以不用添加,防火墙配置可用直接禁用

image-20230808173546015

取消图形化安装

image-20230808173619651

保存

image-20230808183434136

选择文件路径,在/var/ftp/下保存

image-20230808183526998

image-20230808183602483

image-20230808183657948

再到终端里添加配置文件

 [root@31yml ~]#cd /var/ftp/
 [root@31yml ftp]#ls
 #查看应答文件
 centos7  ks.cfg  pub
 [root@31yml ftp]#cd
 [root@31yml ~]#ls
 anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
 ​
 [root@localhost ftp]#cd /var/lib/tftpboot/
 [root@localhost tftpboot]#ls
 initrd.img  menu.c32  pxelinux.0  pxelinux.cfg  vmlinuz
 [root@localhost tftpboot]#cd pxelinux.cfg
 [root@localhost pxelinux.cfg]#vim default 
 #将应答文件 位置指明
 ​
 default menu.c32
 prompt 1
 ​
 label auto
         menu label Auto Install CentOS Linux ^7
         kernel vmlinuz
         append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
 ​
 label text
         kernel vmlinuz
         append text initrd=initrd.img method=ftp://192.168.100.100/centos7
 ​
 ​
 label rescue
         kernel vmlinuz
         append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7

 

之后再次启动虚拟机,就会自动安装并配置系统