Cobbler批量安装操作系统配置实战

Cobbler 批量安装操作系统 (基于Centos7.x )

要想批量部署操作系统,得具备以下条件:

客户机支持 pxe 网络引导

服务器端和客户端建立网络通信(DHCP)

服务器端要有可供客户机开机引导的引导文件

服务器端的可引导文件还必须能传递到客户机(TFTP)

客户机无人值守安装包括安装定制的软件或服务(KICKSTART)文件

1、认识 PXE

预启动执行环境(Preboot eXecution Environment,PXE,也被称为预执行环境)提供了一种使用网络接口(Network Interface)启动计算机的机制。这种机制让计算机的启动可以不依赖本地数据存储设备(如硬盘)或本地已安装的操作系统

PXE:支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicasttrivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成Client端自动化基本软件设置,从而引导预先安装在服务器中的终端操作系统。PXE可以引导多种操作系统,如:windows95/98/2000/windows2003/windows2008/winXP/win7/win8,linux等。

PXE是由Intel设计的协议,它可以使计算机通过网络启动。协议分为client和server两端,PXE client在网卡的ROM中,当计算机引导时,BIOS把PXE client调入内存执行,并显示出命令菜单,经用户选择后,PXE client将放置在远端的操作系统通过网络下载到本地运行,无盘工作站就是通过PXE来进行启动的。PXE协议的成功运行需要解决以下两个问题:IP地址的分配,下载Linux内核和根文件系统。

IP地址的分配可以用DHCP server来给PXE client分配一个IP地址,DHCP Server是用来给DHCP Client动态分配IP地址的协议,不过由于这里是给PXE Client分配IP地址,所以在配置DHCP Server时,需要增加相应的PXE特有配置。

下载Linux内核和根文件系统,PXE client所在的ROM中,已经存在了TFTP Client。PXE Client使用TFTP Client,通过TFTP协议到TFTP Server上下载所需的文件。

2、PXE 的工作原理

PXE client是需要安装操作系统的计算机,TFTP Server和DHCP Server运行在另外一台Server上。Bootstrap文件、配置文件、Linux内核以及Linux根文件系统都放置在Linux Server上TFTP服务器的根目录下。PXE client在工作过程中,需要三个二进制文件:bootstrap、Linux 内核和Linux根文件系统。Bootstrap文件是可执行程序,它向用户提供简单的控制界面,并根据用户的选择,下载合适的Linux内核以及 Linux根文件系统。下图就是PXE工作的原理图:

在这里插入图片描述

简单总结一下:

PXE Client发送广播包请求DHCP分配IP地址DHCP

Server回复请求,给出IP地址以及Boot

PXE Client从Server下载引导文件执行引导程序

3、PXE 应用

PXE最直接的表现是,在网络环境下工作站可以省去硬盘,但又不是通常所说的无盘站的概念,因为使用该技术的PC在网络方式下的运行速度要比有盘PC快3倍以上。当然使用PXE的PC也不是传统意义上的TERMINAL终端,因为使用了PXE的PC并不消耗服务器的CPU,RAM等资源,故服务器的硬件要求极低。

网络克隆 PXE 现在最为广泛的应用一个是网吧的无盘技术。在有盘领域的网络维护和安装中PXE可以是最好用的网吧系统统一安装和维护的引导技术,PXE的引导速度和稳定性都是一流的!

4、认识 Kickstart

官方文档:https://fedoraproject.org/wiki/Anaconda/Kickstart/zh-cn

KickStart 是一种无人职守安装方式,是由redhat公司为了解决无人值守安装操作系统而设计的。系统管理员通过创建一个单一的 kickstart 配置文件就可以实现自动化安装操作系统。KickStart的工作原理是通过记录典型的安装过程中所需人工干预填写的各种参数,并生成一个名为ks.cfg的文件;在其后的安装过程中(不只局限于生成KickStart安装文件的机器)当出现要求填写参数的情况时,安装程序会首先去查找KickStart生成的文件,当找到合适的参数时,就采用找到的参数,当没有找到合适的参数时,才需要安装者手工干预。这样,如果KickStart文件涵盖了安装过程中出现的所有需要填写的参数时,安装者完全可以只告诉安装程序从何处取ks.cfg文件,然后去忙自己的事情。等安装完毕,安装程序会根据ks.cfg中设置的重启选项来重启系统,并结束安装。

