cobbler無人值守自動安裝(centos7,Redhat6)

 

無人值守安裝(自動按定製的需求來批量安裝linux系統)
kickstart
cobbler
linux安裝系統的方法:
1,光盤	    把iso鏡像刻錄(windows下有類似nero這類軟件)到光盤裏,通過光驅,再修改BIOS第一啓動順序爲光驅進行安裝       
2,u盤    把iso鏡像刻錄(windows下有類似nero這類軟件)到U盤裏(具體過程請上網自已查),通過USB接口,再修改BIOS第一啓動順序爲U盤進行安裝
3,硬盤	  在linux硬盤上有iso鏡像,可以通過grub進行硬盤安裝第二個操作系統,或者重裝系統(只重格根分區,不要重格iso所在的分區)
4,網絡	   	
======================================================================
/share/soft/rhel-server-6.5-x86_64-dvd.iso  3.6G  3.6G     0 100% /yum

rhel6的iso鏡像裏的基本文件說明:

Packages:這個目錄是存放rhel6所有的rpm包(但不代表其它的linux也都一樣,比如rhel5是分爲四個目錄來存放rpm包的)

repodata: (repository data軟件倉庫數據目錄,你可以把它比喻成一個倉庫貨物清單),在yum倉庫裏找軟件包不是直接去找包在哪,而是找repodata(這是配置yum要知道的一個關鍵知識點)

images和isolinux目錄:簡單來說和啓動引導系統有關(包括vmlinuz和initrd.img啓動內核的文件)

EFI:簡單來說EFI是BIOS的升級版

=========================================================
硬盤安裝linux系統
# df -h
/dev/sda6              47G  4.0G   40G   9% /share
準備下面這些東西,最好都放到一個專門的分區掛載目錄,我這裏是/dev/sda6
/share/rhel-server-6.5-x86_64-dvd.iso
/share/isolinux/
/share/images/
/share/vmlinuz
/share/initrd.img
重啓你的操作系統,在grub界面按C鍵,
grub > root (hd0,5)
grub > kernel /vmlinuz
grub > initrd /initrd.img
grub > boot
就會進入裝系統的界面,和以前裝系統的過程幾乎一樣,只有一個地方不一樣就是有一步它會問你你的安裝方式,你選擇hard drive,然後再會問你iso的路徑,你選擇/dev/sda6就可以了;
========================================================
基於pxe(preboot execute environment)技術
pxe是intel公司的技術,工作server/client的網絡模式,支持客戶端從服務端下載軟件,再使用tftp(trival  file tranfer protocol) 協議下載一個啓動軟件包到客戶端內存中執行。

tftp	簡單文件傳輸協議,提供簡單的,不可靠的文件傳輸。基於UDP的69端口。

要求的技術和服務:

1。nfs服務器或者用http,ftp三種協議之一  (共享安裝光盤目錄文件)
2。tftp服務器	--共享啓動相關文件
3。dhcp服務器	--客戶端獲取IP,網關,DNS指向,主機名,NIS域,NTP
4。kickstart程序生成的ks.cfg配置文件(此文件就定義了安裝系統如何分區,如何格式化,root密碼等等)   取一個安裝名字,可以由客戶端自動選擇是否用此安裝名安裝

滿足上面的1,2,3三點,就是安裝服務器

客戶端(支持pxe的網卡)選擇網絡啓動--通過dhcp獲取IP可以和服務器通迅--通過tftp下載系統引導文件--按照ks.cfg配置文件裏的方式來自動安裝操作系統--在安裝最後一步要安裝軟件包,會按照ks.cfg裏配置的軟件包來安裝

我們這裏用一個虛擬機做服務器,另一個虛擬機做客戶端,並使用host-only(vmnet1)網絡,防止都在同一個網絡造成dhcp獲取混亂(我這裏的用的網段爲192.168.100.0/24,這是一個公網網段,我在這裏只是實驗測試爲了方便而已)

		cobbler server    -------    client
		192.168.100.2/24
安裝前準備:	(這裏特別要注意一點:你的虛擬機的cobbler-server的/var目錄空間最少要有4G以上空間)
1,關閉iptables,selinux
2,配置靜態IP
3,設置主機名
4,時間同步
5,配置本地iso鏡像裏的yum倉庫
====================================================
此爲Redhat6版本的安裝及配置方法
我這裏直接把宿主機裏的鏡像/share/iso/rhel-server-6.5-x86_64-dvd.iso直接放到cobbler server虛擬機的光驅裏
# mkdir /yum		
# mount /dev/cdrom /yum

