PXE+Kickstart 自动化部署系统

PXE(Preboot eXecute Environment,预启动执行环境)是由Intel公司开发的技术,可以让计算机通过网络来启动操作系统(前提是计算机上安装的网卡支持PXE技术),主要用于在无人值守安装系统中引导客户端主机安装Linux操作系统。

Kickstart是一种无人值守的安装方式,其工作原理是预先把原本需要运维人员手工填写的参数保存成一个ks.cfg文件,当安装过程中需要填写参数时则自动匹配Kickstart生成的文件。所以只要Kickstart文件包含了安装过程中需要人工填写的所有参数,那么从理论上来讲完全不需要运维人员的干预,就可以自动完成安装工作。

由于当前的客户端主机并没有完整的操作系统,也就不能完成FTP协议的验证了,所以需要使用TFTP协议帮助客户端获取引导及驱动文件。vsftpd服务程序用于将完整的系统安装镜像通过网络传输给客户端。当然,只要能将系统安装镜像成功传输给客户端即可,因此也可以使用httpd来替代vsftpd服务程序。

PXE的工作原理图解



配置DHCP服务程序

配置DHCP服务的目的是为了给局域网内暂时没有IP地址的机器分配一个IP地址,同时传输引导配置文件pxelinux.0,需要注意的是,应该开启DHCP的BOOTP功能,这样当用户获取到IP地址后,会主动请求获取引导驱动文件,从而进入下一步操作.

1.首先通过Yum仓库,安装DHCP服务程序.

[root@localhost ~]# yum install -y dhcp
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 12:dhcp-4.2.5-68.el7.x86_64 already installed and latest version
Nothing to do

2.编辑DHCP主配置文件,写入以下内容,开启BOOTP功能.

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf 
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#

subnet 192.168.1.0 netmask 255.255.255.0 {          #指明分配网段
         range 192.168.1.100 192.168.1.200;         #分配的网段(100-200)
         next-server 192.168.1.10;                  #指定TFTP服务器的地址
         filename "pxelinux.0";                     #指定PXE引导程序的文件名
         default-lease-time 21600;
         max-lease-time 4320;
}

3.启动DHCP服务,并设置开机自启动

[root@localhost ~]# systemctl restart dhcpd
[root@localhost ~]# systemctl enable dhcpd


配置TFTP服务程序

TFTP作为一种基于UDP协议的简单文件传输协议,不需要用户认证即可获取到用户所需的文件资源,因此接下来配置TFTP服务程序,为客户主机提供引导及驱动文件,当客户端有了基本的驱动程序之后,在通过VSFTP服务程序将完整的光盘镜像文件传输过去.

1.首先通过Yum仓库,安装TFTP服务程序.

[root@localhost ~]# yum install -y tftp tftp-server xinetd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package tftp-5.2-22.el7.x86_64 already installed and latest version
Package tftp-server-5.2-22.el7.x86_64 already installed and latest version
Nothing to do

2.TFTP是由xinetd服务守护的,所以要开启TFTP只需要修改xinetd服务的几个参数即可

[root@localhost ~]# vim /etc/xinetd.d/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 /var/lib/tftpboot   #设置默认工作目录
    disable         = no                     #设置ftp开机自启动
    per_source      = 11
    cps         = 100 2
    flags           = IPv4
}

3.重启xinetd服务,并设置为开机自启动

[root@localhost ~]# systemctl restart xinetd
[root@localhost ~]# systemctl enable xinetd


配置SYSLinux服务程序

SYSLinux是一个用于提供引导加载的服务程序,与其说SYSLinux是一个服务程序,不如说我们更需要里面的引导文件,在安装SYSLinux服务程序软件包后/usr/share/syslinux目录下回出现很多引导文件.

1.首先通过Yum仓库,安装SYSLinux服务程序.

[root@localhost ~]# yum install -y syslinux mtools
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package syslinux-4.05-13.el7.x86_64 already installed and latest version
Package mtools-4.0.18-5.el7.x86_64 already installed and latest version
Nothing to do

2.然后拷贝pxelinux.0引导文件到/var/lib/tftpboot目录下

