Ubuntu 环境下使用 Cobbler 定制镜像的完整流程
一 核心思路与准备
- 定制目标通常包括:定制安装源(ISO 或网络镜像)、定制无人值守应答文件(Ubuntu 使用 preseed.cfg)、定制内核启动参数(kopts)、定制 PXE 菜单与自动化策略。
- 关键组件与目录:
- 组件:cobbler/cobbler-web、dhcp/tftp(xinetd)、httpd、debmirror、pykickstart。
- 目录:/var/lib/cobbler/kickstarts(存放 preseed)、/var/www/cobbler/ks_mirror(导入的镜像)、/var/lib/cobbler/loaders(引导程序,如 pxelinux.0)。
二 导入 Ubuntu 镜像
- 方式一(ISO 快速导入)
- 挂载 ISO
- mount -t iso9660 -o loop,ro /path/ubuntu-20.04-server-amd64.iso /mnt/iso
- 导入到 Cobbler(自动创建 Distro/Profile)
- cobbler import --name ubuntu-20.04 --arch x86_64 --path /mnt/iso
- 校验
- cobbler distro list
- cobbler profile list
- 方式二(debmirror 网络镜像,适合多机复用与更新)
- 安装与配置 debmirror(Ubuntu 需配置 /etc/debmirror.conf,注释掉 @dists/@arches 限制)
- 同步镜像
- debmirror -a amd64 --method http --dist focal --section main,restricted,universe,multiverse --host archive.ubuntu.com /var/www/cobbler/ubuntu-focal
- 导入生成的本地镜像目录
- cobbler import --path /var/www/cobbler/ubuntu-focal --name ubuntu-20.04
- 导入后,镜像内容位于 /var/www/cobbler/ks_mirror/,引导文件由 Cobbler 通过 TFTP 分发。
三 定制无人值守安装 Preseed
- 准备 preseed(示例路径:/var/lib/cobbler/kickstarts/ubuntu-20.04.seed)
- 基础区域与键盘
- d-i debian-installer/locale string en_US.UTF-8
- d-i console-setup/ask_detect boolean false
- d-i console-setup/layoutcode string us
- 网络(可按需改为静态)
- d-i netcfg/choose_interface select auto
- d-i netcfg/dhcp_timeout string 120
- 镜像源(使用 Cobbler 内置链接,避免硬编码外部源)
- d-i mirror/country string manual
- d-i mirror/http/hostname string $http_server
- d-i mirror/http/directory string /cobbler/ks_mirror/ubuntu-20.04
- d-i mirror/http/proxy string
- 分区示例(全盘 ext4 + 交换分区)
- d-i partman-auto/method string regular
- d-i partman-auto/disk string /dev/sda
- d-i partman-auto/expert_recipe string
boot-root ::
100000 80 100000 ext4
$bootable{ } $primary{ }
method{ format } format{ }
use_filesystem{ } filesystem{ ext4 }
mountpoint{ / }
.
102 60 1000 linux-swap
$primary{ }
method{ swap } format{ }
.
- d-i partman-partitioning/confirm_write_new_label boolean true
- d-i partman/choose_partition select finish
- d-i partman/confirm boolean true
- 账户与引导
- d-i passwd/root-login boolean true
- d-i passwd/root-password-crypted password $1$$ # 用 openssl passwd -1 生成
- d-i passwd/user-fullname string Ubuntu User
- d-i passwd/username string ubuntu
- d-i passwd/user-password-crypted password $1$$
- d-i user-setup/allow-password-weak boolean true
- d-i user-setup/encrypt-home boolean false
- d-i finish-install/reboot_in_progress note
- 将 preseed 关联到 Profile
- 查看自动生成的 profile 名:cobbler profile list
- 关联应答文件:cobbler profile edit --name ubuntu-20.04-x86_64 --kickstart /var/lib/cobbler/kickstarts/ubuntu-20.04.seed
- 说明
- Ubuntu 使用 preseed(而非 kickstart);Cobbler 的 kickstart 字段在 Ubuntu 场景下实际承载 preseed 路径。
- 若使用 ISO 导入,Cobbler 会自动创建 profile;如需完全自定义,可先删除自动 profile 后自建。
四 定制内核参数与 PXE 菜单
- 设置内核参数(示例:禁用自动分区、指定安装源)
- cobbler profile edit --name ubuntu-20.04-x86_64
–kopts “auto-install/enable=true priority=critical url=http://$http_server/cblr/svc/op/ks/profile/ubuntu-20.04-x86_64 netcfg/choose_interface=auto”
- 调整 PXE 菜单超时与默认项(可选)
- 编辑 /var/lib/tftpboot/pxelinux.cfg/default,设置 TIMEOUT/ONTIMEOUT,或调整默认 label。
- 同步到服务
- 说明
- 通过 --kopts 可注入安装器参数;PXE 菜单由 Cobbler 自动生成,必要时再微调。
五 网络与常见排错要点
- 基础配置
- /etc/cobbler/settings:设置 server(Cobbler 地址)、next_server(TFTP 地址)、如需由 Cobbler 托管 DHCP 则 manage_dhcp=1;设置 default_password_crypted(openssl passwd -1 生成)。
- DHCP 模板:编辑 /etc/cobbler/dhcp.template,配置 subnet、range、router、dns,filename “pxelinux.0”、next-server $next_server。
- TFTP:编辑 /etc/xinetd.d/tftp,将 disable 改为 no;必要时执行 cobbler get-loaders 拉取引导程序。
- 启动服务:systemctl enable --now httpd cobblerd;修改 DHCP 模板后执行 cobbler sync 使配置生效。
- 常见报错与处理
- “Some network boot-loaders are missing …”:执行 cobbler get-loaders 或安装/更新 syslinux 包。
- 导入 ISO 后找不到内核/initrd:检查 /var/www/cobbler/ks_mirror//images/ 是否包含 linux/initrd.gz;必要时重新导入或检查 ISO 完整性。
- 客户端拿不到 IP 或找不到启动文件:核对 DHCP 的 next-server 与 filename,确认 cobbler sync 已执行且 TFTP 正常。