批量安装操作系统Cobbler

captains 2026-01-06 AM 109℃ 0条

批量安装操作系统Cobbler

1、概述

  • 基于Cobbler批量安装操作系统
  • PXE

2、环境

  • 服务器环境---用于部署cobbler

    • 操作系统-redhat7.x
    • 配置2C/4G/50G
    • 192.168.8.83

3、cobbler集成的服务

  • PXE服务支持
  • DHCP服务管理
  • DNS 服务管理
  • 电源管理
  • Kickstart服务支持
  • YUM仓库管理
  • TFTP(PXE启动时需要)
  • Apache

4、cobbler命令

cobbler check       //核对当前设置是否有问题
cobbler list        //列出所有的cobbler元素
cobbler report      //列出元素的详细信息
cobbler sync        //同步配置到数据目录,更改配置最好都要执行下
cobbler reposync    //同步yum仓库
cobbler distro      //查看导入的发行版系统信息
cobbler system      //查看添加的系统信息
cobbler profile     //查看配置信息

5、cobbler服务部署

5.1、配置yum源

# 配置epel源
[root@localhost yum.repos.d]# cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7


[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1


[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1


# 配置epel-testing
[root@localhost yum.repos.d]# cat /etc/yum.repos.d/epel-testing.repo 
[epel-testing]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7


[epel-testing-debuginfo]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-debug-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1


[epel-testing-source]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/testing/7/SRPMS
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-source-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1


# 配置基础yum源
[root@localhost yum.repos.d]# cat /etc/yum.repos.d/online.repo 
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
baseurl=http://vault.centos.org/7.9.2009/os/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://vault.centos.org/7.9.2009/updates/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://vault.centos.org/7.9.2009/extras/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://vault.centos.org/7.9.2009/centosplus/$basearch/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
baseurl=http://vault.centos.org/7.9.2009/contrib/$basearch/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


# 更新yum源
yum clean all && yum makecache

5.2、安装软件

  • 软件安装
yum -y install httpd dhcp tftp python-ctypes cobbler  xinetd cobbler-web pykickstart
  • 设置开机自启
systemctl enable --now httpd cobblerd
  • 修改server的ip、tftp的地址为本机ip
[root@localhost ~]# vim /etc/cobbler/settings 
[root@localhost ~]# 
[root@localhost ~]# sed -i 's/^server: 127.0.0.1/server: 192.168.8.83/' /etc/cobbler/settings
[root@localhost ~]# sed -i 's/^next_server: 127.0.0.1/next_server:  192.168.8.83/' /etc/cobbler/settings
  • 下载文件
[root@localhost ~]# cobbler sync
task started: 2025-12-08_214045_sync
task started (id=Sync, time=Mon Dec  8 21:40:45 2025)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/rhel-7-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/rhel-7-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
copying distros to tftpboot
copying files for distro: rhel-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/rhel-7/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/rhel-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/rhel-7/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/rhel-7-x86_64/initrd.img
copying images
generating PXE configuration files
generating PXE menu structure
copying files for distro: rhel-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/rhel-7/images/pxeboot/vmlinuz -> /var/www/cobbler/images/rhel-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/rhel-7/images/pxeboot/initrd.img -> /var/www/cobbler/images/rhel-7-x86_64/initrd.img
Writing template files for rhel-7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: rhel-7-x86_64
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout: 
received on stderr: 
running: service dhcpd restart
received on stdout: 
received on stderr: Redirecting to /bin/systemctl restart dhcpd.service

running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.manage_genders
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
  • 启动rsync并设置开机自启
systemctl enable --now rsyncd
systemctl restart xinetd cobblerd httpd
  • 生层随机密码
 [root@localhost ~]#  openssl passwd -1 -salt "$RANDOM" '123456'
$1$4083$9jFt/kdeFNmOe4QlSuV4D.
  • 将新生成的加密密码加入到配置文件
[root@localhost ~]# vim /etc/cobbler/settings
....    //此处为省略内容
default_password_crypted: "$1$4083$9jFt/kdeFNmOe4QlSuV4D."
.....   //此处为省略内容
  • 重启cobbler
systemctl restart cobblerd
  • 检查配置文件
cobbler check
  • 修改cobbler配置文件,让cobbler控制dhcp
# 修改cobbler配置文件
vim /etc/cobbler/settings
allow_dynamic_settings: 1
manage_dhcp: 1

# 重启
systemctl restart cobbled

# 编辑
cobbler setting edit --name=manage_dhcp --value=1
cobbler sync

# 重启
systemctl restart cobbled

# 修改dhcp, 重点是subnet
[root@localhost ~]# cat /etc/cobbler/dhcp.template 
# ******************************************************************
# Cobbler managed dhcpd.conf file
#
# generated from cobbler dhcp.conf template ($date)
# 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 192.168.8.0 netmask 255.255.255.0 {
    option routers 192.168.8.2;
    option domain-name-servers 114.114.114.114;
    option subnet-mask 255.255.255.0;

    range dynamic-bootp 192.168.8.90 192.168.8.100;
    default-lease-time 21600;
    max-lease-time 43200;

    next-server $next_server;

    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 if option pxe-system-type = 00:09 {
            filename "grub/grub-x86_64.efi";
        } else {
            filename "pxelinux.0";
        }
    }
}

