一、PXE技术原理概述

    PXEPreboot Execution Environment,预启动执行环境),由Intel公司提出,是一个用于通用网络启动的协议,通用网络启动允许在网络上的客户端从远程启动服务器上下载启动文件。这样就提供了网络管理员管理用于客户端的启动文件和操作系统的能力。PXE在操作系统自动部署和无盘工作站环境中有大量应用。本文主要探讨PXE在自动部署操作系统方面的优势。


二、PXE自动部署操作系统启动过程

PXE 自动部署操作系统启动过程如下:

1)客户端计算机启动,由于BIOS设置了网卡启动,所以网卡PXE  ROM中的程序被调入内存执行。

2)客户端在网络中寻找 DHCP 服务器,然后请求一个 IP 地址;

3)DHCP 服务器为客户端提供 IP 地址和其他网络参数。

4)DHCP 服务器联系到 TFTP 服务器为此客户端发送一个bootstrap(引导程序)

注:服务器的防火墙要确保为关闭状态,否则TFTP会连接超时。

关闭防火墙的命令:    chkconfig iptables off    (重启后永久性生效)

           serviceiptables stop       (即时生效,重启后失效)


5)客户端收到  bootstrap(引导文件 pxelinux.0)后执行,bootstrap 会请求TFTP传送bootstrap的配置文件(pxelinux.cfg/default)收到后读配置文件,根据该配置文件内容和客户情况,客户端请求  TFTP  传送内核映像文件(vmlinuz)和系统启动文件(initrd.img)

6)启动内核。

7)内核根据 bootstrap 的配置文件,通过网络获取操作系统自动安装脚本,并通过网络服务(nfs/ftp/http)获得系统所需安装文件,按照自动安装脚本的配置进行安装。


三、部署操作

服务器CentOS5.8(IP为10.10.10.107,网关10.10.10.1)上面安装了DHCP,TFTP,NFS等服务器

客户端(IP为10.10.10.200/24--10.10.10.220/24)即需要安装操作系统的节点。

1、配置DHCP、tftp、NFS

yum -y install dhcp tftp-server 

vi /etc/dhcpd.conf

[root@localhost ~]# grep -v "^#" /etc/dhcpd.conf

[root@localhost ~]# cat /etc/dhcpd.conf 

ddns-update-style none;

ignore client-updates;

allow booting;

allow bootp;

default-lease-time 21600;

max-lease-time 43200;

option routers              10.10.10.1;

subnet 10.10.10.0 netmask 255.255.255.0 {

        range dynamic-bootp 10.10.10.200 10.10.10.220;

        next-server 10.10.10.107;

        filename "/data/sys/kickstart/ks.cfg";

        next-server 10.10.10.107;

        filename "pxelinux.0";

}

启动服务:

[root@localhost ~]# service dhcpd start

[root@localhost ~]# chkconfig dhcpd on

2、配置TFTP服务:

[root@localhost ~]# cat /etc/xinetd.d/tftp

# default: off

# description: The tftp server serves files using the trivial file transfer \

#       protocol.  The tftp protocol is often used to boot diskless \

#       workstations, download configuration files to network-aware printers, \

#       and to start the installation process for some operating systems.

service tftp

{

        socket_type             = dgram

        protocol                = udp

        wait                    = yes

        user                    = root

        server                  = /usr/sbin/in.tftpd

        server_args             = -s /tftpboot

        disable                 = no            ----》改为no

        per_source              = 11

        cps                     = 100 2

        flags                   = IPv4

}

启动xinetd服务:

[root@localhost ~]# /etc/init.d/xinetd start

[root@localhost ~]# chkconfig xinetd on 


3、配置pxelinux.0文件

 也就是配置bootstrap,bootstrap文件在dhcpd.conf中被指定为pxelinux.0文件,放置在/tftpboot。

 Linux内核以及Linux根文件系统也放置在/tftpboot。pxelinux.0在执行过程中,要读配置文件,所有的配置文件都放在/tftpboot/pxelinux.cfg/目录下。

操作流程如下:

[root@localhost ~]# cp /usr/lib/syslinux/pxelinux.0  /tftpboot/

[root@localhost ~]# mount /dev/cdrom /mnt/

[root@localhost ~]# cp /mnt/p_w_picpaths/pxeboot/{initrd.img,vmlinuz} /tftpboot/

