红联Linux门户
Linux帮助

kickstart 全自动化部署安装RHEL 7.0(详细图解版)

发布时间:2015-09-01 17:21:59来源:红联作者:luoj1992
一、简介
1.1 什么是PXE
PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端基本软件设置,从而引导预先安装在服务器中的终端操作系统。
严格来说,PXE 并不是一种安装方式,而是一种引导方式。进行 PXE 安装的必要条件是在要安装的计算机中必须包含一个 PXE 支持的网卡(NIC),即网卡中必须要有 PXE Client。PXE 协议可以使计算机通过网络启动。此协议分为 Client端和 Server 端,而PXE Client则在网卡的 ROM 中。当计算机引导时,BIOS 把 PXE Client 调入内存中执行,然后由 PXE Client 将放置在远端的文件通过网络下载到本地运行。运行 PXE 协议需要设置 DHCP 服务器和 TFTP 服务器。DHCP 服务器会给 PXE Client(将要安装系统的主机)分配一个 IP 地址,由于是给 PXE Client 分配 IP 地址,所以在配置 DHCP 服务器时需要增加相应的 PXE 设置。此外,在 PXE Client 的 ROM 中,已经存在了 TFTP Client,那么它就可以通过 TFTP 协议到 TFTP Server 上下载所需的文件了。
PXE的工作过程:
1. PXE Client 从自己的PXE网卡启动,向本网络中的DHCP服务器索取IP;
2. DHCP 服务器返回分配给客户机的IP 以及PXE文件的放置位置(该文件一般是放在一台TFTP服务器上) ;
3. PXE Client 向本网络中的TFTP服务器索取pxelinux.0 文件;
4. PXE Client 取得pxelinux.0 文件后之执行该文件;
5. 根据pxelinux.0 的执行结果,通过TFTP服务器加载内核和文件系统 ;
6. 进入安装画面, 此时可以通过选择HTTP、FTP、NFS 方式之一进行安装;
详细工作流程,请参考下面这幅图:


1.2 什么是Kickstart
Kickstart是一种无人值守的安装方式。它的工作原理是在安装过程中记录典型的需要人工干预填写的各种参数,并生成一个名为ks.cfg的文件。如果在安装过程中(不只局限于生成Kickstart安装文件的机器)出现要填写参数的情况,安装程序首先会去查找Kickstart生成的文件,如果找到合适的参数,就采用所找到的参数;如果没有找到合适的参数,便需要安装者手工干预了。所以,如果Kickstart文件涵盖了安装过程中可能出现的所有需要填写的参数,那么安装者完全可以只告诉安装程序从何处取ks.cfg文件,然后就去忙自己的事情。等安装完毕,安装程序会根据ks.cfg中的设置重启系统,并结束安装。
PXE+Kickstart 无人值守安装操作系统完整过程如下:


-----------------------------------------------------------------
系统环境
实验环境:VMware Workstation 11
系统平台:RHEL7
网络模式:LAN区段
DHCP / TFTP IP:192.168.153.130
HTTP / FTP / NFS IP:192.168.153.130
防火墙已关闭/iptables: Firewall is not running.
SELINUX=disabled
基础服务教程:http://dwz.cn/GHgmQ
---------------------------------------------------分割线-------------------------------------------------------------------
前期准备
所需要用到的服务:DHCP、TFTP、VSFTP

配置yum仓库,挂载光盘镜像
#vim /etc/yum.repos.d/rhel7.repo

[rhel7]
name=rhel7
basurel=file:///mnt
enabled=1
gpgcheck=0

将光盘挂载到/mnt中
#mount /dev/cdrom /mnt

---------------------------------------------------分割线-------------------------------------------------------------------
配置DHCP

安装DHCP服务
# yum -y install dhcp

修改/etc/dhcp/dhcpd.conf 配置文件,内容如下:

subnet 192.168.153.0 netmask 255.255.255.0 { #所属网段及掩码;
range 192.168.153.100 192.168.153.120; #IP地址池范围;
option domain-name "linuxidc.seagate.com";
option routers 192.168.153.130; #路由器IP,可以写网关IP;
option broadcast-address 192.168.153.255;
next-server 192.168.153.130; #TFTP Server 的IP地址;
filename "pxelinux.0"; #pxelinux 启动文件位置;
default-lease-time 600;
max-lease-time 7200;
}

启动DHCP服务
#systemctl enable dhcpd.service
#systemctl start dhcpd.service

---------------------------------------------------分割线-------------------------------------------------------------------
配置TFTP

安装TFTP
# yum install tftp-server -y //此步骤会安装两个包,一个是tftp-server另一个是xinetd。

修改/etc/xinetd.d/tftp配置文件,内容如下:

service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no #把这行改成no即可;
per_source = 11
cps = 100 2
flags = IPv4
}

启动xinetd服务
#systemctl enable xinetd.service
#systemctl start xinetd.service

---------------------------------------------------分割线-------------------------------------------------------------------
配置vsftp

安装vsftp
#yum -y install vsftpd

启动vsftpd服务
#systemctl start vsftpd.service
#systemctl enable vsftpd.service

创建iso文件夹目录,用来存放光盘软件包
#mkdir /var/ftp/iso

拷贝光盘中所有文件到iso文件夹中
#cp -rf /mnt/* /var/ftp/iso/

---------------------------------------------------分割线-------------------------------------------------------------------
配置PXE启动所需要的文件

#yum -y install syslinux
说明:syslinux是一个功能强大的引导加载程序,而且兼容各种介质。更加确切地说:SYSLINUX是一个小型的Linux操作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。

