通过 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

相关推荐
章豪Mrrey nical15 小时前
前后端分离工作详解Detailed Explanation of Frontend-Backend Separation Work
后端·前端框架·状态模式
派大鑫wink17 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
程序员爱钓鱼17 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII17 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home17 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
zfj32117 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
weixin_4624462317 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL18 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
小信啊啊18 小时前
Go语言切片slice
开发语言·后端·golang
Victor35620 小时前
Netty(20)如何实现基于Netty的WebSocket服务器?
后端