Deploy the pxe (Preboot eXecution Environment) remote installation service on the server side in Linux to install the system remotely for the client and combine with Kickstart configuration to realize unattended automatic installation

1. Introduction of PXE service

1. What is PXE and PXE server

PXE is a network boot technology developed by Intel. It works in Client / Server mode, allowing clients to download boot images from remote servers via the network and load installation files or the entire operating system. PXE can boot and install multiple operating systems such as Windows and Linux.
The PXE remote installation server integrates the CentOS 7 installation source, TFTP service, and DHCP service, and can send PXE boot program, Linux kernel, boot menu and other data to the client bare metal, as well as provide installation files.

2. Prerequisites that must be met to build PXE services
  • The client's network card supports the PXE protocol (integrated BOOTROM chip), and the motherboard supports network boot.
  • There is a DHCP server in the network to automatically assign addresses and specify the location of the boot file for clients.
  • The server provides the download of boot image files through TFTP (Trivial File Transfer Protocol).
    Note: The first condition is actually the hardware requirement. At present, most servers and most PCs can provide this support. You only need to allow booting from Network or LAN in the BIOS settings.
3. Advantages of PXE service
  • Scale : install multiple servers at the same time;
  • Automation : install systems, configure various services;
  • Remote realization : No CD, U disk and other installation media are required.
4. PXE working principle

1. After the Client is turned on, it sends a broadcast to request an IP address from the DHCP server. After the DHCP service on the PXE Server receives the request, it verifies that the client's MAC and other information are passed, and then sends an available IP address to the Client and starts the file pxelinux.0 The storage address (provided by TFTP) is sent to the Client

2. The Client that has obtained the address requests the pxelinux.0 file from the TFTP server, and the PXE Server (TFTP service) sends the file size information of pxelinux.0 to the Client. The Client verifies that if the file size information is acceptable, it returns the consent information, and then the TFTP service sends Client sends pxelinux.0 file

3. The Client executes the pxelinux.0 file, and then requests the configuration information file from TFTP, (pxelinux.cfg/default in the working directory of the TFTP service), and the TFTP service sends the configuration file to the Client

4. The client requests the Linux kernel and root file system from the TFTP service, and the TFTP service transfers the Linux kernel (vmlinuz) and root file system (initrd.img) files to the client

5. Client starts the Linux kernel, then gets kickstart and system installation files (ks.cfg) through the http protocol, and automatically installs CentOS according to the kickstart file (unattended automatic installation is realized through Kickstart configuration)

2. Build a PXE remote installation server to install the Linux system

1. Build an experimental environment for PXE remote installation server

First of all, there are two servers with graphics cards with PXE protocol, one is built as a PXE remote installation server and the Linux operating system has been installed, and the other is used as a client to install the Linux system using the PXE server.
The server deployment is ready to check the deployment configuration connection

2. Prepare Linux installation source
[root@localhost ~]# mkdir -p /var/ftp/centos7         //创建一个放安装文件的目录
[root@localhost ~]# cp -rf /mnt/* /var/ftp/centos7/   
 //将挂载到mnt目录下的所有光盘文件复制到刚创建的目录中
3. Install and enable FTP and TFTP services
[root@localhost ~]# yum -y install vsftpd     //安装FTP服务
已加载插件:fastestmirror, langpacks
centos                                                   | 3.6 kB     00:00     
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-22.el7 将被 安装
--> 解决依赖关系完成
......
已安装:
  vsftpd.x86_64 0:3.0.2-22.el7                                                  
完毕!
[root@localhost ~]# systemctl start vsftpd             //启动FTP服务
[root@localhost ~]# systemctl enable vsftpd             //开机自动启动FTP服务
[root@localhost ~]# yum -y install tftp-server.x86_64     //安装TFTP服务
.........
已安装:
  tftp-server.x86_64 0:5.2-13.el7                                               
完毕!
[root@localhost ~]# vi /etc/xinetd.d/tftp   //编辑TFTP的配置文件,设置启动项 
//TFTP服务由tftp-server软件包提供,默认由xinetd超级服务进行管理,因此配置文件位于/etc/xinetd.d/tftp
........
server_args             = -s /var/lib/tftpboot   //指定的TFTP根目录
disable                 = no                              //将yes改为no  开机自启
.......
[root@localhost ~]# systemctl start tftp     //开启tftp服务
4. Prepare the Linux kernel and initialize the image file

