您的位置:首页 > 运维架构 > Linux

PXE实现批量部署linux系统

2016-03-01 10:26 726 查看
pxe批量部署linux服务器


1、pxe介绍

PXE是 有intel设计的协议,它可以使计算机通过网络启动,协议分为client和server两端,PXEclient在网卡的ROM中,当计算机引导 时,BIOS把PXE client调入内存中执行,并显示出命令菜单,经用户选择PXE启动后,PXE client获取网络地址信息,然后将放置在远端的服务器上的操作系统文件通过网络下载到本地运行

既然是通过网络传输,就需要IP地址,也就是说在PXE启动过程中,PXE客户端会请求DHCP服务器分配IP地址,之后PXEclient使用TFTP client通过TFTP协议连接服务器下载启动安装程序所需要的文件到本地


2、PXE安装过程



第一步:PXE client向DHCPserver发送请求

首先,开机启动PXE安装,此时PXE client会通过 PXE Boot ROM(自动芯片)以UDP(简单用户数据协议)的形式在网络中发送一个广播请求,请求DHCP服务器分配IP地址等相关信息。

第二步:DHCP服务器应答PXE client

DHCP验证是否是合法的PXE client的请求,验证通过后,回应PXE client,回应中包含了为PXE client分配的ip之地址、TFTP(pxelinux启动程序)的位置,以及配置文件所在的位置。

第三步:PXE client请求下载启动文件

客户端收到DHCP的回应后,向TFTP服务器请求传送启动系统安装所需要的文件,这些文件包括:pxelinux0、pxelinux.cnf/default(内核文件)、vmlinuz、initrd.img等文件

第四步:TFTP服务器响应客户端请求并传送文件

当 TFTP收到客户端的请求之后,发给客户端所需要的启动文件,BootROM由TFTP通讯协议从BootServer下载启动安装程序所必须的文件 (pxelinux0、pxelinux.cnf/default),default文件下载完之后会根据该文件定义的引导顺序,启动linux安装程序 的引导内核。

第五步:请求下载自动应答文件(ftp服务器)

PXE client通 过default文件成功的引导linux安装内核后,安装程序首先确定你通过什么安装介质来安装linux,如果是通过网络安装(NFS、HTTP、 FTP),则会在这个时候初始化网络,并定位安装系统所需的二进制包以及配置文件的位置,接着会读取该文件中指定的自动应答文件ks.cfg,然后根据 ks.cfg中的文件位置请求下载相关文件。

第六步:根据ks.cfg文件内容进行安装系统

将ks.cfg文件下载回来后,通过该文件找到OS Server,并按照该文件的配置请求下载安装过程需要的软件包。


3、pxe的部署

系统:CentOS release 6.4 (Final)- 2.6.32-358.el6.x86_64

所需服务:vsftpd,dhcp,tftp

步骤:

1)配置DHCP,用于给客户端提供IP地址及其它信息

2)配置TFTP服务器,用于提供客户端PXE引导所必须的文件

3)配置FTP服务器,用于存放安装文件

4)配置Kickstart,用于自动应答安装

5)使用PXE功能引导客户机

3.1 配置dhcp服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@PXE-SERVER ~]# yum install dhcp –y
[root@PXE-SERVER ~]# vim /etc/dhcp/dhcpd.conf
[root@PXE-SERVER ~]# cat /etc/dhcp/dhcpd.conf
ddns-update-style none;
ignore client-updates;
subnet 192.168.1.0 netmask 255.255.255.0 {
optionrouters 192.168.1.1;
option subnet-mask 255.255.255.0;
optiondomain-name "JeckFY.org";
optiondomain-name-servers 192.168.1.1;
optiontime-offset -18000;
range192.168.1.128 192.168.1.254;
default-lease-time21600;
max-lease-time43200;
next-server 192.168.1.1;
filename"pxelinux.0";
}
[root@PXE-SERVER ~]# service dhcpd configtest
Syntax: OK
[root@PXE-SERVER ~]# service dhcpd start
Starting dhcpd: [ OK ]
[root@PXE-SERVER ~]# netstat -anput | grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 1300/dhcpd
[root@PXE-SERVER ~]# ps -ef | grep dhcp
dhcpd 1300 1 0 22:40 ? 00:00:00 /usr/sbin/dhcpd -user dhcpd-group dhcpd
root 1307 1155 022:41 pts/0 00:00:00 grep dhcp
[root@PXE-SERVER ~]# chkconfig dhcpd on

3.2 配置tftp服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1)安装软件
[root@PXE-SERVER ~]# yum install xinetd tftp* syslinux -y
[root@PXE-SERVER ~]# vim /etc/xinetd.d/tftp
[root@PXE-SERVER ~]# cat /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivialfile transfer \
# protocol. The tftp protocol is often used to bootdiskless \
# workstations,download configuration files to network-aware printers, \
# and to start theinstallation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no #开启tftp服务,默认为yes
per_source = 11
cps = 100 2
flags = IPv4
}
2)拷贝所需文件
TFTP服务器为客户端主要提供pxelinux.0引导文件,

