PXE network boot installation based automation of Linux system

PXE network boot installation based automation of Linux system

  In practice, the traditional pure manual installation of the operating system that has some limitations, for example: now most servers do not bring their own CD-ROM drive, an external optical drive to install the system needs to insert the disc, the entire installation process requires manual to confirm, the installation settings manually set each item must be on duty at the front of the machine, most of the time spent waiting for the installation process. If there are a large number of servers you need to install the operating system, the traditional manual way to Taiwan by way of installing the system, the efficiency is very low, but also because of the need to install an interactive settings manually configure the system can not ensure that every machine system installation configurations are exactly the same, it can not be completely uniform standardization. (During operation of a large number of repeats, the manually is error prone)

  In order to solve the above problems, the way PXE network can be used to achieve the automation system installed, the whole installation process without human intervention, providing a great deal of work efficiency.

First, the basic concept of PXE

PXE (Pre-boot Execution Environment, Pre-Boot Execution Environment) by Inter developed network boot technology, working in the Client / Server model, PXE NIC ROM built-in support for dhcp and tftp protocol that allows a client over the network from remote server to download a boot image, and load the installation files or the entire operating system.

Second, and related components of the basic principles of PXE server

PXE服务器需要的服务:

    DHCP服务:为客户端分配IP地址,定位启动引导文件

    TFTP服务:提供网卡启动引导程序、系统内核文件及initrd镜像文件下载

    FTP服务(或http/nfs):提供系统镜像的yum安装源及ks应答文件下载


客户端机应具备的条件:

    网卡必须支持PXE协议(现在大多数的网卡都已支持)

    主板BIOS支持从网络启动

1, the PXE server Schematic mounting system:

  Typically, in order to save resources, we can also above the schematic provide DHCP services, TFTP service, three different servers FTP (or HTTP, NFS) services integrate deployed to the same server as the server side PXE, depending on the case may be.

2, PXE network installation system advantages:

规模化:高效率,同时安装多台服务器,轻松应对大规模批量安装

自动化:安装过程中无需人工干预、实现自动化无人值守安装

标准化:按照自设定的系统安装规则配置硬盘分区及系统组件包,实现系统安装的统一标准化

远程实现:不需要光驱光盘、U盘等外部安装介质

Three, PXE server built Detailed steps

  In this paper, centos7, for example, to build a PXE server process is summarized as follows:

1, ready YUM source operating system image, and released by the HTTP service

Install httpd service:

[root@centos7 ~]#yum -y install httpd 
[root@centos7 ~]#systemctl start httpd.service  #启动httpd服务
[root@centos7 ~]#systemctl enable httpd.service #设置httpd服务开机启动

Create a system image ISO file storage directory

[root@centos7 myiso]#mkdir -p /data/myios  #创建 /data/myios 作为系统镜像文件存放目录

By xftp the like are posted to the ISO file system image / data / myios directory

[root@centos7 myiso]#ll
total 14390272
-rw-r--r-- 1 root root  3991928832 Sep  7 13:51 CentOS-6.10-x86_64-bin-DVD1.iso
-rw-r--r-- 1 root root 10743709696 Sep  7 13:54 CentOS-7-x86_64-Everything-1810.iso

Create a system image of each source in httpd documentroot default directory / var / www / html subdirectory

[root@centos7 ~]#mkdir -pv /var/www/html/centos/{6,7}/os/x86_64/
mkdir: created directory ‘/var/www/html/centos’
mkdir: created directory ‘/var/www/html/centos/6’
mkdir: created directory ‘/var/www/html/centos/6/os’
mkdir: created directory ‘/var/www/html/centos/6/os/x86_64/’
mkdir: created directory ‘/var/www/html/centos/7’
mkdir: created directory ‘/var/www/html/centos/7/os’
mkdir: created directory ‘/var/www/html/centos/7/os/x86_64/’
[root@centos7 ~]#

ISO file system to mount the corresponding directory httpd

vim编辑修改 /etc/fstab文件
[root@centos7 ~]#vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Sep  7 12:12:42 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4d76f02b-0ebf-4238-89a6-e18709a1025d /                       xfs     defaults        0 0
UUID=b2d24305-3936-4e25-8a40-18b53cd4d809 /boot                   xfs     defaults        0 0
UUID=e5586dc6-a9e2-453b-90da-c009996214ad /data                   xfs     defaults        0 0
UUID=3417eb37-c6ee-4c17-aea5-153413c631c6 swap                    swap    defaults        0 0
/data/myiso/CentOS-6.10-x86_64-bin-DVD1.iso /var/www/html/centos/6/os/x86_64 iso9660 defaults 0 0
/data/myiso/CentOS-7-x86_64-Everything-1810.iso /var/www/html/centos/7/os/x86_64 iso9660 defaults 0 0

