rockylinux livecd制作

-
-
2026-01-06
RedHat系列linux,livecd制作,可以用参考Overview - fedora-kickstarts - Pagure.io

使用livemedia-creator制作 

livemedia-creator — Lorax 41.3 documentation

基本思路是用 qemu 配合 kickstart 和 Anaconda boot.iso 安装到磁盘镜像中,然后用光盘镜像创建可启动介质。
 

安装构建工具和依赖

sudo dnf install -y lorax lorax-templates dracut-live isomd5sum syslinux grub2-efi-x64-modules grub2-pc-modules

安装虚拟机工具

sudo dnf install -y virt-install qemu-kvm libvirt-daemon-kvm

启动服务

sudo systemctl enable --now libvirtd
sudo systemctl start libvirtd

创建构建目录

mkdir -p ~/rocky9-livecd
mkdir -p ~/rocky9-livecd/kickstarts

下载构建安装livecd时的引导镜像

https://download.rockylinux.org/pub/rocky/9/isos/x86_64/Rocky-9.7-x86_64-boot.iso

cd ~/rocky9-livecd
wget https://download.rockylinux.org/pub/rocky/9/isos/x86_64/Rocky-9.7-x86_64-boot.iso

创建kickstarts配置my.ks

# rocky-live-base.ks
#

lang zh_CN.UTF-8
keyboard us
timezone Asia/Shanghai
# selinux --enforcing
selinux --disabled
firewall --disabled
xconfig --startxonboot
zerombr
clearpart --all
part / --size 12288 --fstype ext4
# services --enabled=NetworkManager,ModemManager --disabled=sshd
services --enabled=NetworkManager,sshd
network --bootproto=dhcp --device=link --activate
rootpw --lock --iscrypted locked
shutdown

# 主要安装源 - 你的本地 BaseOS 仓库
url --url "https://mirrors.aliyun.com/rockylinux/9.7/BaseOS/x86_64/os/"
repo --name="appstream" --baseurl="https://mirrors.aliyun.com/rockylinux/9.7/AppStream/x86_64/os/"
repo --name="extras" --baseurl="https://mirrors.aliyun.com/rockylinux/9.7/extras/x86_64/os/"
repo --name="epel" --baseurl="https://mirrors.aliyun.com/epel/9/Everything/x86_64/"
# 你的本地 AppStream 仓库
repo --name="my-local-repo" --baseurl="http://172.27.175.219/repos/rocky9/Custom/x86_64/os/Packages/"

%packages
# 基础包
@core
kernel
memtest86+
kernel-modules
kernel-modules-extra

authselect-compat
chrony
cryptsetup
device-mapper-multipath
dosfstools
dracut-network
e2fsprogs
efibootmgr
fcoe-utils
firewalld
glibc-all-langpacks
grub2-efi-x64
grub2-efi-x64-cdboot
grub2-pc
grub2-tools
grub2-tools-efi
grub2-tools-extra
iscsi-initiator-utils
kdump-anaconda-addon
libblockdev-plugins-all
libreport-plugin-bugzilla
lvm2
mdadm
realmd
restore
shim-x64
syslinux-extlinux
teamd
tmux
xfsprogs


# live环境包
anaconda-install-env-deps
anaconda-live
device-mapper-multipath
aajohan-comfortaa-fonts
dracut-live
glibc-all-langpacks
livesys-scripts


# xfce 桌面环境,以及应用
@Xfce
-gdm
lightdm
lightdm-gtk-greeter
xorg-x11-server-Xorg
xorg-x11-drivers
xfce4-about
xfce4-mount-plugin
mousepad
ristretto
firefox


# 中文支持
google-droid-sans-fonts
google-noto-cjk-fonts-common
google-noto-emoji-color-fonts
google-noto-fonts-common
google-noto-sans-cjk-ttc-fonts
google-noto-sans-gurmukhi-fonts
google-noto-sans-sinhala-vf-fonts
google-noto-serif-cjk-ttc-fonts
fontconfig
glibc-langpack-zh

# 中文输入法
ibus
ibus-libpinyin
ibus-gtk2
ibus-gtk3

# 远程工具
teamviewer
todesk

# 工具
wget
vim
nmap
zip
unzip

open-vm-tools
open-vm-tools-desktop

%end

%post
# Enable livesys services
systemctl enable livesys.service
systemctl enable livesys-late.service
systemctl enable vmtoolsd

# enable tmpfs for /tmp
systemctl enable tmp.mount

# make it so that we don't do writing to the overlay for things which
# are just tmpdirs/caches
# note https://bugzilla.redhat.com/show_bug.cgi?id=1135475
cat >> /etc/fstab << EOF
vartmp   /var/tmp    tmpfs   defaults   0  0
EOF

# work around for poor key import UI in PackageKit
rm -f /var/lib/rpm/__db*
echo "Packages within this LiveCD"
rpm -qa --qf '%{size}\t%{name}-%{version}-%{release}.%{arch}\n' |sort -rn
# Note that running rpm recreates the rpm db files which aren't needed or wanted
rm -f /var/lib/rpm/__db*