拷贝启动文件到/var/lib/tftpboot里
#cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#mkdir /var/lib/tftpboot/pxelinux.cfg
#cd /mnt/isolinux/
#cp -rf initrd.img vmlinuz vesamenu.c32 boot.msg /var/lib/tftpboot/
#cp isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

检查
[root@localhost tftpboot]# pwd
/var/lib/tftpboot
[root@localhost tftpboot]# ls
initrd.img pxelinux.0 pxelinux.cfg vesamenu.c32 vmlinuz boot.msg
[root@localhost tftpboot]# cd pxelinux.cfg/
[root@localhost pxelinux.cfg]# ls
default

---------------------------------------------------分割线-------------------------------------------------------------------
生成ks.cfg 文件
ks.cfg是kickstart安装配置文件,系统就是按照ks.cfg来安装的。我们将在后面配置他

安装Kickstart
#yum -y install system-config-kickstart
在桌面环境下配置Kickstart
启动X Windows 环境
#startx
配置Kickstart
#system-config-kickstart









后面几项不用管,直接保存

保存在/root/下,后缀不要动


root目录下有个anaconda-ks.cfg文件,我们进去把安装软件脚本拷贝到咱们刚才创建的那个ks.cfg中
#vim /root/anaconda-ks.cfg

........
%packages
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@kde-desktop
@multimedia
@print-client
@x11

%end
把anaconda-ks.cfg文件最下方的脚本粘贴到咱们的ks.cfg中

#vim /root/ks.cfg
把上面一串@的所有内容都粘贴进去,包括两个%哪行。

把ks文件拷贝到/var/ftp/里面
#cp /root/ks.cfg /var/ftp/

编辑/var/lib/tftpboot/pxelinux.cfg/default文件
添加一个引导选项,最后一行指向ftp应答文件的网络路径
#vim /var/lib/tftpboot/pxelinux.cfg/default


cp /var/www/html/cdrom/isolinux/*.msg /var/lib/tftpboot/

---------------------------------------------------分割线-------------------------------------------------------------------
检查

检查SELinux是否关闭
#setenforce 0 //关闭SELinux
检查防火墙,开放dhcp,ftp,tftp服务,或者关闭防火墙
#firewall-cmd --permanent --add-service=dhcp
#firewall-cmd --permanent --add-service=ftp
#firewall-cmd --permanent --add-port=69/udp
#firewall-cmd --reload
返回结果都是“success”
注:这里也可以通过systemctl stop firewall来关闭防火墙

检查所有服务是否正常启动
#systemctl is-active dhcpd
#systemctl is-active vsftpd
返回结果都是“active”
#netstat -tulnp | grep :69
udp 0 0 0.0.0.0:69 0.0.0.0:*
3912/xinetd



---------------------------------------------------分割线-------------------------------------------------------------------
default文件

[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 600

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Red Hat Enterprise Linux 7.0
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label rhel //标红的部分是咱们添加的部分
menu label ^Install RHEL7.0
menu default
kernel vmlinuz
append initrd=initrd.img inst.stage2=ftp://192.168.153.130/iso inst.ks=ftp://192.168.153.130/ks.cfg quiet


label linux
menu label ^Install Red Hat Enterprise Linux 7.0
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 quiet

label check
menu label Test this ^media & install Red Hat Enterprise Linux 7.0
menu default
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
menu title Troubleshooting

label vesa
menu indent count 5
menu label Install Red Hat Enterprise Linux 7.0 in ^basic graphics mode
text help
Try this option out if you're having trouble installing
Red Hat Enterprise Linux 7.0.
endtext
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 xdriver=vesa nomodeset quiet

label rescue
menu indent count 5
menu label ^Rescue a Red Hat Enterprise Linux system
text help
If the system will not boot, this lets you access files
and edit config files to try to get it booting again.
endtext
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rescue quiet

label memtest
menu label Run a ^memory test
text help
If your system is having issues, a problem with your
system's memory may be the cause. Use this utility to
see if the memory is working correctly.
endtext
kernel memtest

menu separator # insert an empty line

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

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
menu label Return to ^main menu
menu exit

menu end

---------------------------------------------------分割线-------------------------------------------------------------------
ks.cfg文件

[root@localhost ~]# cat /var/ftp/ks.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'# Reboot after installation
reboot
# Root password
rootpw --iscrypted $1$J36yI8p5$UCVRjlL947gD1e5zZR9uR/
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="ftp://192.168.153.130/iso"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled

# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --asprimary --fstype="ext4" --size=10240

%packages //这里及以后的内容是从/root/anaconda-ks.cfg复制过来的
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@kde-desktop
@multimedia
@print-client
@x11

%end

---------------------------------------------------分割线-------------------------------------------------------------------
安装测试

见附件视频。
[attach]42284[/attach][attach]42285[/attach]
文章评论

共有 5 条评论

  1. porrt8 于 2016-01-13 19:15:40发表:

    最近正愁批量装服务器慢,有这篇东西真是太好了

  2. fangzhan9527269 于 2015-11-07 12:15:41发表:

    推荐新手读下《Linux就该这么学》,里面包含了Linux技术知识以及所有红帽RHCSA+RHCE的考点知识点。
    书籍可以在线学习,挺方便的~ http://www.linuxprobe.com/

  3. badoo 于 2015-09-04 11:54:55发表:

    拜读强文

  4. jackyduys 于 2015-09-02 17:33:57发表:

    非常攒, 感谢分享.学习一下.

  5. nx61201 于 2015-09-02 14:38:56发表:

    非常攒, 感谢分享.学习一下.