5、cobbler 简介

官方网站:https://cobbler.github.io/

GITHUB: https://github.com/cobbler/cobbler

Cobbler 是一个 Linux 服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。

Cobbler 可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。

Cobbler 是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。

Cobbler 内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。

Cobbler 是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows。该工具使用python开发,小巧轻便(才15k行代码),使用简单的命令即可完成PXE网络安装环境的配置,同时还可以管理DHCP,DNS,以及yum包镜像。

Cobbler是一个应用程序,可以大批量的部署操作系统,可以同时部署多个不同的操作系统,配合kickstart文件可以定制和无人值守的方式部署操作系统。它对PXE进行二次开发和封装,同时提供二次开发接口。

Cobbler 的配置结构基于一组注册的对象。每个对象表示一个与另一个实体相关联的实体(该对象指向另一个对象,或者另一个对象指向该对象)。当一个对象指向另一个对象时,它就继承了被指向对象的数据,并可覆盖或添加更多特定信息。以下对象类型的定义为:
在这里插入图片描述

发行版(Distro):

表示一个操作系统。它承载了内核和 initrd 的信息,以及内核参数等其他数据。

配置文件(Profile):

包含一个发行版、一个 kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据。

系统(System):

表示要配给的机器。它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息。

存储库(Repo):

保存一个 yum 或 rsync 存储库的镜像信息。

镜像(Image):

可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)。

基于注册的对象以及各个对象之间的关联,Cobbler 知道如何更改文件系统以反映具体配置。因为系统配置的内部是抽象的,所以您可以仅关注想要执行的操作。

6、Cobbler原理

Cobbler由Python语言开发,是对PXE和Kickstart以及DHCP的封装。融合很多特性,提供了CLI和Web的管理形式。更加方便的实行网络安装。同时,Cobbler也提供了API接口,使用其它语言也很容易做扩展。它不紧可以安装物理机,同时也支持kvm、xen虚拟化、Guest OS的安装。更多的是它还能结合Puppet等集中化管理软件,实现自动化的管理。

Cobbler 支持众多的发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu 和 SuSE。当添加一个操作系统(通常通过使用 ISO 文件)时,Cobbler 知道如何解压缩合适的文件并调整网络服务,以正确引导机器。Cobbler 使用命令行方式管理,也提供了基于Web的图形化配置管理工具(cobbler-web)。通过配置cobbler自动部署DHCP、TFTP、HTTP,在安装过程中加载kiskstart无人值守安装应答文件实现无人值守。从客户端使用PXE引导启动安装。

在这里插入图片描述

1、Server端

第一步,启动Cobbler服务

第二步,进行Cobbler错误检查,执行cobbler check命令

第三步,进行配置同步,执行cobbler sync命令

第四步,复制相关启动文件文件到TFTP目录中

第五步,启动DHCP服务,提供地址分配

第六步,DHCP服务分配IP地址

第七步,TFTP传输启动文件

第八步,Server端接收安装信息

第九步,Server端发送ISO镜像与Kickstart文件

2、Client端

第一步,客户端以PXE模式启动

第二步,客户端获取IP地址

第三步,通过TFTP服务器获取启动文件

第四步,进入Cobbler安装选择界面

第五步,客户端确定加载信息

第六步,根据配置信息准备安装系统

第七步,加载Kickstart文件

第八步,传输系统安装的其它文件

第九步,进行安装系统

3、cobbler集成的服务

PXE服务支持

DHCP服务管理

DNS服务管理(可选bind,dnsmasq)

电源管理

Kickstart服务支持

YUM仓库管理

TFTP(PXE启动时需要)

Apache (提供kickstart的安装源,并提供定制化的kickstart配置)

7、安装 cobbler

1、环境说明

