CentOS KickStart 无人值守安装及自动部署ks脚本

介绍

kickstart - 通过网络 PXE 的方式来进行自动化安装 Redhat/Centos 操作系统的一种流程方式,主要通过 NFS 、FTP、HTTP 来执行,需要执行 kickstart,流程如下:

Snipaste_2021-06-21_11-28-55


自动部署Kickstart 脚本使用说明:

本脚本仅支持 Centos7 KickStart 自动部署,注意以下 必要条件:

  • 自动部署的主机必须连接至公网网络
  • 目前仅支持 CentOS 7 操作系统自动部署

使用步骤如下:

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

@@@ 请将您的 iso 镜像文件拷贝到该目录下 @@@
wget https://mirrors.aliyun.com/centos-vault/7.8.2003/isos/x86_64/CentOS-7-x86_64-Minimal-2003.iso
cp -a CentOS-7-x86_64-Minimal-2003.iso ./iso/ 

sh install.sh

出现如下配置信息,则表示部署成功。

 == KickStart 配置完成!==


下文描述构建 KickStart 整体过程:


构建 DHCP服务

首先配置 DHCP 服务:

[root@10.0.0.15 ~]#yum install dhcp -y
[root@10.0.0.15 ~]#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"; 默认写法

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

启动服务:
[root@10.0.0.15 ~]#systemctl start dhcpd; systemctl enable dhcpd

PS:查看 dhcp 日志文件为:/var/log/messages


配置TFTP服务

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

配置 PXE 启动引导
syslinux 注解:

  • syslinux是一个功能强大的引导加载程序,并且兼容各种介质
  • syslinux是一个小型的linux操作系统,目的是简化linux首次安装的时间,并建立维护或其他特殊用途的启动盘
  • 如果没有找到pxelinux.0这个文件,可以安装下syslinux
[root@10.0.0.15 ~]#yum install syslinux -y

[root@10.0.0.15 ~]#mkdir -pv /mnt/centos/
[root@10.0.0.15 ~]#mount /dev/cdrom /mnt/centos/	# 挂载 cdrom ,也可直接挂载 iso 文件

[root@10.0.0.15 ~]#cd /var/lib/tftpboot/
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /mnt/centos/isolinux/boot.msg ./
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /mnt/centos/images/pxeboot/{initrd.img,vmlinuz} ./
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /usr/share/syslinux/pxelinux.0 ./
[root@10.0.0.15 /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
 
[root@10.0.0.15 /var/lib/tftpboot]#mkdir pxelinux.cfg
[root@10.0.0.15 /var/lib/tftpboot]#cd pxelinux.cfg/
[root@10.0.0.15 /var/lib/tftpboot/pxelinux.cfg]#cp -a /mnt/centos/isolinux/isolinux.cfg ./default
[root@10.0.0.15 ~]#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=CentOSx207x20x86_64 quiet
label check
  menu label Test this ^media & install CentOS 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOSx207x20x86_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=CentOSx207x20x86_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=CentOSx207x20x86_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服务。

[root@10.0.0.15 ~]#systemctl start tftp; systemctl enable tftp

基于HTTP协议的PXE环境

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

通过浏览器访问,出现如下页面表示配置成功。


接下来,需要配置 ks.cfg 文件:

[root@10.0.0.15 ~]#yum install system-config-kickstart -y
[root@10.0.0.15 ~]#system-config-kickstart

image-20210621152101316


通过 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 的根目录下:

[root@10.0.0.15 ~]#cp -a ks.cfg /var/www/html/

最后,检查下服务是否启动正常,有以下三个服务:

[root@10.0.0.15 ~]#systemctl status tftp httpd dhcpd

测试


这里创建一台虚拟机进行测试:

image-20210621153841829


故意创建了一台硬盘大于 2T 的主机,检测 ks.cfg 文件是否能够分区成功。使用 PXE 启动安装:

image-20210621154002391


获取 IP 地址成功,并通过 tftp 加载 文件

image-20210621161351828


安装完成后,需要对分区进行扩容。

[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

image-20210621155336802


出现该问题请检查 dhcp 配置文件是否配置正确:

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

修改完毕记得重启服务:
[root@10.0.0.15 ~]#systemctl restart dhcpd
原文地址:https://www.cnblogs.com/hukey/p/14919346.html