Cobbler+Kickstart无人值守安装CentOS 6/7

Cobbler+Kickstart无人值守安装CentOS 6/7

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

Cobbler 其实就是对 PXE 的二次封装,PXE 的使用可参考「配置DHCP+TFTP+PXE+KICKSTART自动化安装CentOS 7」,关于Cobbler 的详细介绍可参考「Cobbler 自动化部署」,本编文章仅对 Cobbler 无人值守安装 CentOS6/7 的完整流程做详细概述。

  • 环境:提供一台主机作为 Cobbler 主机,我这里该主机 IP 为 172.16.1.200,系统为 CentOS 7.5,注意,要关闭网卡提供的 DHCP 服务;
  • 目标:使用 Cobbler + Kickstart 无人值守安装 CentOS 6/7;

下面就开始吧~~~


1、安装 Cobbler 相关程序包(需要添加 epel 源):

$ yum install cobbler cobbler-web pykickstart debmirror httpd syslinux dhcp tftp-server xinetd fence-agents -y

2、配置并启动 dhcp 服务并启动:

$ vim /etc/dhcp/dhcpd.conf
option domain-name "zze.xyz";
option domain-name-servers 172.16.1.2, 223.5.5.5;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 172.16.1.0 netmask 255.255.255.0 {
    range 172.16.1.121 172.16.1.140;
    option routers 172.16.1.2;
    filename "pxelinux.0";
}

$ systemctl restart dhcpd

3、启动 tftp 服务:

$ vim /etc/xinetd.d/tftp 
 disable         = no

$ systemctl restart xinetd

4、启动 httpd 服务:

$ systemctl start httpd

5、启动 Cobbler 服务并检查当前环境:

# 启动
$ systemctl start cobblerd
# 检查
$ 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 : 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.
4 : enable and start rsyncd.service with systemctl
5 : comment out 'dists' on /etc/debmirror.conf for proper debian support
6 : comment out 'arches' on /etc/debmirror.conf for proper debian support
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、修复 Cobbler 检查输出的警告内容:

# 生成默认密码
$ openssl passwd -1 -salt `openssl rand -hex 4`
Password: root1234
$1$4f7986c0$GGZBFkEIK.8Vg.ek1rGyX.

$ vim /etc/cobbler/settings
# cobbler 本机监听地址
server: 172.16.1.200
# 文件服务器地址
next_server: 172.16.1.200
# 替换默认密码为上面生成的密码
default_password_crypted: "$1$4f7986c0$GGZBFkEIK.8Vg.ek1rGyX."
# 自己管理 dhcp 服务
manage_dhcp: 0
# 自己管理 tftp 服务
manage_tftpd: 0

# 启动 rsyncd 服务
$ systemctl enable rsyncd && systemctl restart rsyncd

# 注释如下两行
$ vim /etc/debmirror.conf 
# @dists="sid";
# @arches="i386";

7、重启 Cobblerd 服务并再次检查:

$ systemctl restart cobblerd
$ cobbler check
The following are potential configuration items that you may want to fix:

1 : 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.

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

如果安装了 syslinux 程序包(在第一步就安装了),那么就可以忽略此警告。

8、挂载 CentOS 7 镜像:

image.png

$ mount -r /dev/cdrom /media/

9、导入镜像到 Cobbler 作为 repo,这一步 cobbler 会把光盘中的内容统统复制到自己的管理目录,所有有点慢:

$ cobbler import --path=/media/ --name=centos7-x86_64
task started: 2020-05-24_164355_import
task started (id=Media import, time=Sun May 24 16:43:55 2020)
...
*** TASK COMPLETE ***

复制完成后会生成 /var/www/cobbler 目录,该目录下文件如下:

# cobbler 默认通过 httpd 把该目录配置为了一个站点目录,对应配置文件为 /etc/httpd/conf.d/cobbler.conf 
$ cd /var/www/cobbler && ls
images  ks_mirror  links  localmirror  misc  pub  rendered  repo_mirror  svc
# 在 ks_mirror 下会生成 --name 指定的名称的同名目录,镜像中所有文件都复制到了该目录中
$ ls ks_mirror/
centos7-x86_64  config

10、重启 httpd 服务并测试使用浏览器访问该目录:

$ systemctl restart httpd

image.png

11、新建 kickstart 配置文件:

$ vim /var/lib/cobbler/kickstarts/centos7.cfg
install                                                   
keyboard 'us'                                             
rootpw root1234     
url --url="http://172.16.1.200/cobbler/ks_mirror/centos7-x86_64/"                
lang en_US                                                
auth  --useshadow  --passalgo=sha512           
text                                                      
selinux --disabled                                        
skipx
unsupported_hardware
firewall --disabled                                       
network  --bootproto=dhcp --device=eth0                  
reboot                                                    
timezone Asia/Shanghai
bootloader --location=mbr                                 
zerombr                                                   
clearpart --all --initlabel
# 对于安装 CentOS 6,将下面的 xfs 改成 ext4 即可
part /boot --fstype="xfs"  --size=1024
part swap --fstype="swap"  --size=1024
part / --fstype="xfs" --grow  --size=1

%packages                               
wget
vim
%end

%post                                  
systemctl disable postfix.service      
%end

12、根据已有的 distro 和 kickstart 配置文件创建 profile:

# 查看已有的 distro
$ cobbler distro list
   centos7-x86_64
# 添加 profile
$ cobbler profile add --name="centos7-x86_64-withks" --distro="centos7-x86_64" --kickstart=/var/lib/cobbler/kickstarts/centos7.cfg
# 查看已有的 profile
$ cobbler profile list
   centos7-x86_64
   centos7-x86_64-withks

13、如果希望指定超时后默认选定的菜单为 centos7-x86_64-withks,则可使用下面命令来设定:

 $ cobbler system add --name=default --profile=centos7-x86_64-withks

14、安装界面的模板文件为 /etc/cobbler/pxe/pxedefault.template,可通过该文件指定标题、超时时间等信息:

$ vim /etc/cobbler/pxe/pxedefault.template
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | https://www.zze.xyz/
TIMEOUT 50
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile

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

$pxe_menu_items

MENU end

15、同步,同步完成后 /var/lib/tftpboot/pxelinux.cfg/default 中会生成对应的 LABEL:

$ cobbler sync
$ cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | https://www.zze.xyz/
TIMEOUT 50
TOTALTIMEOUT 6000
ONTIMEOUT centos7-x86_64-withks

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

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

LABEL centos7-x86_64-withks
        kernel /images/centos7-x86_64/vmlinuz
        MENU LABEL centos7-x86_64-withks
        append initrd=/images/centos7-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://172.16.1.200/cblr/svc/op/ks/profile/centos7-x86_64-withks
        ipappend 2



MENU end

16、新建空白虚拟机,添加一块和 Cobbler 主机中 DHCP 服务同网段的网卡,启动,安装过程则会自动进行:

image.png

注意:新建的空白虚拟机的内存大小必须大于 2G,否则按提示空间不足。
对于 CentOS 6 的安装我这里也做了测试,步骤与上述基本相同,仅需要将挂载的镜像更换为 CentOS 6 镜像并且将 kickstart 配置中的文件系统类型改为 ext4 即可。

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/cobbler.html

Buy me a cup of coffee ☕.