kickstart+PXE+dhcp+nfs 批量無人值守自動化安裝作業系統

民工哥發表於2020-11-03

實際生產環境中肯定會有一次性安裝多臺伺服器作業系統的工作,因此,光靠傳統的一臺臺安裝,很費時費力,數量不多的情況,還是比較好應付,如果一次是幾十、幾百臺,就比較麻煩了,因此,實際生產環境中都用採用批量無人值守的安裝方式來安裝作業系統,所以今天來詳細介紹下,通過利用kickstart+PXE+dhcp+nfs組合的方式批量安裝作業系統。

1、原理與過程介紹

原理:

利用PXE協議使用伺服器通過網路方式啟動,後向DHCP伺服器請求IP,連線後將服務端的檔案下載到本地,執行一系統列的操作

過程:

第一步:PXE-client傳送請求

將支援PXE啟動方式的伺服器設定成PXE啟動,PEX客戶端通過PXE boot ROM以udp的形式傳送一個廣播包,請求DHCP伺服器分配IP地址

第二步:DHCP應答請求並回應

DHCP伺服器收到請求後,驗證是否來自合法的PXE客戶端請求,驗證通過後,回應PXE客戶端,回應資訊中包括分配的IP地址,pxelinux啟動程式(TFTP)的位置,以及配置檔案的位置

第三步:PXE-client請求下載啟動檔案

PXE客戶端收到回應後,向TFTP伺服器請求下載所需的啟動系統安裝檔案(檔案包括:pxelinux.0、pxelinux.cfg/default、vmlinuz、initrd.img等檔案)

第四步:TFTP伺服器響應請求並傳送檔案

當TFTP伺服器收到請求後,伺服器會響應請求並應答請求,之後傳送所需的檔案給客戶端

第五步:PXE-client請求下載自動應答檔案

PXE客戶端通過pxelinux.cfg/default檔案成引導linux安裝後,安裝程式必須先確定通過什麼方式安裝系統,如果是通過網路,則會在此時進行初始化網路,並定位安裝系統所需要的二進位制包以及配置檔案的位置,接著讀取檔案中指定的自動應答檔案ks.cfg,然後根據檔案位置請求下載檔案

第六步:客戶端安裝系統

將ks.cfg下載到本地,通過檔案找到安裝系統的ISO檔案位置,並請求下載所需的軟體包,正常連線後,開始傳輸軟體包,最終開始安裝作業系統,安裝完成後重新啟動

2、配置NFS及共享目錄


建立NFS共享目錄

[root@kickserver ~]# mkdir /data/sys -p

建立系統ISO檔案掛載目錄

[root@kickserver ~]# mkdir /isodir

將光碟掛載到掛載目錄

[root@kickserver ~]# mount /dev/cdrom /isodir
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@kickserver ~]# ls /isodir/
CentOS_BuildTag  GPL Packages RPM-GPG-KEY-CentOS-6
RPM-GPG-KEY-CentOS-Testing-6  EFI   images    RELEASE-NOTES-en-US.html
RPM-GPG-KEY-CentOS-Debug-6   TRANS.TBL EULA  isolinux
 repodata   RPM-GPG-KEY-CentOS-Security-6

檢查NFS是否安裝

[root@kickserver ~]# rpm -qa |grep nfs
nfs-utils-lib-1.1.5-11.el6.x86_64
nfs-utils-1.2.3-70.el6_8.2.x86_64
nfs4-acl-tools-0.3.3-8.el6.x86_64
nfs-utils-lib-devel-1.1.5-11.el6.x86_64
[root@kickserver ~]# rpm -qa |grep rpcbind
rpcbind-0.2.0-12.el6.x86_64

配置nfs

[root@kickserver ~]# echo "/data/sys 172.16.1.235/24(ro,sync)" >>/etc/exports
[root@kickserver ~]# echo "/isodir 172.16.1.235/24(ro,sync)" >>/etc/exports
[root@kickserver ~]# tail -2 /etc/exports 
/data/sys 172.16.1.235/24(ro,sync)
/isodir 172.16.1.235/24(ro,sync)

啟動服務(注意先後順序)

[root@kickserver ~]# /etc/init.d/rpcbind start
Starting rpcbind:                      [  OK  ]
[root@kickserver ~]# /etc/init.d/nfs start
Starting NFS services:             [  OK  ]
Starting NFS quotas:               [  OK  ]
Starting NFS mountd:              [  OK  ]
Starting NFS daemon:              [  OK  ]
Starting RPC idmapd:                [  OK  ]
[root@kickserver ~]# chkconfig rpcbind on
[root@kickserver ~]# chkconfig nfs on
[root@kickserver ~]# chkconfig --list |egrep "nfs|rcpbind"
nfs  0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on6:off

檢查配置

[root@kickserver ~]# showmount -e
clnt_create: RPC: Port mapper failure - Timed out

出現這個錯誤提示,首先檢查防火牆是否關閉

[root@kickserver ~]# /etc/init.d/iptables status
iptables: Firewall is not running.

也可能是本地hosts解析的問題,修改下hosts檔案

127.0.0.1 kickserver------>增加這個配置

[root@kickserver ~]# showmount -e
Export list for kickserver:
/isodir   172.16.1.235/24
/data/sys 172.16.1.235/24

3、安裝配置TFTP-server


[root@kickserver ~]# yum install tftp-server* -y

------------------------具體過程省略

配置TFTP伺服器

[root@kickserver ~]# vi /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       = yes------>修改成no
        per_source     = 11
        cps                 = 100 2
        flags                = IPv4
}

