一、PXE

1、pxe概念

PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件设置,从而引导预先安装在服务器中的终端操作系统。PXE可以引导多种操作系统,如:Windows95/98/2000/windows2003/windows2008/winXP/win7/win8,linux系列系统等。

2、pxe的工作原理

(1)Client向PXE Server上的DHCP发送IP地址请求消息,

(2)DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client

(3)Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0

(4)Client执行接收到的pxelinux.0文件

(5)Client向TFTP Server发送针对本机的配置信息文件(在 TFTP 服务的pxelinux.cfg目录下),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。

(6)Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client

(7)Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统

(8)Client启动Linux内核

(9)Client下载安装源文件,读取自动化安装脚本 timg.jpg

3、PXE依赖的服务、软件包及文件

(1)dhcp服务

如果不理解dhcp请查看

(2)tftp服务

(a)tftp的安装包

tftp 客户端,tftp-server服务端

(b)端口号为69/UDP

(c)配置为文件

/etc/xinetd.d/tftp

service tftp{        socket_type             = dgram        protocol                = udp        wait                    = yes        user                    = root        server                  = /usr/sbin/in.tftpd        server_args             = -s /var/lib/tftpboot        disable                 = yes   #此选项是否开启tftp服务        per_source              = 11        cps                     = 100 2        flags                   = IPv4}

(d)如何 CentOS 7如何启动此服务?[root@localhost ~]# yum install -y tftp-server

方法一:

直接修改配置文件 2017-09-17_190343.png[root@localhost ~]# systemctl start tftp #启动服务[root@localhost ~]# ss -unl #查看端口是否启动 2017-09-17_190608.png

方法二:

直接启动服务并设置开机自动启动[root@localhost ~]# systemctl start tftp[root@localhost ~]# systemctl enable tftp

(3)syslinux包

(a)概念

syslinux是一个功能强大的引导加载程序,而且兼容各种介质。它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。它的安装很简单,一旦安装syslinux好之后,sysLinux启动盘就可以引导各种基于DOS的工具,以及MS-DOS/Windows或者任何其它操作系统。不仅支持采用BIOS结构的主板,而且从6.0版也开始支持采用EFI结构的新型主板。 (b)文件介绍

后缀名 解释
none or other Linux内核映像
.0 PXE 启动引导程序(NBP)
.bin “光盘引导扇区”
.bs “磁盘引导扇区”
.bss “磁盘引导扇区”,
.c32 COM32映像文件
.cbt COMBOOT映像文件
.com COMBOOT映像文件
.img 磁盘映像文件
.ima 软盘映像文件

制作PXE常使用的文件menu.c32、pxelinux.0。

(4)kickstart文件

如果不理解怎么配置kickstart文件,请查看

二、PXE自动化安装CentOS 7

在进行操作之前确保iptables服务时关闭的,selinux是禁用的。

(a)配置yum源和ks文件 [root@localhost ~]# yum install -y vsftpd#安装vsftpd服务 [root@localhost ~]# systemctl enable vsftpd #设置为开机启动 [root@localhost ~]# systemctl start vsftpd 启动服务[root@localhost ~]# cd /var/ftp/pub/

[root@localhost ~]# mkdir centos/7 -pv #创建目录[root@localhost ~]# mount /dev/sr0 centos/7 #挂载光盘[root@localhost pub]# rz 将ks文件上传至当前目录 具体的可以参考 2017-09-17_213759.png

(b)安装syslinux包 [root@localhost ~]# yum install -y syslinux(c)按装tftp服务,并将配置文件复制[root@localhost ~]# yum install -y tftp-server #安装tftp服务包[root@localhost ~]# cd /var/lib/tftpboot/ 

[root@localhost tftpboot]# cp /usr/share/syslinux/{pxelinux.0,menu.c32} .#将pxelinux.0和menu.c32复制到当前目录下[root@localhost tftpboot]# cp /media/cdrom/isolinux/{vmlinuz,initrd.img} .#将内核文件和虚拟文件系统复制到当前目录下[root@localhost tftpboot]# mkdir pxelinux.cfg[root@localhost tftpboot]# cp /media/cdrom/isolinux/isolinux.cfg pxelinux.cfg/default#将isolinux.cfg文件复制到pxelinux.cfg目录下并改名为default[root@localhost tftpboot]# tree #目录结构

.├── initrd.img├── menu.c32├── pxelinux.0├── pxelinux.cfg│   └── default└── vmlinuz1 directory, 5 files

[root@localhost tftpboot]# vim pxelinux.cfg/default编辑default配置文件

default menu.c32timeout 600menu title CentOS 7 PXE Installlabel desktop  menu label ^desktop  kernel vmlinuz  append initrd=initrd.img ks=ftp://192.168.4.51/pub/ks7.cfgmenu end

(d)新建一台虚拟机,测试

三、PXE自动化CentOS 6

(1)准备yum源和ks6.cfg[root@centos6~]#yum install -y httpd#安装httpd服务[root@centos6/var/www/html]#mkdir centos/6 -pv[root@centos6/var/www/html]#mkdir ksdir/[root@centos6/var/www/html]#mount /dev/sr0 centos/6/#挂载光盘[root@centos6/var/www/html]#cp ks6.cfg ksdir/` #复制ks文件并修改

#version=DEVEL# System authorization informationauth --enableshadow --passalgo=sha512# Use ftp/http installation mediaurl --url=ftp://192.168.4.137/centos/6# Use graphical installgraphical# Run the Setup Agent on first bootfirstboot --enableignoredisk --only-use=sda# Keyboard layoutskeyboard --vckeymap=us --xlayouts='us'# System languagelang en_US.UTF-8# Network informationnetwork  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto network  --hostname=localhost.localdomain# Root password# System servicesservices --disabled="chronyd"# System timezonetimezone Asia/Shanghai# X Window System configuration informationxconfig  --startxonboot# System bootloader configurationbootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sdazerombr# Partition clearing informationclearpart --allpart /boot --fstype="xfs" --ondisk=sda --size=1024part swap --fstype="swap" --ondisk=sda --size=2048part / --fstype="xfs" --ondisk=sda --size=50000part /app --fstype="xfs" --ondisk=sda --size=50000%packages@^gnome-desktop-environment@base@core@desktop-debugging@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@x11kexec-toolsautofslftpdhcpvsftpd%end%post#Create repomkdir /media/cdrommkdir /etc/yum.repos.d/bakmv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bakcat > /etc/yum.repos.d/base <
> /etc/fstab << EOF/dev/sr0        /media/cdrom    iso9660 defaults 0 0cat >> /root/.bashrc << EOFalias cdnet="cd /etc/sysconfig/network-scripts/"EOFcat >> /etc/profile.d/env.shexport PS1='\[\e[31m\][\u@\h\w]\$\[\e[0m\]'EOF%end

[root@centos6/var/www/html]#service httpd start #启动http服务 (2)安装syslinux-nonlinux包[root@centos6/var/www/html]#yum install -y syslinux-nonlinux(3)安装tfpt服务并复制文件[root@centos6/var/www/html]#cd /var/lib/tftpboot/[root@centos6/var/lib/tftpboot]#cp /usr/share/syslinux/{pxelinux.0,menu.c32} .#将菜单需要的文件复制到当前目录[root@centos6/var/lib/tftpboot]#cp /media/cdrom/isolinux/{vmlinuz,initrd.img} .#复制内核文件和虚拟文件系统到当前目录下[root@centos6/var/lib/tftpboot]#mkdir pxelinux.cfg[root@centos6/var/lib/tftpboot]#cp /media/cdrom/isolinux/isolinux.cfg pxelinux.cfg/default#复制菜单显示内容文件[root@centos6/var/lib/tftpboot]#vim pxelinux.cfg/default #修改default文件 default menu.c32 #prompt 1 timeout 600

menu title  PXE Install CentOS 6label desktop  menu label ^desktop  menu default  kernel vmlinuz  append initrd=initrd.img ks=http://192.168.4.137/ksdir/ks6.cfgmenu end

(4)安装dhcp服务并配置 [root@centos6~]#yum install -y dhcpd[root@centos6~]#cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.con[root@centos6~]#vim /etc/dhcp/dhcpd.conf 2017-09-17_223216.png[root@centos6~]#service dhcpd start #启动http服务 (5)新建一台虚拟机,查看是否可以成功

四、如何使用PXE能支持CentOS 6和CentOS 7的安装 (a)前几步骤都和上述安装过程类似 (b)修改配置文件 `[root@centos6~]#vim /var/lib/tftpboot/pxelinux.cfg/default

default menu.c32timeout 600menu title CentOS Linux  PXE Installlabel centos7  menu label Auto Install CentOS Linux ^7  kernel centos7/vmlinuz  append initrd=centos7/initrd.img ks=http://192.168.4.135/ksdir/ks7-1.cfglabel centos6  menu label Auto Install CentOS Linux ^6  kernel centos6/vmlinuz  append initrd=centos6/initrd.img ks=http://192.168.4.135/ksdir/ks6-1.cfglabel manual7  menu label ^Manual Install CentOS Linux 7  kernel centos7/vmlinuz  append initrd=centos7/initrd.img inst.repo=http://192.168.4.135/centos/7label manual6  menu label Manual ^Install CentOS Linux 6  kernel centos6/vmlinuz  append initrd=centos6/initrd.img inst.repo=http://192.168.4.135/centos/6menu end

(c)新建虚拟机测试