# go ahead and pre-make the man -k cache (#455968)
/usr/bin/mandb

# make sure there aren't core files lying around
rm -f /core*

# remove random seed, the newly installed instance should make it's own
rm -f /var/lib/systemd/random-seed

# convince readahead not to collect
# FIXME: for systemd

echo 'File created by kickstart. See systemd-update-done.service(8).' \
    | tee /etc/.updated >/var/.updated

# Drop the rescue kernel and initramfs, we don't need them on the live media itself.
# See bug 1317709
rm -f /boot/*-rescue*

# Disable network service here, as doing it in the services line
# fails due to RHBZ #1369794
systemctl disable network

# Remove machine-id on pre generated images
rm -f /etc/machine-id
touch /etc/machine-id

%end

%post

mkdir -p /etc/xdg/autostart/
cat > /etc/xdg/autostart/ibus.desktop <<EOF
[Desktop Entry]
Name=Input Method Daemon
Comment=Start IBus daemon
Exec=/usr/bin/ibus-daemon --daemonize --xim
Terminal=false
Type=Application
Categories=System;Utility;
EOF


cat >> /etc/environment << EOF
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export IM_CONFIG_PHASE=2
EOF

fc-cache -fv

# xfce configuration

# create /etc/sysconfig/desktop (needed for installation)

cat > /etc/sysconfig/desktop <<EOF
PREFERRED=/usr/bin/startxfce4
DISPLAYMANAGER=/usr/sbin/lightdm
EOF

# set livesys session type
sed -i 's/^livesys_session=.*/livesys_session="xfce"/' /etc/sysconfig/livesys


systemctl enable teamviewerd.service
systemctl start teamviewerd.service
systemctl enable todeskd.service

%end

build-rocky9-livecd.sh

#!/bin/bash

# 定义一些变量,方便修改和管理
BASE_DIR="/home/${user}/rocky9-livecd"
RESULTS_DIR="results"
ISO_BUILD_DIR="iso-build" # 新的ISO存放目录
KS_FILE="kickstarts/my.ks"
ISO_NAME="Rocky-9-Custom-LiveCD-LocalRepo-x86_64.iso"
PROJECT_NAME="Rocky Linux 9 Custom LiveCD (Local Repo)"
RELEASE_VER="9"
VOL_ID="Rocky_9_LiveCD_Local"
BOOT_ISO="/home/${user}/rocky9-livecd/Rocky-9.7-x86_64-boot.iso"

# 切换到工作目录
cd "${BASE_DIR}" || { echo "错误:无法切换到目录 ${BASE_DIR}"; exit 1; }

# 清理旧的 results 目录
echo "清理旧的 ${RESULTS_DIR} 目录..."
rm -rf "${RESULTS_DIR}"

# 确保 iso-build 目录存在
echo "检查并创建 ${ISO_BUILD_DIR} 目录..."
mkdir -p "${ISO_BUILD_DIR}" || { echo "错误:无法创建目录 ${ISO_BUILD_DIR}"; exit 1; }

echo "开始执行 livemedia-creator..."
sudo livemedia-creator \
    --ks "${KS_FILE}" \
    --resultdir "${RESULTS_DIR}" \
    --tmp tmp \
    --make-iso \
    --iso-name "${ISO_NAME}" \
    --project "${PROJECT_NAME}" \
    --releasever "${RELEASE_VER}" \
    --volid "${VOL_ID}" \
    --iso "${BOOT_ISO}"

# 检查 livemedia-creator 是否成功执行
if [ $? -eq 0 ]; then
    echo "livemedia-creator 执行成功。"

    # 获取当前时间,格式为 年-月-日-时-分
    TIMESTAMP=$(date +"%Y-%m-%d-%H-%M")
    NEW_RESULTS_DIR_NAME="${RESULTS_DIR}-${TIMESTAMP}"

    echo "将 ${RESULTS_DIR} 移动并重命名为 ${ISO_BUILD_DIR}/${NEW_RESULTS_DIR_NAME}..."
    mv "${RESULTS_DIR}" "${ISO_BUILD_DIR}/${NEW_RESULTS_DIR_NAME}"

    if [ $? -eq 0 ]; then
        echo "操作完成。新的结果目录位于:${ISO_BUILD_DIR}/${NEW_RESULTS_DIR_NAME}"
        echo "生成的 ISO 文件通常在 ${ISO_BUILD_DIR}/${NEW_RESULTS_DIR_NAME}/images 目录下。"
    else
        echo "错误:移动或重命名 ${RESULTS_DIR} 失败。"
        exit 1
    fi
else
    echo "错误:livemedia-creator 执行失败。"
    exit 1
fi

echo "脚本执行完毕。"

运行脚本,等待完成

sh build-rocky9-livecd.sh

自定义系统或者软件配置

如此便制作成功,如需添加软件包,可以在ks文件%packages块中添加到自己需要的软件包

如有配置文件文件需要添加,或者某些配置需要配置

可将配置打成rpm包,添加到%packages块中,这样比较方便,灵活

或者在%post块中,添加shell命令,进行配置

 

 


目录