使用Cobbler批量安裝linux系統


原博主:http://xiaoshuaigege.blog.51cto.com/6217242/1967134linux

Cobbler 簡介

Cobbler項目是在2008年由RedHat發佈的網絡安裝服務器套件。是一種快速網絡安裝linux操做系統的服務,支持衆多的Linux發行版:Red Hat、Fedora、CentOS、Debian、Ubuntu和SuSE,也能夠支持網絡安裝windows。git

PXE 是由Intel開發的技術,基於Client/Server的網絡模式,支持遠程主機經過網絡從遠端服務器下載映像,並由此支持經過網絡啓動操做系統。也能夠實現經過網絡批量的安裝操做系統。github

Cobbler 正是基於PXE的二次封裝,將多種安裝參數封裝到一個菜單當中。Cobbller提供了CLI和WEB兩種安裝方式,使用起來更加的友好。web

Cobbler 工做流程

關於Cobbler的工做流程,能夠用下面的圖進行歸納。redis

Cobbler的工做原理

實驗環境準備

由於咱們正在搭建的是網絡服務,因此須要有至少兩臺主機,其中一臺主機承擔服務器的角色。我在在這個實驗中,使用的CentOS 7 搭建的網絡服務,承擔Server角色。windows

關閉防火牆和SELinux

防火牆和SELinux 在實驗過程當中有可能會致使意想不到的問題,因此咱們這裏將其關閉,實際生產中,請根據本身的實際狀況進行調整。至於關閉防火牆和SELinux就不列出了,這很簡單。centos

安裝Cobbler

Cobbler 默認不在附帶在光盤中,因此要經過EPEL源進行下載安裝。 配置好epel源之後,執行yum install cobbler 就能夠安裝Cobbler了。
在安裝好Cobbler以後,咱們就會發現,Cobbler還附帶安裝了不少其餘的服務,以下圖所示 。這也就是解釋了在Cobbler的工做流程中爲何可以用到那麼多服務,從圖中能夠看出,還缺乏DHCP服務,因此接下里咱們也要安裝一下。其中,syslinux 就是安裝的就是PXE服務。服務器

Cobbler附帶的服務

設置經常使用服務

將Cobbler服務設爲開機啓動網絡

systemctl enable  cobblerd
systemctl start  cobblerd

將tftp 設爲開機啓動app

systemctl enable  tftp
systemctl start  tftp

將http 設爲開機啓動

systemctl enable  httpd
systemctl start  httpd

安裝DHCP服務 爲稍後使用Cobbler來管理DHCP作準備

在稍後,咱們會利用Cobbler來自動管理DHCP。

yum install  dhcp

運行Cobbler Check 進行環境檢查

 Cobbler Check 命令可以很是迅速的幫助咱們找到目前環境中還缺乏的配置,而後咱們根據這裏的提示進行相應的配置調整。 這裏出現了不少的問題,看到以後不要驚慌,根據提示一點一點去處理就行了。另外,由於實際生產環境中,全部的主機都會有所不一樣,因此這裏的提示信息應該也會有所不一樣,咱們這裏只是根據實驗環境進行解決,其他的問題,能夠留言一塊兒討論。

# 執行Cobbler check 檢查目前環境還缺乏哪些東西
[root@localhost ~]#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.

首先咱們來看第一個問題。信息提示,在/etc/cobbler/settings 這個配置文件中的server 字段必定要有一個其餘主機可以訪問到的主機名或者IP地址,不然KickStart的一些特性將沒法使用。其實也很好理解,咱們目前的主機是用來提供網絡服務的。其餘主機經過訪問咱們的Cobbler服務,進行下載安裝操做系統,若是咱們的Server字段別人不能訪問,那怎麼能夠呢!  因此打開該配置文件,修改server字段爲本機的IP地址。

修改Server文件

修改完成以後,咱們注意到,以前的提示信息末尾有一段提示信息 Restart cobblerd and then run 'cobbler sync' to apply changes. ,因此咱們運行一下這兩個命令。

