日期:2021.12.14
前言:
目前实验进度已经实现了环境内新虚拟机通过网络启动后:
1.通过DHCP服务器获取IP地址、子网掩码、网管、DNS等网络参数
2.通过DHCP服务器获取TFTP服务器IP地址、pxelinux.0文件路径
3.访问TFTP服务器获取启动菜单文件,在菜单文件选择安装操作系统后,获取vmliunz和initrd.img文件
4.访问HTTP服务器获取安装操作系统的相关文件及系统仓库,实现手动安装
本次实验会将第4部变成,访问HTTP服务器获取kickstart文件及系统仓库,实现自动安装
参照:
- 马哥教育王晓春老师课程
- Kickstart Generator (需红帽账号登陆)
https://access.redhat.com/labs/kickstartconfig/ - 27.3. Kickstart 语法参考
https://docs.redhat.com/zh-cn/documentation/red_hat_enterprise_linux/7/html/installation_guide/sect-kickstart-syntax - 自己整理了一些常用的kickstart命令和选项
https://www.cnblogs.com/liwei1987/p/18611343
新虚拟机的硬件环境:bios启动,10G硬盘1块,网卡1张、
在这个硬件环境下,redhat家族的linux发行版中,8和9的kickstart文件基本能通用,7略有不同之处是在最小化安装的软件包名称上。8、9的最小化软件包名称为 @^minimal-environment,7的为@^minimal。
kickstart.cfg文件示例
[root@RHEL9 ~]# cat /data/ks/centos7.cfg
#文本安装
text
#语言
lang en_US
#键盘
keyboard --xlayouts='us'
#时区
timezone Asia/Shanghai#网卡配置
network --bootproto=dhcp
#安装包地址
url --url=http://192.168.5.253/iso/centos7/#初始化无效分区表
zerombr
#清除所有分区,初始化硬盘
clearpart --all --initlabel#安装bootloader到硬盘vda
bootloader --location=mbr --boot-drive=vda#创建512M /boot 分区
part /boot --asprimary --fstype=ext4 --ondisk=vda --size=512
#在剩余空间上创建 pv
part pv.01 --asprimary --fstype="lvmpv" --ondisk=vda --size=1 --grow
#创建 vg
volgroup vg01 --pesize=4096 pv.01
#创建 2048M 交换分区
logvol swap --fstype="swap" --size=2048 --name=swap --vgname=vg01
#在剩余空间上创建根分区
logvol / --fstype="xfs" --size=1 --grow --name=root --vgname=vg01#不配置X服务
skipx
#系统首次启动时不启动Initial Setup软件包
firstboot --disable
#开启selinux
selinux --enforcing
#开启防火墙,关闭cockpit,dhcpv6-client服务
firewall --enabled --remove-service=cockpit,dhcpv6-client
#关闭kdump
%addon com_redhat_kdump --disable
%end#最小化安装
%packages
@^minimal
%end#root密码
rootpw $6$T9Kt0AXi34CEQEQp$eFuKEtcBqli4.5Qj1m3/ACp2yPRuortne7n5K0C0V7lWrfZws3H3mj.mdaC58rShxu8/IRYkLwB0Kkl6/iRbj. --iscrypted#利用安装后脚本写入公钥,并将脚本执行日志输出到/root/ks-post.log
%post --log=/root/ks-post.log
[ -d /root/.ssh ] || mkdir -p /root/.ssh
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDDatyT4rkUtIASwqIlM5cHq8A6coBAdJr8FQgy8t2qUL9M/0TjirRX4qORRbJyW6F3nMf94ZIkp15LStcFA8miOpUC/eh6xLof/sHxjTvP06giT3j05w7G2UZlFptflU97flXwsBTD+XAltjpdWDcw0B/ig7NJS6v3zgmhqtnD018tRe9GKLKiEjMmpCmqIBeu+sShWJbhBuGXzETPHSvHZK1kLnVgHWPZ+5dMr/c4djahYMC2NycYtye8+6EaibaIc9DDWz8yRQigQDsFm/PKG+ys9nPYyhEMoW6toiTQKvngvdkVPp3XXF5jakPgFs474AyBlTy0fB5yagLx4C8S9grpbHBEjXSN6Nas/ruPGojjvWZCB13gGkGScuFvkN4KRmlOiRMJGZmy8Jqp3FxCpwkt5fEMHfM0yE+n/hYoW+emJAmKpRIWcmos4vO35XiXai0OAotG/XHaIzAXEsAYaqQSkQLEg2jDQiEbPJH1LXURYAo6NPxynHvZYOve7W8= root@RHEL9' > /root/.ssh/authorized_keys
%end#安装完成后重启
reboot
创建 /data/ks/ 目录存放各个发行版的 ks 文件,并创建 /var/www/html/ks 的软链接到 /data/ks/,将 ks 文件通过 http 服务共享出来
[root@RHEL9 ~]# mkdir /data/ks
[root@RHEL9 ~]# ln -s /data/ks /var/www/html/ks
在 /data/ks/ 目录下创建各发行版的 ks 文件,脚本走起
[root@RHEL9 ~]# cat ks2http.sh
#ks2http.sh
#Date: 2024-12-17
#!/bin/bashdistribution=('rhel9' 'rhel8' 'rocky9' 'rocky8' 'alma9' 'alma8' 'euro9' 'euro8' 'oracle9' 'oracle8' 'miracle9' 'miracle8' 'centos8' 'centos7' )for distr in ${distribution[@]};do
cat <<EOF>> /data/ks/${distr}.cfg
#文本安装
text
#语言
lang en_US
#键盘
keyboard --xlayouts='us'
#时区
timezone Asia/Shanghai#网卡配置
network --bootproto=dhcp
#安装包地址
url --url=http://192.168.5.253/iso/${distr}/#初始化无效分区表
zerombr
#清除所有分区,初始化硬盘
clearpart --all --initlabel#安装bootloader到硬盘vda
bootloader --location=mbr --boot-drive=vda#创建512M /boot 分区
part /boot --asprimary --fstype=ext4 --ondisk=vda --size=512
#在剩余空间上创建 pv
part pv.01 --asprimary --fstype="lvmpv" --ondisk=vda --size=1 --grow
#创建 vg
volgroup vg01 --pesize=4096 pv.01
#创建 2048M 交换分区
logvol swap --fstype="swap" --size=2048 --name=swap --vgname=vg01
#在剩余空间上创建根分区
logvol / --fstype="xfs" --size=1 --grow --name=root --vgname=vg01#不配置X服务
skipx
#系统首次启动时不启动Initial Setup软件包
firstboot --disable
#开启selinux
selinux --enforcing
#开启防火墙,关闭cockpit,dhcpv6-client服务
firewall --enabled --remove-service=cockpit,dhcpv6-client
#关闭kdump
%addon com_redhat_kdump --disable
%end#最小化安装
%packages
@^minimal-environment
%end#root密码
rootpw $6$T9Kt0AXi34CEQEQp$eFuKEtcBqli4.5Qj1m3/ACp2yPRuortne7n5K0C0V7lWrfZws3H3mj.mdaC58rShxu8/IRYkLwB0Kkl6/iRbj. --iscrypted#利用安装后脚本写入公钥,并将脚本执行日志输出到/root/ks-post.log
%post --log=/root/ks-post.log
[ -d /root/.ssh ] || mkdir -p /root/.ssh
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDDatyT4rkUtIASwqIlM5cHq8A6coBAdJr8FQgy8t2qUL9M/0TjirRX4qORRbJyW6F3nMf94ZIkp15LStcFA8miOpUC/eh6xLof/sHxjTvP06giT3j05w7G2UZlFptflU97flXwsBTD+XAltjpdWDcw0B/ig7NJS6v3zgmhqtnD018tRe9GKLKiEjMmpCmqIBeu+sShWJbhBuGXzETPHSvHZK1kLnVgHWPZ+5dMr/c4djahYMC2NycYtye8+6EaibaIc9DDWz8yRQigQDsFm/PKG+ys9nPYyhEMoW6toiTQKvngvdkVPp3XXF5jakPgFs474AyBlTy0fB5yagLx4C8S9grpbHBEjXSN6Nas/ruPGojjvWZCB13gGkGScuFvkN4KRmlOiRMJGZmy8Jqp3FxCpwkt5fEMHfM0yE+n/hYoW+emJAmKpRIWcmos4vO35XiXai0OAotG/XHaIzAXEsAYaqQSkQLEg2jDQiEbPJH1LXURYAo6NPxynHvZYOve7W8= root@RHEL9' > /root/.ssh/authorized_keys
%end#安装完成后重启
reboot
EOF
done
将 /data/sh/centos7.cfg 文件 最小化安装包的 @^minimal-environment改为 @^minimal
[root@RHEL9 ~]# sed -Ei 's/^(@\^minimal)-environment$/\1/' /data/ks/centos7.cfg
[root@RHEL9 ~]# tail -20 /data/ks/centos7.cfg
%addon com_redhat_kdump --disable
%end#最小化安装
%packages
@^minimal
%end#root密码
rootpw \$6\$T9Kt0AXi34CEQEQp\$eFuKEtcBqli4.5Qj1m3/ACp2yPRuortne7n5K0C0V7lWrfZws3H3mj.mdaC58rShxu8/IRYkLwB0Kkl6/iRbj. --iscrypted#利用安装后脚本写入公钥,并将脚本执行日志输出到/root/ks-post.log
%post --log=/root/ks-post.log
[ -d /root/.ssh ] || mkdir -p /root/.ssh
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDDatyT4rkUtIASwqIlM5cHq8A6coBAdJr8FQgy8t2qUL9M/0TjirRX4qORRbJyW6F3nMf94ZIkp15LStcFA8miOpUC/eh6xLof/sHxjTvP06giT3j05w7G2UZlFptflU97flXwsBTD+XAltjpdWDcw0B/ig7NJS6v3zgmhqtnD018tRe9GKLKiEjMmpCmqIBeu+sShWJbhBuGXzETPHSvHZK1kLnVgHWPZ+5dMr/c4djahYMC2NycYtye8+6EaibaIc9DDWz8yRQigQDsFm/PKG+ys9nPYyhEMoW6toiTQKvngvdkVPp3XXF5jakPgFs474AyBlTy0fB5yagLx4C8S9grpbHBEjXSN6Nas/ruPGojjvWZCB13gGkGScuFvkN4KRmlOiRMJGZmy8Jqp3FxCpwkt5fEMHfM0yE+n/hYoW+emJAmKpRIWcmos4vO35XiXai0OAotG/XHaIzAXEsAYaqQSkQLEg2jDQiEbPJH1LXURYAo6NPxynHvZYOve7W8= root@RHEL9' > /root/.ssh/authorized_keys
%end#安装完成后重启
reboot
重写 /var/lib/tftpboot/pxelinux/pxelinux.cfg/default PXE启动菜单引导文件,将 inst.repo 的参数改为 inst.ks 的参数
[root@RHEL9 ~]# sed -Ei 's/(append.+)repo(.+)iso(.+)/\1ks\2ks\3.cfg/' /var/lib/tftpboot/pxelinux/pxelinux.cfg/default
[root@RHEL9 ~]# cat /var/lib/tftpboot/pxelinux/pxelinux.cfg/default
default vesamenu.c32
prompt 1
timeout 600display boot.msglabel rhel9menu label Install rhel9kernel rhel9/vmlinuzappend initrd=rhel9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/rhel9.cfg
label rhel8menu label Install rhel8kernel rhel8/vmlinuzappend initrd=rhel8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/rhel8.cfg
label rocky9menu label Install rocky9kernel rocky9/vmlinuzappend initrd=rocky9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/rocky9.cfg
label rocky8menu label Install rocky8kernel rocky8/vmlinuzappend initrd=rocky8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/rocky8.cfg
label alma9menu label Install alma9kernel alma9/vmlinuzappend initrd=alma9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/alma9.cfg
label alma8menu label Install alma8kernel alma8/vmlinuzappend initrd=alma8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/alma8.cfg
label euro9menu label Install euro9kernel euro9/vmlinuzappend initrd=euro9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/euro9.cfg
label euro8menu label Install euro8kernel euro8/vmlinuzappend initrd=euro8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/euro8.cfg
label oracle9menu label Install oracle9kernel oracle9/vmlinuzappend initrd=oracle9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/oracle9.cfg
label oracle8menu label Install oracle8kernel oracle8/vmlinuzappend initrd=oracle8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/oracle8.cfg
label miracle9menu label Install miracle9kernel miracle9/vmlinuzappend initrd=miracle9/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/miracle9.cfg
label miracle8menu label Install miracle8kernel miracle8/vmlinuzappend initrd=miracle8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/miracle8.cfg
label centos7menu label Install centos7kernel centos7/vmlinuzappend initrd=centos7/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/centos7.cfg
label centos8menu label Install centos8kernel centos8/vmlinuzappend initrd=centos8/initrd.img ip=dhcp inst.ks=http://192.168.5.253/ks/centos8.cfg
label localmenu label Boot from local drivemenu defaultlocalboot 0xffff
创建一个虚拟机用于安装RockyLinux8,内存给到3G,cup2核
创建RockyLinux8虚拟机脚本,网卡启动
[root@RHEL9 ~]# cat rocky8create-net.sh
#guestcreate.sh
#Date: 2024-11-08
#!/bin/bashbootdev=network
#bootdev=cdrom
#isopath=/data/ISO/guestname=rocky8
guestmem=3145728
guestcups=2
hddir=/kvm/
hdsize=10G
hdpath=${hddir}${guestname}.img
vncport=5902
vncpasswd=hatred#wanmac='52:54:00:92:31:FE'
dmzmac='52:54:00:72:31:01'
#lanmac='52:54:00:10:31:FE'xmldir=/data/xml/
xmlpath=${xmldir}${guestname}.xml[ -f ${hdpath} ] || qemu-img create -f qcow2 ${hdpath} ${hdsize}cat > ${xmlpath} << EOF
<domain type="kvm"><name>${guestname}</name><uuid>$(uuidgen)</uuid><memory>${guestmem}</memory><vcpu>${guestcups}</vcpu><os><type arch="x86_64" machine="q35">hvm</type><boot dev="hd"/></os><features><acpi/><apic/></features><cpu mode="host-passthrough"/><clock offset="utc"><timer name="rtc" tickpolicy="catchup"/><timer name="pit" tickpolicy="delay"/><timer name="hpet" present="no"/></clock><pm><suspend-to-mem enabled="no"/><suspend-to-disk enabled="no"/></pm><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type="file" device="disk"><driver name="qemu" type="qcow2" discard="unmap"/><source file="${hdpath}"/><target dev="vda" bus="virtio"/></disk><controller type="usb" model="qemu-xhci" ports="15"/><controller type="pci" model="pcie-root"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><console type="pty"/><channel type="unix"><source mode="bind"/><target type="virtio" name="org.qemu.guest_agent.0"/></channel><input type="tablet" bus="usb"/><graphics type="vnc" port="${vncport}" listen="0.0.0.0" passwd="${vncpasswd}"/><video><model type="virtio"/></video><memballoon model="virtio"/><rng model="virtio"><backend model="random">/dev/urandom</backend></rng></devices>
</domain>
EOF[[ -n ${bootdev} ]] && sed -Ei '/ <boot dev="hd"\/>/a \ <boot dev="'''${bootdev}'''"\/>' ${xmlpath}
[[ -n ${isopath} ]] && sed -Ei '/ <controller type="usb" model="qemu-xhci" ports="15"\/>/i \ <disk type="file" device="cdrom">\n <driver name="qemu" type="raw"\/>\n <source file="'''${isopath}'''"\/>\n <target dev="sda" bus="sata"\/>\n <readonly\/>\n <\/disk>' ${xmlpath}
[[ -n ${wanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="WANbridge"/>\n <mac address="'''${wanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${dmzmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="DMZbridge"/>\n <mac address="'''${dmzmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${lanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="LANbridge"/>\n <mac address="'''${lanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}virsh create ${xmlpath}
自动安装过程
重新配置定义rockylinux8虚拟机
rockylinux8虚拟机重定义脚本
[root@RHEL9 ~]# cat rocky8define.sh
#guestdefine.sh
#Date: 2024-11-09
#!/bin/bash#配置文件位置
xmlpath=/data/xml/rocky8.xml
guestmem=1048576
guestcups=1#获取虚拟机名称
guestname=$(sed -En 's/^ <name>([[:alnum:]]+)<\/name>/\1/p' ${xmlpath})#确保虚拟机关闭
virsh guestinfo ${guestname} && virsh destroy ${guestname}#重新调整虚拟机内存
[[ -n ${guestmem} ]] && sed -Ei.bak 's/(^ <memory>)[[:digit:]]+(<\/memory>)/\1'''${guestmem}'''\2/' ${xmlpath}#重新调整虚拟机cpu
[[ -n ${guestcups} ]] && sed -Ei 's/(^ <vcpu>)[[:digit:]]+(<\/vcpu>)/\1'''${guestcups}'''\2/' ${xmlpath}#删除vnc远程连接
sed -Ei '/<graphics type="vnc" port=/d' ${xmlpath}#删除启动项cdrom
sed -Ei '/<boot dev="cdrom"\/>/d' ${xmlpath}#删除启动项network
sed -Ei '/<boot dev="network"\/>/d' ${xmlpath}#删除设备cdrom
sed -Ei '/<disk type="file" device="cdrom">/,/<\/disk>/d' ${xmlpath}#定义虚拟机
virsh define ${xmlpath}#启动虚拟机
virsh start ${guestname}#设置虚拟机开机启动
virsh autostart ${guestname}
实现创建ks文件和修改pxe菜单文件的脚本
[root@RHEL9 ~]# cat rhel9ks2http.sh
#rhel9ks2http.sh
#Date: 2024-12-18
#!/bin/bashkspath=/data/ks/#创建存放ks文件的目录[[ -d ${kspath} ]] || mkdir ${kspath}#在http根目录下创建软连接至存放ks文件的目录[[ -L /var/www/html/ks ]] || ln -s ${kspath} /var/www/html/ks#创建各发行版的ks文件distribution=('rhel9' 'rhel8' 'rocky9' 'rocky8' 'alma9' 'alma8' 'euro9' 'euro8' 'oracle9' 'oracle8' 'miracle9' 'miracle8' 'centos8' 'centos7' )for distr in ${distribution[@]};do
cat <<EOF>> ${kspath}${distr}.cfg
#文本安装
text
#语言
lang en_US
#键盘
keyboard --xlayouts='us'
#时区
timezone Asia/Shanghai#网卡配置
network --bootproto=dhcp
#安装包地址
url --url=http://192.168.5.253/iso/${distr}/#初始化无效分区表
zerombr
#清除所有分区,初始化硬盘
clearpart --all --initlabel#安装bootloader到硬盘vda
bootloader --location=mbr --boot-drive=vda#创建512M /boot 分区
part /boot --asprimary --fstype=ext4 --ondisk=vda --size=512
#在剩余空间上创建 pv
part pv.01 --asprimary --fstype="lvmpv" --ondisk=vda --size=1 --grow
#创建 vg
volgroup vg01 --pesize=4096 pv.01
#创建 2048M 交换分区
logvol swap --fstype="swap" --size=2048 --name=swap --vgname=vg01
#在剩余空间上创建根分区
logvol / --fstype="xfs" --size=1 --grow --name=root --vgname=vg01#不配置X服务
skipx
#系统首次启动时不启动Initial Setup软件包
firstboot --disable
#开启selinux
selinux --enforcing
#开启防火墙,关闭cockpit,dhcpv6-client服务
firewall --enabled --remove-service=cockpit,dhcpv6-client
#关闭kdump
%addon com_redhat_kdump --disable
%end#最小化安装
%packages
@^minimal-environment
%end#root密码
rootpw $6$T9Kt0AXi34CEQEQp$eFuKEtcBqli4.5Qj1m3/ACp2yPRuortne7n5K0C0V7lWrfZws3H3mj.mdaC58rShxu8/IRYkLwB0Kkl6/iRbj. --iscrypted#利用安装后脚本写入公钥,并将脚本执行日志输出到/root/ks-post.log
%post --log=/root/ks-post.log
[ -d /root/.ssh ] || mkdir -p /root/.ssh
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDDatyT4rkUtIASwqIlM5cHq8A6coBAdJr8FQgy8t2qUL9M/0TjirRX4qORRbJyW6F3nMf94ZIkp15LStcFA8miOpUC/eh6xLof/sHxjTvP06giT3j05w7G2UZlFptflU97flXwsBTD+XAltjpdWDcw0B/ig7NJS6v3zgmhqtnD018tRe9GKLKiEjMmpCmqIBeu+sShWJbhBuGXzETPHSvHZK1kLnVgHWPZ+5dMr/c4djahYMC2NycYtye8+6EaibaIc9DDWz8yRQigQDsFm/PKG+ys9nPYyhEMoW6toiTQKvngvdkVPp3XXF5jakPgFs474AyBlTy0fB5yagLx4C8S9grpbHBEjXSN6Nas/ruPGojjvWZCB13gGkGScuFvkN4KRmlOiRMJGZmy8Jqp3FxCpwkt5fEMHfM0yE+n/hYoW+emJAmKpRIWcmos4vO35XiXai0OAotG/XHaIzAXEsAYaqQSkQLEg2jDQiEbPJH1LXURYAo6NPxynHvZYOve7W8= root@RHEL9' > /root/.ssh/authorized_keys
%end#安装完成后重启
reboot
EOF
done#修改centos7的ks文件sed -Ei 's/^(@\^minimal)-environment$/\1/' ${kspath}centos7.cfg#重新创建pxe启动的菜单文件pxepath=/var/lib/tftpboot/pxelinux/
pxefile=${pxepath}pxelinux.cfg/default
kspath=http://192.168.5.253/ks/cat << EOF >| ${pxefile}
default vesamenu.c32
prompt 1
timeout 600display boot.msgEOFfor distr in ${distribution[@]} ; do
cat << EOF >> ${pxefile}
label ${distr}menu label Install ${distr}kernel ${distr}/vmlinuzappend initrd=${distr}/initrd.img ip=dhcp inst.ks=${kspath}${distr}.cfg
EOF
donecat << EOF >> ${pxefile}
label localmenu label Boot from local drivemenu defaultlocalboot 0xffff
EOF