『1-14』Linux之无人值守安装脚本Kickstart

Linux之无人值守安装脚本Kickstart

通过模板生成 kickstart文件

在已经装好的rhel8中,/root/anaconda-ks.cfg就是安装当前系统时回答的所有问题的答案
生成的kickstart,此文件为kickstart模板
操作步骤:
1.共享资源:
1.生成ks文件并共享文件到httpd服务中
2.编写kickstart文件

一.kickstart自动安装脚本的作用

在企业中安装多台操作系统时面临的问题

当安装Linux操作系统时,安装过程会需要回答很多关于设定的问题
这些问题必须手动选择,否则无法进行安装
当只安装1台Linux系统,手动选择设定工作量比较轻松
当安装多台Linux,这些设定需要重复多次,这些重复动作是效率底下的操作

如何解决以上问题?

用文件来记录所有安装过程中问题的答案,并让所有需要安装的主机自动读取

kickstart作用

以上解决方案中记录系统安装过程中所有问题答案的文件叫kickstart脚本

二.实验环境

1.主机名称:node26
2.ip:172.25.254.26
3.火墙,selinux关闭
4.httpd服务开启
5.配置软件仓库能正常工作

三、kickstart自动安装脚本的制作

在rhel7系统中提供图形的kickstart制作方式
在rhel8中已经把图形的工具取消,并添加到rhn网络中
在rhel8中如果无法通过rhn网络制作kickstart,可以使用模板生成

通过模板生成kickstart文件

在已经装好的rhel8中,/root/anaconda-ks.cfg 就是安装当前系统时回答的所有问题的答案
生成的kickstart,此文件为kickstart模板
操作步骤:

1.先按想要的系统手动设定一遍,进系统得/root/anaconda-ks.cfg模板文件。

2.共享资源:##搭建dhcpd服务器##

让被安装的主机可以获得ip来访问网络资源及kickstart文件
dnf install httpd -y
systemctl enable --now httpd
systemctl disable --now firewalld
setenforce 0 ##selinux调整为警告模式 !很重要,否则有时子目录无法访问。
mkdir /var/www/html/westos
df 查看磁盘设备名称在这里插入图片描述vmware虚拟机的硬件光驱为/dev/sr{1..10}

umount /dev/sr1卸载设备
mount /dev/sr1 /var/www/html/westos 挂载设备到指定位置,/var/www/html为httpd的共享目录。临时挂载。
永久挂载

echo umount /dev/sr1 >>/etc/rc.d/rc.local
echo mount /dev/sr1 /var/www/html/westos >>/etc/rc.d/rc.local
chmod a+x  /etc/rc.d/rc.local  ##给开机自启文件执行权限。

测试资源共享:
[root@node26 ks]# firefox 172.25.254.26/rhel8.2  ##能正常打开

或者

[root@node26 ks]# curl 172.25.254.26/rhel8.2
[root@node26 ks]# curl 172.25.254.26/rhel8.2
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://172.25.254.26/rhel8.2/">here</a>.</p>
</body></html>
[root@node26 ks]# 
共享资源的意义

在安装操作系统时,每个被安装的操作系统都要有安装资源
如果使用镜像安装,每个安装的系统都需要加载一个镜像
这样会浪费存储,通过网络来共享镜像中的资源,让安装系统的主机能通过
网络访问被共享的资源这样就可以解决多台主机需要多个镜像安装的问题.

3.生成xxx-ks.cfg文件并共享文件到httpd服务中

用模板自作kickstart

在已经装好的rhel8中,/root/anaconda-ks.cfg 就是安装当前系统时回答的所有问题的答案
生成的kickstart,此文件为kickstart模板

共享ks文件 ##只能用cp,不能用mv,mv无法访问
cp /root/anaconda-ks.cfg  /var/www/html/westos.cfg   
vim /var/www/html/westos.cfg
#version=RHEL8    ##只是用我们系统中的第一快硬盘
ignoredisk --only-use=sda
# Partition clearing information	##把sda硬盘的所有内容删掉
clearpart --all --initlabel
# Use graphical install
#graphical		##安装过程开启图形,开图形非常卡
##安装过程不开图形
text			
##安装资源
repo --name="AppStream" --baseurl=http://172.25.254.20/westos_8/AppStream
# Use netsource installation media 安装资源
url --url=http://172.25.254.20/westos_8
# Keyboard layouts ##键盘布局为美式键盘
keyboard --vckeymap=us --xlayouts='us'
# System language ##系统支持的语言
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8
##软件
##系统 
# Network information   ##网卡设定
network --bootproto=dhcp --device=ens160 --onboot=on --ipv6=auto --no-activate
##主机名设定
network --hostname=localhost.localdomain
# Root password
rootpw --plaintext westos
##系统默认开启的加密认证方式
authselect --enableshadow --passalgo=sha512
# X Window System configuration information
#xconfig --startxonboot	##安装完成后开机启动图形
##安装完成后开机不启动图形
skipx
# Run the Setup Agent on first boot##首次启动初始化禁止 就是进系统之前那个证书那个。
firstboot --disable
# System services ##在开机时开启或关闭的服务
services --disabled="chronyd,firewalld" --enabled="sshd"
# System timezone##系统时区,启用utc计时方式,不其同ntp时间同步
timezone Asia/Shanghai --isUtc --nontp
#user --name=westos --password=$6$W8HmY8FoDLmjcv65$Xo.SIxQ4rAWK19QBUvXP1et1us191K5w6GlIZvyEMaKx1FBK6EFTiwHC0X5DiktBfJzRsCy
--iscrypted --gecos="westos"
# Disk partitioning information
##让/分区使用全部空闲磁盘空间
part / --fstype="xfs" --grow --size=1
##/boot分区大小为500M
part /boot --fstype="xfs" --size=500
##swap分区大小为500M
part swap --fstype="swap" --size=500
#reboot
%packages
##安装软件组base
@base
##安装单个软件httpd
httpd
%end
##系统安装前自动执行的脚本
#%pre
#%end
##系统安装后自动执行的脚本
%post
cat >> /etc/yum.repos.d/westos.repo <<EOF
[AppStream]
name=AppStream
baseurl=http://172.25.254.20/westos_8/AppStream
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=http://172.25.254.20/westos_8/BaseOS
gpgcheck=0
EOF
%end