添加最后两行,将/data/myiso 目录中的2个iso镜像文件以iso镜像文件系统挂载到httpd的对应documentroot目录下
[root@centos7 ~]#mount -a
mount: /dev/loop0 is write-protected, mounting read-only
mount: /dev/loop1 is write-protected, mounting read-only
[root@centos7 ~]#

df look, ISO image has been mounted to the corresponding directory httpd

Description: 10.10.10.254 is the IP PXE server must be configured well in advance, and is set to a static IP.

Set the local IP and PXE server network segment, through URL access http://10.10.10.254/centos/ can go to the source yum has been released successfully.


2, prepare the system to automatically install ks answer file (kickstart), and use HTTP publishing

kickstart file usage:

Ks role is to achieve the answer file during installation of the system unattended, automated installation. When the system is installed mounting set which rely on the file to read KS predefined, such as hard disk partition, the language system, which mounting assembly packages and the like.

Ks configuration file:

(1) Get anaconda-ks.cfg already installed from the existing system (file system installation wizard anaconda is generated after the system is installed), and then modify the configuration according anaconda-ks.cfg configuration item to which they need ks file (Note that the configuration centos6 ks and centos7 system is not universal, it is necessary to use the corresponding system of anaconda to modify the configuration file)

Ks examples and format settings file is as follows (applicable centos7)

[root@centos7 ksdir]#cat ks7_desktop.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install                                                     #全新安装
# Keyboard layouts
keyboard 'us'                                               #键盘模式,美式US
# Root password
rootpw --iscrypted $1$7Q46UR0F$uZjZh2p9X.MlrV0dW8euj.       #设置root账号口令并采用加密
# Use network installation
url --url="http://10.10.10.254/centos7/os/x86_64"           #系统镜像yum源的URL地址
# System language
lang en_US                                                  #系统默认语言,en_US
# System authorization information
auth  --useshadow  --passalgo=sha512                        #系统默认使用shadow文件作为账号登录验证
# Use text mode install
text                                                        #安装过程默认使用text文本的tui界面
firstboot --disable
# SELinux configuration
selinux --disabled                                          #禁用selinux

# Firewall configuration
firewall --disabled                                         #禁用系统防火墙
# Network information
network  --bootproto=dhcp --device=eth0                     #系统默认的网卡配置
# Reboot after installation
reboot                                                      #安装完成后自动重启系统
# System timezone
timezone Asia/Shanghai                                      #设置系统默认时区 Asia/Shanghai
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr          #安装新的bootload程序,并添加内核启动参数 net.ifnames=0
# Clear the Master Boot Record
zerombr                                                     #清除原有的MBR引导记录
# Partition clearing information
clearpart --all --initlabel                                 #清除原有的硬盘分区标签
# Disk partitioning information                             #硬盘分区信息,按实际需求设定
part /boot --fstype="xfs" --size=1024
part / --fstype="xfs" --size=51200
part swap --fstype="swap" --size=4096
part /data --fstype="xfs" --grow --size=1                   # /data分区使用所有剩余硬盘空间


%packages       #要安装的包组,以%packages行开始,到%end结尾,包组以@符号开头,单个包直接写包名
@desktop-debugging
@fonts
@gnome-desktop
@input-methods
@legacy-x
@remote-desktop-clients
@x11
vinagre

%end              #需要注意的是,centos6中,如果选择最小化安装,ks文件最后需要写上 %packages开始行%end结尾行的2行,否则系统会默认安装所有的包组,centos7系统如果最小化安装,则可以不用写这2行
[root@centos7 ksdir]#

Ks modify the generated file as a template by anaconda file, you can use the command ksvalidator / PATH / KICKSTART_FILE to check ks file syntax is correct.

(2) using the system-config-kickstart tool setup wizard to configure generated by a graphical interface (packet group requires a server with a graphical interface, if the system is to minimize the installation, install the x11 graphics package group)