#for dhcp_tag in $dhcp_tags.keys():
    ## group could be subnet if your dhcp tags line up with your subnets
    ## or really any valid dhcpd.conf construct ... if you only use the
    ## default dhcp tag in cobbler, the group block can be deleted for a
    ## flat configuration
# group for Cobbler DHCP tag: $dhcp_tag
group {
        #for mac in $dhcp_tags[$dhcp_tag].keys():
            #set iface = $dhcp_tags[$dhcp_tag][$mac]
    host $iface.name {
        #if $iface.interface_type == "infiniband":
        option dhcp-client-identifier = $mac;
        #else
        hardware ethernet $mac;
        #end if
        #if $iface.ip_address:
        fixed-address $iface.ip_address;
        #end if
        #if $iface.hostname:
        option host-name "$iface.hostname";
        #end if
        #if $iface.netmask:
        option subnet-mask $iface.netmask;
        #end if
        #if $iface.gateway:
        option routers $iface.gateway;
        #end if
        #if $iface.enable_gpxe:
        if exists user-class and option user-class = "gPXE" {
            filename "http://$cobbler_server/cblr/svc/op/gpxe/system/$iface.owner";
        } else if exists user-class and option user-class = "iPXE" {
            filename "http://$cobbler_server/cblr/svc/op/gpxe/system/$iface.owner";
        } else {
            filename "undionly.kpxe";
        }
        #else
        filename "$iface.filename";
        #end if
        ## Cobbler defaults to $next_server, but some users
        ## may like to use $iface.system.server for proxied setups
        next-server $next_server;
        ## next-server $iface.next_server;
    }
        #end for
}
#end for
  • 重启并同步配置
systemctl restart cobblerd
cobbler sync

5.3、导入ISO镜像

# 挂载镜像
mount /usr/local/src/rhel-server-7.9-x86_64-dvd.iso /mnt/cdrom


# 导入到cobbler
cobbler import --path=/mnt/cdrom --name=rhel-7 arch=x86_64

5.4、检查镜像列表

[root@localhost ~]# cobbler list
distros:
   rhel-7-x86_64


profiles:
   rhel-7-x86_64


systems:


repos:


images:


mgmtclasses:


packages:


files:

5.5、创建kickstarts自动安装脚本

[root@localhost ~]# cat /var/lib/cobbler/kickstarts/rhel-7-x86_64.ks 
auth --enableshadow --passalgo=sha512
bootloader --location=mbr
clearpart --all --initlabel
part /boot --asprimary --fstype="ext4" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="ext4" --grow --size=1
text
firewall --disabled
firstboot --disable
keyboard us
lang en_US
url --url=http://192.168.8.83/cobbler/ks_mirror/rhel-7
$yum_repo_stanza
reboot


rootpw --iscrypted $1$30527$GZ1JrHlLXJvhQQBxufbp.0


selinux --disabled
skipx
timezone Asia/Shanghai --isUtc --nontp
install
zerombr


%packages
@base
@core
kexec-tools


%end


%addon com_redhat_kdump --enable --reserve-mb='auto'


%end


%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

5.6、检查配置

[root@localhost ~]# cobbler validateks
task started: 2025-12-08_220619_validateks
task started (id=Kickstart Validation, time=Mon Dec  8 22:06:19 2025)
----------------------------
osversion: rhel7
checking url: http://192.168.8.83/cblr/svc/op/ks/profile/rhel-7-x86_64
running: /usr/bin/ksvalidator -v "rhel7" "http://192.168.8.83/cblr/svc/op/ks/profile/rhel-7-x86_64"
received on stdout: 
received on stderr: 
Potential templating errors:
Unknown variable found at line 16, column 28: '$GZ1JrHlLXJvhQQBxufbp'
*** all kickstarts seem to be ok ***
*** TASK COMPLETE ***

5.7、关联配置

# 关联
cobbler profile edit --name rhel-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/rhel-7-x86_64.ks


# 修改网卡名字
cobbler profile edit --name rhel-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'


# 检查信息
[root@localhost ~]# cobbler profile report
Name                           : rhel-7-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : rhel-7-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {'biosdevname': '0', 'net.ifnames': '0'}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/rhel-7-x86_64.ks
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

5.8、查看当前cobbler有哪些配置文件

[root@localhost ~]# cobbler profile list
   rhel-7-x86_64

5.9、修改profile,将我们新建的ks文件设为默认的kickstarts安装文件

[root@localhost ~]# cobbler profile edit --name rhel-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/rhel-7-x86_64.ks

5.10、配置网卡名称为传统网卡名称eth0

[root@localhost ~]# cobbler profile edit --name rhel-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

5.11、检查当前系统cobbler配置文件信息

[root@localhost ~]# cobbler profile report
Name                           : rhel-7-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : rhel-7-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {'biosdevname': '0', 'net.ifnames': '0'}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/rhel-7-x86_64.ks
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

5.12、同步配置

cobbler sync

5.13、重启服务端

systemctl restart xinetd cobblerd httpd

5.14、至此完成

标签: none

非特殊说明,本博所有文章均为博主原创。

评论已关闭