无人值守安装Linux系统

一、需求分析

随着互联网技术的不断壮大,服务器的数量也在不断的增加,从初期的几台服务器发展到庞大的数据中心,单靠人工已经无法满足在技术、业务、管理等方面的要求,那么标准化、自动化、架构优化、过程优化等降低IT服务成本的因素越来越被人们所重视。而如何自动化批量部署安装一个稳定的系统是实现自动化的第一步。

二、PXE简介

PXE,就是预启动执行环境,是一种引导启动的方式。这种协议一般由两部分构成,一部分是服务器端,一个是客户端。简单来说,我们通过这种方式可以自己创建一个“安装源”,在安装系统的时候只要能找到这个“源”便可以实现系统的安装。在实现无人值守的安装前,我们必须要搭建一些服务,来实现“安装源”的建立,例如ftp、http、tftp、dhcp等。当一台主机启动时,标准输入输出会将PXE客户端调入我们的内存中进行相关的操作,并提示相关的选项,在这里我们可以进行选择。PXE的客户端通过网络下载(download)启动文件到本地运行。具体过程是,PXE客户端通过网卡向局域网内发送ip请求,然后DHCP服务器会提供给给它一个ip地址和系统安装所需要的文件,接下使用接收到的文件进行系统安装。而安装的过程又需要其他服务器提供的资源,例如:yum源,内核文件等,当主机拿到这些资源,便可以顺利的安装了。最终结果是:任意一台主机在选着网络启动时会获取DHCP服务器分发的ip,通过通过获取到的ip地址与局域网内的TFTP服务器通信并获取启动文件,与FTP或者HTTP通信并获取yum源文件及内核文件等。之后开始自动安装,而这个过程不需要人在做任何操作。

PXE安装优点,这种安装系统的方式可以不受光驱,光盘以及一些外部设备的限制,还可以做到无人值守,大大减轻了运维人员的工作负荷,像在一些主机数量庞大的机房进行批量安装,PXE将是你不二的选择。

三、服务器搭建

1.安装前准备
由于涉及到主机间不同基于不同协议的通信,所以为了避免不必要的麻烦,我们选择关闭防火墙以及selinux。
无人值守安装Linux系统
2.配置静态IP地址

无人值守安装Linux系统
修改完配置后记得重启网卡使配置生效
3.安装服务
[root@db ~]# yum -y install dhcp tftp-server httpd syslimux xinetd
启动服务并设为开机自启

[root@db ~]# systemctl enable dhcpd
[root@db ~]# systemctl enable tftp
[root@db ~]# systemctl enable httpd
[root@db ~]# systemctl start httpd
[root@db ~]# systemctl start tftp
[root@db ~]# vim  /etc/xinetd.d/tftp 
{
...
    disable     =yes         # 修改这一项为"disable     =no"
...
} 
[root@db ~]# systemctl restart xinetd

4.配置DHCP服务

