Ubuntu Cobbler如何定制化脚本

Ubuntu Cobbler定制化脚本实践一 方案总览在 Ubuntu 场景下,Cobbler 的定制化通常通过三类“脚本/模板”实现:Kickstart(preseed/seed):用于安装阶段的无人值守配置;Cloud-init 用户数据(autoinstall):Ubuntu 20.04+ 推荐的无盘/云镜像自动化配置;Cobbler 片段 Snippets 与本地 APT 镜像脚本:

Ubuntu Cobbler定制化脚本实践

一 方案总览

  • Ubuntu 场景下,Cobbler 的定制化通常通过三类“脚本/模板”实现:
    1. Kickstart(preseed/seed):用于安装阶段的无人值守配置;
    2. Cloud-init 用户数据(autoinstall):Ubuntu 20.04+ 推荐的无盘/云镜像自动化配置;
    3. Cobbler 片段 Snippets 与本地 APT 镜像脚本:用于复用配置与离线源加速。下文给出可直接落地的配置方法与示例。

二 使用 Kickstart 定制安装脚本

  • 基本步骤
    1. 准备应答文件(Kickstart/Preseed):可基于 /var/lib/cobbler/kickstarts/ 下的示例创建,例如 sample.seed
    2. 导入镜像并创建 profile:指定应答文件;
    3. 创建 system(可选,按 MAC 精确绑定);
    4. 同步配置并启动安装。
  • 示例命令
    • 导入镜像:sudo cobbler import –path /path/to/ubuntu.iso –name ubuntu-22.04
    • 添加 profile:sudo cobbler profile add –name ubuntu-22.04-custom –kickstart /var/lib/cobbler/kickstarts/my-ubuntu.seed
    • 添加 system(按 MAC):sudo cobbler system add –name host01 –profile ubuntu-22.04-custom –interface eth0 –mac AA:BB:CC:DD:EE:FF
    • 同步:sudo cobbler sync
  • 应答文件要点(示例片段)
    • 语言与键盘:d-i debian-installer/locale string en_US;d-i console-setup/ask_detect boolean false
    • 网络(DHCP 示例):d-i netcfg/choose_interface select auto;d-i netcfg/get_nameservers string 114.114.114.114
    • 镜像源:d-i mirror/http/hostname string $http_server;d-i mirror/http/directory string /cblr/links/$distro
    • 用户与认证:d-i passwd/root-login boolean true;d-i passwd/root-password-crypted password $6$…;d-i passwd/username string ubuntu;d-i passwd/user-password-crypted password $6$…
    • 分区(示例):d-i partman-auto/method string regular;d-i partman-auto/choose_recipe select atomic
    • 包与升级:d-i pkgsel/include string openssh-server vim;d-i pkgsel/upgrade select none
    • 引导与完成:d-i grub-installer/bootdev string /dev/sda;d-i finish-install/reboot_in_progress note
      说明:Kickstart 中可使用 Cobbler 变量(如 $http_server、$distro)与片段(如 $SNIPPET(‘network_config’))复用网络等通用配置。

三 使用 Cloud-init 定制自动化脚本(Ubuntu 20.04+)

  • 适用场景:Subiquity/云镜像自动化安装;通过 user-data(autoinstall) 定义软件包、用户、网络、分区与安装后命令。
  • 基本步骤
    1. /var/lib/cobbler/templates/ 放置 cloud-init_user-data;在 /var/lib/cobbler/snippets/ 放置可复用片段(如 cloud-init_hostname、cloud-init_network);
    2. 在 profile 或 system 上指定内核参数 autoinstall 并指向上述模板;
    3. 同步并 PXE 启动。
  • 示例模板片段
    • /var/lib/cobbler/templates/cloud-init_user-data
      • #cloud-config
        autoinstall:
        version: 1
        apt:
        preserve_sources_list: true
        primary:
        – arches: [amd64]
        uri: http://$http_server/cblr/links/$distro
        fallback: offline-install
        identity:
        hostname: $SNIPPET(‘cloud-init_hostname’)
        password: $6$QPtT0fEvjBSm7htF$…
        realname: ubuntu
        username: ubuntu
        keyboard: {layout: us}
        locale: en_US.UTF-8
        $SNIPPET(‘cloud-init_network’)
        ssh: {allow-pw: true, install-server: true}
        storage: {layout: {name: lvm, sizing-policy: all}}
        package_update: false
        package_upgrade: false
        late-commands:
        – wget -O /target/tmp/autoinstall-user-data.yaml http://$http_server/cblr/svc/op/autoinstall/$what/$name
        – chroot /target /bin/bash -s <<‘EOF’
        ssh-keygen -t rsa -b 2048 -m ssh2 -N “” -f /root/.ssh/id_rsa
        EOF
    • /var/lib/cobbler/snippets/cloud-init_hostname
      • #if $getVar(“system_name”,“”) != “”
        • #if $hostname != “”
          hostname: $hostname
        • #else
          • #set $myhostname = $getVar(‘name’,‘’).replace(“_”,“-”)
            hostname: $myhostname
        • #end if
          #else
        • #set $myhostname = $getVar(‘hostname’,$getVar(‘name’,‘cobbler’)).replace(“_”,“-”)
          hostname: $myhostname
          #end if
    • 提示:Cobbler 3.3.7 默认不附带 cloud-init 示例模板,需按上例自建模板与片段,并在安装引导时启用 autoinstall

四 使用 Snippets 复用脚本与变量

  • 作用:将常用配置(如网络、分区、初始化命令)抽象为 Snippets,在 Kickstart/Cloud-init 中通过 $SNIPPET(‘name’) 引用,便于多环境复用与集中维护。
  • 示例(Kickstart 中引用网络片段)
    • %pre
      $SNIPPET(‘network_config’)
    • 也可在 %post 阶段执行自定义脚本,例如
      %post –interpreter=/bin/bash
      echo “Custom script running…” > /root/custom.log
      apt-get update && apt-get install -y htop
  • 变量:可使用 $http_server、$distro、$name、$hostname、$interfaces 等 Cobbler 内置变量,实现按 profile/system 差异化渲染。

五 离线环境 APT 镜像与脚本化更新

  • 场景:内网无公网时,用 debmirrorUbuntu 官方源镜像到本地,并通过 Cobbler 提供 HTTP 访问,供安装与初始化阶段使用。
  • 示例脚本(镜像 Ubuntu 20.04 主仓库)
    • nohup /usr/bin/debmirror \
      –progress –nocleanup –ignore-release-gpg -e rsync \
      –host mirrors.tuna.tsinghua.edu.cn –root /ubuntu \
      –dist focal,focal-backports,focal-security,focal-updates \
      –section main,restricted,universe,multiverse \
      /mirror/ubuntu/ubuntu20 –nosource -a amd64 \

      /mirror/ubuntu/ubuntu20.log 2>&1 &

  • 提示:
    • 将镜像目录(如 /mirror/ubuntu)通过 Cobbler Web/Apache 发布;
    • 在 Kickstart/Cloud-init 的 apt 源中,将 uri 指向 http://$http_server/ubuntu
    • 定期执行镜像脚本(如 cron)以同步更新。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1486747.html

(0)
派派
上一篇 2026-01-07
下一篇 2026-01-07

发表回复

登录后才能评论