CentOS 7 builds a PXE server to realize BIOS+EFI two ways to guide the installation system in the LAN - the road to dream

Introduction

What are the components of PXE Server?

1. dhcp server

2. tftp server

3. File server, such as http\FTP\NFS, etc.

Environment description:

Operating system: centos7 x86_64 minal

Tested system: centos7 x86_64 minal ISO

Preparations before construction

1. 关闭selinux

sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 

setenforce 0

2. 配置防火墙

# 开启IP伪装,提供给自动获取网段的机器上网

firewall-cmd --add-masquerade --permanent

# 开放必要的服务

firewall-cmd --permanent --add-service={dhcp,proxy-dhcp,http,tftp}

firewall-cmd --reload

Build a DHCP server

# 安装dhcp服务

yum install -y dhcp

# 配置dhcp服务器

vim /etc/dhcp/dhcpd.conf

# 不使用DNS动态更新
ddns-update-style none;
# 忽略客户端DNS更新
ignore client-updates;

authoritative;

# 响应客户端的启动查询(开机时发送的DHCP请求)
allow booting;
allow bootp;
allow unknown-clients;

#DHCP configuration for PXE boot server
option space PXE;
option PXE.mtftp-ip    code 1 = ip-address;
option PXE.mtftp-cport code 2 = unsigned integer 16;
option PXE.mtftp-sport code 3 = unsigned integer 16;
option PXE.mtftp-tmout code 4 = unsigned integer 8;
option PXE.mtftp-delay code 5 = unsigned integer 8;
option arch code 93 = unsigned integer 16; # RFC4578

subnet 10.0.0.0 netmask 255.255.255.0 {
  range 10.0.0.100 10.0.0.199;
  # 设置DNS服务器地址
  option domain-name-servers 223.5.5.5,114.114.114.114;
  # 设置DNS域
  option domain-name "dns1.server.com";
  # 设置客户租的默认网关
  option routers 10.0.0.2;
  option broadcast-address 10.0.0.255;
  # 默认租约时间,单位为秒
  default-lease-time 3600;
  # 设置最大租约时间,单位为秒
  max-lease-time 7200;

  class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          next-server 10.0.0.2;

          # UEFI x86-64 boot (RFC4578 architecture types 7, 8 and 9)
          if option arch = 00:07 {
              filename "uefi/bootx64.efi";
          } else if option arch = 00:08 {
              filename "uefi/bootx64.efi";
          } else if option arch = 00:09 {
              filename "uefi/bootx64.efi";
          } else {
              filename "pxelinux.0";
          }
  }
}

说明:

pxe服务器有两张网卡,一张是管理IP,可以访问互联网;一张是内网段,内网段自动获取IP,内网段IP地址:10.0.0.2  255.255.255.0 10.0.0.1,同时也作为dhcp服务端,地址池为10.0.0.100-199

BIOS的引导文件:pxelinux.0  UEFI引导文件:bootx64.efi

dhcp配置中网关需要指定为dhcp服务端的ip,这样才能实现客户端能通过IP地址伪装访问互联网

# 启动dhcp服务并加入开机启动

systemctl enable dhcpd --now

systemctl status dhcpd

Build a TFTP server

# 安装tftp

yum install -y tftp-server

# 配置tftp服务器

yum install -y syslinux

# 复制引导程序到tftp目录下

# 主要由两部分组成:syslinux和系统镜像部分文件

cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

cp -v /usr/share/syslinux/menu.c32 /var/lib/tftpboot/

cp -v /usr/share/syslinux/mboot.c32 /var/lib/tftpboot/

cp -v /usr/share/syslinux/chain.c32 /var/lib/tftpboot/

----------以上主要是传统BIOS引导方式的一些引导文件

# 挂载镜像以拷贝需要的文件

mount -o loop CentOS-7-x86_64-minal-2009.iso /mnt/

cp /mnt/isolinux/{boot.cat,boot.msg,grub.conf,isolinux.bin,splash.png,TRANS.TBL,vesamenu.c32} /var/lib/tftpboot/

# 创建必要的目录

mkdir /var/lib/tftpboot/pxelinux.cfg
mkdir -p /var/lib/tftpboot/networkboot/centos7
mkdir /pub/centos7

# 拷贝iso中的引导文件

cp /mnt/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/networkboot/centos7

# 将iso文件挂载到文件服务器目录

mount CentOS-7-x86_64-minal-2009.iso /pub/centos7   # 推荐

或者拷贝

cp -rf /mnt/* /pub/centos7/

---------------------------------------------------------

# 编辑传统BIOS引导的菜单

vim /var/lib/tftpboot/pxelinux.cfg/default

default menu.c32
prompt 0
timeout 300
ONTIMEOUT 1

menu title ########## CentOS 7 PXE Boot Menu ##########

label 1
menu label ^1) Install CentOS 7
menu default
kernel /networkboot/centos7/vmlinuz
append initrd=/networkboot/centos7/initrd.img inst.repo=http://10.0.0.2/centos7 

label 2
menu label ^2) Boot from local drive
localboot 0

------------------------------------------------------------

# 创建UEFI引导的目录

mkdir /var/lib/tftpboot/uefi

# 安装启动引导文件相关rpm

yum install grub2-efi-x64 shim-x64

cp -p /boot/efi/EFI/centos/*.efi /var/lib/tftpboot/uefi/
chmod 755 /var/lib/tftpboot/uefi/*.efi

yum install grub2-efi-x64-modules

# 生成启动文件

grub2-mkstandalone -d /usr/lib/grub/x86_64-efi/ -O x86_64-efi --modules="tftp net efinet linux part_gpt efifwsetup" -o /var/lib/tftpboot/uefi/bootx64.efi


# 编辑UEFI引导的菜单文件

vim /var/lib/tftpboot/uefi/grub.cfg

set default="0"
function load_video {
  insmod efi_gop
  insmod efi_uga
  insmod video_bochs
  insmod video_cirrus
  insmod all_video
}
load_video
set gfxpayload=keep
insmod net
insmod efinet
insmod tftp
insmod gzio
insmod part_gpt
insmod ext2
set timeout=60
menuentry 'Install UEFI CentOS Linux 7' --class fedora --class gnu-linux --class gnu --class os {
  linuxefi (tftp)/networkboot/centos7/vmlinuz ip=dhcp inst.repo=http://10.0.0.2/centos7/
  initrdefi (tftp)/networkboot/centos7/initrd.img
}

----------------------------------------------------

# 重启tftp服务

systemctl restart tftp.service && systemctl enable tftp --now

Build file server nginx

 

# 安装扩展源

yum  install epel-release -y

# 安装nginx

yum makecache fast && yum install nginx -y

# 配置nginx

mkdir -p /pub/centos7

vim /etc/nginx/conf.d/default.conf

server {
    listen 80;
    server_name _;
    root /pub;
    location / {
      autoindex on;
      index index.htm index.html;
    }
}

# 检查配置

nginx -t

# 启动nginx

systemctl enable nginx --now

So far, the PXE Server has been set up to support the BIOS+EFI two boot methods and the network boot installation system in the intranet environment.

The client only needs to be on the same switch as 10.0.0.2/24 to set it to start from the network card, automatically obtain the IP address, and enter the installation system.

Note: The installation of centos7 requires at least 2G of memory. Generally, the minimum memory is 4G. If the memory is too small, it will cause problems.

References:

http://m.blog.chinaunix.net/uid-31555782-id-5819768.html

http://hmli.ustc.edu.cn/doc/linux/centos-autoinstall.htm

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/130517291