The Linux kernel (vmlinuz) and root file system (initrd.img) used for PXE network installation can be obtained from the mounted CD and located under the folder /mnt/images/pxeboot. Find these two files and copy them to the root directory of the tftp service.

[root@localhost ~]# cd /mnt/images/pxeboot/    //进入这个这个目录
[root@localhost pxeboot]# ls    -l               //查看目录
总用量 53041  
-rw-r--r-- 1 root root 48434768 9月   5 2017 initrd.img          //根文件系统( initrd.img)
-r--r--r-- 1 root root      441 9月   6 2017 TRANS.TBL
-rwxr-xr-x 1 root root  5877760 8月  23 2017 vmlinuz           // Linux 内核(vmlinuz)
[root@localhost pxeboot]# cp vmlinuz /var/lib/tftpboot       //复制文件到TFTP的根目录下
[root@localhost pxeboot]# cp initrd.img /var/lib/tftpboot
5. PXE boot program and boot menu file

5.1. The boot program for PXE network installation is pxelinux.0, which is provided by the software package syslinux. Install the software package syslinux, the default installation directory is /usr/share/syslinux/, and then copy the file pxelinux.0 in the directory to the root directory of the tftp service.
Note : Boot Loader: It is a small program stored in the MBR (Master Boot Record) or GUID (Globally Unique Identifier) ​​partition table to help load the operating system into the memory. If there is no bootloader, then your operating system will not be able to load into memory.

[root@localhost pxeboot]# yum -y install syslinux
......
已安装:
  syslinux.x86_64 0:4.05-13.el7                                                 
完毕!
[root@localhost pxeboot]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ 
 //复制文件到TFTP的根目录下

5.2. The boot menu is used to guide the boot process of the client, including how to call the kernel and how to load the initial image. The default boot menu file is default, which should be placed in the pxelinux.cfg subdirectory of the tftp root directory. The boot menu configuration is established manually.
The boot menu has three guide entries, namely graphical installation (default), text installation, and rescue mode. Among them, prompt is used to set whether to wait for user selection; label is used to define and separate startup items; kernel and append are used to define boot parameters.

[root@localhost ~]# mkdir /var/lib/tftpboot/pxelinux.cfg           //创建一个启动菜单引导的目录
[root@localhost ~]# vi /var/lib/tftpboot/pxelinux.cfg/default    //编辑启动菜单引导的配置文件
default auto        //指定默认接入口名称
prompt 1           //1 表示等待用户选择安装   0表示不等待用户选择安装,自动安装时为0
label auto          //图形安装(默认安装)
        kernel vmlinuz              //引导加载内核
        append initrd=initrd.img method=ftp://20.0.0.20/centos7
          // 引导加载根文件系统镜像文件,以及文件获取的方式和位置
label linux text         //文本安装  
        kernel vmlinuz       
        append text initrd=initrd.img method=ftp://20.0.0.20/centos7
label linux rescue           //救援模式
        kernel vmlinuz
        append rescue initrd=initrd.img method=ftp://20.0.0.20/centos7
6. Install and enable DHCP service

Since the PXE client is usually a bare metal without a system installed, in order to get in touch with the server and download the relevant boot file correctly, it is necessary to configure the DHCP service in advance to automatically assign an address and inform the location of the boot file. The IP address of the PXE server is 20.0.0.20, the DHCP address pool is 20.0.0.55~20.0.0.155, and the subnet mask is 255.255.255.0.

[root@localhost ~]# yum -y install dhcp       //安装dhcp服务
......
已安装:
dhcp.x86_64 12:4.2.5-58.el7.centos                                            
完毕!                                                            //安装完成
[root@localhost ~]# vi /etc/dhcp/dhcpd.conf
subnet 20.0.0.0 netmask 255.255.255.0 {         //定义dhcp服务的作用域
option routers 20.0.0.20;                              //指定当前作用域的网关
option domain-name-servers 8.8.8.8;            //指定当前作用域DNS服务器
range 20.0.0.100 20.0.0.200;                      //指定当前作用域中可供分配的IP地址范围
next-server 20.0.0.20;                                 //指定TFTP服务器的地址
filename "pxelinux.0";                                 //指定PXE引导程序的文件名
}
[root@localhost ~]# systemctl start dhcpd        //开启dhcp服务
[root@localhost ~]# systemctl enable dhcpd     //设置为开机自启

