s17.运维自动化之系统部署 -- 实战案例:基于cobbler实现系统的自动化安装

596 阅读17分钟

本文已参与「新人创作礼」活动, 一起开启掘金创作之路。

1.实战案例:CentOS 7 基于cobbler实现系统的自动化安装

1.1 环境准备

两台主机

一台主机:CentOS 7 充当 Cobbler,http,dhcp,tftp 服务器,并关闭防火墙和SELinux

一台主机:充当测试机,用于实现自动化安装Linux系统

网络要求:关闭Vmware软件中的NAT模式中的DHCP服务,两个主机网卡基于NAT模式

1.2 安装相关包并启动服务

[root@centos7 ~]# yum -y install cobbler dhcp
​
[root@centos7 ~]# systemctl enable --now cobblerd httpd tftp dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/cobblerd.service to /usr/lib/systemd/system/cobblerd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.

1.3 修改cobbler相关的配置

[root@centos7 ~]# 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 : ksvalidator was not found, install pykickstart
8 : 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
9 : 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.
​
​
#生成新密码,默认安装好的系统root密码为cobbler
[root@centos7 ~]# openssl passwd -1 '123456'
$1$n1dmG22r$1V4Tk4/Ds2SjF839ckuFQ/
​
​
#根据以上提示,只需要做128这三项即可,修改下面四行
[root@centos7 ~]# vim /etc/cobbler/settings
default_password_crypted: "$1$n1dmG22r$1V4Tk4/Ds2SjF839ckuFQ/"
manage_dhcp: 1 #设置为1,表示通过cobbler生成dhcpd.conf配置文件
next_server: 172.31.0.7
server: 172.31.0.7
​
[root@centos7 ~]# systemctl restart cobblerd

1.4 实现dhcp服务