[root@centos7 ~]#yum -y install system-config-kickstart  #此工具包来自epel源
[root@centos7 ~]#yum -y groupinstall x11         #最小化安装的系统需要安装x11图形包组才能支持system-config-kickstart工具调出图形界面的ks文件配置向导窗口 

Run system-config-kickstart kickstart graphical configuration tool to bring up the Wizard window

[root@centos7 ~]#system-config-kickstart 

System-config-kickstart setting tool follows:










After setting, click Save to save the upper left corner

Ks modify the file name, specify the storage path, click Save to save the lower right corner

Note: If you minimize installation generating centos6 system by system-config-kickstart tool ks file, you need to manually modify ks file, and finally add two lines% package start line and% end end of the line, otherwise the installation wizard is installed by default all packet group, as shown below:

Create a directory under ksdir documentroot http server directory, as a dedicated file storage path ks

[root@centos7 ksdir]#cd /var/www/html/
[root@centos7 html]#mkdir ksdir/

Ks file will be configured through either way, uploaded to a dedicated server http ks file storage path, released, and ensure that all files can be a normal visit by ks url

[root@centos7 ksdir]#ll
total 16
-rw-r--r-- 1 root root 2109 Sep  7 20:00 ks6_desktop.cfg #centos6带gnome桌面安装
-rw-r--r-- 1 root root 1757 Sep  7 22:13 ks6_mininal.cfg #centos6最小化安装
-rw-r--r-- 1 root root 1809 Sep  7 20:02 ks7_desktop.cfg #centos7带gnone桌面安装
-rw-r--r-- 1 root root 1695 Sep  7 20:02 ks7_mininal.cfg #centos7最小化安装

3, configure the TFTP service installation and deployment NIC boot files, system kernel and file system initialization image file

Configure and enable the TFTP service, install tftp-server package, listening port UDP 69

TFTP root directory: / var / lib / tftpboot /

[root@centos7 ~]#yum -y install tftp-server
[root@centos7 ~]#systemctl start tftp.socket 
[root@centos7 ~]#systemctl enable tftp.socket

TFTP root directory of the file to be deployed as follows:

[root@centos7 tftpboot]#ll
total 84
drwxr-xr-x 2 root root    39 Sep  7 14:48 centos6       
drwxr-xr-x 2 root root    39 Sep  7 14:48 centos7
-rw-r--r-- 1 root root 55140 Sep  7 14:46 menu.c32
-rw-r--r-- 1 root root 26759 Sep  7 14:46 pxelinux.0
drwxr-xr-x 2 root root    21 Sep  7 21:36 pxelinux.cfg

[root@centos7 tftpboot]#tree 
.
├── centos6                        #centos6系统内核文件和intrd.img存放目录
│   ├── initrd.img
│   └── vmlinuz
├── centos7                        #centos7系统内核文件和intrd.img存放目录
│   ├── initrd.img
│   └── vmlinuz
├── menu.c32                       #PXE启动菜单界面背景图
├── pxelinux.0                     #网卡启动引导文件
└── pxelinux.cfg                   #PXE启动菜单文件存放目录,目录名必须为pxelinux.cfg 
    └── default                    #PXE启动菜单文件,文件名必须为default

Pxelinux.0 ready NIC boot file PXE boot menu screen and background image file menu.c32

yum安装syslinux包
[root@centos7 ~]#yum -y install syslinux

复制pxelinux文件到tftp的根目录下
[root@centos7 ~]#cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

复制PXE启动菜单界面背景图片文件到tftp根目录下
[root@centos7 ~]#cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/

Create system kernel and the file storage directory initrd image file in the root directory tftp, and copy the corresponding files from the system image ISO yum source directory to the directory corresponding tftp

在tftp根目录创建centos6和centos7系统内核文件及initrd镜像文件的存放目录
[root@centos7 ~]#mkdir -pv /var/lib/tftpboot/centos{6,7}
mkdir: created directory ‘/var/lib/tftpboot/centos6’
mkdir: created directory ‘/var/lib/tftpboot/centos7’

分别从centos6和centos7镜像yum源路径复制系统内核文件和initrd镜像文件到tftp根目录中对应的目录
[root@centos7 ~]#cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6/

[root@centos7 ~]#cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/

Preparing to Install menu files in the specified directory and put tftp

在tftp根目录中创建安装菜单文件存放目录 pexlinux.cfg/
[root@centos7 ~]#mkdir /var/lib/tftpboot/pxelinux.cfg/