[root@Cobbler ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

[root@Cobbler ~]# uname -r
3.10.0-693.el7.x86_64

[root@Cobbler ~]# getenforce
Disabled

[root@Cobbler ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@Cobbler ~]# hostname -I
10.0.0.202 172.16.1.202

2、yum源说明

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3、使用yum安装cobbler

yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd  xinetd 

rpm -ql cobbler  # 查看安装的文件,下面列出部分。

/etc/cobbler                  # 配置文件目录
/etc/cobbler/settings         # cobbler主配置文件,这个文件是YAML格式,Cobbler是python写的程序。
/etc/cobbler/dhcp.template    # DHCP服务的配置模板
/etc/cobbler/tftpd.template   # tftp服务的配置模板
/etc/cobbler/rsync.template   # rsync服务的配置模板
/etc/cobbler/iso              # iso模板配置文件目录
/etc/cobbler/pxe              # pxe模板文件目录
/etc/cobbler/power            # 电源的配置文件目录
/etc/cobbler/users.conf       # Web服务授权配置文件
/etc/cobbler/users.digest     # 用于web访问的用户名密码配置文件
/etc/cobbler/dnsmasq.template # DNS服务的配置模板
/etc/cobbler/modules.conf     # Cobbler模块配置文件

/var/lib/cobbler              # Cobbler数据目录
/var/lib/cobbler/config       # 配置文件
/var/lib/cobbler/kickstarts   # 默认存放kickstart文件
/var/lib/cobbler/loaders      # 存放的各种引导程序

/var/www/cobbler              # 系统安装镜像目录
/var/www/cobbler/ks_mirror    # 导入的系统镜像列表
/var/www/cobbler/images       # 导入的系统镜像启动文件
/var/www/cobbler/repo_mirror  # yum源存储目录

/var/log/cobbler              # 日志目录
/var/log/cobbler/install.log  # 客户端系统安装日志
/var/log/cobbler/cobbler.log  # cobbler日志

说明:cobbler是依赖与epel源下载

4、cobbler语法检查前先启动http与cobbler

systemctl start httpd.service
systemctl start cobblerd.service
cobbler check

5、进行语法检查

[root@Cobbler ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work.  This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.

2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.

3 : change 'disable' to 'no' in /etc/xinetd.d/tftp

4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely.  Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.

5 : enable and start rsyncd.service with systemctl

6 : debmirror package is not installed, it will be required to manage debian deployments and repositories

7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one

8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

Restart cobblerd and then run 'cobbler sync' to apply changes.

6、解决当中的报错

1、批量排查命令集
sed -i 's/server: 127.0.0.1/server: 192.168.152.174/' /etc/cobbler/settings
sed -i 's/next_server: 127.0.0.1/next_server: 192.168.152.174/' /etc/cobbler/settings
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
sed -ri "/default_password_crypted/s#(.*: ).*#\1\"`:`\"#" /etc/cobbler/settings
sed -i 's#yes#no#' /etc/xinetd.d/tftp
cobbler get-loaders

systemctl start rsyncd
systemctl enable rsyncd
systemctl enable tftp.socket
systemctl start tftp.socket
systemctl restart cobblerd.service

sed -i.ori 's#192.168.1#192.168.152#g;22d;23d' /etc/cobbler/dhcp.template

cobbler sync
2、分部排查详解
1、解决问题1、2
cp /etc/cobbler/settings{,.ori}
sed -i 's/server: 127.0.0.1/server: 172.16.1.202/' /etc/cobbler/settings
sed -i 's/next_server: 127.0.0.1/next_server: 172.16.1.202/' /etc/cobbler/settings
2、解决问题3
sed 's#yes#no#g' /etc/xinetd.d/tftp -i
3、下载包所需的软件包
# cobbler get-loaders
task started: 2019-07-24_021430_get_loaders
task started (id=Download Bootloader Content, time=Wed Jul 24 02:14:30 2019)
downloading https://cobbler.github.io/loaders/README to /var/lib/cobbler/loaders/README
downloading https://cobbler.github.io/loaders/COPYING.elilo to /var/lib/cobbler/loaders/COPYING.elilo
downloading https://cobbler.github.io/loaders/COPYING.yaboot to /var/lib/cobbler/loaders/COPYING.yaboot
downloading https://cobbler.github.io/loaders/COPYING.syslinux to /var/lib/cobbler/loaders/COPYING.syslinux
downloading https://cobbler.github.io/loaders/elilo-3.8-ia64.efi to /var/lib/cobbler/loaders/elilo-ia64.efi

downloading https://cobbler.github.io/loaders/yaboot-1.3.17 to /var/lib/cobbler/loaders/yaboot
downloading https://cobbler.github.io/loaders/pxelinux.0-3.86 to /var/lib/cobbler/loaders/pxelinux.0
downloading https://cobbler.github.io/loaders/menu.c32-3.86 to /var/lib/cobbler/loaders/menu.c32
downloading https://cobbler.github.io/loaders/grub-0.97-x86.efi to /var/lib/cobbler/loaders/grub-x86.efi
downloading https://cobbler.github.io/loaders/grub-0.97-x86_64.efi to /var/lib/cobbler/loaders/grub-x86_64.efi
*** TASK COMPLETE ***
5、启动rsync服务
# systemctl start rsyncd.service
# systemctl enable rsyncd.service
6、debian相关

安装 debmirro,如果用不到可以忽略

# yum -y install debmirror
# sed -i  's|@dists=.*|#@dists=|'  /etc/debmirror.conf
# sed -i  's|@arches=.*|#@arches=|'  /etc/debmirror.conf
7、修改安装完成后的root密码
openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'
random-phrase-here  随机字符串
your-password-here  密码

示例

1、生成加密密码
[root@Cobbler ~]# openssl passwd -1 -salt 'cobbler' '123456'
$1$CLSN$LpJk4x1cplibx3q/O4O/K/
2、设置密码

使用生成的字符串,替换default_password_crypted默认的密码

# vim /etc/cobbler/settings
default_password_crypted: "$1$cobble$K03Q.A2lkupK0pGFt6f46/"
8,解决问题8

没有用到是可以直接忽略的,但是见到提示就不爽,还是解决一下吧

#  yum -y install fence-agents
9、配置 DHCP 服务

管理dhcp 1为开启,默认为0

sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings

防止循环装系统,适用于服务器第一启动项是PXE启动。
该选项作用:

  • 防止机器循环安装配置始终从网络引导
  • 激活此选项,机器回传Cobbler安装完成
  • Cobbler将系统对象的netboot标志更改为false,强制要求机器从本地磁盘引导。
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings

修改dhcp模板

sed -i.ori 's#192.168.1#172.16.1#g;22d;23d' /etc/cobbler/dhcp.template

或者
# cp /etc/cobbler/dhcp.template{,.bak}
# vim /etc/cobbler/dhcp.template
subnet 192.168.152.0 netmask 255.255.252.0 {
     option routers             192.168.152.2;  # 网关
     option domain-name-servers 192.168.152.2;  # dns
     option subnet-mask         255.255.252.0;  # 子网
     range dynamic-bootp        192.168.152.100 192.168.152.254; # 可分配的ip范围
     default-lease-time         21600;
     max-lease-time             43200;

cobbler 组配置文件位置

/etc/cobbler/settings

注意:修改完成之后重启cobbler服务,使用cobbler sync 进行同步,否则不生效。

同步cobbler配置,会修改tftp,dhcp等服务的配置并重启,可以仔细看一下下面的输出

# systemctl restart cobblerd.service

# cobbler sync
task started: 2019-07-24_043113_sync
task started (id=Sync, time=Wed Jul 24 04:31:13 2019)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
copying: /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying images
generating PXE configuration files
generating PXE menu structure
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
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 ***

7、验证配置

1、再次检查语法
# cobbler check
No configuration problems found.  All systems go.
在有问题就把server 改成本机ip
2、重启所有服务
systemctl restart httpd.service
systemctl restart cobblerd.service
systemctl restart dhcpd.service
systemctl restart rsyncd.service
systemctl restart tftp.socket
3、开机启动
# 启动相关服务并设置开机启动(可选) 与第二种方法二选一
chkconfig httpd on
chkconfig xinetd on
chkconfig cobblerd on
chkconfig dhcpd on
/etc/init.d/httpd restart
/etc/init.d/xinetd restart
/etc/init.d/cobblerd restart
/etc/init.d/dhcpd restart
# 编写Cobbler相关服务启动脚本(可选)
cat >>/etc/init.d/cobbler<<EOF
#!/bin/bash
# chkconfig: 345 80 90
# description:cobbler

case \$1 in
  start)
    /etc/init.d/httpd start
    /etc/init.d/xinetd start
    /etc/init.d/dhcpd start
    /etc/init.d/cobblerd start
    ;;

  stop)
    /etc/init.d/httpd stop
    /etc/init.d/xinetd stop
    /etc/init.d/dhcpd stop
    /etc/init.d/cobblerd stop
    ;;

  restart)
    /etc/init.d/httpd restart
    /etc/init.d/xinetd restart
    /etc/init.d/dhcpd restart
    /etc/init.d/cobblerd restart
    ;;

  status)
    /etc/init.d/httpd status
    /etc/init.d/xinetd status
    /etc/init.d/dhcpd status
    /etc/init.d/cobblerd status
    ;;

  sync)
    cobbler sync
    ;;

  *)
    echo "Input error,please in put 'start|stop|restart|status|sync'!"
    exit 2
    ;;