第一大步:在cobbler server上安裝cobbler
軟件包可以在cobbler官方下載
cobbler-2.6.3-1.el6.noarch.rpm      koan-2.6.9-1.el6.noarch.rpm     repodata
cobbler-web-2.6.3-1.el6.noarch.rpm  libyaml-0.1.4-2.3.x86_64.rpm
Django14-1.4.20-1.el6.noarch.rpm    PyYAML-3.10-3.1.el6.x86_64.rpm

# vim /etc/yum.repos.d/rhel-source.repo	 --在你原來的yum的配置基礎上,再加上下面這一段
[cobbler]
name=cobbler
baseurl=file:///cobbler_soft
enabled=1
gpgcheck=0

確認配置好yum後,直接下面一條命令把相關軟件包全安裝上
# yum install cobbler cobbler-web tftp* rsync xinetd http* syslinux dhcp* pykickstart

第二大步:基本配置
# cobbler check		--查看cobbler潛在的需要修改的選項(有可能因爲你的httpd啓不來,而選成無法顯示下面的結果;按報錯去解決它)
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 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
    https://github.com/cobbler/cobbler/wiki/Selinux
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 : change 'disable' to 'no' in /etc/xinetd.d/rsync
6 : since iptables may be running, ensure 69, 80/443, and 25151 are unblocked
7 : debmirror package is not installed, it will be required to manage debian deployments and repositories
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.

--說明:上面我這裏是有9個需求(不同的機器和環境可能會不一樣,按照它的說明去解決就可以了)

解決需求1,需求2,需求8:
# openssl passwd -1 -salt 'werwqerwqr' '123456'		--123456爲密碼(這是自動安裝客戶端系統成功後的root登錄密碼),werwqerwqr爲隨機字節干擾碼(隨便寫)
$1$werwqerw$.prcfrYFbwuvkD8XspayN.

# vim /etc/cobbler/settings
384 server: 192.168.100.2	--換成cobbler服務器端的IP
272 next_server: 192.168.100.2	--同上
101 default_password_crypted: "$1$werwqerw$.prcfrYFbwuvkD8XspayN."  --把密碼字符串換成你上面產生的字符串(此密碼爲客戶機安裝後的root登錄密碼)

# /etc/init.d/cobblerd restart	--修改後重啓

解決需求3
關閉selinux
# vim /etc/selinux/config 	--把下面的改成disabled,然後reboot你的系統
SELINUX=disabled

--如果真的要完全關閉,必須按上面的操作並重啓生效;如果你覺得麻煩,可以使用setenforce 0暫時代替

解決需求5
# vim /etc/xinetd.d/rsync
        disable = no	--yes改爲no

解決需求6
# /etc/init.d/iptables stop
# chkconfig iptables off
或者開啓iptables,但要開放69, 80/443, 25151這幾個端口

解決需求9
# yum install fence-agents

解決了上面的問題後,再次cobbler check
# cobbler check	    --解決了上面的問題,只餘下兩個問題了(問題1可以使用cobbler get-loaders解決,但需要有外網和外網的yum源;問題2是關於debian系統的,我們這裏可以忽略)
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 : debmirror package is not installed, it will be required to manage debian deployments and repositories
Restart cobblerd and then run 'cobbler sync' to apply changes.

第三大步:導入鏡像
# ls /var/www/cobbler/ks_mirror/
config

# cobbler import --path=/yum/ --name=rhel6.5-x86-64  --將掛載的鏡像目錄/yum位置導入到cobbler,name後面接的是你取的一個名稱
。。。。。。
*** TASK COMPLETE ***

# ls /var/www/cobbler/ks_mirror/	--導入完後,這裏會多了剛導入的鏡像目錄
config  rhel6.5-x86-64

# cobbler distro list		--列表你cobbler導入的鏡像
   rhel6.5-64-x86_64

# cobbler profile list		--列表你的cobbler自動安裝方案(從這裏看到你導入一個鏡像會默認做一個與它同名的安裝方案)
   rhel6.5-64-x86_64

--cobbler可以實現多種不同的iso鏡像導入,並且每個iso鏡像還可以對應多種不同安裝方法(也就是說一個distro可以對應多個profile)
 
第四大步:
修改dhcp,讓cobbler來管理dhcp,並進行cobbler配置同步
修改/etc/cobbler/dhcp.template,此文件是cobbler管理dhcp的模板(不需要象kickstart那樣去修改/etc/dhcp/dhcpd.conf,修改了也沒用,它會在後面做cobbler sync時把/etc/cobbler/dhcp.template拷過去覆蓋/etc/dhcp/dhcpd.conf文件,並啓動dhcp)