从系统ISO镜像yum源路径复制启动菜单文件到对应目录中,并改为为default

vim editor to modify default pxe boot menu file, modified to read as follows:

[root@centos7 pxelinux.cfg]#vim default
default menu.c32        #使用menu.c32作为背景图片
timeout 600             #默认等待时间60秒(600的十分之一)

menu title Auto Install CentOS      #启动菜单的标题

label CentOS6_Mininal                           #label标签说明
  menu label Install CentOS 6.10 Mininal        #安装选项菜单
  kernel centos6/vmlinuz                        #系统内核文件在tftp中的相对路径
  append initrd=centos6/initrd.img ks=http://10.10.10.254/ksdir/ks6_mininal.cfg  #boot启动引导的参数,指定系统initrd镜像文件在tftp中的相对路径以及ks应答文件的访问地址

label CentOS6_Desktop
  menu label Install CentOS 6.10 Desktop
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks=http://10.10.10.254/ksdir/ks6_desktop.cfg

label CentOS7
  menu label Install CentOS 7.6 Mininal
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://10.10.10.254/ksdir/ks7_mininal.cfg

label CentOS7
  menu label Install CentOS 7.6 Desktop
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://10.10.10.254/ksdir/ks7_desktop.cfg

label Local                           
  menu label Boot from Local drive  #从本地硬盘启动
  menu default        #默认启动选项,光标默认选中此项,timeout时间后默认从此项启动,建议默认设定为此项,防止60秒等待时间后未经过人工选择,造成直接进入系统安装
  localboot 0xffff

4, installation configuration DHCP Service

Installation dhcp service

[root@centos7 ~]#yum -y install dhcp

Dhcp service after the installation is complete, the default can not start, you need to make changes to the configuration file, otherwise start error.

The default configuration file no content, copy the configuration file is modified according to prompts from sample files

复制dhcp服务的范例配置文件覆盖默认配置文件
[root@centos7 ~]#cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 

vim editor to modify dhcp service configuration file, and add the following

[root@centos7 ~]#vim /etc/dhcp/dhcpd.conf
subnet 10.10.10.0 netmask 255.255.255.0 {     #dhcp服务器分配的网段需要与本机的固定IP地址位于同一网段 (本机IP为 10.10.10.254)
    range 10.10.10.200 10.10.10.250;    #指定自动分配的IP地址范围
    option routers 10.10.10.1;          #指定网关地址
    option domain-name-servers 114.114.114.114,8.8.8.8;  #指定DNS服务器地址
    option domain-name "test.org";   #DNS搜索域名
    next-server 10.10.10.254;      #指定TFTP服务器IP(关键设定项)
    filename "pxelinux.0";         #指定网卡pxe启动引导文件名(关键设定项)
}

Start dhcp service, and set the boot

[root@centos7 ~]#systemctl start dhcpd.service
[root@centos7 ~]#systemctl enable dhcpd.service

As a successful start, dhcp service will listen port 67 udp, such as the occurrence of an error, you need to check the configuration file.

The use of client machines to install the system test PXE environment

Set the default client machine BIOS to boot from the network card

If they can successfully enter the boot menu selection screen, then prove that the DHCP service is working properly

According to the menu item selected corresponding to the installation system, e.g. centos7.6 selected to minimize installation:

Installation options interface and disk partitions

See centos7.6 minimize installation package 310 needs to be filled

centos6.10 minimization interface installation procedure:

At this point, the entire PXE server build process is complete.

Precautions:

1, previously set down and disabled PXE server selinux

2, previously shut down and disable the system firewall, such as security considerations, the need to maintain the system firewall is turned on, you will need to HTTP service, TFTP service, DHCP service requires the use of network ports opened in the firewall, otherwise the client will not be related to network access service

3, can not configure a new DHCP server in the DHCP server network already exists, otherwise it will cause more DHCP server within a network, resulting confusion led to obtain the IP address of a network failure. You can directly use existing DHCP server, add the appropriate modifications to the configuration file.

4. centos7 mounting system used PXE virtual machine, the virtual machine's memory allocation requires more than 1G, otherwise error occurs in the following figure:

  Given the limited level, expressed in this article there will inevitably leak at the careless, welcome to point out and correct.

Guess you like

Origin www.cnblogs.com/eddie1127/p/11487829.html
Recommended