#重啓服務
[root@localhost ~]#systemctl restart cobblerd

#同步信息
[root@localhost ~]#cobbler sync
...........  # 中間省略輸出信息  


# 從新檢查一遍,看看還缺乏哪些東西。
[root@localhost ~]#cobbler check
The following are potential configuration items that you may want to fix:

1 : 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.
2 : 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.
3 : enable and start rsyncd.service with systemctl
4 : debmirror package is not installed, it will be required to manage debian deployments and repositories
5 : 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
6 : 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.

從新檢查以後,咱們發現解決了2個問題。而後咱們接着來看第一個問題。這個問題是說,next_server 字段也應該是一個具體的地址,而不該該是127.0.0.1.其實這個問題和上面咱們修改過的問題是同樣的。因此咱們從新將next_server的地址修改成咱們本機的地址。

next_server

修改以後,從新啓動服務,並進行同步。

#重啓服務
[root@localhost ~]#systemctl restart cobblerd

#同步信息[root@localhost ~]#cobbler sync
...........  # 中間省略輸出信息  

# 從新檢查一遍,看看還缺乏哪些東西。
[root@localhost ~]#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.
2 : enable and start rsyncd.service with systemctl
3 : debmirror package is not installed, it will be required to manage debian deployments and repositories
4 : 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
5 : 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.

出現的問題繼續減小了。咱們接着來看第一個信息。這個信息是說,在/var/lib/cobbler/loaders 路徑下,少了一些必須的文件,可使用 cobbler get-loaders命令從網絡中下載相關的文件。因此,接下來咱們運行一下 cobbler get-loaders

# 能夠看到 /var/lib/cobbler/loaders 目錄下沒有任何內容
[root@localhost ~]#ls /var/lib/cobbler/loaders

[root@localhost ~]#cobbler get-loaders
task started: 2017-09-20_084605_get_loaders
task started (id=Download Bootloader Content, time=Wed Sep 20 08:46:05 2017)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 ***

此時查看 /var/lib/cobbler/loaders 目錄下,會發現,已經下載了不少的內容。而後重啓和同步服務,並運行 cobbler check 查看還剩下一些什麼問題,咱們接着一個個去解決。

#這個目錄下最關鍵的文件就是  menu.c32   pxelinux.0 
[root@localhost ~]#ls /var/lib/cobbler/loaders
COPYING.elilo     COPYING.yaboot  grub-x86_64.efi  menu.c32    README
COPYING.syslinux  elilo-ia64.efi  grub-x86.efi     pxelinux.0  yaboot

[root@localhost ~]#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 : 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
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.

接下來,我來看剩下的問題。前兩個問題不是很重要,咱們先忽略過去。來看第三個問題,這個問題是說,應該爲每個新安裝的主機設置一個默認的用戶口令。而且信息中提示了使用

openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'

命令來生成加密的用戶口令。 若是不設置,默認的用戶口令就是cobbler

#生成密碼是123456 的用戶口令
[root@localhost ~] openssl passwd -1  123456$1$wuX5VyFf$a0Y/HdzHRCbVWhSFnbV8n/

而後將生成的口令替換到/etc/cobbler/settingsdefault_password_crypted字段中。

修改Cobbler 默認口令

從新啓用並同步服務,而後看看還有什麼問題。

[root@localhost ~]#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 : 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.

 fencing tools 是在集羣環境中才會使用到的工具,咱們暫且先不用管。這樣一來,關於Cobbler的check 工做咱們就完成了。

經過Cobbler 管理DHCP

咱們利用DHCP的cobbler 功能來自動管理DHCP,修改 /etc/cobbler/settings 配置文件中的manage_dhcp字段。 設置爲1,則由Cobbler 來管理DHCP

Cobbler dhcp

修改Cobbler下 /etc/cobbler/dhcp.template 模板文件

 /etc/cobbler/dhcp.template 這個文件是配置DHCP的內容的。可是這個文件是Cobbler來提供的。也就是說,配置好了這個文件,從新啓動Cobbler 服務,Cobbler就會自動地替咱們管理DHCP。