只修改下面這一段,改成你自己對應的IP和網段就可以了
# vim /etc/cobbler/dhcp.template
subnet 192.168.100.0 netmask 255.255.255.0 {	--改你分配的網段,掩碼
     option routers             192.168.100.2;	--改你分配的網關
     option domain-name-servers 192.168.100.2;	--改你分配的DNS指向
     option subnet-mask         255.255.255.0;	--改你分配的IP的掩碼
     range dynamic-bootp        192.168.100.200 192.168.100.254;	--改你分配的IP的範圍
     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";
          }
     }
}


# vim /etc/cobbler/settings	--再去修改這個配置文件,改成dhcp服務由cobbler來管理
242 manage_dhcp: 1		--把0改爲1

# /etc/init.d/cobblerd restart	--保存後,再重啓此服務

# cobbler sync		--同步cobbler配置,並初始化,幫你啓動dhcp等
。。。。。。
*** TASK COMPLETE ***

# /etc/init.d/xinetd restart	--把xinetd服務重啓一下

第五大步:
測試驗證:新建一個vmnet1網段(因爲我前面配置的是這個網段)的虛擬機,然後啓動,會出現cobbler的引導安裝界面,選擇並自動安裝(這裏自動安裝的系統沒有圖形界面,默認只安裝417個包而已)
====================================================================================
centos7.3下搭建cobbler
centos7.3下使用私有網絡(我這裏使用kvm自建的default1網絡,網段爲192.168.100.0/24)實現cobbler自動安裝

					cobbler server    -------    client
					192.168.100.2/24
第一步:
先安裝cobbler相關軟件包 (需要epel源,local源,centos163源;但因爲需要用私有網絡來做實驗,但私有網絡又不能和我的共享源連接,所以先可以使用橋接網絡把下面的包裝上,再換成私有網絡)  
# yum install cobbler cobbler-web tftp* rsync xinetd http* syslinux dhcp* pykickstart fence-agents xinetd debmirror system-config-kickstart

第二步:啓動服務,並使用cobbler check查詢哪些需要修改
# systemctl restart cobblerd.service
# systemctl restart httpd.service 
# systemctl enable cobblerd.service
# systemctl enable httpd.service
#  cobbler check	

第三步:按照cobbler check對應的信息修改
# sed -i 272s/127.0.0.1/192.168.100.2/ /etc/cobbler/settings
# sed -i 384s/127.0.0.1/192.168.100.2/ /etc/cobbler/settings  
# sed -i 242s/0/1/ /etc/cobbler/settings
# openssl passwd -1 -salt 'xcvsfdsdfwq' '123456'
$1$xcvsfdsd$cGxdcHuQGCnu5vJw5M7zX1
# vim /etc/cobbler/settings	--在101行把上面產生的密碼字符串粘貼到""裏替代原來的字符串
default_password_crypted: "$1$xcvsfdsd$cGxdcHuQGCnu5vJw5M7zX1"
# systemctl restart cobblerd.service
# sed -i /disable/s/yes/no/ /etc/xinetd.d/tftp
# systemctl restart rsyncd.service
# systemctl enable rsyncd.service

第四步:導入centos7.3的iso鏡像
# cobbler import --path=/yum/ --name=centos7.3
# cobbler distro list	--導入成功後,確認導入的鏡像名
   centos7.3-x86_64
# cobbler profile list	--導入成功後,確認默認的profile名
   centos7.3-x86_64