4.测试kickstart文件内容

##检测westos.cfg语法
ksvalidator /var/www/html/westos.cfg

5.设定kickstart文件权限:

该文件默认除root无访问权限,加可读权限
chmod 644 /var/www/html/westos.cfg

6.测试效果:系统会自动安装完毕

当出现此问题是:!代表kickstart文件内容设定有错误

测试

在系统安装基面选择
Install Red Hat Enterprise Linux 8.0.0
ks=http://172.25.254.20/westos.cfg
查看效果

7.编辑脚本一键安装

vim westos.sh

  1 #!/bin/bash  #sh头文件
  2 virt-install \	 ##virt-install 虚拟机安装命令  #\表示实际一行,显示换行
  3 --name $1 \	#name是虚拟机名称  $1为命令行文件名后第一个参数
  4 --vcpus 1 \		#\前有一个空格,\后无任何字符,否则报错 cpu数量
  5 --memory 2048 \	#内存
  6 --location http://172.25.254.26/rhel8.2 \ 	#安装镜像位置
  7 --disk /var/lib/libvirt/images/$1.qcow2,size=20,bus=virtio \	磁盘位置,磁盘文件,大小,
  8 --network bridge=br0,model=virtio \	#网络类型,桥接时,网络安装要求网络里有dhcp服务器,否则安装一半无法连接
  9 --extra-args "ks=http://172.25.254.26/ks/graphical-ricky-ks.cfg" &         #ks文件位置,&为后台运行。                                                              

分享一下我的ks文件

1 #version=RHEL8
2 ignoredisk --only-use=vda
3 # Partition clearing information
4 clearpart --none --initlabel
5 # Use graphical install
6 #graphical
7 text
8 # Keyboard layouts
9 keyboard --vckeymap=us --xlayouts='us'
10 # System language
11 lang en_US.UTF-8 --addsupport=zh_CN.UTF-8
12 
13 # Network information
14 network  --bootproto=dhcp --device=enp1s0 --ipv6=auto --activate
15 network  --bootproto=dhcp --device=enp2s0 --ipv6=auto --activate
16 network  --hostname=localhost.localdomain
17 repo --name="AppStream" --baseurl=http://172.25.254.26/rhel8.2/AppStream
18 # Use network installation
19 url --url="http://172.25.254.26/rhel8.2"
20 # Root password
21 rootpw --iscrypted $6$5uiLrMcLSvX7qc.G$2LRudsMjW.ol/8yHKlyPR.FGaQ.hZMh4rojk4    7fTttLCc38KtuLb.keKBnEU.RIlJfnAcCMVKQCZXA/SkrHGf0
22 # X Window System configuration information
23 xconfig  --startxonboot
24 # Run the Setup Agent on first boot
25 #firstboot --enable
26 firstboot --disable
27 # System services
28 services --enabled="chronyd"
29 # System timezone
30 timezone Asia/Shanghai --isUtc
31 user --name=ricky --password=$6$46V8BTdEt4abvPGt$4cQlrMG.tjA6iSv0.T/9IA/p9fW    KwKBgWIIhlbydpKHQ/eAMG2bQyEpI5P.QF7r6MgjlrVjdxSCKuNdj8k40O. --iscrypted --ge    cos="Ricky"
32 # Disk partitioning information
33 #part / --fstype="xfs" --ondisk=vda --size=18431
34 part / --fstype="xfs" --ondisk=vda --grow --size=1
35 part swap --fstype="swap" --ondisk=vda --size=1024
36 part /boot --fstype="xfs" --ondisk=vda --size=1024
37 
38 %packages
39 @^graphical-server-environment
40 kexec-tools
41 
42 %end
43 
44 %addon com_redhat_kdump --enable --reserve-mb='auto'
45 
46 %end
47 
48 %anaconda
49 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
50 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
51 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
52 %end

[root@node26 ks]# sh linux-setup-kickstart.sh perfect

在这里插入图片描述

sh westos ricky ricky为虚拟机名传给$1
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里加了个enter

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述执行脚本后,加enter,然后就做到了这个界面。

猜你喜欢

转载自blog.csdn.net/qq_39679699/article/details/113250574