實戰-使用 Cobbler 自定義安裝系統


環境:
CentOS Linux release 7.6.1810
VMware Workstation Pro 14

定制系統安裝的軟件包

查看默認的 ks 文件

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


profiles:
==========
Name                           : CentOS-7.6-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : CentOS-7.6-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
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <>
Red Hat Management Server      : <>
Repos                          : []
Server Override                : <>
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

注: distros 是發行版本,即光盤鏡像信息。 profiles 指的是 ks 應答文件。 systems 是指通 Cobbler 安裝好的物理機名稱。

[root@Jaking ~]# cd /var/lib/cobbler/kickstarts/
[root@Jaking kickstarts]# ls
default.ks        legacy.ks            sample_esx4.ks   sample.ks
esxi4-ks.cfg      pxerescue.ks         sample_esxi4.ks  sample_old.seed
esxi5-ks.cfg      sample_autoyast.xml  sample_esxi5.ks  sample.seed
install_profiles  sample_end.ks        sample_esxi6.ks  sample.seed.28

可以看到有很多默認的配置文件。

上傳 CentOS7.ks 到 Cobbler 服務器上。CentOS7.ks 這個配置文件里面安裝了開發工具相關軟件包。

[root@Jaking kickstarts]# rz

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

[root@Jaking kickstarts]# ls
CentOS7.ks    install_profiles     sample_end.ks    sample_esxi6.ks  sample.seed.28
default.ks    legacy.ks            sample_esx4.ks   sample.ks
esxi4-ks.cfg  pxerescue.ks         sample_esxi4.ks  sample_old.seed
esxi5-ks.cfg  sample_autoyast.xml  sample_esxi5.ks  sample.seed

[root@Jaking kickstarts]# cat CentOS7.ks
# Cobbler for Kickstart Configurator for CentOS 7 by clsn
install
url --url=$tree
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
#Network information
$SNIPPET('network_config')
#network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $default_password_crypted
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end

%packages
@additional-devel
@development
@platform-devel
bash-completion
chrony
dos2unix
kexec-tools
lrzsz
sysstat
tree
vim
wget
git
net-tools
%end

%post
systemctl disable postfix.service
%end

[root@Jaking kickstarts]# cobbler profile list  #查看當前啟動項,使用的配置文件
   CentOS-7.6-x86_64

修改 name 是 CentOS-7.6-x86_64 的 kickstart 文件為 CentOS7.ks

[root@Jaking kickstarts]# cobbler profile edit --name CentOS-7.6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS7.ks
[root@Jaking kickstarts]# cobbler report | grep Kickstart
Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/CentOS-7.6-x86_64'}
Kickstart                      : /var/lib/cobbler/kickstarts/CentOS7.ks
Kickstart Metadata             : {}

通過給內核傳參數,修改網卡名稱為 eth0

[root@Jaking kickstarts]# cobbler profile edit --name CentOS-7.6-x86_64 --kopts='net.ifnames=0 biosdevname=0'

配置文件內容如下:

[root@Jaking kickstarts]# vim CentOS7.ks #查看配置文件中的,默認就可以,不需要修改
# Cobbler for Kickstart Configurator for CentOS 7 by clsn
Install                 #安裝系統
url --url=$tree      #url 地址為 Cobbler 內置變量
text                    #文本方式安裝,修改為圖形界面則為 Graphical
lang en_US.UTF-8 #語言
keyboard us         #鍵盤
zerombr              #該參數用於清除引導信息,需要讓其生效可以在參數后添加 yes 即可。可選項,一般不用。
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
#指定引導裝載程序怎樣被安裝.對於安裝和升級,這個選項都是必需的.
#Network information
$SNIPPET('network_config') #該參數表示使用下方%pre 中的腳本來配置網絡,相關腳本存放於/var/lib/cobbler/snippets
#如果不需要腳本配置可啟用以下配置
#network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6
--hostname=CentOS7
timezone --utc Asia/Shanghai #時區
authconfig --enableshadow --passalgo=sha512 #加密 shadow
rootpw --iscrypted $default_password_crypted #設置 root 密碼
clearpart --all --initlabel #--all 初除所有分區,后者將磁盤標簽初始化為缺省值設置。
part /boot --fstype xfs --size 1024 #boot分區大小
part swap --size 1024 #swap分區大小
part / --fstype xfs --size 1 --grow #根分區大小
firstboot --disable #決定是否在系統第一次引導時啟動"設置代理”,禁用。
selinux --disabled #在系統里設置 SELinux 狀態
firewall --disabled #在系統狸設置而防火牆狀態
logging --level=info #這個命令控制安裝過程中anaconda的錯誤日志,它對安裝好的系統沒有影響。
reboot #安裝后重啟
%pre #pre 中定了前面使用的具體腳本名稱。
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages #自定義安裝內容,這里可以可以自行生成 ks 文件然后把對應的配置復制到這里。當前配置為最小化安裝,以及安裝系統常用工具。
@additional-devel
@development
@platform-devel
bash-completion
chrony
dos2unix
kexec-tools
lrzsz
sysstat
tree
vim
wget
git
net-tools
%end
%post #安裝后執行操作,可以執行腳本也可以直接執行命令。
systemctl disable postfix.service
%end

定制 Cobbler 引導菜單(名稱、超時時間、默認啟動菜單)

[root@Jaking kickstarts]# vim /etc/cobbler/pxe/pxedefault.template
DEFAULT menu
PROMPT 0
MENU TITLE Jaking #菜單名稱
TIMEOUT 100 #超時時間
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1
        
$pxe_menu_items   #該變量是我自定義的引導菜單,復制到 LABEL local 配置項上方,否則默認啟動還是 local。

MENU end

同步 Cobbler 配置

[root@Jaking kickstarts]# systemctl restart cobblerd
[root@Jaking kickstarts]# cobbler sync
task started: 2020-01-04_050537_sync
task started (id=Sync, time=Sat Jan  4 05:05:37 2020)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/CentOS-7.6-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/CentOS-7.6-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
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 files for distro: CentOS-7.6-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.6-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/CentOS-7.6-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.6-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/CentOS-7.6-x86_64/initrd.img
copying images
generating PXE configuration files
generating PXE menu structure
copying files for distro: CentOS-7.6-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.6-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/CentOS-7.6-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.6-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/CentOS-7.6-x86_64/initrd.img
Writing template files for CentOS-7.6-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: CentOS-7.6-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 ***

新建虛擬機進行測試。

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

注意:內存要足夠大,網絡模式要和 Cobbler 服務端的網絡模式一致。

創建過程省略,直接來到開機啟動界面:

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

可以看到,需要安裝 1262 個包,不再是原來的最小化安裝了。

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

安裝成功后,默認啟動命令行模式
輸入 root 123456 登錄系統:

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

設置默認啟動模式為圖形化模式 graphical.target

[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[root@localhost ~]# systemctl get-default                 
graphical.target
[root@localhost ~]# reboot

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

不能正常啟動圖形化模式

解決方法

安裝圖形化軟件

[root@localhost ~]# yum install -y xorg* gnome* glx*

切換到圖形化模式

[root@localhost ~]# init 5

實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統
實戰-使用 Cobbler 自定義安裝系統實戰-使用 Cobbler 自定義安裝系統

本文原創地址:https://www.linuxprobe.com/?p=170731編輯:傳棋,審核員:逄增寶

導讀 搭建好 Cobbler 服務端后,使用默認的 kickstarts 引導文件對客戶端安裝系統,且默認安裝的是最小化的系統。我們可以自定義 kickstarts 引導文件進而實現對客戶端的自定義安裝系統。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM