Cobbler 3.3.4 Docker 部署

一、背景介绍

Cobbler 是一个 Linux 安装服务器,可以快速设置网络安装环境。Ubuntu 22.04 可以使用新版的 Cobbler 3.3.4 来安装了!

二、生成安装包

1. 拉取git项目,并生成rpm或其他的安装包,此处生成的rpm包,然后将 rpm 安装在 rockylinux/rockylinux:8 docker 中。

2. 项目地址: https://github.com/cobbler/cobbler, 版本: https://github.com/cobbler/cobbler/releases/tag/v3.3.4

3. 生成安装包

wget https://github.com/cobbler/cobbler/archive/refs/tags/v3.3.4.tar.gz
tar xf v3.3.4.tar.gz
cd cobbler-3.3.4
bash docker/rpms/build-and-install-rpms.sh 3.3.4 docker/rpms/Rocky_Linux_8/Rocky_Linux_8.dockerfile
# 在 rpm-build 目录下会生成 rpm 包
root@172-19-13-222:~/cobbler-3.3.4/rpm-build# ls
BUILDROOT      cobbler-3.3.4-1.el8.noarch.rpm  cobbler-3.3.4.tar.gz                  cobbler-tests-containers-3.3.4-1.el8.noarch.rpm
cobbler-3.3.4  cobbler-3.3.4-1.el8.src.rpm     cobbler-tests-3.3.4-1.el8.noarch.rpm
# 最后会使用 cobbler-3.3.4-1.el8.noarch.rpm 

三、Cobbler 集成安装

1. Dockerfile

FROM rockylinux/rockylinux:8

ENV COBBLER_RPM cobbler-3.3.4-1.el8.noarch.rpm
ENV DATA_VOLUMES "/var/lib/cobbler /var/www/cobbler /var/lib/dhcpd"

RUN (cd /lib/systemd/system/sysinit.target.wants/; \
  for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
  rm -f /lib/systemd/system/multi-user.target.wants/*;\
  rm -f /etc/systemd/system/*.wants/*;\
  rm -f /lib/systemd/system/local-fs.target.wants/*; \
  rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
  rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
  rm -f /lib/systemd/system/basic.target.wants/*;\
  rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]

COPY $COBBLER_RPM /$COBBLER_RPM
RUN set -ex \
  && dnf install -y epel-release \
  && dnf install -y /$COBBLER_RPM \
  && dnf install -y dhcp-server pykickstart yum-utils debmirror git rsync-daemon wget xinetd net-tools \
          ipxe-bootimgs shim grub2-efi-x64-modules \
  # Fix the permission of shim-x64
  && chmod a+r -R /boot/efi/EFI \
  && dnf clean all \
  # fix debian repo support
  && sed -i "s/^@dists=/# @dists=/g" /etc/debmirror.conf \
  && sed -i "s/^@arches=/# @arches=/g" /etc/debmirror.conf

RUN echo 'ServerName localhost:80' >> /etc/httpd/conf/httpd.conf \
    && rm -rf /run/httpd && mkdir -p /run/httpd

# use xinetd to set tftp
COPY tftp /etc/xinetd.d/tftp
COPY dhcpd.conf /etc/dhcp/dhcpd.conf

# systemctl3.py 用来解决systemctl 启动失败的问题
RUN wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl3.py -O /usr/local/bin/systemctl \
&& chmod +x /usr/local/bin/systemctl
RUN systemctl enable cobblerd httpd dhcpd rsyncd xinetd 
# DHCP Server
EXPOSE 67
# TFTP
EXPOSE 69
# Rsync
EXPOSE 873
# Web
EXPOSE 80
# Cobbler
EXPOSE 25151
VOLUME ["/var/lib/cobbler", "/var/www/cobbler", "/var/lib/dhcpd"]
COPY entrypoint.sh /entrypoint.sh CMD ["/entrypoint.sh"]

2. dhcpd.conf

# ******************************************************************
# Cobbler managed dhcpd.conf file
# generated from cobbler dhcp.conf template (Mon Nov  6 06:06:34 2023)
# Do NOT make changes to /etc/dhcpd.conf. Instead, make your changes
# in /etc/cobbler/dhcp.template, as /etc/dhcpd.conf will be
# overwritten.
# ******************************************************************

ddns-update-style interim;

allow booting;
allow bootp;

ignore client-updates;
set vendorclass = option vendor-class-identifier;

option pxe-system-type code 93 = unsigned integer 16;

subnet 172.19.3.0 netmask 255.255.255.0 {
     option routers             172.19.3.254;
     option domain-name-servers 8.8.8.8;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        172.19.3.220 172.19.3.235;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                127.0.0.1;
     class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          if option pxe-system-type = 00:02 {
                  filename "ia64/elilo.efi";
          } else if option pxe-system-type = 00:06 {
                  filename "grub/grub-x86.efi";
          } else if option pxe-system-type = 00:07 {
                  filename "grub/grub-x86_64.efi";
          } else {
                  filename "pxelinux.0";
          }
     }

}



# group for Cobbler DHCP tag: default
group {
}

3. entrypoint.sh

#!/bin/bash

set -e

#
# Init configuration
#

SERVER="${SERVER:-${SERVER_IP_V4}}"
if [ -z "$SERVER" ]; then
    echo "env 'SERVER' is required."
    exit 1
fi
if [ -z "$SERVER_IP_V4" ] && [ -z "$SERVER_IP_V6" ]; then
    echo "env ['SERVER_IP_V4', 'SERVER_IP_V6'] require at least one."
    exit 1
fi
if [ -z "$ROOT_PASSWORD" ]; then
    echo "env 'ROOT_PASSWORD' is required."
    exit 1
fi

# set server config
sed -i "s/^server: 127.0.0.1/server: $SERVER/g" /etc/cobbler/settings.yaml
if [ -n "${SERVER_IP_V4}" ]; then
    sed -i "s/^next_server_v4: 127.0.0.1/next_server_v4: $SERVER_IP_V4/g" /etc/cobbler/settings.yaml
    sed -i "s/127.0.0.1/$SERVER_IP_V4/g" /etc/dhcp/dhcpd.conf
fi
if [ -n "${SERVER_IP_V6}" ]; then
    sed -i "s/^next_server_v6: ::1/next_server_v6: $SERVER_IP_V6/g" /etc/cobbler/settings.yaml
fi
CRYPTED_PASSWORD=$(openssl passwd -1 "$ROOT_PASSWORD")
sed -i "s#^default_password.*#default_password_crypted: \"$CRYPTED_PASSWORD\"#g" /etc/cobbler/settings.yaml
# set menu title
if [ -n "${MENU_TITLE}" ]; then
    sed -ri "s#^MENU TITLE Cobbler.+#MENU TITLE $MENU_TITLE#g" /etc/cobbler/boot_loader_conf/pxe_menu.template
    sed -ri "s#^MENU TITLE Cobbler.+#MENU TITLE $MENU_TITLE#g" /etc/cobbler/iso/buildiso.template
fi

#
# Init data volumes
#

for v in $DATA_VOLUMES; do
    # shellcheck disable=SC2086
    if [ -z "$(ls -A $v)" ]; then
        mv ${v}.save/* $v
    fi
done

#
# Boot cobbler
#

systemctl start cobblerd httpd dhcpd rsyncd xinetd

(
    sleep 6
    cobbler sync
    tail -n +1 -f /var/log/cobbler/cobbler.log
) &
exec /usr/sbin/init

4. tftp 使用

service tftp
{
        socket_type = dgram
        protocol    = udp
        wait        = yes
        user        = root
        server      = /usr/sbin/in.tftpd
        server_args = -s /var/lib/tftpboot
        disable     = no
        per_source  = 11
        cps         = 100 2
        flags       = IPv4
}

四、Cobbler docker build

docker build -t test/cobbler:3.3.4 .

  

  

  

  

 

 
posted @ 2024-03-01 15:55  cptao  阅读(615)  评论(0编辑  收藏  举报