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

相关推荐

  • Linux文件系统

    Linux文件系统 根目录下的文件 / ├── bin 存放用户使用的基本命令(可执行程序,二进制文件) 不能单独分区的 ├── boot 跟内核有关的文件 grub 内核和BootLoader ├── cgroup 用来资源限制资源隔离 docker 容器化 ├── dev device 设备文件 b block device 块设备 硬盘或者CD-rom…

    Linux干货 2017-04-01
  • PHP数组实际占用内存大小的分析

    我们在前面的php高效写法提到,尽量不要复制变量,特别是数组。一般来说,PHP数组的内存利用率只有 1/10, 也就是说,一个在C语言里面100M 内存的数组,在PHP里面就要1G。下面我们可以粗略的估算PHP数组占用内存的大小,首先我们测试1000个元素的整数占用的内存: <?php      &nb…

    系统运维 2015-05-28
  • 马哥教育网络班21期-第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash m=0 n=0 for i in `awk -F: '{print $NF}'&nb…

    Linux干货 2016-09-15
  • keepalived+nginx

    keepalived可以认为是VRRP协议在Linux上的实现,主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。本文基于如下的拓扑图: 配置keepalived+nginx的方…

    Linux干货 2016-11-04
  • 逻辑卷管理(LVM)

    逻辑卷管理(LVM) LVM概念 是系统上对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上的一个逻辑层,用来提高磁盘分区管理的灵活性。 安装Linux系统时,最常见的问题是如何评估各分区大小,以分配合适的硬盘空间。随着LVM的出现,这些问题都引刃而解了,用户在无需停机的情况下方便地情况下方便的调整个分区的大小。 LVM重点在于可以弹性的调整文件系统的容量…

    Linux干货 2016-11-22
  • N25-Bazinga-第三周作业

    N25-Bazinga-第三周作业 1.列出当前系统上登录的所有用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# w | cut -d' ' -f1 | uniq -c |&nbs…

    Linux干货 2016-12-21