ks.cfg
环境:RHEL9.5+TFTP+HTTP
[root@mu02 kickstart]# cat ks.cfg
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8
keyboard --xlayouts='us'
timezone Asia/Shanghai --utc
rootpw --iscrypted --allow-ssh 123
reboot
text
url --url=http://192.168.9.252
bootloader --append="rhgb quiet crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M"
zerombr
clearpart --all --initlabel
autopart
#network --bootproto=dhcp
firstboot --disable
selinux --disabled
firewall --disabled
%pre
#!/bin/bash
# 假设你的MAC-IP映射文件可以通过HTTP访问
url="http://192.168.9.252/kickstart/mac_ip_map.csv"
# mac_ip_map.csv 文件中不要有空行,格式为00:00:00:00:00:00,192.168.9.1
# 使用curl下载映射文件
curl -o /tmp/mac_ip_map.csv $url
# 初始化变量
declare -A mac_ip_map
network_config=""
hostname=""
last_digit=""
# 读取MAC-IP映射文件并存储在一个关联数组中
while IFS=, read -r mac ip; do
mac=$(echo $mac | tr -d '[:space:]')
mac="${mac^^}"
mac_ip_map["$mac"]="$ip"
done < /tmp/mac_ip_map.csv
# 获取所有网卡的MAC地址及其名称
for interface in $(ls /sys/class/net); do
# 提取每个网络接口的MAC地址
mac=$(cat /sys/class/net/$interface/address | tr '[:lower:]' '[:upper:]')
# 判断是否为IB网卡(假设接口名以ib开头)
if [[ "$interface" == ib* ]]; then
# 如果之前已经找到了普通网卡的IP地址,则使用其最后一位数字作为IB网卡的第四位
if [ -n "$last_digit" ]; then
ib_ip="11.11.10.$last_digit"
network_config+="network --bootproto=static --ip=$ib_ip --netmask=255.255.254.0 --device=$interface --onboot=yes"$'\n'
fi
else
# 如果找到了匹配的IP地址
if [ -n "${mac_ip_map[$mac]}" ]; then
ip="${mac_ip_map[$mac]}"
last_digit=$(echo $ip | awk -F. '{print $NF}')
# 设置主机名为node+IP地址的最后一位
hostname="node$last_digit"
network_config+="network --bootproto=static --ip=$ip --hostname=$hostname --netmask=255.255.254.0 --gateway=192.168.9.254 --nameserver=192.168.9.253 --device=$interface --onboot=yes"$'\n'
fi
fi
done
# 检查是否找到匹配项
if [ -z "$hostname" ]; then
echo "No matching IP found for any MAC address."
exit 1
fi
# 输出到ks文件中,以便后续使用
cat <<EOF > /tmp/ks_post.cfg
$network_config
# hostname $hostname
EOF
%end
%include /tmp/ks_post.cfg
%post
echo "[BaseoS]" >>/etc/yum.repos.d/redhat.repo
echo "name=BaseOS" >> /etc/yum.repos.d/redhat.repo
echo "baseurl=http://192.168.9.252/BaseOS" >>/etc/yum.repos.d/redhat.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/redhat.repo
echo "enabled=1" >> /etc/yum.repos.d/redhat.repo
echo "[AppStream]" >> /etc/yum.repos.d/redhat.repo
echo "name=AppStream" >> /etc/yum.repos.d/redhat.repo
echo "baseurl=http://192.168.9.252/AppStream" >>/etc/yum.repos.d/redhat.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/redhat.repo
echo "enabled=1" >> /etc/yum.repos.d/redhat.repo
%end
%packages
@^graphical-server-environment
@development
@platform-devel
@additional-devel
@hardware-monitoring
@debugging
@infiniband
@java-platform
@legacy-unix
telnet
@network-tools
@standard
@base-x
@gnome-desktop
@fonts
nfs-utils
nfs-utils-coreos
nfs4-acl-tools
nfsv4-client-utils
%end