#修改dhcp的模版文件下面的行,用来生成dhcp的配置文件
[root@centos7 ~]# vim /etc/cobbler/dhcp.template
subnet 172.31.0.0 netmask 255.255.248.0 {
     option routers             172.31.0.2;
     option domain-name-servers 180.76.76.76,223.6.6.6;
     option subnet-mask         255.255.248.0;
     range dynamic-bootp        172.31.7.1 172.31.7.254; 
[root@centos7 ~]# cobbler sync
task started: 2021-11-29_220814_sync
task started (id=Sync, time=Mon Nov 29 22:08:14 2021)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/images
copying bootloaders
trying hardlink /usr/share/syslinux/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /usr/share/syslinux/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
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 ***

1.5 下载启动的相关文件

[root@centos7 ~]# cobbler get-loaders
task started: 2021-11-29_220848_get_loaders
task started (id=Download Bootloader Content, time=Mon Nov 29 22:08:48 2021)
downloading https://cobbler.github.io/loaders/README to /var/lib/cobbler/loaders/README
Exception occured: <class 'urlgrabber.grabber.URLGrabError'>
Exception value: [Errno 14] HTTPS Error 404 - Not Found
Exception Info:
  File "/usr/lib/python2.7/site-packages/cobbler/remote.py", line 82, in run
    rc = self._run(self)
   File "/usr/lib/python2.7/site-packages/cobbler/remote.py", line 176, in runner
    return self.remote.api.dlcontent(self.options.get("force",False), self.logger)
   File "/usr/lib/python2.7/site-packages/cobbler/api.py", line 735, in dlcontent
    return grabber.run(force)
   File "/usr/lib/python2.7/site-packages/cobbler/action_dlcontent.py", line 73, in run
    urlgrabber.grabber.urlgrab(src, filename=dst, proxies=proxies)
   File "/usr/lib/python2.7/site-packages/urlgrabber/grabber.py", line 773, in urlgrab
    return default_grabber.urlgrab(url, filename, **kwargs)
   File "/usr/lib/python2.7/site-packages/urlgrabber/grabber.py", line 1220, in urlgrab
    return _run_callback(opts.failfunc, opts)
   File "/usr/lib/python2.7/site-packages/urlgrabber/grabber.py", line 1071, in _run_callback
    return cb(obj)
   File "/usr/lib/python2.7/site-packages/urlgrabber/grabber.py", line 1065, in _do_raise
    raise obj.exception
​
!!! TASK FAILED !!!
​
#把cobbler离线安装的getLoader离线包上传到服务器
[root@centos7 ~]# ls
anaconda-ks.cfg  loaders
[root@centos7 ~]# mv loaders/* /var/lib/cobbler/loaders
mv: overwrite ‘/var/lib/cobbler/loaders/README’? y
[root@centos7 ~]# ll /var/lib/cobbler/loaders
total 1128
-rw-r--r-- 1 root root    631 Nov 29 22:09 COPYING.elilo
-rw-r--r-- 1 root root  18007 Nov 29 22:09 COPYING.syslinux
-rw-r--r-- 1 root root    626 Nov 29 22:09 COPYING.yaboot
-rw-r--r-- 1 root root 356493 Nov 29 22:09 elilo-ia64.efi
-rw-r--r-- 1 root root 243679 Nov 29 22:09 grub-x86_64.efi
-rw-r--r-- 1 root root 237224 Nov 29 22:09 grub-x86.efi
-rw-r--r-- 1 root root  54964 Nov 29 22:09 menu.c32
-rw-r--r-- 1 root root  16794 Nov 29 22:09 pxelinux.0
-rw-r--r-- 1 root root   1054 Nov 29 22:09 README
-rw-r--r-- 1 root root 198236 Nov 29 22:09 yaboot
​
[root@centos7 ~]# tree /var/lib/cobbler/loaders
/var/lib/cobbler/loaders
├── COPYING.elilo
├── COPYING.syslinux
├── COPYING.yaboot
├── elilo-ia64.efi
├── grub-x86_64.efi
├── grub-x86.efi
├── menu.c32
├── pxelinux.0
├── README
└── yaboot
​
0 directories, 10 files
​
[root@centos7 ~]# cobbler sync
task started: 2021-11-29_221122_sync
task started (id=Sync, time=Mon Nov 29 22:11:22 2021)
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_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.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 ***
​
[root@centos7 ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── boot
│   └── grub
│       └── menu.lst
├── etc
├── grub
│   ├── efidefault
│   ├── grub-x86_64.efi
│   ├── grub-x86.efi
│   └── images -> ../images
├── images
├── images2
├── memdisk
├── menu.c32
├── ppc
├── pxelinux.0
├── pxelinux.cfg
│   └── default
├── s390x
│   └── profile_list
└── yaboot
​
10 directories, 10 files
​
[root@centos7 ~]# cobbler check
The following are potential configuration items that you may want to fix:1 : enable and start rsyncd.service with systemctl
2 : debmirror package is not installed, it will be required to manage debian deployments and repositories
3 : ksvalidator was not found, install pykickstart
4 : 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.
​
[root@centos7 ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
​
[root@centos7 ~]# cobbler check
The following are potential configuration items that you may want to fix:1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
2 : ksvalidator was not found, install pykickstart
3 : 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.
​
[root@centos7 ~]# yum -y install pykickstart
[root@centos7 ~]# cobbler check
The following are potential configuration items that you may want to fix:1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
2 : 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.
​
[root@centos7 ~]# yum -y install debmirror
​
[root@centos7 ~]# systemctl restart cobblerd
[root@centos7 ~]# cobbler check
The following are potential configuration items that you may want to fix:1 : comment out 'dists' on /etc/debmirror.conf for proper debian support
2 : comment out 'arches' on /etc/debmirror.conf for proper debian support
3 : 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.
​
[root@centos7 ~]# vim /etc/debmirror.conf
#注释掉下面两行
#@dists="sid";
#@arches="i386";
​
[root@centos7 ~]# cobbler check
The following are potential configuration items that you may want to fix:1 : 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.
​
[root@centos7 ~]# yum -y install fence-agents
​
[root@centos7 ~]# systemctl restart cobblerd
[root@centos7 ~]# cobbler check
No configuration problems found.  All systems go.
​
[root@centos7 ~]# cobbler sync
task started: 2021-11-29_221909_sync
task started (id=Sync, time=Mon Nov 29 22:19:09 2021)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/grub-x86.efi
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/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.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 ***

1.6 修改菜单的标题信息(可选)

[root@centos7 ~]# vim /etc/cobbler/pxe/pxedefault.template
MENU TITLE Cobbler | http://www.raymonds.cc/ 
​
[root@centos7 ~]# cobbler sync
task started: 2021-11-29_222106_sync
task started (id=Sync, time=Mon Nov 29 22:21:06 2021)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/grub-x86.efi
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/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.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 ***
​
[root@centos7 ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://www.raymonds.cc/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local
​
LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1
​
​
​
MENU end

1.7 导入CentOS系统的安装文件,生成相应的YUM源

[root@centos7 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  9.5G  0 rom  
sr1     11:1    1   10G  0 rom  
sr2     11:2    1  3.7G  0 rom  
​
[root@centos7 ~]# mkdir /mnt/centos{6,7}
[root@centos7 ~]# mkdir /mnt/rocky8
[root@centos7 ~]# mount /dev/sr0 /mnt/centos7
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos7 ~]# mount /dev/sr1 /mnt/rocky8
mount: /dev/sr1 is write-protected, mounting read-only
[root@centos7 ~]# mount /dev/sr2 /mnt/centos6
mount: /dev/sr2 is write-protected, mounting read-only
​
[root@centos7 ~]# cobbler import --name=rocky-8.5-x86_64 --path=/mnt/rocky8 --arch=x86_64
task started: 2021-11-29_222652_import
task started (id=Media import, time=Mon Nov 29 22:26:52 2021)
Found a candidate signature: breed=redhat, version=rhel8
No signature matched in /var/www/cobbler/ks_mirror/rocky-8.5-x86_64
!!! TASK FAILED !!!
#出现No signature matched报错
​
[root@centos7 ~]# vim /var/lib/cobbler/distro_signatures.json
   "rhel8": {
    "signatures":["BaseOS"],
    "version_file":"(redhat|sl|slf|rocky|centos|oraclelinux|vzlinux)-release-(?!notes)([\w]*-)*8(Server)*[\.-]+(.*)\.rpm",  #把这一行centos前面加上rocky
​
[root@centos7 ~]# systemctl restart cobblerd
[root@centos7 ~]# cobbler import --name=rocky-8.5-x86_64 --path=/mnt/rocky8 --arch=x86_64
task started: 2021-11-29_223329_import
task started (id=Media import, time=Mon Nov 29 22:33:29 2021)
Found a candidate signature: breed=redhat, version=rhel8
Found a matching signature: breed=redhat, version=rhel8
Adding distros from path /var/www/cobbler/ks_mirror/rocky-8.5-x86_64:
creating new distro: rocky-8.5-x86_64
trying symlink: /var/www/cobbler/ks_mirror/rocky-8.5-x86_64 -> /var/www/cobbler/links/rocky-8.5-x86_64
creating new profile: rocky-8.5-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/rocky-8.5-x86_64 for rocky-8.5-x86_64
processing repo at : /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/AppStream
need to process repo/comps: /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/AppStream
looking for /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/AppStream/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/rocky-8.5-x86_64/AppStream/repodata
processing repo at : /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/BaseOS
need to process repo/comps: /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/BaseOS
looking for /var/www/cobbler/ks_mirror/rocky-8.5-x86_64/BaseOS/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/rocky-8.5-x86_64/BaseOS/repodata
*** TASK COMPLETE ***
#现在导入镜像成功
​
[root@centos7 ~]#  cobbler import --name=centos-7.9-x86_64 --path=/mnt/centos7 --arch=x86_64
task started: 2021-11-29_223455_import
task started (id=Media import, time=Mon Nov 29 22:34:55 2021)
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.9-x86_64:
creating new distro: centos-7.9-x86_64
trying symlink: /var/www/cobbler/ks_mirror/centos-7.9-x86_64 -> /var/www/cobbler/links/centos-7.9-x86_64
creating new profile: centos-7.9-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.9-x86_64 for centos-7.9-x86_64
processing repo at : /var/www/cobbler/ks_mirror/centos-7.9-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/centos-7.9-x86_64
looking for /var/www/cobbler/ks_mirror/centos-7.9-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/centos-7.9-x86_64/repodata
*** TASK COMPLETE ***
​
[root@centos7 ~]# cobbler import --name=centos-6.10-x86_64 --path=/mnt/centos6 --arch=x86_64
task started: 2021-11-29_223721_import
task started (id=Media import, time=Mon Nov 29 22:37:21 2021)
Found a candidate signature: breed=redhat, version=rhel6
Found a matching signature: breed=redhat, version=rhel6
Adding distros from path /var/www/cobbler/ks_mirror/centos-6.10-x86_64:
creating new distro: centos-6.10-x86_64
trying symlink: /var/www/cobbler/ks_mirror/centos-6.10-x86_64 -> /var/www/cobbler/links/centos-6.10-x86_64
creating new profile: centos-6.10-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-6.10-x86_64 for centos-6.10-x86_64
processing repo at : /var/www/cobbler/ks_mirror/centos-6.10-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/centos-6.10-x86_64
looking for /var/www/cobbler/ks_mirror/centos-6.10-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/centos-6.10-x86_64/repodata
*** TASK COMPLETE ***
​
[root@centos7 ~]# du -sh /var/www/cobbler/ks_mirror/*
3.8G    /var/www/cobbler/ks_mirror/centos-6.10-x86_64
9.6G    /var/www/cobbler/ks_mirror/centos-7.9-x86_64
16K /var/www/cobbler/ks_mirror/config
11G /var/www/cobbler/ks_mirror/rocky-8.5-x86_64
​
[root@centos7 ~]# cobbler distro list
   centos-6.10-x86_64
   centos-7.9-x86_64
   rocky-8.5-x86_64
[root@centos7 ~]# cobbler profile list
   centos-6.10-x86_64
   centos-7.9-x86_64
   rocky-8.5-x86_64
#默认生成的是最小化安装

066.jpg

1.8 准备 kickstart文件,并关联至指定的YUM源

[root@centos7 ~]# ls
anaconda-ks.cfg  centos6.cfg  centos7.cfg  loaders  rocky8.cfg
[root@centos7 ~]# mv centos* rocky8.cfg /var/lib/cobbler/kickstarts/
[root@centos7 ~]# ll /var/lib/cobbler/kickstarts/
total 76
-rw-r--r-- 1 root root 1831 Nov 26 20:13 centos6.cfg
-rw-r--r-- 1 root root 2126 Nov 26 20:47 centos7.cfg
-rw-r--r-- 1 root root  115 Aug 30  2019 default.ks
-rw-r--r-- 1 root root   22 Aug 30  2019 esxi4-ks.cfg
-rw-r--r-- 1 root root   22 Aug 30  2019 esxi5-ks.cfg
drwxr-xr-x 2 root root   56 Nov 29 22:00 install_profiles
-rw-r--r-- 1 root root 1424 Aug 30  2019 legacy.ks
-rw-r--r-- 1 root root  292 Aug 30  2019 pxerescue.ks
-rw-r--r-- 1 root root 2566 Nov 26 21:33 rocky8.cfg
-rw-r--r-- 1 root root 2825 Aug 30  2019 sample_autoyast.xml
-rw-r--r-- 1 root root 1856 Aug 30  2019 sample_end.ks
-rw-r--r-- 1 root root    0 Aug 30  2019 sample_esx4.ks
-rw-r--r-- 1 root root  324 Aug 30  2019 sample_esxi4.ks
-rw-r--r-- 1 root root  386 Aug 30  2019 sample_esxi5.ks
-rw-r--r-- 1 root root  386 Aug 30  2019 sample_esxi6.ks
-rw-r--r-- 1 root root 1913 Aug 30  2019 sample.ks
-rw-r--r-- 1 root root 3419 Aug 30  2019 sample_old.seed
-rw-r--r-- 1 root root 6694 Aug 30  2019 sample.seed
-rw-r--r-- 1 root root 6706 Jun 18  2019 sample.seed.28
​
[root@centos7 ~]# vim /var/lib/cobbler/kickstarts/centos7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$r6bueld4$D66DkBZeburidFINHY6Pf1
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
​
​
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url=$tree #注意此行必须指定
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1
​
%packages
vim
lrzsz
@core
%end
​
[root@centos7 ~]# vim /var/lib/cobbler/kickstarts/rocky8.cfg
#version=RHEL8
# Use graphical install
reboot
text
url --url=$tree #注意此行必须指定
selinux --disabled
firewall --disabled
​
%packages
@^minimal-environment
kexec-tools
vim
lrzsz
%end
​
# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8
​
# Network information
network  --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network  --hostname=localhost.localdomain
​
# Run the Setup Agent on first boot
firstboot --disabled
​
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1# System timezone
timezone Asia/Shanghai --isUtc --nontp
​
# Root password
rootpw --iscrypted $6$1p3L2ik4nOlvKZV4$rQdULbSTNv22KpUksNUINmWVOgEpXOPgLbgxiXdI.VnPeure6xrEQXI9QTQ6X1P9LQorkJHH2p7OSRl5LmgIB.
​
%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
​
[root@centos7 ~]# vim /var/lib/cobbler/kickstarts/centos6.cfg
# Kickstart file automatically generated by anaconda.#version=DEVEL
install
text
reboot
url --url=$tree #注意此行必须指定
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw  --iscrypted $6$eAT2HmLxImeMrIbd$jP8mNSU6/A9/ISOSP6dc72h6mbbfzTsCFMEcfL8c7PNla8L9wGQnW5Nl8wuP5mHFkoxlwIQNt3jjcErIE.w9H/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
zerombr
clearpart --all --initlabel
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="ext4" --grow --size=1
​
%packages
@core
@server-policy
@workstation-policy
vim
lrzsz
%end
​
#将kickstart文件,关联指定的YUM源和生成菜单列表
[root@centos7 ~]# cobbler profile add --name=CentOS-7.9_mini --distro=CentOS-7.9-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7.cfg
[root@centos7 ~]# cobbler profile add --name=Rocky-8.5_mini --distro=Rocky-8.5-x86_64 --kickstart=/var/lib/cobbler/kickstarts/rocky8.cfg
[root@centos7 ~]# cobbler profile add --name=CentOS-6.10_mini --distro=CentOS-6.10-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos6.cfg#删除默认生成的菜单
[root@centos7 ~]# cobbler profile remove --name=centos-7.9-x86_64
[root@centos7 ~]# cobbler profile remove --name=rocky-8.5-x86_64
[root@centos7 ~]# cobbler profile remove --name=centos-6.10-x86_64
[root@centos7 ~]# cobbler profile list
   CentOS-6.10_mini
   CentOS-7.9_mini
   Rocky-8.5_mini

1.9 测试客户端基于Cobbler实现自动安装

067.jpg

068.jpg

069.jpg

1.10 准备ubuntu 18.04 启动文件,并关联至指定的APT源

[root@centos7 ~]# vim /var/lib/cobbler/kickstarts/ubuntu1804.seed
# Mostly based on the Ubuntu installation guide
# https://help.ubuntu.com/18.04/installation-guide/
# Debian sample
# https://www.debian.org/releases/stable/example-preseed.txt# Preseeding only locale sets language, country and locale.
d-i debian-installer/locale string en_US
​
# Keyboard selection.
# Disable automatic (interactive) keymap detection.
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/xkb-keymap select us
d-i keyboard-configuration/toggle select No toggling
d-i keyboard-configuration/layoutcode string us
d-i keyboard-configuration/variantcode string
​
# netcfg will choose an interface that has link if possible. This makes it
# skip displaying a list if there is more than one interface.
#set $myhostname = $getVar('hostname',$getVar('name','cobbler')).replace("_","-")
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string $myhostname# If non-free firmware is needed for the network or other hardware, you can
# configure the installer to always try to load it, without prompting. Or
# change to false to disable asking.
# d-i hw-detect/load_firmware boolean true# NTP/Time Setup
d-i time/zone string Asia/Shanghai
d-i clock-setup/utc boolean true
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server  string ntp1.aliyun.com
​
# Setup the installation source
d-i mirror/country string manual
d-i mirror/http/hostname string $http_server
d-i mirror/http/directory string $install_source_directory
d-i mirror/http/proxy string
​
#set $os_v = $getVar('os_version','')
#if $breed == "ubuntu" and $os_v and $os_v.lower() != 'precise'
# Required at least for ubuntu 12.10+ , so test os_v is not precise. Olders versions are not supported anymore
d-i live-installer/net-image string http://$http_server/cobbler/links/$distro_name/install/filesystem.squashfs
#end if# Suite to install.
# d-i mirror/suite string precise
# d-i mirror/udeb/suite string precise# Components to use for loading installer components (optional).
#d-i mirror/udeb/components multiselect main, restricted# Disk Partitioning
# Use LVM, and wipe out anything that already exists
#d-i partman/choose_partition select finish
#d-i partman/confirm boolean true
#d-i partman/confirm_nooverwrite boolean true
#d-i partman-auto/method string lvm
#d-i partman-lvm/device_remove_lvm boolean true
#d-i partman-lvm/confirm boolean true
#d-i partman-lvm/confirm_nooverwrite boolean true
#d-i partman-md/device_remove_md boolean true
#d-i partman-partitioning/confirm_write_new_label boolean true
​
d-i partman-auto/disk string /dev/sda
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-md/device_remove_md boolean true
d-i partman-partitioning/confirm_write_new_label boolean true
#d-i partman/default_filesystem string ext4
##d-i partman/mount_style select uuid# You can choose one of the three predefined partitioning recipes:
# - atomic: all files in one partition
# - home:   separate /home partition
# - multi:  separate /home, /usr, /var, and /tmp partitions
d-i partman-auto/choose_recipe select atomic
​
# If you just want to change the default filesystem from ext3 to something
# else, you can do that without providing a full recipe.
# d-i partman/default_filesystem string ext4# root account and password
d-i passwd/root-login boolean true
d-i passwd/root-password-crypted password $default_password_crypted# skip creation of a normal user account.
d-i passwd/make-user boolean false# You can choose to install restricted and universe software, or to install
# software from the backports repository.
# d-i apt-setup/restricted boolean true
# d-i apt-setup/universe boolean true
# d-i apt-setup/backports boolean true# Uncomment this if you don't want to use a network mirror.
# d-i apt-setup/use_mirror boolean false# Select which update services to use; define the mirrors to be used.
# Values shown below are the normal defaults.
# d-i apt-setup/services-select multiselect security
# d-i apt-setup/security_host string security.ubuntu.com
# d-i apt-setup/security_path string /ubuntu
d-i apt-setup/services-select multiselect security
d-i apt-setup/security_host string 172.31.0.7
d-i apt-setup/security_path string /cobbler/ks_mirror/ubuntu-18.04.6-X86_64
​
$SNIPPET('preseed_apt_repo_config')
​
# Enable deb-src lines
# d-i apt-setup/local0/source boolean true# URL to the public key of the local repository; you must provide a key or
# apt will complain about the unauthenticated repository and so the
# sources.list line will be left commented out
# d-i apt-setup/local0/key string http://local.server/key# By default the installer requires that repositories be authenticated
# using a known gpg key. This setting can be used to disable that
# authentication. Warning: Insecure, not recommended.
# d-i debian-installer/allow_unauthenticated boolean true# Package selection
# Default for minimal
tasksel tasksel/first multiselect standard
# Default for server
# tasksel tasksel/first multiselect standard, web-server
# Default for gnome-desktop
# tasksel tasksel/first multiselect standard, gnome-desktop# Individual additional packages to install
# wget is REQUIRED otherwise quite a few things won't work
# later in the build (like late-command scripts)
#d-i pkgsel/include string ntp ssh wget
d-i pkgsel/include string wget ssh
#d-i pkgsel/upgrade select none
#d-i pkgsel/update-policy select none
​
​
# Debian needs this for the installer to avoid any question for grub
# Please verify that it suit your needs as it may overwrite any usb stick
#if $breed == "debian"
d-i grub-installer/grub2_instead_of_grub_legacy boolean true
d-i grub-installer/bootdev string default
#end if# Use the following option to add additional boot parameters for the
# installed system (if supported by the bootloader installer).
# Note: options passed to the installer will be added automatically.
d-i debian-installer/add-kernel-opts string $kernel_options_post# Avoid that last message about the install being complete.
d-i finish-install/reboot_in_progress note
​
## Figure out if we're kickstarting a system or a profile
#if $getVar('system_name','') != ''
#set $what = "system"
#else
#set $what = "profile"
#end if# This first command is run as early as possible, just after preseeding is read.
# d-i preseed/early_command string [command]
d-i preseed/early_command string wget -O- \
   http://$http_server/cblr/svc/op/script/$what/$name/?script=preseed_early_default | \
   /bin/sh -s
​
# This command is run immediately before the partitioner starts. It may be
# useful to apply dynamic partitioner preseeding that depends on the state
# of the disks (which may not be visible when preseed/early_command runs).
# d-i partman/early_command \
#       string debconf-set partman-auto/disk "$(list-devices disk | head -n1)"# This command is run just before the install finishes, but when there is
# still a usable /target directory. You can chroot to /target and use it
# directly, or use the apt-install and in-target commands to easily install
# packages and run commands in the target system.
# d-i preseed/late_command string [command]
d-i preseed/late_command string wget -O- \
   http://$http_server/cblr/svc/op/script/$what/$name/?script=preseed_late_default | \
   chroot /target /bin/sh -s
​
​
[root@centos7 ~]# mkdir /mnt/ubuntu18
[root@centos7 ~]# mount /dev/sr3 /mnt/ubuntu18/
mount: /dev/sr3 is write-protected, mounting read-only
​
[root@centos7 ~]# cobbler import --path=/mnt/ubuntu18/ --name=ubuntu-18.04.6 --kickstart=/var/lib/cobbler/kickstarts/ubuntu1804.seed --arch=x86_64
task started: 2021-11-29_234051_import
task started (id=Media import, time=Mon Nov 29 23:40:51 2021)
Found a candidate signature: breed=freebsd, version=10.1
Found a candidate signature: breed=freebsd, version=10.0
Found a candidate signature: breed=freebsd, version=8.3
Found a candidate signature: breed=freebsd, version=8.2
Found a candidate signature: breed=freebsd, version=8.4
Found a candidate signature: breed=freebsd, version=11.0
Found a candidate signature: breed=freebsd, version=10.3
Found a candidate signature: breed=freebsd, version=11.2
Found a candidate signature: breed=freebsd, version=11.1
Found a candidate signature: breed=freebsd, version=12.0
Found a candidate signature: breed=freebsd, version=10.2
Found a candidate signature: breed=freebsd, version=11.3
Found a candidate signature: breed=freebsd, version=9.0
Found a candidate signature: breed=freebsd, version=9.1
Found a candidate signature: breed=freebsd, version=9.2
Found a candidate signature: breed=freebsd, version=9.3
Found a candidate signature: breed=ubuntu, version=precise
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/Release
received on stdout: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/Release: ASCII text
​
received on stderr: 
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/Release.gpg
received on stdout: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/Release.gpg: PGP signature
​
received on stderr: 
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/main/binary-amd64/Release
received on stdout: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/main/binary-amd64/Release: ASCII text
​
received on stderr: 
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/main/binary-i386/Release
received on stdout: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64/dists/bionic/main/binary-i386/Release: ASCII text
...
received on stderr: 
Found a matching signature: breed=ubuntu, version=bionic
Adding distros from path /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64:
creating new distro: ubuntu-18.04.6-hwe-x86_64
trying symlink: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64 -> /var/www/cobbler/links/ubuntu-18.04.6-hwe-x86_64
creating new profile: ubuntu-18.04.6-hwe-x86_64
creating new distro: ubuntu-18.04.6-x86_64
trying symlink: /var/www/cobbler/ks_mirror/ubuntu-18.04.6-x86_64 -> /var/www/cobbler/links/ubuntu-18.04.6-x86_64
creating new profile: ubuntu-18.04.6-x86_64
associating repos
checking for apt repo(s)
adding apt repo for ubuntu-18.04.6-hwe-x86_64
Added repos for ubuntu-18.04.6-hwe-x86_64
adding apt repo for ubuntu-18.04.6-x86_64
Added repos for ubuntu-18.04.6-x86_64
*** TASK COMPLETE ***
​
[root@centos7 ~]# cobbler profile list
   CentOS-6.10_mini
   CentOS-7.9_mini
   Rocky-8.5_mini
   ubuntu-18.04.6-hwe-x86_64
   ubuntu-18.04.6-x86_64
[root@centos7 ~]# cobbler profile remove --name=ubuntu-18.04.6-hwe-x86_64
[root@centos7 ~]# cobbler profile list
   CentOS-6.10_mini
   CentOS-7.9_mini
   Rocky-8.5_mini
   ubuntu-18.04.6-x86_64

070.jpg

1.11 实现cobbler 的web管理

[root@centos7 ~]# yum -y install cobbler-web
​
[root@centos7 ~]# systemctl restart httpd

通过浏览器访问下面地址:https://172.31.0.7/cobbler_web

071.jpg

用户名:cobbler,默认密码:cobbler

072.jpg

1.12 创建cobbler的自定义的web用户

[root@centos7 ~]# cat /etc/cobbler/users.digest 
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
​
[root@centos7 ~]# htdigest -c /etc/cobbler/users.digest Cobbler admin #-c 会覆盖创建
Adding password for admin in realm Cobbler.
New password: 
Re-type new password: 
​
[root@centos7 ~]# cat /etc/cobbler/users.digest 
admin:Cobbler:38a2b8b1b63ae825e30c892e13a200f5
3
​
[root@centos7 ~]#  htdigest /etc/cobbler/users.digest Cobbler raymond #创建多个用户就不要加-c,不然之前创建的用户就会被覆盖
Adding user raymond in realm Cobbler
New password: 
Re-type new password: 
​
[root@centos7 ~]# cat /etc/cobbler/users.digest 
admin:Cobbler:38a2b8b1b63ae825e30c892e13a200f5
3
raymond:Cobbler:b8a729b4058869661cb9304f6f7a1888

使用上面命令创建的用户登录web界面

073.jpg

登录成功后,可以看到下面界面

074.jpg