通过 ks.cfg 文件实现 Centos 系统自动部署

通过 ks.cfg 文件实现 Centos 系统自动部署

CentOS

一、环境准备

在 vmware 中部署 CentOS7.9,并准备好环境。部署参考文档:VMware 虚拟机安装CentOS镜像超详细步骤

bash 复制代码
#! /bin/bash
# touch envPre.sh && chmod +x envPre.sh
# vi envPre.sh
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i 's|http|https|g' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache

yum -y install mkisofs genisoimage syslinux vim

echo "开始复制镜像文件到指定目录..."
mkdir -p /opt/centos/mount /opt/centos/build
mount -t iso9660 -o ro /dev/sr0 /opt/centos/mount/
cp -arf /opt/centos/mount/*  /opt/centos/build
cp ~/anaconda-ks.cfg /opt/centos/build/isolinux/ks.cfg
cp /etc/yum.repos.d/CentOS-Base.repo  /opt/centos/build/isolinux/
umount /opt/centos/mount/
echo "脚本执行完成"

二、编写 ks.cfg 文件

以下是参考文件,各配置项的详细解释可参考官方文档:Red Hat Documentation

bash 复制代码
# vim /opt/centos/build/isolinux/ks.cfg

#version=DEVEL
# System authorization information.
# --enableshadow:Encrypt /etc/shadow
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use text install
text
# Disable the first time run wizard
firstboot --disable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# System hostname
network  --hostname=server
# Root password
rootpw --lock
user --name=manager --groups=wheel --password=myXhml6MgaVsI --iscrypted
# System timezone
timezone Asia/Shanghai --isUtc
selinux --disable
reboot
# Partition
%include /tmp/part-include
%pre
#!/bin/sh
act_mem=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
if [ ${act_mem} -le $((2*1024*1024)) ]; then
    swap_size=$((2*1024))
elif [ ${act_mem} -gt $((2*1024*1024)) ] && [ ${act_mem} -le $((16*1024*1024)) ]; then
    swap_size=$((8*1024))
else
    swap_size=$((16*1024))
fi
cat >> /tmp/part-include << EOF
bootloader --location=mbr
clearpart --all --initlabel
zerombr
ignoredisk --only-use=sda
part biosboot --fstype="biosboot" --size=1
part /boot --fstype="ext4" --size=1024
part /boot/efi --fstype="ext4" --size=1024
part swap  --fstype="swap" --size=${swap_size}
part / --fstype="ext4" --size=1 --grow
EOF
%end
# Install software
%packages
@^minimal
@core
kexec-tools
%end
# Log the system crash information to /var/crash
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
# User Policies
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
# Shell
%post --nochroot --log=/mnt/sysimage/var/log/inst1.log
cp -rf /run/install/repo/CentOS-Base.repo /mnt/sysimage/tmp
%end
%post
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
cp -rf /tmp/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
#set network device
NET_DEV=`ip a | grep mtu | awk '{print $2}' | grep -v lo | head -n 1 | sed 's/://'`
sed -i 's@ONBOOT=no@ONBOOT=yes@' /etc/sysconfig/network-scripts/ifcfg-$NET_DEV
sed -i 's@ONBOOT="no"@ONBOOT="yes"@' /etc/sysconfig/network-scripts/ifcfg-$NET_DEV
sed -i '/BOOTPROTO/d' /etc/sysconfig/network-scripts/ifcfg-$NET_DEV
cat >> /etc/sysconfig/network-scripts/ifcfg-$NET_DEV <<EOF
BOOTPROTO=static
IPADDR=192.168.1.1
PREFIX=24
GATEWAY=192.168.1.1
DNS1=223.5.5.5
EOF
%end

三、修改启动引导文件

  1. BIOS 启动文件:vim /opt/centos/build/isolinux/isolinux.cfg
bash 复制代码
# 选择启动项等待时间,单位是毫秒
timeout 30

label linux
  menu label ^Install CentOS 7
  menu default # 设置默认启动
  kernel vmlinuz
# 修改 append 后面的内容,LABEL 项建议按照此处设置为 Centos7
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=cdrom:/isolinux/ks.cfg 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
  1. EFI 启动:vim /opt/centos/build/EFI/BOOT/grub.cfg
bash 复制代码
# 找到以下内容进行修改
set default="0"
set timeout=30

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Install CentOS 7' --class fedora --class gnu-linux --class gnu --class os {
        linuxefi /images/pxeboot/vmlinuz inst.ks=cdrom:/isolinux/ks.cfg  inst.stage2=hd:LABEL=CentOS7 quiet
        initrdefi /images/pxeboot/initrd.img
}

四、生成镜像文件

bash 复制代码
# CentOS7.iso 的名称对应引导文件中设置的值
genisoimage -v -cache-inodes -joliet-long -R -J -T -V \
CentOS7 -o /opt/CentOS7.iso -c isolinux/boot.cat \
-b isolinux/isolinux.bin  -no-emul-boot -boot-load-size 4 \
-boot-info-table  -eltorito-alt-boot -b images/efiboot.img \
-no-emul-boot /opt/centos/build

‍

‍

相关推荐
高频因子挖掘机5 小时前
300 只股票 × 两年日 K:为什么逐只循环会变成维护灾难?
后端·github·api
苏三说技术5 小时前
Spring AI Alibaba已停更了,Java还有希望吗?
后端
松就是我902986 小时前
如何搭建一个 Agent 系统:方法论、理想形态与一次真实落地
后端
Layer6 小时前
即时通讯系统的关键设计
后端·架构
golang学习记7 小时前
rust开发,选VS Code还是RustRover?
开发语言·后端·rust
打工仔折腾 AI7 小时前
从零写一个CAD 05:以鼠标为中心的滚轮缩放,招法能复用但顺序不能反
开发语言·后端·python·性能优化·计算机外设·swift·ai agent 实战
geovindu9 小时前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
颜进强10 小时前
20 · NestJs循环依赖与 forwardRef:容器为什么在环面前会死,"占位再补齐"怎么救
前端·后端·ai编程
颜进强10 小时前
19 · NestJs @Global 落地账:装饰器与 `isGlobal` 参数,各在什么场景上岗
前端·后端·ai编程
她的男孩10 小时前
分片上传的大文件人人可下载:文件模块 isPrivate 在合并时被抹成 false,另有 4 个静默坑
java·后端·架构