1. 程式人生 > 其它 >CentOS KickStart 無人值守安裝及自動部署ks指令碼

CentOS KickStart 無人值守安裝及自動部署ks指令碼

介紹

kickstart - 通過網路 PXE 的方式來進行自動化安裝 Redhat/Centos 作業系統的一種流程方式,主要通過 NFS 、FTP、HTTP 來執行,需要執行 kickstart,流程如下:


自動部署Kickstart 指令碼使用說明:

本指令碼僅支援 Centos7 KickStart 自動部署!

使用步驟如下:

git clone https://gitee.com/hukey/kick-start.git
cd kick-start
mkdir iso

@@@ 請將您的 iso 映象檔案拷貝到該目錄下 @@@
cp -a CentOS-7-x86_64-Minimal-2003.iso ./iso/ 

sh install.sh

出現如下配置資訊,則表示部署成功。

 == KickStart 配置完成!==


下文描述構建 KickStart 整體過程:


構建 DHCP服務

首先配置 DHCP 服務:

[[email protected] ~]#yum install dhcp -y
[[email protected] ~]#vim /etc/dhcp/dhcpd.conf
subnet 10.0.0.0 netmask 255.255.255.0 {
  range 10.0.0.10 10.0.0.100;
  option domain-name-servers 10.0.0.2;
  option routers 10.0.0.2;
  default-lease-time 600;
  max-lease-time 7200;
  next-server 10.0.0.128;        # #TFTP Server 的IP地址,也就是本機
  filename "pxelinux.0";             # pxelinux 啟動檔案位置;
}