esac
EOF

# chmod +x /etc/init.d/cobbler
# chkconfig cobbler on
4、Cobbler 的命令行管理
1、查看命令帮助
[root@linux-node1 ~]# cobbler
usage
=====
cobbler <distro|profile|system|repo|image|mgmtclass|package|file> ... 
        [add|edit|copy|getks*|list|remove|rename|report] [options|--help]
cobbler <aclsetup|buildiso|import|list|replicate|report|reposync|sync|validateks|version|signature|get-loaders|hardlink> [options|--help]
[root@linux-node1 ~]# cobbler import --help  # 导入镜像
Usage: cobbler [options]

Options:
  -h, --help            show this help message and exit
  --arch=ARCH           OS architecture being imported
  --breed=BREED         the breed being imported
  --os-version=OS_VERSION
                        the version being imported
  --path=PATH           local path or rsync location
  --name=NAME           name, ex 'RHEL-5'
  --available-as=AVAILABLE_AS
                        tree is here, don't mirror
  --kickstart=KICKSTART_FILE
                        assign this kickstart file
  --rsync-flags=RSYNC_FLAGS
                        pass additional flags to rsync

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

先自行下载一个centos7镜像,然后挂载到本地,之后导入到cobbler

# mkdir /mnt/centos7.6
# mount -t iso9660 -o loop CentOS-7-x86_64-Minimal-1810.iso  /mnt/centos7.6
# cobbler import --path=/mnt/centos7.6 --name=CentOS-7.6-1810 --arch=x86_64
task started: 2019-07-24_050656_import
task started (id=Media import, time=Wed Jul 24 05:06:56 2019)
Found a candidate signature: breed=redhat, version=rhel6
Found a candidate signature: breed=redhat, version=rhel7
Found a matching signature: breed=redhat, version=rhel7
Adding distros from path /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64:
creating new distro: CentOS-7.6-1810-x86_64
trying symlink: /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64 -> /var/www/cobbler/links/CentOS-7.6-1810-x86_64
creating new profile: CentOS-7.6-1810-x86_64
associating repos
checking for rsync repo(s)
checking for rhn repo(s)
checking for yum repo(s)
starting descent into /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64 for CentOS-7.6-1810-x86_64
processing repo at : /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64
looking for /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64/repodata
*** TASK COMPLETE ***
  • –path 镜像路径
  • –name 指定安装源的名字
  • –arch 指定导入镜像的体系结构