[root@db ~]# cd /etc/dhcp/
[root@db dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example dhcpd.conf          #复制板配置文件 
[root@db ~]# vim dhcpd.conf           # 打开模板文件并添加一下内容 
subnet  192.168.92.0 netmask 255.255.255.0{                  #这里写的ip地址为子网的地址
    range 192.168.92.200 192.168.92.224;            #子网的范围,之后主机请求获取的ip地址就是这其中的一个
    next-server 192.168.92.33;         # 指明tftp服务器的地址  
    option routers 192.168.92.33;          #网关地址也就是服务器地址
    filename "pxelinux.0";           # 指明PXE文件位置,这个在申请ip的时候会发送给安装主机 
}
[root@db dhcp]# systemctl restart dhcpd          #重启服务

5.准备yum源文件以及kickstart文件

[root@db dhcp]# cd /var/www/html/
[root@db html]# mkdir -p centos/{6,7}
[root@db html]# mkdir ksdir
关于Yum源文件,我们可以从光盘中复制,也可以直接把光盘挂载到指定目录,这里我选择把光盘挂载到指定目录
[root@db dhcp]# mount /dev/sr0 centos/6     # 挂载CentOS 6光盘值6目录下 
[root@db dhcp]# mount /dev/sr1 centos/7     # 挂载CentOS 7光盘值7目录下 
[root@db dhcp]# yum  install -y system-config-kickstart 
[root@db dhcp]# system-config-kickstart

无人值守安装Linux系统
可以按照顺序自定义配置,之后保存到/var/www/html/ksdir/目录下
或者复制/root目录下的anaconda-ks.cfg文件进行修改

[root@db ~]# cp anaconda-ks.cfg /var/www/html/ksdir/ks7.cfg
[root@db ~]# vim /var/www/html/ksdir/ks7.cfg
[root@db ~]# chmod +r /var/www/html/ksdir/ks7.cfg
[root@db ~]# cat /var/www/html/ksdir/ks7.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url="http://192.168.92.33/centos/7"       #指明yum源路径
# Use graphical install
text          # 将cdrom修改为text,我们不是基于光盘安装的,我们是基于字符界面安装
# Run the Setup Agent on first boot
firstboot --enable
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 --ipv6=auto --activate
network  --hostname=centos7.cyz

# Root password
rootpw --iscrypted $6$cvwgC8f2Z/MY2UC9$MhmVS1.BVZmfMZAnM9LeYhW19587XIJ9d4cE9mG0GLAt4ETX/HNjBxUoOkhMRLcH2gY2mOT361w0CrFaangje0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /app --fstype="xfs" --ondisk=sda --size=19073
part swap --fstype="swap" --ondisk=sda --size=4096
part /boot --fstype="xfs" --ondisk=sda --size=190
part / --fstype="xfs" --ondisk=sda --size=19073

%packages           #安装软件包,我这个是mini安装所以安装包比较少,可以自定义安装。
@^minimal
@core
vim
%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

centos6的kickstart文件

#version=DEVEL
install
text             # 基于字符界面安装
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw  --iscrypted $6$hfb25YOYZDU3YZTl$VxTkHGGJGGBbr59OPnY5kTJzvJ9hb9NRwrh5FMHLIAlXh9VQ74PYoK7QzPWYN0zaJrm3mv/IP0fDkHxFglNi6/
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
url --url="http://192.168.92.33/centos/6"      # yum源的路径 
selinux --disabled     # 关闭selinux
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
zerombr             # 清除mbr
clearpart --all     # 清除分区信息
reboot              # 安装完成后重启
part /boot --fstype=ext4 --size=1000         # 分区信息
part / --fstype=ext4 --size=50000
part /app --fstype=ext4 --size=40000
part swap --size=2048

#repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100         # 注释掉此行

%packages
@base
@core
@debugging
@basic-desktop
@desktop-debugging
@desktop-platform
@directory-client
@fonts
@general-desktop
@graphical-admin-tools
@input-methods
@internet-applications
@internet-browser
@java-platform
@kde-desktop
@legacy-x
@network-file-system-client
@office-suite
@print-client
@remote-desktop-clients
@server-platform
@server-policy
@workstation-policy
@x11
mtools
pax
python-dmidecode
oddjob
wodim
sgpio
genisoimage
device-mapper-persistent-data
abrt-gui
qt-mysql
samba-winbind
certmonger
pam_krb5
krb5-workstation
xterm
xorg-x11-xdm
libXmu
rdesktop
%end

%post         # 安装后脚本,安装完系统后向做什么配置,都可以写在%post...%end中间
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end

6.准备内核文件、菜单文件

[root@db ~]# cd /var/lib/tftpboot/
[root@db tftpboot]# mkdir -p centos{6,7}     # 创建目录文件 
[root@db tftpboot]# cp /var/www/html/centos/6/{vmlinuz,initrd.img} centos6/     # 复制内核,虚拟根文件 
[root@db tftpboot]# cp /var/www/html/centos/7/{vmlinuz,initrd.img} centos7/     # 由于之前挂载了不同系统,直接复制对应文件即可 
[root@db tftpboot]# cp /usr/share/syslinux/pxelinux.0 . # pxelinux文件来自syslinux包,直接复制即可 
[root@db tftpboot]# cp /usr/share/syslinux/vesamenu.c32 .         #复制菜单文件
[root@db tftpboot]# mkdir pxelinux.cfg 
[root@db tftpboot]# cp /var/www/html/centos/6/isolinux/isolinux.cfg pxelinux.cfg/default # 复制并改名 
[root@db tftpboot]# vim pxelinux.cfg/default 
[root@db tftpboot]# cat 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 CentOS 7
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 linux
  menu label ^Install CentOS 7
    menu default      #光标默认在这一行
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://192.168.92.33/ksdir/ks7.cfg

label centos6
  menu label Auto Install CentOS Linux ^6
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks=http://192.168.92.33/ksdir/ks6.cfg 
        label linux          
      menu label Boot from ^local drive
      localboot 0xffff
menu end

四、调试并安装
新创建一台虚拟机,设置网络段在上述DHCP服务所在的网络段,选择使用物理驱动器自动检测。
无人值守安装Linux系统
无人值守安装Linux系统
系统已经装好了。

猜你喜欢

转载自blog.51cto.com/13438667/2118282