@@@ 配置檔案說明 @@@
subnet 10.0.0.0 netmask 255.255.255.0 { -> 整個網段
range 10.0.0.10 10.0.0.100; DHCP分配IP範圍
option domain-name-servers 10.0.0.2; 這裡填寫閘道器的地址
option routers 10.0.0.2; 路由的地址,閘道器即路由
default-lease-time 600; dhcp預設租約時間(單位:秒)
max-lease-time 7200; dhcp預設最大租約時間(單位:秒)
next-server 10.0.0.128; 這裡配置為本機的IP地址
filename "pxelinux.0"; 預設寫法

@@@@@@@@@@@@@@@@@

啟動服務:
[[email protected] ~]#systemctl start dhcpd; systemctl enable dhcpd

PS:檢視 dhcp 日誌檔案為:/var/log/messages


配置TFTP服務

[[email protected] ~]#yum install tftp-server -y

配置 PXE 啟動引導
syslinux 註解:

  • syslinux是一個功能強大的引導載入程式,並且相容各種介質
  • syslinux是一個小型的linux作業系統,目的是簡化linux首次安裝的時間,並建立維護或其他特殊用途的啟動盤
  • 如果沒有找到pxelinux.0這個檔案,可以安裝下syslinux
[[email protected] ~]#yum install syslinux -y

[[email protected] ~]#mkdir -pv /mnt/centos/
[[email protected] ~]#mount /dev/cdrom /mnt/centos/	# 掛載 cdrom ,也可直接掛載 iso 檔案

[[email protected] ~]#cd /var/lib/tftpboot/
[[email protected] /var/lib/tftpboot]#cp -a /mnt/centos/isolinux/boot.msg ./
[[email protected] /var/lib/tftpboot]#cp -a /mnt/centos/images/pxeboot/{initrd.img,vmlinuz} ./
[[email protected] /var/lib/tftpboot]#cp -a /usr/share/syslinux/pxelinux.0 ./
[[email protected] /var/lib/tftpboot]#ll -tsh 
total 60M
4.0K -rw-r--r-- 1 root root   84 Apr 21  2020 boot.msg
 53M -rw-r--r-- 1 root root  53M Apr 21  2020 initrd.img
6.5M -rwxr-xr-x 1 root root 6.5M Apr  1  2020 vmlinuz
 28K -rw-r--r-- 1 root root  27K Oct 31  2018 pxelinux.0
 
[[email protected] /var/lib/tftpboot]#mkdir pxelinux.cfg
[[email protected] /var/lib/tftpboot]#cd pxelinux.cfg/
[[email protected] /var/lib/tftpboot/pxelinux.cfg]#cp -a /mnt/centos/isolinux/isolinux.cfg ./default
[[email protected] ~]#umount /mnt/centos/	

新增 default 檔案內容,注意只是添加了 ks 部分的內容:

# 開頭部分修改:
default ks
prompt 1
timeout 6

...
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.105/ks.cfg
...

default 全文:

default ks
prompt 1
timeout 6
display boot.msg
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
menu helpmsgrow 15
menu tabmsgrow 13
menu color border * #00000000 #00000000 none
menu color sel 0 #ffffffff #00000000 none
menu color title 0 #ff7ba3d0 #00000000 none
menu color tabmsg 0 #ff3a6496 #00000000 none
menu color unsel 0 #84b8ffff #00000000 none
menu color hotsel 0 #84b8ffff #00000000 none
menu color hotkey 0 #ffffffff #00000000 none
menu color help 0 #ffffffff #00000000 none
menu color scrollbar 0 #ffffffff #ff355594 none
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
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
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
label check
  menu label Test this ^media & install CentOS 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
menu separator # insert an empty line
menu begin ^Troubleshooting
  menu title Troubleshooting
label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.105/ks.cfg
label rescue
  menu indent count 5
  menu label ^Rescue a CentOS 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=CentOS\x207\x20x86_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

注意:修改完成,啟動 tftp服務。

[[email protected] ~]#systemctl start tftp; systemctl enable tftp

基於HTTP協議的PXE環境

[[email protected] ~]#yum install httpd -y
[[email protected] ~]#mkdir -pv /var/www/html/centos/
[[email protected] ~]#mount /dev/cdrom /var/www/html/centos/
[[email protected] ~]#systemctl start httpd ;systemctl enable httpd

通過瀏覽器訪問,出現如下頁面表示配置成功。


接下來,需要配置 ks.cfg 檔案:

[[email protected] ~]#yum install system-config-kickstart -y
[[email protected] ~]#system-config-kickstart

通過 system-config-kickstart 命令可實現自定義配置。


這裡直接提供一個 最小化安裝的centos7.X 的通用檔案:

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="http://10.0.0.105/centos" ##### 這裡需要手動修改為自己主機的http連結
# Root password
rootpw --iscrypted $1$x1wkiXJv$45UMW./5aUCEkfymzt4WQ/   #### 預設root密碼為:123456
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
text
# 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
# Reboot after installation
reboot
# System timezone
timezone --utc Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
#bootloader --location=gpt
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel 
# Disk partitioning information
part biosboot --fstype=biosboot --size=1
part /boot/efi --fstype="efi" --ondisk=sda --size=1024 --fsoptions="defaults,uid=0,gid=0,umask=0077,shortname=winnt"
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=1000
part pv.01 --size=1 --grow
volgroup vg00 pv.01
logvol / --vgname=vg00 --size=8192 --name=lv_root

%pre
parted -s /dev/sda mklabel gpt
%end

%packages 
openssh-clients
@core
%end

ks.cfg 檔案放置到 http 的根目錄下:

[[email protected] ~]#cp -a ks.cfg /var/www/html/

最後,檢查下服務是否啟動正常,有以下三個服務:

[[email protected] ~]#systemctl status tftp httpd dhcpd

測試


這裡建立一臺虛擬機器進行測試:


故意建立了一臺硬碟大於 2T 的主機,檢測 ks.cfg 檔案是否能夠分割槽成功。使用 PXE 啟動安裝:


獲取 IP 地址成功,並通過 tftp 載入 檔案


安裝完成後,需要對分割槽進行擴容。

[root@localhost ~]# lsblk 
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                8:0    0    3T  0 disk 
├─sda1             8:1    0    1M  0 part 
├─sda2             8:2    0  500M  0 part /boot
├─sda3             8:3    0    1G  0 part /boot/efi
├─sda4             8:4    0 1000M  0 part [SWAP]
└─sda5             8:5    0  2.9T  0 part 
  └─vg00-lv_root 253:0    0    8G  0 lvm  /
sr0               11:0    1 1024M  0 rom

[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree 
  vg00   1   1   0 wz--n- <2.93t <2.92t
  
[root@localhost ~]# lvextend -l 100%VG /dev/vg00/lv_root
[root@localhost ~]# xfs_growfs /
[root@localhost ~]# df -Th 
Filesystem               Type      Size  Used Avail Use% Mounted on
devtmpfs                 devtmpfs  898M     0  898M   0% /dev
tmpfs                    tmpfs     910M     0  910M   0% /dev/shm
tmpfs                    tmpfs     910M  9.6M  901M   2% /run
tmpfs                    tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/vg00-lv_root xfs       3.0T  1.3G  3.0T   1% /
/dev/sda2                xfs       497M  143M  354M  29% /boot
/dev/sda3                vfat     1022M   12K 1022M   1% /boot/efi
tmpfs                    tmpfs     182M     0  182M   0% /run/user/0



問題總彙


問題1 TFTP cannot open connection


出現該問題請檢查 dhcp 配置檔案是否配置正確:

[[email protected] ~]#cat /etc/dhcp/dhcpd.conf 
...
  next-server 10.0.0.15;        # #TFTP Server 的IP地址,也就是本機IP
...

修改完畢記得重啟服務:
[[email protected] ~]#systemctl restart dhcpd