# 在/etc/cobbler/dhcp.template 找到下面地這段內容進行修改整理。 
# 按照本身地要求進行DHCP地配置。

subnet 172.18.2.0 netmask 255.255.255.0 {
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        172.18.2.100 172.18.2.254;
     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 {
                  filename "pxelinux.0";          
                  }
     }}

從新啓動和同步Cobbler 服務。

# 從新啓動服務
[root@localhost ~]#systemctl restart cobblerd

[root@localhost ~]#cobbler sync
........#中間省略不少輸出


#查看DHCP的配置文件
[root@localhost ~]#cat /etc/dhcp/dhcpd.conf 
# ******************************************************************
# Cobbler managed dhcpd.conf file
# generated from cobbler dhcp.conf template (Wed Sep 20 01:36:15 2017)
# 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 172.18.2.0 netmask 255.255.255.0 {
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        172.18.2.100 172.18.2.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                172.18.2.77;
     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 {
                  filename "pxelinux.0";          
                  }
     }}
     
     # group for Cobbler DHCP tag: default
     group {
     }

製做 yum 源

與PXE不一樣,Cobbler能夠自動幫助咱們生成系統安裝的yum 源,而不須要咱們本身去製做,咱們只要指定光盤路徑就行了

# 執行下面的命令製做 CentOS 7.3 的啓動光盤
[root@localhost ~]#cobbler import --path=/misc/cd --name=centos7.3

# 而後更換ISO鏡像,製做6.9的光盤內容
[root@localhost ~]#cobbler import --path=/misc/cd --name=centos6.9

Cobbler 製做的光盤yum源到底放在了什麼地方呢??

#進入到下面的這個路徑下,能夠看到這裏生成了兩個源,其實也就是將光盤裏的內容複製過來了而已。
[root@localhost ~]#cd /var/www/cobbler/ks_mirror/
[root@localhost ks_mirror]#ls
centos6.9  centos7.3  config

# 執行下面的命令能夠看到,這裏已經成功的製做了兩個系統yum源。
[root@localhost ks_mirror]#cobbler distro list
   centos6.9-x86_64
   centos7.3-x86_64

修改啓動菜單

yum 源文件建立成功以後,能夠進行自定義的配置修改。

# 查看生成的啓動菜單文件
[root@localhost tftpboot]#cat /var/lib/tftpboot/pxelinux.cfg/default 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/   #能夠修改成本身的標籤。
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

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

LABEL centos6.9-x86_64
        kernel /p_w_picpaths/centos6.9-x86_64/vmlinuz
        MENU LABEL centos6.9-x86_64
        append initrd=/p_w_picpaths/centos6.9-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://172.18.2.77/cblr/svc/op/ks/profile/centos6.9-x86_64
        ipappend 2

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



MENU end

啓動網絡安裝

從新新建一個虛擬機,做爲客戶端,而後啓動網絡安裝,就能夠看到咱們剛剛製做的界面了。

Cobbler 啓動界面

自定義KickStart文件

首先準備KickStart文件

在上面的實驗中,咱們沒有進行任何的安裝配置,所有都是Cobbler幫助咱們完成的。但是在實際生產中,咱們對每臺主機的硬盤分區,安裝的軟件都有相應的要求,因此咱們須要本身來定製KickStart文件,這樣咱們就可以定製地批量安裝操做系統了。
咱們就利用實驗環境(CentOS 7.3)中的KickStart文件來定製咱們本身的KickStart文件。

# 首先進入到Cobbler 的KickStart 目錄下
[root@localhost] cd /var/lib/cobbler/kickstarts/

# 將根目錄下的ks 文件複製到當前目錄下
[root@localhost kickstarts]cp /root/anaconda-ks.cfg  centos7.cfg


[root@localhost kickstarts]#cat centos7.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512

# Use CDROM installation mediaurl 