啟動服務

[root@kickserver ~]# /etc/init.d/xinetd start
Starting xinetd:                     [  OK  ]
[root@kickserver ~]# chkconfig xinetd on
[root@kickserver ~]# chkconfig --list|grep xinetd
xinetd 0:off1:off 2:on  3:on 4:on  5:on  6:off

4、配置PXE引導(bootstarp)

[root@kickserver ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/  
[root@kickserver ~]# ll /var/lib/tftpboot/
-rw-r--r--. 1 root root 26759 Dec 13 16:07 /var/lib/tftpboot/pxelinux.0

拷貝網路核心啟動檔案

[root@kickserver ~]# cp /isodir/images/pxeboot/initrd.img /var/lib/tftpboot/
[root@kickserver ~]# cp /isodir/images/pxeboot/vmlinuz /var/lib/tftpboot/
[root@kickserver ~]# ll /var/lib/tftpboot/
-r-xr-xr-x. 1 root root 4128368 Dec 13 16:12 vmlinuz
-r--r--r--. 1 root root 33383679 Dec 13 16:11 initrd.img
-rw-r--r--. 1 root root 26759 Dec 13 16:07 pxelinux.0

配置引導檔案

[root@kickserver ~]# cd /var/lib/tftpboot/
[root@kickserver tftpboot]# mkdir pxelinux.cfg
[root@kickserver tftpboot]# cp /isodir/isolinux/isolinux.cfg ./pxelinux.cfg/default
[root@kickserver tftpboot]# vi ./pxelinux.cfg/default
menu label ^Install or upgrade an existing system
default test
#prompt 1
timeout 600
display boot.msg
menu background splash.jpg
menu title Welcome to CentOS 6.5!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
label test
  kernel vmlinuz
  append ks=nfs:172.16.1.235:/data/sys/kickstart/ks.cfg initrd=initrd.img test
#增加此行配置檔案

5、安裝配置DHCP

[root@kickserver tftpboot]# yum install dhcp* -y

配置DHCP伺服器

[root@kickserver tftpboot]# cd /etc/dhcp/
[root@kickserver dhcp]# vi dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see 'man 5 dhcpd.conf'
#
ddns-update-style none;
ignore client-updates;
allow booting;
allow bootp;
default-lease-time 21600;
max-lease-time 43200;
option routers 172.16.1.1; 
subnet 172.16.1.0 netmask 255.255.255.0 { 
range dynamic-bootp 172.16.1.100 172.16.1.120;
next-server 172.16.1.235; 
filename "/data/sys/kickstart/ks.cfg";
next-server 172.16.1.235;
filename "/var/lib/tftpboot/pxelinux.0";
}
增加上述標記部分的配置內容

啟動服務

[root@centos6 dhcp]# /etc/init.d/dhcpd start
Starting dhcpd:                            [  OK  ]
[root@centos6 dhcp]# chkconfig dhcpd on
[root@centos6 dhcp]# chkconfig --list|grep dhcpd
dhcpd0:off 1:off 2:on 3:on 4:on5:on 6:off

6、安裝配置kickstart

[root@centos6 dhcp]# mkdir /data/sys/kickstart -p
#建立檔案目錄
[root@centos6 dhcp]# cp /root/anaconda-ks.cfg /data/sys/kickstart/ks.cfg
#拷貝配置檔案
[root@centos6 dhcp]# ll /data/sys/kickstart/ks.cfg
-rw------- 1 root root 1229 Dec 13 18:16 /data/sys/kickstart/ks.cfg
#修改預設許可權,例其它使用者可讀
[root@centos6 dhcp]# chmod 644 /data/sys/kickstart/ks.cfg
[root@centos6 dhcp]# ll /data/sys/kickstart/ks.cfg       
-rw-r--r-- 1 root root 1229 Dec 13 18:16 /data/sys/kickstart/ks.cfg

配置ks.cfg檔案(重要步驟)

[root@centos6 dhcp]# cd /data/sys/kickstart/
[root@centos6 kickstart]# vi ks.cfg 
# Kickstart file automatically generated by anaconda.
#version=DEVEL
install
#cdrom  
#註釋預設的安裝方式,新增下面的配置
nfs --server=172.16.1.235 --dir=/isodir
lang en_US.UTF-8
keyboard us
network --onboot no --device eth0 --bootproto dhcp --noipv6
#rootpw  --iscrypted $6$zKfIpmK0g7MKWBVy$I8sk1Q8CAmkEA/zUwhNYC.A1DJOw6un2qbww2empzQx04DGjSvmLbZ2ESVMyOzU0DyT9qsz/IfNsD0Teim//N1
rootpw 123456
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone --utc 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
#clearpart --all --drives=sda

#part /boot --fstype=ext4 --size=500
#part pv.008002 --grow --size=1

#volgroup vg_centos6 --pesize=4096 pv.008002
#logvol / --fstype=ext4 --name=lv_root --vgname=vg_centos6 --grow --size=1024 --maxsize=51200
#logvol swap --name=lv_swap --vgname=vg_centos6 --grow --size=1984 --maxsize=1984
zerombr
#清除mbr引導
repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100
%packages  
#這個軟體包可自行定義增加
@base
@compat-libraries
@core
@debugging
@development
@server-policy
@workstation-policy
sgpio
device-mapper-persistent-data
systemtap-client
%end

7、配置PXE客戶端引導並啟動伺服器

重新啟動伺服器

可以清楚的看到載入之前配置的檔案

整個過程無需人工干預,全部自動化完成

image

相關文章