一、PXE简介

二、PXE自动化安装系统的流程

三、在CentOS 7上搭建PXE环境

四、在CentOS 6上搭建PXE环境



一、PXE简介


PXE的全称为Preboot eXecution Environment,即与预启动执行环境,也称为预执行环境,是一种通过网路实现自动化安装系统的机制,由Intel研发。PXE是基于C/S架构的,使用PXE机制能使没有任何操作系统的主机基于网络完成操作系统的安装,实现PXE客户端主机自动化安装操作系统,因此无需依赖于客户端的硬盘。客户端启动时,其网卡上的ROM中的代码可以扮演成DHCP服务器的客户端,从而获取一个IP地址(注意是网卡地址,不是内核地址,因为还没安装操作系统),再扮演成文件服务器的客户端去获取安装系统所需要的文件,加载至内存中,从而完成操作系统的安装。



二、PXE自动化安装系统的流程


使用PXE机制安装系统,要求网卡支持网络引导,整个启动安装流程如下。

wKioL1mPIV6B-UWmAAHkJaROoq0847.jpg

  • PXE网卡启动

  • PXE网卡附加的ROM上的代码扮演成DHCP Server的客户端,此时DHCP会分配一个IP地址及子网掩码给Client,并告知启动引导文件pxelinux.0的存放位置(该文件一般存放在TFTP服务器上)

  • PXE Client(PXE网卡)向TFTP Server请求pxelinux.0文件,TFTP Server将pxelinux.0发给Client

  • PXE Client执行接受到的pxelinux.0文件

  • PXE Client向TFTP Server请求配置文件(pxelinux.cfg/default),TFTP Server将配置文件发给Client,而Client则根据配置文件执行后续操作

  • PXE Client向TFTP Server请求vmlinuz,TFTP Server收到请求后将vmlinuz发给Client

  • PXE Client向TFTP Server请求initrd.img,TFTP Server收到请求后将initrd.img发给Client

  • PXE Client进入系统安装步骤,并通过向TFTP Server请求获取kickstart文件,实现自动化安装系统



三、在CentOS 7上搭建PXE环境


实验环境:

DHCP Server和TFTP Server为同一台主机,IP地址为192.168.10.150,主机名为centos7。


实验前提:

确保SElinux和防火墙不会影响实验。


涉及到的服务:

(1) dhcp服务(提供ip/netmask, gw, dns; filename, next-server)

(2) tftp服务(提供bootloader, kernel, initrd)

(3) yum仓库(可由ftp、http(s)、nfs服务提供)

(4) kickstart文件


1、DHCP的安装配置

安装

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

配置

复制并修改模板文件,如下。

[root@centos7 ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@centos7 ~]# vim /etc/dhcp/dhcpd.conf

option domain-name "itab.com";
option domain-name-servers 114.114.114.114;
option routers 192.168.10.2;

default-lease-time 43200;
max-lease-time 86400;

log-facility local7;

subnet 192.168.10.0 netmask 255.255.255.0 {
	range 192.168.10.101 192.168.10.140;
	filename "pxelinux.0";         # 指明引导文件名称
	next-server 192.168.10.150;    # 指明引导文件所在服务器主机的IP地址
}

启动

[root@centos7 ~]# systemctl restart dhcpd.service
[root@centos7 ~]# systemctl enable dhcpd.service
[root@centos7 ~]# ss -unl | grep :67
UNCONN     0      0            *:67                       *:*


2、TFTP的安装配置

TFTP全称为Trivial FTP,即简单文件传输服务,用于在客户端和服务器端之间传输开销不大的文件,监听在69/UDP端口上。

安装

[root@centos7 ~]# yum -y install tftp tftp-server

注意:tftp是客户端程序包,tftp-server是服务器程序包。

启动

[root@centos7 ~]# systemctl start tftp.socket
[root@centos7 ~]# systemctl enable tftp.socket
[root@centos7 ~]# ss -unl | grep :69
UNCONN     0      0           :::69                      :::*

注意:

①TFTP服务器的默认文件根目录是/var/lib/tftpboot/

②在CentOS 6上tftp服务是由xinetd服务代理,需要先安装xinetd服务,并执行chkconfig tftp on命令开启tftp服务。


3、yum仓库准备

yum仓库可以自己定制,也可以使用互联网上的yum仓库。这里直接将光盘yum仓库挂载在http下,以让客户端能够访问。

安装

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

挂载

[root@centos7 ~]# mkdir -p /var/www/html/centos/7/x86_64
[root@centos7 ~]# mount -r /dev/sr0 /var/www/html/centos/7/x86_64    # 只读挂载

启动

[root@centos7 ~]# systemctl start httpd.service
[root@centos7 ~]# systemctl enable httpd.service
[root@centos7 ~]# ss -tnl | grep :80
LISTEN     0      128         :::80                      :::*