vmlinuz内核文件,initrd.img基础安装平台文件以及default文件。
挂载OS镜像到/media目录下,用于拷贝所需文件
[root@PXE-SERVER ~]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mountingread-only
拷贝pxelinux.0vmlinuz initrd.img,以及生成default文件
[root@PXE-SERVER ~]# cp /usr/share/syslinux/pxelinux.0/var/lib/tftpboot/
[root@PXE-SERVER ~]# cp /media/isolinux/vmlinuz /var/lib/tftpboot/
[root@PXE-SERVER ~]# cp /media/isolinux/initrd.img/var/lib/tftpboot/
[root@PXE-SERVER ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@PXE-SERVER ~]# cp /media/isolinux/isolinux.cfg/var/lib/tftpboot/pxelinux.cfg/
[root@PXE-SERVER ~]# mv /var/lib/tftpboot/pxelinux.cfg/isolinux.cfg/var/lib/tftpboot/pxelinux.cfg/default
[root@PXE-SERVER ~]# ls /var/lib/tftpboot/
initrd.img pxelinux.0 pxelinux.cfg vmlinuz
[root@PXE-SERVER ~]# ls /var/lib/tftpboot/pxelinux.cfg/
default
3)编辑default文件
[root@PXE-SERVER ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default linux #default项中标记的启动内核为默认启动项
prompt 0 #显示boot:提示符。为0时则不提示,将会直接启动default参数中指定的内容。

为1时会等待输入label后关键字,从而启动相应内核
timeout 600 #等待超时时间
display boot.msg
label linux
menu label ^Install orupgrade an existing system
menu default
kernel vmlinuz #kernel 参数指定要启动的内核。
append initrd=initrd.imgks=ftp://192.168.1.1/ks.cfg ksdevice=eth0

# ks指定了ks文件的路径,ksdevice指定从哪块网卡获取安装文件
4)启动tftp
[root@PXE-SERVER ~]# service xinetd start
Starting xinetd: [ OK ]
[root@PXE-SERVER ~]# chkconfig xinetd on
[root@PXE-SERVER ~]# netstat -anput | grep xinetd
udp 0 0 0.0.0.0:69 0.0.0.0:*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@PXE-SERVER ~]# yum install vsftpd –y
[root@PXE-SERVER ~]# mkdir -p /var/ftp/centos
[root@PXE-SERVER ~]# cp -fr /media/* /var/ftp/centos/
[root@PXE-SERVER ~]# ls /var/ftp/centos/
CentOS_BuildTag RELEASE-NOTES-en-US.html
EFI repodata
EULA RPM-GPG-KEY-CentOS-6
GPL RPM-GPG-KEY-CentOS-Debug-6
images RPM-GPG-KEY-CentOS-Security-6
isolinux RPM-GPG-KEY-CentOS-Testing-6
Packages TRANS.TBL
[root@PXE-SERVER ~]# service vsftpd start
Starting vsftpd for vsftpd: [ OK ]
[root@PXE-SERVER ~]# chkconfig vsftpd on
[root@PXE-SERVER ~]# netstat -anput | grep vsftpd
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1444/vsftpd

3.4 生成ks.cfg文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@PXE-SERVER~]#yum install system-config-kickstart –y
进入图形化界面配置kickstart
[root@PXE-SERVER ~]# init 5
[root@PXE-SERVER ~]#system-config-Kickstart
按照提示进行配置,最后保存到/var/ftp/目录下
[root@PXE-SERVER ~]# cat /var/ftp/ks.cfg
#platform=x86, AMD64, 或 IntelEM64T
#version=DEVEL
# Firewall configuration
firewall --enabled
# Install OS instead of upgrade
install ====>表示全新安装,而不是升级update
# Use network installation
url --url="ftp://192.168.1.1/centos" ====>OS文件所在的位置
# Root password
rootpw --iscrypted $1$5axKiu3u$l2pavqIifA/MUgXUHadKa. ====>root密码
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical ====>表示采用图形化模式安装(不是安装图形化界面)
firstboot –disable ====>安装完时候自动重启(若启用该设置会导致第二次自动安装)
# System keyboard
keyboard us ====>键盘语言
# System language
lang en_US ====>系统语言环境
# SELinux configuration
selinux –enforcing ====>selinux是否启用
# Installation logging level
logging --level=info ====>日志级别

# System timezone
timezone Africa/Abidjan ====>时区设置
# Network information
network --bootproto=dhcp--device=eth0 --onboot=on ====>网卡设置
# System bootloader configuration
bootloader --location=mbr ====>硬盘模式为mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel ====>清除全部分区
# Disk partitioning information ====>分区信息
part /boot --fstype="ext4" --size=200
part swap --fstype="swap" --size=4096
part / --fstype="ext4" --grow --size=1
%packages ====>默认安装软件包
@base
@chinese-support
@development
@system-admin-tools
%end

至此:pxe服务器端已经部署完毕。在客户机端开机选择pxe启动,就可以实现自动批量装系统


扩展

多版本同时批量部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
修改default的文件
default linux
prompt 1
timeout 600
#display boot.msg
display memu
label centos5.6
kernel centos5.6/vmlinuz
appendinitrd=centos5.6/initrd.img ks=ftp://192.168.1.1/ks/centos5.6.cfg ksdevice=eth0
label centos6.0
kernel centos6.0/vmlinuz
appendinitrd=centos6.0/initrd.img ks=ftp://192.168.1.1/ks/centos6.0.cfg ksdevice=eth0
label centos6.4
kernel centos6.4/vmlinuz
appendinitrd=centos6.4/initrd.img ks=ftp://192.168.1.1/ks/centos6.4.cfg ksdevice=eth0
在根据default文件的各个所需文件的路径。创建对应的文件即可

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: