PXE自动化安装Centos7

一、PXE介绍

    PXE: Preboot Excution Environment,由Intel公司研发,可以使没有任何操作系统的主机能够基于网络完成系统的安装工作,实现服务器的自动化安装系统

二、PXE工作原理

  • Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client
  • Client向PXE Server上的TFTP发送获取pxelinux.0请求消息, TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0
  • Client执行接收到的pxelinux.0文件
  • Client向TFTP Server发送针对本机的配置信息文件(在TFTP 服务的pxelinux.cfg目录下), TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作
  • Client向TFTP发送Linux内核请求信息, TFTP接收到消息之后将内核文件发送给Client
  • Client向TFTP发送根文件请求信息, TFTP接收到消息之后返回Linux根文件系统
  • Client启动Linux内核
  • Client下载安装源文件,读取自动化安装脚本

三、PXE Server 的配置

   PXE自动化安装Centos7

                                                            图1 PXE Server 的依赖服务

1. DHCP服务的配置

 安装包:dhcp-4.2.5-47.el7.centos.x86_64

[root@centos7 ~]#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf  --> 使用模本配置文件
[root@centos7 ~]#vi /etc/dhcp/dhcpd.conf

subnet 192.168.196.0 netmask 255.255.255.0 {                --> 定义网段
  range 192.168.196.10 192.168.196.40;                      --> 定义分配IP范围
  option routers 192.168.196.1;                             --> 配置路由
  filename "pxelinux.0";                                    --> 启动文件名称
  next-server 192.168.196.188;                              --> dhcp服务器地址                           
}
[root@centos7 ~]#systemctl start dhcpd
[root@centos7 ~]#systemctl enable dhcpd

配置完成,并设置DHCP静态IP192.168.196.188。目标主机开机获取IP后,可以访问PXE Sever

2. tftp、httpd服务的配置

安装包:tftp-5.2-13.el7.x86_64    httpd-2.4.6-45.el7.centos.x86_64

[root@Centos7 ~]#yum -q -y tftp  
[root@Centos7 ~]#yum -q -y httpd
[root@Centos7 ~]#systemctl start tftpd.socket httpd
[root@Centos7 ~]#systemctl enable tftp.socket
[root@Centos7 ~]#systemctl enable httpd

注意:centos6里tftp是由xinetd服务代理的,需要先安装xinetd服务,再chkconfig tftp on 命令开启tftp服务

3.关闭SElinux、防火墙

[root@Centos7 ~]#setenforce 0
[root@Centos7 ~]#vi /etc/selinux/config                      --> 修改配置文件
    SELINUX=permissive
[root@Centos7 ~]#systemctl stop firewalld
[root@Centos7 ~]#systemctl disable firewalld

4.kickstart应答文件准备

[root@centos7 ~]#mkdir /var/www/html/{centos7,ks}                       --> PXE Sever http服务会提供网络源于ks文件
[root@centos7 ~]#cp ~/anaconda-ks.cfg /var/www/html/ks/centos7-m.cfg    --> 以系统安装ks文件为范本
[root@centos7 ks]#vi centos7-m.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
url --url=http://192.168.196.188/centos7/                                --> 网络源由PXE Sever http服务提供
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=ens33 --onboot=on --ipv6=auto --activate  -->网卡配置
network  --hostname=centos7.ffu.com
# Root password
rootpw --iscrypted $6$8sIxlzOyC1RQ4g.X$UXgct3LHH67rVNH5StXAall/82K/5OkK6.QijHagr.DB4zw8IbnI0CYIUUAhTSDa.dfVfnZabRVZ8nFq5Cc1c1
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=2048
part /app --fstype="ext4" --ondisk=sda --size=50000
part / --fstype="ext4" --ondisk=sda --size=100000
part /boot --fstype="ext4" --ondisk=sda --size=1000

%packages                                                                   --> 安装包选择
@^minimal
@core
kexec-tools
%end                                                                        -->各个模块要以%end结束

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

%post                                                                       -->安装完成后运行自定义脚本
useradd ffu
echo 123456|passwd --stdin ffu rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo << EOF
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=0
EOF
%end

%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
 
[root@centos7 ks]#chmod a+r centos7-m.cfg                                      --> 为other加读权限,以便能通过http访问
[root@centos7 ks]#ll
-rw-r--r--. 1 root root 2042 Jul 24 15:00 centos7-m.cfg

5. 安装源准备

[root@centos7 ks]# mount /dev/sr0 /var/www/html/centos7/

6. 启动菜单、内核等相关文件准备

a.相关文件准备

[root@centos7 tftpboot]#mkdir /var/lib/tftpboot/pxelinux.cfg
[root@centos7 tftpboot]#cp /var/www/html/centos7/isolinux/isolinux.cfg  pxelinux.cfg/default    -->复制安装光盘启动菜单并重命名
[root@centos7 tftpboot]#cp /usr/share/syslinux/menu.c32 /usr/share/syslinux/pxelinux.0  ./      -->复制syslinux的菜单及启动文件
[root@centos7 tftpboot]#cp /var/www/html/centos7/isolinux/vmlinuz /var/www/html/centos7/isolinux/initrd.img ./
[root@centos7 tftpboot]#tree                                                                    -->目录结构
.
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── vmlinuz

b.启动菜单定制

[root@centos7 tftpboot]#vi pxelinux.cfg/default
default menu.c32                                                            -->所用菜单文件
timeout 600
menu title PXE CentOS Linux 7 Install Menu

label linux-mini
  menu label ^Auto-install CentOS Linux 7 Mini
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.196.188/ks/centos7-m.cfg      -->ks文件由PXE Sever http服务提供

label linux-desktop
  menu label Test Auto-install CentOS Linux 7 ^Desktop
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.196.188/ks/centos7.cfg        -->可根据不同目的定制不同应答文件
label linux-manual
  menu label Test ^Manual-Install CentOS Linux 7
  kernel vmlinuz
  append initrd=initrd.img

label local
  menu label Boot from ^local drive
  menu default
  localboot 0xffff

四、主机自动化安装系统实现

a.使用VMware实验,根据以上步骤配置PXE Server,然后新建虚拟机

PXE自动化安装Centos7

b.开机进入启动菜单,选择最小化安装

PXE自动化安装Centos7

c.安装完成

PXE自动化安装Centos7

原创文章,作者:ffu,如若转载,请注明出处:http://www.178linux.com/82613

(1)
ffuffu
上一篇 2017-07-24
下一篇 2017-07-25

相关推荐

  • bash脚本编程之算术运算和文件查找

    算数运算在每个编程语言里面是最基本的功能,在bash里面也是.相对于其他编程语言来说在bash里面不能直接以变量加上变量的形式来表现;比如我们先声明两个变量num1和num2然后再做运算。 num1=2 num2=3 echo "$num1+$num2" 2+3 这里我们显示的结果直接为2+3只是做了变量的替换,而不是做两个变量…

    Linux干货 2016-12-23
  • 运行级别

    运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式。运行级别通常分为7等,分别是从0到6,但如果必要的话也可以更多。 例如在大多数Linux操作系统下一共有如下7个典型的运行级别: 0 停机,关机 1 单用户,无网络连接,不运行守护进程,不允许非超级用户登录 2 多用户,无网络连接,不运行守护进程 3 多用户,正常启…

    Linux干货 2017-07-10
  • 22期+第一周课堂练习

    一.计算机的组成及其功能 计算机的组成及其功能结构图      各部件的关系图          二.按系列罗列Linux的发行版,并描述不同版本之间的联系与区别 1.各版本逻辑结构图 2.各版本的介绍   Redhat,应该称为Redhat系列,包括RHEL(Redh…

    Linux干货 2016-08-15
  • 第六周作业 bash编程学得好痛苦啊,啊啊啊~~需要挺住!

    请详细总结vim编辑器的使用并完成以下练习题 VIM很强大,但是万变不离其宗,最核心的三模式以下图表示: 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; :%s/^[[:blank:]]\+.*/\0#/g 2、复制/boot/grub/grub.conf至/…

    Linux干货 2016-12-19
  • 第十二周作业

    1、 请描述一次完整的http请求处理过程;  HTTP:超文本传输协议 完整的http请求过程 1)、建立TCP/IP连接:经过3次握手,建立连接或拒绝。 2)、浏览器向服务器发送HTTP请求。 3)、浏览器发送请求头信息。 4)、服务器应答:服务器接受请求后,会回送应答。 5)、服务器发送应答头信息。 6)、服务器向浏览器发送数据。 7)、服务…

    2017-05-17
  • Centos6.5上搭建openvpn

    前言     为了方便远程办公时访问公司的内部系统,如:svn、OA、wiki、禅道等等;通通在防火墙上做了端口映射。发现有时也不好用,所有开始弄OPENVPN。 openvpn简介     官方网站:https://openvpn.net 打不开请爬墙   &nbsp…

    Linux干货 2016-02-14