3、查看镜像列表
# cobbler distro list
   CentOS-7.6-1810-x86_64

镜像保存在http的目录内

# ls /var/www/cobbler/ks_mirror/

使用cobbler查看,此处的一些变量我们在写 kickstarts 配置的时候可能会用到,例如tree

# cobbler distro  report
Name                           : CentOS-7.6-1810-x86_64
Architecture                   : x86_64
TFTP Boot Files                : {}
Breed                          : redhat
Comment                        :
Fetchable Files                : {}
Initrd                         : /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64/images/pxeboot/initrd.img
Kernel                         : /var/www/cobbler/ks_mirror/CentOS-7.6-1810-x86_64/images/pxeboot/vmlinuz
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/CentOS-7.6-1810-x86_64'}
Management Classes             : []
OS Version                     : rhel7
Owners                         : ['admin']
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Template Files                 : {}

8、kickstarts 文件配置

在传统安装操作系统时,需要大量的交互操作,为了减少交互的过程,kickstart就产生了,我们需要提前定义好这个kickstart的配置文件,并让安装程序知道kickstart配置文件的位置,在安装过程中读取kickstart配置即可实现无人值守的自动化安装操作系统。

1、修改 kickstart 文件(内存必须 2G 以上)

默认的kickstarts文件放在/var/lib/cobbler/kickstarts/目录下,默认有一些示例

# ls /var/lib/cobbler/kickstarts/
default.ks    esxi5-ks.cfg      legacy.ks     sample_autoyast.xml  sample_esx4.ks   sample_esxi5.ks  sample.ks        sample.seed
esxi4-ks.cfg  install_profiles  pxerescue.ks  sample_end.ks        sample_esxi4.ks  sample_esxi6.ks  sample_old.seed

一般centos系统安装完成后会在/root/anaconda-ks.cfg
产生一个ks配置文件,记录安装过程,我们可以根据这个文件修改一下,修改好后放到/var/lib/cobbler/kickstarts目录

default.ks    esxi5-ks.cfg      legacy.ks     sample_autoyast.xml  sample_esx4.ks   sample_esxi5.ks  sample.ks        sample.seed
esxi4-ks.cfg  install_profiles  pxerescue.ks  sample_end.ks        sample_esxi4.ks  sample_esxi6.ks  sample_old.seed
一般centos系统安装完成后会在/root/anaconda-ks.cfg
产生一个ks配置文件,记录安装过程,我们可以根据这个文件修改一下,修改好后放到/var/lib/cobbler/kickstarts目录
mv sample_end.ks sample_end.ks.back  #将原生成的ks文件备份重命名备份一下,我们使用自定义的ks文件
# 最小换安装
# cat /var/lib/cobbler/kickstarts/centos7u6-x64.ks
#Kickstart Configurator for cobbler by Jason Zhao
#platform=x86, AMD64, or Intel EM64T
#System  language
lang en_US
#System keyboard
keyboard us
#Sytem timezone
timezone Asia/Shanghai
#Root password
rootpw --iscrypted $default_password_crypted
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
url --url=$tree
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype xfs --size 1024 --ondisk sda
part swap --fstype="swap" --size 2048 --ondisk sda
part / --fstype xfs --size 1 --grow --ondisk sda
#System authorization infomation
auth  --useshadow  --enablemd5
#Network information
$SNIPPET('network_config')
# network --bootproto=dhcp --device=em1 --onboot=on
# Reboot after installation
reboot
#Firewall configuration
firewall --disabled
#SELinux configuration
selinux --disabled
#Do not configure XWindows
skipx
#Package install information
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
 
%packages
@ base
@ core
sysstat
iptraf
ntp
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
nmap
screen
%end
 
%post
systemctl disable postfix.service
%end

# 图形化安装
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$m1pE0DG6$vALBphGGynqvUzfJaWZ6U1
# Use network installation
url --url="$tree"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled

# Network information
network  --bootproto=dhcp --device=eth0
network  --bootproto=dhcp --device=eth1
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --asprimary --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1
%packages
@base
@core
@compat-libraries
@debugging
@development
@gnome-desktop
@X Window System
%end
2、查看 kickstart 的配置
# cobbler profile  report  --name=CentOS-7.6-1810-x86_64
Name                           : CentOS-7.6-1810-x86_64
TFTP Boot Files                : {}
Comment                        :
DHCP Tag                       : default
Distribution                   : CentOS-7.6-1810-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks  # 这里还是默认的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
3、修改指定的 kickstart 文件
# cobbler profile list
CentOS-7.6-1810-x86_64

# cobbler profile getks --name "CentOS-7.6-1810-x86_64"   #自检 
# cobbler profile edit --name=CentOS-7.6-1810-x86_64  --kickstart=/var/lib/cobbler/kickstarts/centos7u6-x64.ks
# cobbler profile edit --name=CentOS-7.6-1810-x86_64 --kopts='net.ifnames=0 biosdevname=0'   #修改centos配置文件中的kickstart值
# cobbler sync   #执行rsync同步
4、 查看 kickstart 关联

在查看一下,kickstart已经关联到了我们自己的ks文件

# cobbler profile  report  --name=CentOS-7.6-1810-x86_64 | grep -i kickstart
Kickstart                      : /var/lib/cobbler/kickstarts/centos7.6.ks
Kickstart Metadata             : {}
5、检查服务是否启动
systemctl  restart dhcpd xinetd cobblerd tftp

确保下面这些服务在运行,其中 25151 是 xmlrpc_port 端口

netstat -anutlp|egrep "httpd|rsync|xinetd|dhcpd|25151"
6、修改启动菜单

启动菜单,local是默认选项,之后才是我们自己定义的启动项,在下面的模板中可以看出ONTIMEOUT 超时后会以$pxe_timeout_profile这个变量的值来启动

# cat /etc/cobbler/pxe/pxedefault.template
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile



LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

$pxe_menu_items


MENU end

查看源代码,pxe_timeout_profile是从system中取的,如果没有设置话默认会设置为local

def make_pxe_menu(self):
    """
    Generates both pxe and grub boot menus.
    """
    # only do this if there is NOT a system named default.
    default = self.systems.find(name="default")

    if default is None:
        timeout_action = "local"
    else:
        timeout_action = default.profile

    menu_items = self.get_menu_items()

    # Write the PXE menu:
    metadata = {"pxe_menu_items": menu_items['pxe'], "pxe_timeout_profile": timeout_action}
    outfile = os.path.join(self.bootloc, "pxelinux.cfg", "default")
    template_src = open(os.path.join(self.settings.boot_loader_conf_template_dir, "pxedefault.template"))
    template_data = template_src.read()
    self.templar.render(template_data, metadata, outfile, None)
    template_src.close()

    # Write the grub menu:
    outfile = os.path.join(self.bootloc, "grub", "menu_items.cfg")
    fd = open(outfile, "w+")
    fd.write(menu_items['grub'])
    fd.close()

这里在捊一下distro,profile,system三者间的关系

  • distro 可以理解为“操作系统”,我们之前导入iso时会生成distro
  • profile 我理解的是这里定义的是操作系统安装时的一些参数?(理解可能有误,欢迎指正)
  • system 这里就是定义启动时的菜单选项了

根据上面的代码我们需要加一个system的配置

# cobbler system add  --name=default --profile=CentOS-7.6-1810-x86_64
# cobbler  system list
   default
# cobbler sync   # 同步,然后在查看一下default的配置
# cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT CentOS-7.6-1810-x86_64



LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL CentOS-7.6-1810-x86_64
        kernel /images/CentOS-7.6-1810-x86_64/vmlinuz
        MENU LABEL CentOS-7.6-1810-x86_64
        append initrd=/images/CentOS-7.6-1810-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://10.10.1.13/cblr/svc/op/ks/profile/CentOS-7.6-1810-x86_64
        ipappend 2




MENU end

这里的顺序更改一定要注意,要么就是自动化安装操作系统时划分vlan防止有原来的服务器重启将服务器重新安装,或者安装完后将cobbler服务停止。

7、自动安装系统

以上cobbler算是都配置完成了,现在可以愉快的安装操作系统了,新建一台虚拟机,这步略过了没啥可介绍的,虚拟机指定用pxe 网络启动即可,后边都是图片了,我就不上传了,只要ks文件没问题,几乎不会出问题。如果有问题,自行拆招吧。。

可以用以下命令查看安装状态

# cobbler status   安装完后才有ip状态
ip             |target              |start            |state
10.10.0.103    |profile:CentOS-7.6-1810-x86_64|Thu Jul 25 03:59:58 2019|installing (59m 35s)

到此cobbler就安装完成,

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值