可以看到httpd能够正常提供服务,如下。

wKioL1mOlbTBoEzTAACpdDc9Tt8437.png


4、kickstart文件准备

以系统安装的kickstart文件为模板,并根据需要进行修改(可使用system-config-kickstart修改),这里的kickstart文件放在httpd服务器上。

[root@centos7 ~]# mkdir /var/www/html/kickstarts
[root@centos7 ~]# cp ~/anaconda-ks.cfg /var/www/html/kickstarts/centos7.cfg
[root@centos7 ~]# vim /var/www/html/kickstarts/centos7.cfg    # 修改模板后,如下

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
url --url=http://192.168.10.150/centos/7/x86_64    # 指定安装源位置(由httpd提供)
text                                               # 文本界面安装
# Run the Setup Agent on first boot
firstboot --disable
reboot                                             # 安装后重启
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Network information
network  --bootproto=dhcp --device=eno16777736 --onboot=on --ipv6=auto --activate  # 网卡配置
network  --hostname=cento7
# Root password
rootpw --iscrypted $1$1uGcgMef$MUVhg/xFGVZve5oUXQzmN/
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --append="crashkernel=auto vga=785" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="xfs" --ondisk=sda --size=10240
part /boot --fstype="xfs" --ondisk=sda --size=512

%packages
@^minimal
@core
kexec-tools
%end

%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

为kickstart文件添加读权限。

[root@centos7 ~]# chmod a+r /var/lib/tftpboot/kickstarts/centos7.cfg


5、配置PXE环境

a.安装syslinux

syslinux是一个功能强大的bootloader,其详细介绍可使用“yum info syslinux”命令查看。

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


b.准备引导文件

[root@centos7 ~]# rpm -ql syslinux | grep "\<pxelinux.0\>"
/usr/share/syslinux/pxelinux.0
[root@centos7 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/


c.准备内核等文件

[root@centos7 ~]# cp /var/www/html/centos/7/x86_64/p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/
[root@centos7 ~]# cp /usr/share/syslinux/{chain.c32,mboot.c32,menu.c32,memdisk} /var/lib/tftpboot/

注意:memdisk可以将内存模拟为硬盘,用于将initrd.img在内存中展开并模拟成硬盘使用。


d.定制启动菜单

[root@centos7 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@centos7 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32                    # 所用菜单文件
prompt 5
timeout 30
menu title CentOS 7 PXE Menu        # 主标题"CentOS 7 PXE Menu"

label linux_autoinst                # 选项标题"linux_autoinst"
    menu label Install CentOS 7 x86_64
    kernel vmlinuz
    append initrd=initrd.img inst.repo=http://192.168.10.150/centos/7/x86_64 ks=http://192.168.10.150/kickstarts/centos7.cfg
                                    # kickstart文件由http服务提供

查看TFTP Server文件根目录结构。

[root@centos7 ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── chain.c32
├── initrd.img
├── mboot.c32
├── memdisk
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── vmlinuz

1 directory, 8 files


6、在主机上自动化安装操作系统实现

a.使用vmware软件进行实验,根据以上步骤配置好PXE Server,然后新建虚拟机,作为PXE Client,期望其实现自动化安装系统

wKiom1mO3W2hBo03AAE8cZlKDxY056.png


b.自动化安装过程

wKiom1mO2vLz5KEnAABZ_XwZI6g030.png


wKiom1mO2vPTMgabAAA3IE5bPZk889.png


wKioL1mO2vPzb3LvAAAK6STrgB0557.png

安装完成!



四、在CentOS 6上搭建PXE环境


实验环境:

DHCP Server和TFTP Server为同一台主机,IP地址为192.168.10.128,主机名为centos6。


1、DHCP的安装配置

安装

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

配置

复制并修改模板文件,如下。

[root@centos6 ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@centos6 ~]# vim /etc/dhcp/dhcpd.conf

option domain-name "itab.com";
option domain-name-servers 114.114.114.114;
option routers 192.168.10.2;

default-lease-time 43200;
max-lease-time 86400;

log-facility local7;

subnet 192.168.10.0 netmask 255.255.255.0 {
	range 192.168.10.101 192.168.10.140;
	filename "pxelinux.0";         # 指明引导文件名称
	next-server 192.168.10.128;    # 指明引导文件所在服务器主机的IP地址
}

启动

[root@centos6 ~]# service dhcpd configtest    # 检查语法错误
Syntax: OK
[root@centos6 ~]# service dhcpd start                                           [  OK  ]
[root@centos6 ~]# chkconfig dhcpd on
[root@centos6 ~]# ss -unl
State      Recv-Q Send-Q                                          Local Address:Port                                            Peer Address:Port 
UNCONN     0      0                                                           *:67                                                         *:*


2、TFTP的安装配置

安装

[root@centos6 ~]# yum -y install tftp-server

启动

[root@centos6 ~]# chkconfig tftp on
[root@centos6 ~]# service xinetd restart
[root@centos6 ~]# chkconfig xinetd on

在CentOS如果要使tftp服务开机自动启动,除了以上使用"chkconfig tftp on"命令外,还有另一种方法,那就是编辑/etc/xinetd.d/tftp文件,将其中的"disable = yes"修改为"disable = no"。因为tftp服务在CentOS 6上是由xinetd服务控制,因此xinetd服务也要设置为开机自动启动,这样tftp服务才能开机自动启动。


3、yum仓库准备

此处为了实验方便,直接将yum仓库挂载在ftp服务下,让客户端能够访问。

安装

[root@centos6 ~]# yum -y install vsftpd

挂载

[root@centos6 ~]# mkdir -p /var/ftp/pub/centos/7/x86_64
[root@centos6 ~]# mount -r /dev/sr0 /var/ftp/pub/centos/7/x86_64

启动

[root@node ~]# service vsftpd start
[root@node ~]# chkconfig vsftpd on


可以看到vsftpd能够正常提供服务,如下。

wKiom1mPBKmCRUvMAAG6vUeKSgY753.png


4、kickstart文件准备

以系统安装的kickstart文件为模板,并根据需要进行修改(可使用system-config-kickstart修改),这里的kickstart文件放在vsftpd服务器上。

[root@centos6 ~]# mkdir /var/ftp/kickstarts
[root@centos6 ~]# cp ks.cfg /var/ftp/kickstarts/centos6.cfg
[root@centos6 ~]# vim /var/ftp/kickstarts/centos6.cfg    # 修改模板后,如下

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="ftp://192.168.10.128/pub/centos/7/x86_64"           # 指定安装源位置(由httpd提供)
# Root password
rootpw --iscrypted $1$wakiRJZe$7pfhY4KCgf9B9ZEp/WPQN0
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text                                                            # 文本界面安装
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone  Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on         # 网卡配置
# System bootloader configuration
bootloader --append="crashkernel=auto rhgb quiet vga=785" --location=mbr --driveorder="sda"
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=512
part / --fstype="ext4" --size=10240
part swap --fstype="swap" --size=2048

%post
echo "Hello~My Tab!"

rm -rf /etc/yum.repo.d/*
cat > /etc/yum.repo.d/local.repo <<EOF
[local]
name=this is a local repo.
baseurl=mirrors.163.com/centos/6.8/os/x86_64
gpgcheck=0
EOF

cat > /etc/yum.repo.d/epel <<EOF
[local]
name=this is a epel repo.
baseurl=https://dl.fedoraproject.org/pub/epel/6/x86_64/
gpgcheck=0
EOF

mkdir /media/cdrom
echo "/dev/sr0  /media/cdrom  iso9660  defaults   0 0"  >> /etc/fstab
%end

%packages
@base
@console-internet

%end


5、配置PXE环境

a.安装syslinux

[root@centos6 ~]# yum -y install syslinux


b.准备引导文件

[root@centos6 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/


c.准备内核等文件

[root@centos6 ~]# mount --bind /var/ftp/pub/centos/7/x86_64/ /media/cdrom/
[root@centos6 ~]# cp /media/cdrom/p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/
[root@centos6 ~]# cp /media/cdrom/isolinux/{boot.msg,vesamenu.c32,splash.jpg} /var/lib/tftpboot/


d.定制启动菜单

[root@centos6 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@centos6 ~]# cp /media/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@centos6 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default 

default vesamenu.c32                              # 所用菜单文件
#prompt 1
timeout 50

display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.8 iTab!            # 主标题
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000

label linux_autoinst
  menu label ^Install CentOS 6.8 automatically    # 选项标题
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=ftp://192.168.10.128/kickstarts/centos6.cfg    # kickstart文件由http服务提供
label linux
  menu label ^Install or upgrade an existing system
  kernel vmlinuz
  append initrd=initrd.img
label vesa
  menu label Install system with ^basic video driver
  kernel vmlinuz
  append initrd=initrd.img nomodeset
label rescue
  menu label ^Rescue installed system
  kernel vmlinuz
  append initrd=initrd.img rescue
label local
  menu label Boot from ^local drive
  localboot 0xffff
label memtest86
  menu label ^Memory test
  kernel memtest
  append -


查看TFTP Server文件根目录结构。

[root@centos6 ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── boot.msg
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
│   └── default
├── splash.jpg
├── vesamenu.c32
└── vmlinuz

1 directory, 7 files


6、在主机上自动化安装操作系统实现

a.创建虚拟机

wKiom1mPFe_C8NWPAADA0yUvfLY632.png


b.自动安装系统

wKiom1mPHCHj9L9dAAadTPwsnzo792.png


wKioL1mPHCHwXABBAABF8OvMI3Y923.png


wKiom1mPHCKCrqNGAAAL518meFU142.png

安装完成!