Kickstart+PXE定製網絡批量自動安裝

什麼是KickStart

KickStart是一種無人職守安裝方式。KickStart的工作原理是通過記錄典型的安裝過程中所需人工干預填寫的各種參數,並生成一個名爲ks.cfg的文件;在其後的安裝過程中(不只侷限於生成KickStart安裝文件的機器)當出現要求填寫參數的情況時,安裝程序會首先去查找KickStart生成的文件,當找到合適的參數時,就採用找到的參數,當沒有找到合適的參數時,才需要安裝者手工干預。這樣,如果KickStart文件涵蓋了安裝過程中出現的所有需要填寫的參數時,安裝者完全可以只告訴安裝程序從何處取ks.cfg文件,然後去忙自己的事情。等安裝完畢,安裝程序會根據ks.cfg中設置的重啓選項來重啓系統,並結束安裝。

什麼是PXE

PXE(preboot execute environment)是由Intel公司開發的最新技術,工作於Client/Server的網絡模式,支持工作站通過網絡從遠端服務器下載映像,並由此支持來自網絡的操作系統的啓動過程,其啓動過程中,終端要求服務器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)協議下載一個啓動軟件包到本機內存中並執行,由這個啓動軟件包完成終端基本軟件設置,從而引導預先安裝在服務器中的終端操作系統。

網絡拓撲圖:

 

在機房中批量上線安裝服務器通常會使用如上圖所示的結構批量安裝,由於我們的工作電腦通常都是windows系統(office需要),所以再裝個VMware虛擬機,在虛擬機中架設PXE server不失爲一種好方法。

VMware workstation虛擬機的設置:安裝好CentOS並且虛擬機網絡連接方式爲橋接(bridge)

以下所有配置都在虛擬機中的CentOS完成:

使用PXE+Kickstart就可以製作網絡多機器自動安裝,網絡安裝的方式有三種:NFS、HTTP和FTP。綜合起來看,部署這樣的自動安裝環境需要的條件是:

1.DHCP 服務器

2.TFTP 服務器

3.KickStart所生成的ks.cfg配置文件

4.一臺存放系統安裝文件的服務器,如 NFS、HTTP 或 FTP 服務器

5.帶有一個 PXE 支持網卡的將安裝的主機

把系統安裝光盤mount到主機上

#mount /dev/cdrom /mnt/cdrom

安裝所需要的服務

yum -y install dhcp dhcp-devel nfs-utils nfs-utils-lib tftp tftp-server syslinux

 

配置TFTP,制定可供下載文件的目錄。

  1. 1、編輯TFTP配置文件 
  2. # default: off 
  3. # description: The tftp server serves files using the trivial file transfer \ 
  4. #       protocol.  The tftp protocol is often used to boot diskless \ 
  5. #       workstations, download configuration files to network-aware printers, \ 
  6. #       and to start the installation process for some operating systems. 
  7. service tftp 
  8.         disable = no    #找到disable,修改其值爲no 
  9.         socket_type             = dgram 
  10.         protocol                = udp 
  11.         wait                    = yes 
  12.         user                    = root 
  13.         server                  = /usr/sbin/in.tftpd 
  14.         server_args             = -s /tftpboot #此處爲TFTP提供下載的位置 
  15.         per_source              = 11 
  16.         cps                     = 100 2 
  17.         flags                   = IPv4 
  18.  
  19. 2,TFTP目錄建立與設置 
  20. #mkdir /tftpboot 
  21. #cp /usr/share/syslinux/pxelinux.0 /tftpboot/ 
  22. #mkdir /tftpboot/pxelinux.cfg 
  23. #cp /mnt/cdrom/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default 
  24. #cp /mnt/cdrom/isolinux/* /tftpboot #把光盤中isolinux裏的所有文件複製到/tftpboot下 

重啓動TFTP:service xinetd restart

 