--url=$tree	# url能夠指定詳細路徑也可使用$tree變量來進行替代

# Use graphical install
reboot	#安裝成功以後能夠重啓
text	#採用字符界面進行安裝
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda

# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'

# System language
lang en_US.UTF-8

# Network information 
#修改網絡環境
network  --bootproto=dhcp --device=ens33 --onboot=yes --ipv6=auto --activate
network  --hostname=localhost.localdomain



selinux --disabled #禁用SElinux 也能夠禁用防火牆

# Root password	#設置root用戶的初始密碼rootpw --iscrypted $6$BlwRFg7fgO1i8eQa$F9yjtePt1aOnsIwmNO7mexdnBOsJTVSSyTnwp2hS2lSY87thmNylXw43.Ycj6lfY1wk2NmoQjO/WzEEGBUlZt.

# System services
services --disabled="chronyd"

# System timezone
timezone Asia/Shanghai --isUtc --nontp
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
zerombr	#清楚MBR分區
clearpart --all #清楚系統中原有分區

# Disk partitioning information
part / --fstype="xfs" --ondisk=sda --size=56320
part swap --fstype="swap" --ondisk=sda --size=2000
part /app --fstype="xfs" --ondisk=sda --size=51200
part /boot --fstype="xfs" --ondisk=sda --size=1024

%packages
@^graphical-server-environment
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@hardware-monitoring
@input-methods
@internet-browser
@multimedia
@print-client
@x11
kexec-tools

%end


%post
#刪除舊的yum倉庫#可根據實際狀況稍做調整。
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
EOF
%end

將KickStart文件和yum源作關聯

在Cobbler 自動建立了yum源以後,會自動有一個ks文件與該源相對應。如今咱們須要將本身製做的KS文件與原有的yum源進行關聯。

# --name 咱們須要添加的啓動菜單的名字
# --distro  咱們自定製的ks文件關聯的yum庫
# --kickstart 咱們自定製的ks文件的路徑
[root@localhost kickstarts]#cobbler profile add --name=centos7.3-x86_64-desktop --distro=centos7.3-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7.cfg


# 查看Cobbler中如今還有多少個啓動項
[root@localhost kickstarts]#cobbler profile list
   centos6.9-x86_64
   centos7.3-x86_64
   centos7.3-x86_64-desktop

從新修改/var/lib/tftpboot/pxelinux.cfg/default文件

在執行Cobbler sync 同步操做以後,Cobbler會自動的將/var/lib/tftpboot/pxelinux.cfg/default文件從新生成,因此,最好從新按照本身的需求修改一下這個文件。而後就能夠開始進行網絡安裝操做系統了。

Cobbler啓動界面2

從圖中能夠清楚地看到咱們新增地一個啓動選項。至此,咱們搭建Cobbler 的環境就成功了。

注意事項

  • 在實際生產中,通常是局域網環境,因此在配置DHCP服務的時候,要注意局域網的工做環境。

  • Cobbler實際上就是對PXE的封裝,它幫助咱們實現了不少的內容,免去了咱們不少的手動配置,可是這樣的話,咱們也不能清楚的瞭解底層的運行機制,出了問題,不便於處理,因此最好仍是嘗試手動配置一下PXE來搭建網絡操做系統安裝環境,以便了解的更詳細一下其中的運行機制。

  • 最好手動配置ks文件,這樣可以最大限度上定製本身的集羣操做系統。

結語

Cobbler 其實還支持基於Web的管理方式。在配置好Cobbler 服務環境以後,安裝一個cobbler-web 的軟件包,而後就能夠進行相應的配置管理了,這裏咱們不作詳細介紹,感興趣的朋友能夠去查閱一下資料。 Cobbler_web 的圖形管理界面以下圖所示,從圖中咱們可以看到咱們本身指定的菜單和系統yum源。

Cobbler-web管理

經過上面的操做,咱們已經可以徹底搭建一個基於Cobbler的網絡服務,用來給生產環境中批量的安裝操做系統了。

相關文章
相關標籤/搜索