使用Kickstart自定义CentOS ISO镜像

工作中使用CentOS,会遇到一些针对CentOS定制化的需求,比如安全加固、安装自定义软件等。本文介绍了如何使用Linux的Kickstart,自定义CentOS安装,并自定义安装软件,生成自定义的ISO镜像文件。
本文,以CentOS7.6为基础,生成自定义镜像,实现以下需求:

  • 自动化安装,无需通过页面交互指定配置,将配置写在Kickstart文件中。
  • CentOS安装过程中,完成Docker安装。
  • CentOS安装过程中,执行特定脚本,对系统做更改。
  • 最后生成自定义的ISO镜像文件。

有关Kickstart的介绍,可参见官方文档:https://docs.centos.org/en-US/centos/install-guide/Kickstart2/

镜像工具安装

因为最后要生成自定义文件,所以先安装制作工具:

yum -y install anaconda createrepo mkisofs rsync syslinux

光盘挂载

以原始CentOS ISO镜像文件为基础,添加自定义的内容,所以要先挂载原始ISO文件,具体操作如下:

#创建挂载点
mkdir /mnt/cdrom

#将原始的ISO文件上传到指定目录下(本文中是/home/IOS)
mount -o loop /home/IOS/CentOS-7-x86_64-Minimal-1810.iso /mnt/cdrom/

# 同步/mnt/cdrom/下的文件到/ISO/路径下,除了Packages和repodata文件夹
/usr/bin/rsync -a /mnt/cdrom/ /ISO/

制作Kickstart文件ks.cfg

  • 在上面同步的/ISO的/isolinux下创建ks.cfg,内容如下(这是我自己的,该文件的详细说明及用法,参见上面的官方文档):
#version=CentOS
#platform=x86, AMD64, or Intel EM64T
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# Root password:123456。此密码可以从安装好的CentOS系统/root/anaconda-ks.cfg文件找到,可以直接复制过来。也可以参照官网文档自定义
rootpw --iscrypted $6$4W0hA07RBGc/HsSg$gG/K5SsPRv/IiGzCkUVuIZdAQhLcbOdKNUkD1v6P0AhiJCgWJd.KoCw.pqFTKs6fsI66QGgoZlBhtSrSvQBbv0
# (Required) Wrapper around the authconfig command CCE-14063-2 (row 80)
authconfig --enableshadow --passalgo=sha512
# System language
lang en_US.UTF-8
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use CDROM installation media
cdrom
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Network information
network  --bootproto=dhcp --device=enp0s3 --ipv6=auto --activate
network  --hostname=localhost.localdomain
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --location=mbr --driveorder=sda --append=""
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
autopart --type=lvm

%packages
@^minimal
@core

%end

%post --nochroot

#!/bin/sh
#在/ISO目录下创建postinstall,此目录下放需要内置的软件,比如docker的离线安装包。下面的命令是将安装包拷贝到安装后的root目录下
cp -r /run/install/repo/postinstall /mnt/sysimage/root

%end

%post

#!/bin/sh

cd /root/postinstall
#解压docker
tar -zxvf docker-24.0.4.tgz -C /home
#将docker拷贝到相应目录下
sudo cp /home/docker/* /usr/bin/
#拷贝注册docker服务所需文件
sudo cp docker.service /usr/lib/systemd/system/
#开机启动docker
sudo systemctl enable docker

%end

%post
#执行自定义脚本
#!/bin/sh

cp /etc/pam.d/sshd /etc/pam.d/sshd.bak
sed -i "/#%PAM-1.0/a\auth        required      pam_tally2.so onerr=fail deny=3 unlock_time=500 even_deny_root root_unlock_time=500"  /etc/pam.d/sshd

%end

上面脚本中的的docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
 
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
 
[Install]
WantedBy=multi-user.target

修改isolinux.cfg

cd /ISO/isolinux

chmod 644 isolinux.cfg

sudo vi isolinux.cfg
label linux
  menu label ^Install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=cdrom:/isolinux/ks.cfg quiet
  • inst.ks为ks.cfg文件位置;
  • inst.stage2为安装介质位置,hd:LABEL为介质标签,例如CentOS7。这个和后续生成ISO镜像文件的命令genisoimage的参数-V有关。
  • modprobe.blacklist=nouveau; 禁用nouveau驱动安装,用于NVIDIA驱动的安装准备工作;
  • net.ifnames=0 biosdevname=0; 用于禁用centos7的”一致性网络设备命名法”.
chmod 444 isolinux.cfg

制作iso镜像

cd /ISO

# 注意参数中的-V,和上面的isolinux.cfg文件有关
genisoimage -joliet-long -V CentOS7 -o CentOS-7-custom.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -cache-inodes -T -eltorito-alt-boot -e images/efiboot.img -no-emul-boot /ISO

上面脚本执行后,最后输出如下内容(省略了前端的一些输出),说明成功:

 97.27% done, estimate finish Sun Jul  9 14:53:02 2023
 98.26% done, estimate finish Sun Jul  9 14:53:02 2023
 99.26% done, estimate finish Sun Jul  9 14:53:02 2023
Total translation table size: 124018
Total rockridge attributes bytes: 55014
Total directory bytes: 92160
Path table size(bytes): 156
Done with: The File(s)                             Block(s)    503497
Writing:   Ending Padblock                         Start Block 503609
Done with: Ending Padblock                         Block(s)    150
Max brk space used a4000
503759 extents written (983 MB)

Hybird模式

采用“hybird模式”(混合模式),操作系统可以直接刻录成物理光盘,也可以直接做成可引导的U盘。

isohybrid -v /ISO/CentOS-7-custom.iso

制作镜像MD5值

implantisomd5 /ISO/CentOS-7-custom.iso

系统安装

  • 本人测试时,使用的vm box,安装的虚拟机。
  • 虚拟机加载上面生成的镜像后,过一会后,若能看到下面的界面(text模式),则说明可正常安装,等待安装完成。

c18ea44689dea35a26596c8b318b76d.png

  • 安装完成后,会自动重启。本人测试时,遇到一个问题:重启后,会重新安装。此时,可强制退出虚拟机。然后在虚拟机的设置中,删除存储,控制器下的盘片,删除后,再重启即可。

image.png

  • 重启后,验证ks.cfg中已安装的软件、已执行的脚本。

写在最后

在自定义安装软件这块需求中,还有一种在Kickstart中使用comps.xml文件在方式,这种比较复杂,适合一些简单的RPM包安装,也可以在Kickstart文件中的%packages下一个个指定RPM包。由于直接安装RPM包,涉及一些包之间的依赖,所以这种方式不推荐,本人也没有测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值