第五步:修改dhcp配置
# vim /etc/cobbler/dhcp.template  --在此文件的第21行到第25行修改成你對應的網段和ip	
subnet 192.168.100.0 netmask 255.255.255.0 {
     option routers             192.168.100.2;
     option domain-name-servers 192.168.100.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.100.100 192.168.100.254;

第六步:使用cobbler sync同步,並啓動xinetd服務
# cobbler sync
# systemctl restart xinetd.service
# systemctl enable xinetd.service

第七步:新建另一個虛擬機(保證相同私有網絡,並且最好2G內存,內存小的話會在安裝時報空間不夠的錯誤),進行安裝測試
(注意:新建的虛擬機需要選擇已網絡爲優先啓動項。不然會提示獲取不到安裝文件)
==============================================================================
自定義ks文件
自動安裝我們已經實現了,但如何自定義我們想安裝的profile
重點就是你需要自定義一個ks文件
自定義ks文件的方法
1,如果你很熟悉此文件,直接拷別人的模版修改或者直接使用/root/anaconda-ks.cfg來修改
2,使用一個圖形工具system-config-kickstart來幫助你配置(下面我就是使用這種方法)

# yum install system-config-kickstart
# system-config-kickstart	
# vim /etc/yum.repos.d/local.repo    --這裏有個小問題,需要把軟件倉庫裏改成development,圖形才能選擇包,否則圖形在選擇軟件包那一步看不到
[development]
name=development
baseurl=file:///yum/
enabled=1
gpgcheck=0
--圖形的操作過程這裏省略

此處共享給大家兩個做好的ks.cfg文件模板參考:
第一個:這個可以直接複製後進行安裝,完整的centos7.3系統,可直接複製進行安裝,只是需要將IP換成你自己搭建的服務器的IP
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$6RbFC2nC$GBugZL7ACuGhck8DbUZgM/
# Use network installation
url --url="http://172.16.13.250/cobbler/ks_mirror/centos7.3/"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append="rhgb quiet" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=300
part swap --fstype="swap" --size=4000
part / --fstype="xfs"  --size=150000
part /data --fstype="xfs" --size=80000
%packages
@additional-devel
@desktop-debugging
@development
@fonts
@gnome-desktop
@input-methods
@kde-desktop
@legacy-x
@platform-devel
@remote-desktop-clients
@x11
initial-setup
initial-setup-gui
vinagre
%end

第二個:
這個ks文件是用來安裝雙系統的ks文件,如果需要安裝雙系統的朋友,可以直接複製,然後將IP換成你自己的搭建的cobbler服務器的IP
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$6RbFC2nC$GBugZL7ACuGhck8DbUZgM/
# Use network installation
url --url="http://172.16.13.250/cobbler/ks_mirror/centos7.3/"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append="rhgb quiet" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --none
# Disk partitioning information
part swap --fstype="swap" --size=4000
part / --fstype="xfs" --grow --size=1
%packages
@additional-devel
@desktop-debugging
@development
@fonts
@gnome-desktop
@input-methods
@kde-desktop
@legacy-x
@platform-devel
@remote-desktop-clients
@x11
initial-setup
initial-setup-gui
vinagre
%end

=========================================================================
# cobbler profile help	--查看幫助
# cobbler profile list	--查看有哪些profile
   centos7.3-x86_64	

# cobbler profile report --name=centos7.3-x86_64 |grep Kickstart
Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks  --這就是這個安裝方案的kickstart文件
Kickstart Metadata             : {}

我現在自定義一個ks文件,也想做成一個profile,步驟如下:
步驟一:
自定義ks文件(使用system-config-kickstart或直接找一個模版文件修改),得到如下的文件

# vim /var/lib/cobbler/kickstarts/centos7.3.ks	--複製下面一大段信息,粘到此文件裏
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$6RbFC2nC$GBugZL7ACuGhck8DbUZgM/
# Use network installation
url --url="http://192.168.100.2/cobbler/ks_mirror/centos7.3/"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append="rhgb quiet" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=300
part swap --fstype="swap" --size=2000
part / --fstype="xfs" --grow --size=1
%post
rm /etc/yum.repos.d/* -rf
cat > /etc/yum.repos.d/local.repo << EOF
[base]
name=base
baseurl=file:///yum
enabled=1
gpgcheck=0
EOF
%end
%packages
@additional-devel
@desktop-debugging
@development
@fonts
@gnome-desktop
@input-methods
@kde-desktop
@legacy-x
@platform-devel
@remote-desktop-clients
@x11
initial-setup
initial-setup-gui
vinagre
%end
步驟二:把準備好的ks文件做成一個新的profile
# cobbler profile add --name=new_centos7.3 --distro=centos7.3-x86_64  --kickstart=/var/lib/cobbler/kickstarts/centos7.3.ks
# cobbler profile list		--在原來基礎上就多了自定義的安裝方案
   centos7.3-x86_64
   new_centos7.3

步驟三:
測試
然後就可以新建一個客戶端虛擬機,使用上面的new_centos7.3安裝名字來測試了

==================================================================
對上面操作的擴展(僅供參考)
# cobbler profile edit --name=new_centos7.3 --kickstart=/var/lib/cobbler/kickstarts/xxx.ks   
# cobbler profile remove --name=new_centos7.3	
======================================================================

客戶端使用koan與服務器的cobbler聯繫,實現自動重裝系統
客戶端需要安裝koan軟件包(epel源裏就有)
# yum install koan  
# koan --server=192.168.100.2 --list=profiles	   --192.168.100.2爲cobbler服務器IP,得到的結果和在cobbler服務器上cobbler profile list命令得到的結果一樣
   centos7.3-x86_64
   new_centos7.3
# koan --replace-self --server=192.168.100.2 --profile=new_centos7.3  --指定本客戶端按照名爲new_centos7.3的profile重裝系統
# reboot  --敲完上面的命令,使用reboot,就會重裝了(沒敲上面的命令那reboot就是重啓)

=====================================================================================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章