7. Verify PXE network installation

7.1, change the client's preferred startup item

After setting up the PXE remote installation server, you can use the client to perform the installation test.

  • For newly purchased servers or bare PCs, no additional settings are generally required;
  • To reinstall the system for a host with an existing system, you may need to modify the BIOS settings, set " Boot First " to "NETWORK" or "LAN", and then restart the host;
  • If you use a virtual machine created by VMware for testing, the virtual machine's memory must be at least 2GB , otherwise an error will be reported when starting the installation.
    First boot the client, enter the first item of BIOS setup
    Insert picture description here
7.2, PXE installation process

Insert picture description here
Insert picture description here

7.3, PXE installation is complete

Insert picture description here

Three, Kickstart configuration to achieve unattended automatic installation

Using PXE technology to remotely install the CentOS 7 system, the installation medium is no longer limited to CD-ROM, mobile hard disk and other equipment, greatly improving the flexibility of system installation. However, a series of interactive operations such as manual language selection, keyboard type, and installation source designation are still required during installation, which is very inconvenient when batch installation is required. By using the Kickstart tool to configure the installation answer file, various settings during the installation process are automatically completed, thereby eliminating the need for manual intervention and improving the efficiency of network installation.

1. Prepare to install the answer file

After installing the system-config-kickstart tool in the system, you can configure the installation answer file through the graphical wizard tool. If the user is familiar with the configuration of the automatic answer file, he can also directly edit the answer file automatically created after installation (/root/anaconda-ks.cfg), and use it after appropriate revision as needed.

[root@localhost ~]# yum -y install system-config-kickstart    //安装图形界面kickstart应用程序
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
......
已安装:
  system-config-kickstart.noarch 0:2.9.6-1.el7                                  
作为依赖被安装:                                                 //YUM会自动安装依赖关系包
  gnome-python2.x86_64 0:2.28.1-14.el7                                          
......                                          
完毕!          //安装成功

Open the kickstart application to edit
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

2. Enable Q&A files

Once the answer file for automatic installation is available, just place it in the FTP directory of the PXE installation server and modify the
boot menu appropriately to realize batch automatic installation based on the network.

[root@localhost ~]# cp ks.cfg /var/ftp/centos7/    //将文件复制到FTP目录下
[root@localhost ~]# vi /var/lib/tftpboot/pxelinux.cfg/default     //编辑启动菜单引导文件
default auto
prompt 0                 //0表示不等待用户选择
label auto
  kernel vmlinuz
  append initrd=initrd.img method=ftp://20.0.0.20/centos7 ks=ftp://20.0.0.20/ks.cfg  
                                  //获取自动安装应答文件的方式和端口
//最小化安装需要在ks.cfg文件中最后一行下面添加配置信息  (重要)   不添加无法自动安装      
  [root@localhost ~]# vi /var/ftp/centos7/ks.cfg 
   .............
   
%packages
@^minimal
%end
//图形界面安装需要在ks.cfg文件中最后一行下面添加配置信息  (重要)   不添加无法自动安装  
  [root@localhost ~]# vi /var/ftp/centos7/ks.cfg 
   .............
   
   //配置信息可以从图形界面安装的系统中的家目录下的anaconda-ks.cfg 文件中找到复制到ks.cfg中
 %packages  
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
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
3. Verify unattended installation

Insert picture description hereInsert picture description here
Insert picture description here
Verify that the script is complete after unattended installation

[root@localhost ~]# vi /etc/yum.repos.d/centos7.repo 
[base]
name=CentOS7.6
baseurl=ftp://20.0.0.20/centos7
enabled=1
gpgcheck=0                                                  //脚本编译完成

Guess you like

Origin blog.csdn.net/wulimingde/article/details/107791861