DHCP服務器設置

  1. # DHCP Server Configuration file. 
  2. #   see /usr/share/doc/dhcp*/dhcpd.conf.sample 
  3. #   see 'man 5 dhcpd.conf' 
  4. ddns-update-style interim; 
  5. ignore client-updates; 
  6.  
  7. subnet 10.20.30.0 netmask 255.255.255.0 { 
  8.         option routers          10.20.30.1; 
  9.         option subnet-mask      255.255.255.0; 
  10.         option domain-name-servers 10.20.30.2; 
  11.  
  12.         range dynamic-bootp 10.20.30.2 10.20.30.100; 
  13.         default-lease-time 21600; 
  14.         max-lease-time 43200; 
  15.         next-server 10.20.30.2;  ###重要 
  16.         filename "pxelinux.0";   ###重要 
  17.  
  18. 單就網絡安裝來說,只要在已經安裝配置好的DHCP服務器上添加指定服務器IP和指定pxelinux.0便可。 

重啓動DHCP:service dhcpd restart

編輯/tftpboot/pxelinux.cfg/default

  1. default vesamenu.c32   
  2. #prompt 1 
  3. timeout 10    #超時時間 
  4.  
  5. display boot.msg 
  6.  
  7. menu background splash.jpg 
  8. menu title Welcome to CentOS 6.3! 
  9. menu color border 0 #ffffffff #00000000 
  10. menu color sel 7 #ffffffff #ff000000 
  11. menu color title 0 #ffffffff #00000000 
  12. menu color tabmsg 0 #ffffffff #00000000 
  13. menu color unsel 0 #ffffffff #00000000 
  14. menu color hotsel 0 #ff000000 #ffffffff 
  15. menu color hotkey 7 #ffffffff #ff000000 
  16. menu color scrollbar 0 #ffffffff #00000000 
  17.  
  18. label linux 
  19.   menu label ^Install or upgrade an existing system 
  20.   menu default 
  21.   kernel vmlinuz 
  22. #  append ks=ftp://10.20.30.2/pub/ks.cfg initrdinitrd=initrd.img  ## 設置重點 
  23.   append ks=nfs:10.20.30.2:/tftpboot/ks.cfg initrdinitrd=initrd.img ##設置重點 
  24. label vesa 
  25.   menu label Install system with ^basic video driver 
  26.   kernel vmlinuz 
  27.   append initrdinitrd=initrd.img xdriver=vesa nomodeset 
  28. label rescue 
  29.   menu label ^Rescue installed system 
  30.   kernel vmlinuz 
  31.   append initrdinitrd=initrd.img rescue 
  32. label local 
  33.   menu label Boot from ^local drive 
  34.   localboot 0xffff 
  35. label memtest86 
  36.   menu label ^Memory test 
  37.   kernel memtest 
  38.   append - 
  39.  
  40. ############FTP、NFS、HTTP三種安裝方式的配置路徑寫法########## 
  41. ############主要是爲了讓安裝引導程序找到ks.cfg配置文件######## 
  42. ############################################################## 
  43. #  append ks=ftp://10.20.30.2/pub/ks.cfg initrdinitrd=initrd.img 
  44. #  append ks=nfs:10.20.30.2:/tftpboot/ks.cfg initrdinitrd=initrd.img 
  45. #  append ks=http://10.20.30.2/linux/ks.cfg initrdinitrd=initrd.img 
  46. ############################################################## 

 

對安裝光盤的定製

  1. #/root/anaconda-ks.cfg . 安裝系統時由anaconda自動生成的ks.cfg文件 
  2. #vi anaconda-ks.cfg  
  3.    
  4.   
  5. # Kickstart file automatically generated by anaconda.  
  6.   
  7. #version=DEVEL  
  8. install  
  9. #cdrom  
  10. lang en_US.UTF-8  
  11. keyboard us  
  12. network --onboot yes --device eth0 --bootproto dhcp --noipv6  #這裏我把它設置爲開機DHCP自動獲取IP,默認爲--onboot no。  
  13. rootpw  --iscrypted $6$FLLlvfqZKzvs/9bu$oflTVbcg8ZDgfCvBCDeyIBrgswymV7.W6ZoKc0Ona0GT/SCmBvMokbD2Ty86GH.qMOzXKlh6fTUQJRq4C50FC1  
  14. firewall --service=ssh  
  15. authconfig --enableshadow --passalgo=sha512  
  16. selinux --enforcing  
  17. timezone --utc Asia/Shanghai  
  18. #url --url=ftp://10.20.30.2/pub ###FTP 的方式
  19. #nfs --server=10.20.30.2 --dir=/mnt/cdrom ###NFS的方式
  20. #url --url=http://10.20.30.2/linux ###HTTP的方式
  21. bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"  
  22. # The following is the partition information you requested  
  23. # Note that any partitions you deleted are not expressed  
  24. # here so unless you clear all partitions first, this is  
  25. # not guaranteed to work  
  26. zerombr  #注意此處,清空MBR  
  27. clearpart --all --initlabel #重新初始化硬盤分區  
  28.   
  29. part /boot --fstype=ext4 --size=200  #/boot分區,200M  
  30. part swap --size=1024                #swap 分區,1024M  
  31. part / --fstype=ext4 --grow --size=1 #根分區/,意爲剩下所有的空閒空間都留給/  
  32.   
  33. #repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100  #暫未明白這是什麼意思,先註釋掉  
  34.   
  35. reboot  #安裝完成後自動重啓  
  36.   
  37. %packages --nobase   #這裏定義需要安裝的包  
  38. @core                #最小化安裝只有一個core就夠了。  
  39.   
  40.   
  41. %post                #%post後面可以接一些命令、腳本。如/bin/touch /root/test.cfg,安裝完成後會在/root目錄下看到test.cfg文件  
  42. /bin/touch /root/test.cfg  
  43.   
  44. #######################################################  
  45. #下面是我創建SSH 公鑰的腳本  
  46. #######################################################  
  47. if [ ! -d /root/.ssh ]; then  
  48.         mkdir /root/.ssh/  
  49. fi  
  50.   
  51. if [ ! -f /root/.ssh/authorized_keys ]; then  
  52.         touch /root/.ssh/authorized_keys  
  53. fi  
  54.   
  55. cat <<EOF > /root/.ssh/authorized_keys  
  56. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDDZCIIaD6lCRhmRY4wwdhLcqfstvd0aT90Py9g0RTtZxkh7NcJuR/+zupmQ0Bet3UW2sGfiP3JSviQvi82QK9+orh0IC+cCboE0usgSH89nra0LLc0y2Iv816lhjh5Dmn9ubfT5uCkgnJMIu10qUqZbu2b/ypMwMe/5jEFBdG89  
  57. w  
  58. EOF  
  59.   
  60. %end  #必須的結束標誌  

 

安裝方式之一:FTP

  1. 安裝vsftpd之後默認允許匿名訪問FTP公共目錄/var/ftp/pub,所以無需對vsftd配置文件進行修改。
  2. 只需要把ks.cfg文件和光盤中的所有文件複製到/var/ftp/pub就可以了。 
  3. #cp /root/anaconda-ks.cfg /var/ftp/pub/ks.cfg 
  4. #cp -a /mnt/cdrom/* /var/ftp/pub 
  5. #分別修改ks.cfg和/tftpboot/pxelinux.cfg/default文件對應的方式

安裝方式之二:NFS

 

  1. 編輯NFS的配置文件,添加以下兩句 
  2. /tftpboot *(ro,sync)  #/tftpboot 可被網絡上任何主機讀取 
  3. /mnt/cdrom *(ro,sync) #/mnt/cdrom 可被網絡上任何主機讀取
  4. 分別修改ks.cfg和/tftpboot/pxelinux.cfg/default文件對應的方式

 

 安裝方式之三:HTTP

  1. 1,把ks.cfg和/mnt/cdrom裏的所有文件複製到apache指定的目錄下  
  2. 2,分別修改ks.cfg和/tftpboot/pxelinux.cfg/default文件對應的方式 

 

 

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