我的内容文档

CentOS 7 自定义 ISO 镜像制作技术文档

2025/09/09
24
0

文档概述

本文档详细记录了基于 CentOS 7 最小化安装镜像制作自定义 ISO 的完整流程,使用 Kickstart 实现自动化安装配置。该流程适用于需要批量部署标准化 CentOS 7 系统的场景。

准备工作

系统环境要求

  • 已安装 CentOS 7 或类似 Linux 发行版

  • 拥有 root 权限或 sudo 权限

  • 至少 10GB 可用磁盘空间

所需软件包

  • genisoimage(用于生成 ISO 镜像)

  • vim(文本编辑器)或 vi

操作步骤

1. 创建挂载目录并挂载原始 ISO

# 创建镜像挂载点

  mkdir /mnt/centos_iso

#挂载原始 CentOS ISO 镜像

  mount -o loop CentOS7_x86_64-Minimal-1708.iso /mnt/centos_iso

2. 复制镜像内容到工作目录

# 进入挂载点
cd /mnt/centos_iso/

# 创建本地工作目录
mkdir /home/centos7

# 复制所有文件到工作目录(保留权限和属性)
cp -frav * /home/centos7

3. 修改启动配置文件

# 进入 isolinux 目录
cd /home/centos7/isolinux/

# 编辑启动配置文件
vim isolinux.cfg

# 编辑 Kickstart 配置文件
vim ks.cfg

isolinux.cfg文件内容
default vesamenu.c32
timeout 20

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

#添加下列该段内容即可
label custom
  menu label ^Install CentOS 7 By ZhangBing - Text Mode
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.ks=cdrom:/isolinux/ks.cfg inst.stage2=hd:LABEL=CENTOS7 quiet text

label linux
  menu label ^Install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet

label check
  menu label Test this ^media & install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
        Try this option out if you're having trouble installing
        CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
        If the system will not boot, this lets you access files
        and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
        If your system is having issues, a problem with your
        system's memory may be the cause. Use this utility to
        see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

ks.cfg文件内容
# 平台架构:适用于x86、AMD64或Intel EM64T架构
#platform=x86, AMD64, 或 Intel EM64T

# 发行版本:使用开发版本(实际部署时应指定具体版本)
#version=DEVEL

# 安装模式:执行全新安装而非升级
install

# 键盘布局:设置为美式英语键盘
keyboard us

# Root密码:使用明文密码(安全风险!建议使用加密密码)
rootpw --plaintext Huawei@123

# 系统语言:设置为美式英语
lang en_US.UTF-8

# 认证配置:使用SHA512加密算法
auth --useshadow --passalgo=sha512

# 安装介质来源:使用光盘安装
cdrom

# 安装界面:使用图形化安装界面
text

# SELinux配置:禁用SELinux(安全风险!生产环境建议启用)
selinux --disabled

#配置IP地址
network --bootproto=dhcp --device=eth0 --onboot=on --hostname=my-server

#指定网卡配置IP地址
#network --bootproto=static --device=<网卡名称> --ip=<IP地址> --netmask=<子网掩码> --gateway=<网关> --nameserver=<DNS服务器> --onboot=on --hostname=<主机名>

# X Window系统:不配置X Window系统(适用于服务器)
skipx

# 防火墙配置:禁用防火墙(安全风险!生产环境建议启用)
firewall --disabled

# 安装后操作:安装完成后重启系统
reboot

# 时区设置:设置为亚洲/上海时区
timezone Asia/Shanghai --isUtc

# 引导加载程序配置:安装到主引导记录(MBR)
bootloader --location=mbr --append="net.ifnames=0 biosdevname=0 quiet"

# 磁盘操作:清除主引导记录
zerombr

# 分区清理:清除所有分区并初始化磁盘标签
clearpart --all --initlabel

#下列两种磁盘方式都可以选择

# 手动选择磁盘分区配置:
# /boot分区:主分区,xfs文件系统,512MB
#part /boot --asprimary --fstype="xfs" --ondisk=sda --size=512
# 交换分区:8GB(根据内存大小调整,通常为物理内存的1-2倍)
#part swap --fstype="swap" --ondisk=sda --size=8192
# 根分区:20GB,xfs文件系统
#part / --fstype="xfs" --ondisk=sda --size=20480
# 扩展分区:使用剩余所有空间
#part /home --fstype="xfs" --grow --ondisk=sda --size=1


# 自动选择磁盘分区配置:
# /boot分区:主分区,xfs文件系统,512MB
part /boot --fstype="xfs" --size=512
# 交换分区:8GB(根据内存大小调整,通常为物理内存的1-2倍)
part swap --fstype="swap" --size=8192
# 根分区:20GB,xfs文件系统
part / --fstype="xfs" --size=20480
# home分区:使用剩余所有空间
part /home --fstype="xfs" --grow



# 软件包选择:仅安装核心包组(最小化安装)
%packages --nobase
@core
%end

# 安装后脚本部分
# %post --log=/root/ks-post.log

配置文件修改要点:

  • 在 isolinux.cfg中指定 Kickstart 文件路径

  • 在 ks.cfg中定义系统安装参数、软件包选择和分区方案等

4. 安装 ISO 制作工具

# 安装 genisoimage 软件包
yum install genisoimage

5. 生成自定义 ISO 镜像

# 返回工作目录根路径
cd /home/centos7

# 使用 mkisofs 命令生成新 ISO
mkisofs -r -V "CENTOS7" -b isolinux/isolinux.bin -c isolinux/boot.cat \
-cache-inodes -J -l -no-emul-boot -boot-load-size 4 -boot-info-table \
-o /centos7_by_pve.iso .