[root@localhost ~]# cp -a /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost ~]# ls -l /var/lib/tftpboot/
total 28
-rw-r--r--. 1 root root 26826 May 10  2016 pxelinux.0

3.挂载RHEL光盘,并拷贝Linux的系统菜单和微内核

[root@localhost ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only

[root@localhost ~]# cp -a /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/
[root@localhost ~]# cp -a /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/

[root@localhost ~]# cp -a /mnt/isolinux/vesamenu.c32 /var/lib/tftpboot/
[root@localhost ~]# cp -a /mnt/isolinux/boot.msg /var/lib/tftpboot/

4.然后再TFTP目录中新建pxelinux.cfg目录,并将开机选项菜单复制到TFTP目录中,重命名为default.

[root@localhost ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@localhost ~]# cp -a /mnt/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/
[root@localhost ~]# mv /var/lib/tftpboot/pxelinux.cfg/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

5.编辑这个default文件,修改以下内容

[root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default

 59 menu separator # insert an empty line
 60 
 61 label linux
 62   menu label ^Install Red Hat Enterprise Linux 7.5
 63   menu default                                                      #添加默认选择菜单
 64   kernel vmlinuz
 65   append initrd=initrd.img ks=ftp://192.168.1.10/pub/ks.cfg         #指定主服务器IP地址
 66 
 67 label check
 68   menu label Test this ^media & install Red Hat Enterprise Linux 7.5
 69 #  menu default                                                     #注释掉,不用测试功能
 70   kernel vmlinuz
 71   append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.5\x20Server.x86_64 rd.live.check quiet
 72 
 73 menu separator # insert an empty line


配置VSFTP服务程序

前面的微内核传输完毕后,加载了开机菜单,下面我们就要使用VSFTP完整的传输RHEL镜像到远程主机了,当然你也可以使用Web网站替代VSFTP的功能,不过还是推荐使用VSFTP.

1.首先通过Yum仓库,安装VSFTP服务程序.

[root@localhost ~]# yum install -y vsftpd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package vsftpd-3.0.2-22.el7.x86_64 already installed and latest version
Nothing to do

2.拷贝RHEL光盘文件到/var/ftp/pub目录下,并赋予相应的权限.

[root@localhost ~]# mkdir -r /var/ftp/pub
[root@localhost ~]# cp -r /mnt/* /var/ftp/pub

[root@localhost ~]# chmod 777 -R /var/ftp/pub
[root@localhost ~]# chown ftp.ftp -R /var/ftp/pub

3.开启VSFTP匿名访问模式,并设置开机自启动

[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# systemctl enable vsftpd


创建KickStart应答文件

KickStart其实准确的说,并不是一个服务程序,而是一个应答文件,其中包含了系统安装过程中所需要的配置参数选项等,在我们安装完系统后,root的家目录里会有一个anaconda-ks.cfg文件,其实这就是安装完本系统的剧本,我们也可以多次利用.

1.这里我们直接复制下面的应答文件,改个名字即可使用啦.

#此处应配置生成装机文件    本步骤跳过(以下是测试脚本)
#-------------------------------------------------------------------
[root@localhost ~]# vim ks.cfg

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation#
url --url="ftp://192.168.1.10/pub/"                     #此处修改为服务器IP
# Root password
rootpw --iscrypted $1$K4WAmqxk$ccusVq9PIk6f1uMqJ48fI1
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info

# System timezone
timezone  Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all  
# Disk partitioning information
part /boot --asprimary --fstype="ext4" --size=500
part swap --fstype="swap" --size=2000
part / --fstype="ext4" --grow --size=1
clearpart --all --initlabel

%packages
@base

%end
#-------------------------------------------------------------------

[root@localhost ~]# cp -a ks.cfg /var/ftp/pub           #拷贝生成的脚本到指定目录下

如果您觉得以上脚本不能满足生产需求,您可以安装system-config-kickstart软件包,这是一款图形界面工具,可以方便的配置生成系统安装脚本.

另开一台机器,测试效果即可


猜你喜欢

转载自www.cnblogs.com/LyShark/p/9948177.html
今日推荐