[root@localhost ~]# cp /mnt/isolinux/*.msg  /tftpboot/

[root@localhost ~]# mkdir /tftpboot/pxelinux.cfg

[root@localhost ~]# cp /mnt/isolinux/isolinux.cfg  /tftpboot/pxelinux.cfg/default

[root@localhost ~]# chmod u+w /tftpboot/pxelinux.cfg/default

查看default文件的内容:

[root@localhost ~]# cat /tftpboot/pxelinux.cfg/default

default text

prompt 1         

timeout 6     

label text

   kernel vmlinuz

   #append  ks=http://10.10.10.107/ks/ks.cfg  initrd=initrd.img text

   append  ks=nfs:10.10.10.107:/data/sys/kickstart/ks.cfg  initrd=initrd.img text

4、配置NFS或http服务器

1)如果使用NFS做安装树,修改/etc/export文件,设置NFS

vim /etc/export

/mnt/    *(ro)

service portmap start

service nfs start

如果后面对此配置有改动,只需要重载即可,如

exportfs -avr


2)如果使用http服务做安装树

mkdir /var/www/html/centos

mount /dev/cdrom  /var/www/html/centos

service httpd start

注意:客户机安装的时候,如果使用的是http服务,填写http的地方,上面填写http的服务IP地址,下面填写光盘内容所在的目录。这里我们填写10.10.10.107;centos


这里我们用NFS服务器:

mount /dev/cdrom /mnt 

mkdir -p /data/sys

cp -a /mnt/*  /data/sys/

echo "/data/sys  10.10.10.0/24(ro,sync)" >>/etc/export


启动服务:

service portmap start

service nfs start

chkconfig portmap on 

chkconfig nfs on


5、ks.cfg配置文件的内容 

mkdir -p /data/sys/kickstart

1)配置ks.cfg文件

[root@localhost ~]# cat /data/sys/kickstart/ks.cfg 

# Kickstart file automatically generated by anaconda.

install

#url --url http://10.10.10.107/ks

nfs --server=10.10.10.107 --dir=/data/sys

lang en_US.UTF-8

keyboard us

xconfig --startxonboot

network --device eth0 --bootproto dhcp

rootpw  redhat

#rootpw --iscrypted $1$JubQ218O$c45gs3bCu.ZJlPkDX5UAK1

firewall --disabled

authconfig --enableshadow --enablemd5

selinux --disabled

timezone --utc Asia/Shanghai

bootloader --location=mbr --driveorder=sda 

firstboot --disable

zerombr

# The following is the partition information you requested

# Note that any partitions you deleted are not expressed

# here so unless you clear all partitions first, this is

# not guaranteed to work

#clearpart --linux

clearpart --all

part /boot --fstype ext3 --size=200   --asprimary

part / --fstype ext3 --size=1 --grow  --asprimary

part swap --size=1024

reboot


%packages

@base

@core

@development-libs

@development-tools

@editors

@system-tools

@x-software-development

@chinese-support


%post

#base init configuration

echo "nameserver 211.138.24.66" >>/etc/resolv.conf

echo "nameserver 8.8.8." >>/etc/resolv.conf

mkdir -p /application/tools

mkdir -p /server/{scripts,backup}


#add user peng

useradd peng

echo "redhat" |passwd --stdin peng 


#allow peng sudo 

echo "peng ALL=(ALL)  NOPASSWD:ALL" >>/etc/sudoers


#service configuration 

for service in `chkconfig --list |grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $service off;done

for service in crond network syslog sshd;do chkconfig --level 3 $service on;done


#config ssh

\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"`

sed -i 's%#Port 22%Port 52113%g' /etc/ssh/sshd_config

sed -i 's%#PermitRootLogin yes%PermitRootLogin no%g' /etc/ssh/sshd_config

sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%g' /etc/ssh/sshd_config

sed -i 's%#UseDNS yes%UseDNS no%g' /etc/ssh/sshd_config


2)赋予权限

chmod 644 /data/sys/kickstart/ks.cfg 


6、客户机通过PXE安装系统测试

   将客户端设定为从网络启动,启动后将会进入自动安装系统界面,整个安装过程和CDROM安装没有多大差别,不同的是安装是选择从NFS安装或http,并指定NFS服务器的IP和目录。