PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统即所谓的无盘工作站。


PXE工作条件

  • 客户端

    • 硬件支持:网卡或主板集成了PXEClient才可以和PXEServer进行通信

  • 服务端

    • DHCP服务:既然要实现网络通信IP地址自然是必不可少的

    • TFTP(Trivial FTP)或MFTP(Multicast TFTP)服务,简单FTP服务提供PXE启动需要的pxelinux.0、vmlinux和initrd等文件

    • DNS服务,有时会用到,可选

    • FTP/HTTP/NFS 输出系统安装树,软件仓库


操作演示

# step 1: 安装dhcp
[root@node1 ~]# yum install -y dhcp
# 为省事可以直接拷个配置文件的样例过去再做修改
[root@node1 ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf 
cp: overwrite `/etc/dhcp/dhcpd.conf'? y
[root@node1 ~]# vim /etc/dhcp/dhcpd.conf 
option domain-name "bob.org";    # 域名后缀
option domain-name-servers 172.16.100.2;    # dns 服务器

default-lease-time 43200;    # IP地址租约
max-lease-time 86400;        # 最大租约时间
    
log-facility local7;         # 日志文件

# 一个subnet定义一个IP地址池
subnet 172.16.100.0 netmask 255.255.0.0 {
  range 172.16.100.100 172.16.100.200;
  option routers 172.16.100.2;    
  next-server 172.16.100.11;    # 指定tftp服务器地址
  filename="pxelinux.0";        # 指定pxlinux.0文件地址,必须用双引号,地址是相对tftp服务根目录的
}

# 安装tftp和syslinux, 安装syslinux会自动安装上syslinux-nonlinux和mtools
#(pxelinux.0由syslinux-nonlinux提供的)
[root@node1 ~]# yum install tftp-server syslinux -y
[root@node1 ~]# rpm -ql tftp-server
/etc/xinetd.d/tftp    # tftp服务脚本,是Xinet管理的服务
/usr/sbin/in.tftpd
/usr/share/doc/tftp-server-0.49
/usr/share/doc/tftp-server-0.49/CHANGES
/usr/share/doc/tftp-server-0.49/README
/usr/share/doc/tftp-server-0.49/README.security
/usr/share/doc/tftp-server-0.49/README.security.tftpboot
/usr/share/man/man8/in.tftpd.8.gz
/usr/share/man/man8/tftpd.8.gz
/var/lib/tftpboot        # tftp服务的根目录

# 可以看到pxlinux.0是由syslinux-nonlinux这个软件包提供的
[root@node1 ~]# rpm -ql syslinux-nonlinux | grep pxelinux.0
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0

# 把pxelinux.0复制到tftp服务的要目录下,因为dhcp配置里指定的是根目录
[root@node1 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

# 为方便直接把光盘挂到ftp目录下面
[root@node1 ~]# mount -r /dev/cdrom /var/ftp/pub/centos/

# 为tftp下面提供vmlinuz和initrd.img
[root@node1 ~]# cp /var/ftp/pub/centos/p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/
[root@node1 ~]# cd /var/lib/tftpboot/
[root@node1 tftpboot]# ls
initrd.img  pxelinux.0  vmlinuz

# tftp默认的菜单配置文件是放在pxelinux.cfg/default下的
# 这个相当于光盘下isolinux/isolinux.cfg,可以把这个文件复制过来修改一下就好
[root@node1 tftpboot]# mkdir pxelinux.cfg

# 为tftp目录下准备菜单程序和boot.msg,背景图片splash.jpg
# vesamenu.c32为菜单程序,可从光盘里复制过来,syslinx-nonlinux包也提供了,在/usr/share/syslinux/下
[root@node1 tftpboot]# cp /var/ftp/pub/centos/isolinux/{boot.msg,splash.jpg,vesamenu.c32} ./
[root@node1 tftpboot]# ls
boot.msg  initrd.img  isolinux.cfg  pxelinux.0  splash.jpg  vesamenu.c32  vmlinuz
[root@node1 tftpboot]# cp /var/ftp/pub/centos/isolinux/isolinux.cfg pxelinux.cfg/default

# 根据需要修改下default文件
[root@node1 tftpboot]# vim pxelinux.cfg/default 

default vesamenu.c32
#prompt 1
timeout 600

display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.4 via PXE!
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
  menu label ^Install or upgrade an existing system
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=ftp://172.16.100.11/pub/ks.cfg
label rescue
  menu lalbe ^Rescue
  kernel vmlinuz
  append initrd=initrd.img rescue

# tftp是xinet管理的服务工作在端口69/udp, 启动tftp和dhcp服务
[root@node1 ~]# chkconfig tftp on
[root@node1 ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@node1 ~]# service dhcpd start
Starting dhcpd:                                            [  OK  ]      
[root@node1 ~]# netstat -unlp | grep 67
udp        0      0 0.0.0.0:67    0.0.0.0:*               15641/dhcpd         
[root@node1 ~]# netstat -unlp | grep 69
udp        0      0 0.0.0.0:69    0.0.0.0:*               16097/xinetd

# 连上去查看tftp是否工作正常,tftp没有ls命令
[root@node1 ~]# tftp 172.16.100.11
tftp> help
tftp-hpa 0.49
Commands may be abbreviated.  Commands are:

connect 	connect to remote tftp
mode    	set file transfer mode
put     	send file
get     	receive file
quit    	exit tftp
verbose 	toggle verbose mode
trace   	toggle packet tracing
literal 	toggle literal mode, ignore ':' in file name
status  	show current status
binary  	set mode to octet
ascii   	set mode to netascii
rexmt   	set per-packet transmission timeout
timeout 	set total retransmission timeout
?       	print help information
help    	print help information

# verbose打开冗余模式,可以显示更多详细信息
tftp> verbose
Verbose mode on.

# get文件成功代表tftp正常工作
tftp> get pxelinux.0
getting from 172.16.100.11:pxelinux.0 to pxelinux.0 [netascii]
Received 26981 bytes in 0.0 seconds [72143767 bit/s]


安装过程截图:

这个界面一闪就过去了,想看到的话得把tftp停一下就可以看到了

wKiom1P_JZOAsGUmAAD2Z4n_LMo664.jpg

wKioL1P_Jqui5-MAAAER_uLuSJU951.jpg

安装成功

wKiom1P_KFPBjv5oAAEDxC0oKu4443.jpg