天天看點

Linux系統自動化安裝之pxe實作

Linux系統自動化安裝之pxe實作

我們要在配置檔案中配置好dhcp配置設定的位址池(這個位址池必須要和主機在同一網段) ,網關,dns等,最重要的還要配置好TFTP伺服器的位址,和引導系統的檔案名稱pxelinux.0,這樣用戶端才能夠知道tftp伺服器的位址和tftp上的那個檔案是引導檔案。range 後面跟位址池範圍,option routers 指定網關,option domain-name-servers 指定dns位址 ,filename指定啟動引導檔案的名字,next-server 指定TFTP伺服器位址。這裡需要注意的是每行結束都需要有分号。當然我們可以用 service dhcpd configtest 來測試我們的配置檔案是否存在文法錯誤。

  1、PXE是什麼?

  pxe是Preboot Excution Environment的縮寫,是intel公司研發,基于client/server的網絡模式,支援遠端主機通過網絡從遠端伺服器下載下傳鏡,并由此支援通過網絡啟動作業系統的預啟動執行環境。

  2、PXE的工作原理

Linux系統自動化安裝之pxe實作

它的工作流程是這樣的:

  1、用戶端向PXE 伺服器上的DHCP發送ip位址請求消息,DHCP檢測到用戶端是否合法(主要是檢測用戶端的網卡MAC位址)如果合法則傳回用戶端的ip位址,同時将啟動檔案pxelinx.0位置資訊一并傳給用戶端;

  2、用戶端拿到IP位址和存放啟動檔案伺服器(tftp伺服器)位址後,它就會用向tftp伺服器發送擷取pxelinux.0的請求消息,tftp伺服器接收到消息後,再向用戶端發送pxelinux.0檔案的大小,試探用戶端是否滿意,當tftp接收到用戶端發回的同意大小資訊後,tftp伺服器就正式向用戶端發送pxelinux.0檔案;

  3、用戶端收到tftp發送過來的pxelinux.0檔案後,用戶端會再向tftp伺服器發送針對本機的配置資訊檔案(在TFTP伺服器的pxelinux.cfg目錄下),TFTP伺服器将配置檔案發送給用戶端後,繼而用戶端根據配置檔案執行後續的操作;

  4、用戶端向TFTP伺服器發送請求Linux核心資訊,tftp接收到消息後将核心檔案發送給用戶端,用戶端接收到核心檔案後,又向tftp發送擷取根檔案請求的資訊,當tftp伺服器收到根檔案請求資訊後,就把根檔案發送給用戶端;

  5、用戶端此時有了Linux核心檔案和根檔案後,用戶端嘗試啟動Linux核心;

  6、用戶端下載下傳安裝源檔案,讀取自動化安裝腳本(ks.cfg)

從上面的流程我們可以知道,我們首先要配置好DHCP伺服器,TFTP伺服器,然後準備必要的啟動檔案,和配置檔案,以及自動安裝腳本。

  DHCP伺服器的搭建,以及配置

[root@localhost ~]# yum install dhcp
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.1.1-63.P1.el6.centos will be installed
--> Processing Dependency: dhcp-common = 12:4.1.1-63.P1.el6.centos for package: 12:dhcp-4.1.1-63.P1.el6.centos.x86_64
--> Running transaction check
---> Package dhcp-common.x86_64 12:4.1.1-53.P1.el6.centos will be updated
--> Processing Dependency: dhcp-common = 12:4.1.1-53.P1.el6.centos for package: 12:dhclient-4.1.1-53.P1.el6.centos.x86_64
---> Package dhcp-common.x86_64 12:4.1.1-63.P1.el6.centos will be an update
--> Running transaction check
---> Package dhclient.x86_64 12:4.1.1-53.P1.el6.centos will be updated
---> Package dhclient.x86_64 12:4.1.1-63.P1.el6.centos will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package          Arch        Version                        Repository    Size
================================================================================
Installing:
 dhcp             x86_64      12:4.1.1-63.P1.el6.centos      updates      824 k
Updating for dependencies:
 dhclient         x86_64      12:4.1.1-63.P1.el6.centos      updates      323 k
 dhcp-common      x86_64      12:4.1.1-63.P1.el6.centos      updates      145 k

Transaction Summary
================================================================================
Install       1 Package(s)
Upgrade       2 Package(s)

Total download size: 1.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): dhclient-4.1.1-63.P1.el6.centos.x86_64.rpm        | 323 kB     00:00     
(2/3): dhcp-4.1.1-63.P1.el6.centos.x86_64.rpm            | 824 kB     00:00     
(3/3): dhcp-common-4.1.1-63.P1.el6.centos.x86_64.rpm     | 145 kB     00:00     
--------------------------------------------------------------------------------
Total                                           2.0 MB/s | 1.3 MB     00:00     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <[email protected]>
 Package: centos-release-6-9.el6.12.3.x86_64 (@anaconda-CentOS-201703281317.x86_64/6.9)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating   : 12:dhcp-common-4.1.1-63.P1.el6.centos.x86_64                 1/5 
  Installing : 12:dhcp-4.1.1-63.P1.el6.centos.x86_64                        2/5 
  Updating   : 12:dhclient-4.1.1-63.P1.el6.centos.x86_64                    3/5 
  Cleanup    : 12:dhclient-4.1.1-53.P1.el6.centos.x86_64                    4/5 
  Cleanup    : 12:dhcp-common-4.1.1-53.P1.el6.centos.x86_64                 5/5 
  Verifying  : 12:dhcp-common-4.1.1-63.P1.el6.centos.x86_64                 1/5 
  Verifying  : 12:dhcp-4.1.1-63.P1.el6.centos.x86_64                        2/5 
  Verifying  : 12:dhclient-4.1.1-63.P1.el6.centos.x86_64                    3/5 
  Verifying  : 12:dhcp-common-4.1.1-53.P1.el6.centos.x86_64                 4/5 
  Verifying  : 12:dhclient-4.1.1-53.P1.el6.centos.x86_64                    5/5 

Installed:
  dhcp.x86_64 12:4.1.1-63.P1.el6.centos                                         

Dependency Updated:
  dhclient.x86_64 12:4.1.1-63.P1.el6.centos                                     
  dhcp-common.x86_64 12:4.1.1-63.P1.el6.centos                                  

Complete!
      

  說明:dhcp實作的軟體有dhcp和dnsmasq兩個軟體,我們這用的DHCP,yum安裝DHCP伺服器包後,我們再來寫配置檔案

[root@localhost ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf 
cp: overwrite `/etc/dhcp/dhcpd.conf'? y
      

  說明:我們打開/etc/dhcp/dhcpd.conf,裡面内容是注釋,它告訴我們參照/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample檔案來配置,于是乎我們就幹脆把模闆檔案複制到/etc/dhcp/下并命名為dhcpd.conf,接下來在修改即可

# DHCP server to understand the network topology.

subnet 172.16.56.0 netmask 255.255.255.0 {
        range 172.16.56.100 172.16.56.200;
        option routers 172.16.56.5;
        option domain-name-servers 172.16.56.5;
        filename "pxelinux.0";
        next-server 172.16.56.5;
}      

  說明:我們要在配置檔案中配置好dhcp配置設定的位址池(這個位址池必須要和主機在同一網段) ,網關,dns等,最重要的還要配置好TFTP伺服器的位址,和引導系統的檔案名稱pxelinux.0,這樣用戶端才能夠知道tftp伺服器的位址和tftp上的那個檔案是引導檔案。range 後面跟位址池範圍,option routers 指定網關,option domain-name-servers 指定dns位址 ,filename指定啟動引導檔案的名字,next-server 指定TFTP伺服器位址。這裡需要注意的是每行結束都需要有分号。當然我們可以用 service dhcpd configtest 來測試我們的配置檔案是否存在文法錯誤。

  接着我們配置好DHCP伺服器後,就可以啟動DHCP服務了

[root@localhost ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                            [  OK  ]
[root@localhost ~]# ss -uln
State       Recv-Q Send-Q           Local Address:Port             Peer Address:Port 
UNCONN      0      0                            *:67                          *:*     
UNCONN      0      0                            *:68                          *:*     
UNCONN      0      0                    127.0.0.1:737                         *:*     
UNCONN      0      0                            *:55660                       *:*     
UNCONN      0      0                            *:111                         *:*     
UNCONN      0      0                            *:631                         *:*     
UNCONN      0      0                            *:679                         *:*     
UNCONN      0      0                           :::111                        :::*     
UNCONN      0      0                           :::49813                      :::*     
UNCONN      0      0                           :::679                        :::*     
[root@localhost ~]# 
      

  說明:我們可以看到udp的67、68端口已經起來了,說明我們的DHCP伺服器已經正常啟動。

  測試DHCP服務是否正常,用戶端是否能夠順利拿到位址,在同一區域網路裡另外拿一台主機,在上面用dhclient 工具來拿位址

[root@host_A ~]# dhclient -d
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/00:0c:29:47:f6:ed
Sending on   LPF/eth0/00:0c:29:47:f6:ed
Sending on   Socket/fallback
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x758b581e)
DHCPACK from 172.16.56.5 (xid=0x758b581e)
bound to 172.16.56.100 -- renewal in 282 seconds.
      

  說明:我們可以看到用戶端已經拿到了位址,當然我們也可以在服務端看到那些位址是配置設定出去了,配置設定給誰,租期是好久等資訊。

[root@localhost ~]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.1.1-P1

server-duid "\000\001\000\001%Q\205x\000\014)\350\366{";

lease 172.16.56.100 {
  starts 0 2019/11/03 12:34:19;
  ends 0 2019/11/03 12:44:19;
  cltt 0 2019/11/03 12:34:19;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:47:f6:ed;
}
[root@localhost ~]# 
      

  說明:在DHCP伺服器的/var/lib/dhcpd/dhcpd.leases 檔案中記錄了伺服器配置設定出去的位址,用戶端的MAC 租期等資訊。到此我們DHCP伺服器的搭建就完成了。

  接下來我們在來搭建tftp伺服器。

[root@localhost yum.repos.d]# yum install -y tftp
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tftp.x86_64 0:0.49-8.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package          Arch               Version                  Repository          Size
=======================================================================================
Installing:
 tftp             x86_64             0.49-8.el6               mybase              32 k

Transaction Summary
=======================================================================================
Install       1 Package(s)

Total download size: 32 k
Installed size: 45 k
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : tftp-0.49-8.el6.x86_64                                              1/1 
  Verifying  : tftp-0.49-8.el6.x86_64                                              1/1 

Installed:
  tftp.x86_64 0:0.49-8.el6                                                             

Complete!
[root@localhost yum.repos.d]# yum install -y tftp-server
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tftp-server.x86_64 0:0.49-8.el6 will be installed
--> Processing Dependency: xinetd for package: tftp-server-0.49-8.el6.x86_64
--> Running transaction check
---> Package xinetd.x86_64 2:2.3.14-40.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package              Arch            Version                    Repository       Size
=======================================================================================
Installing:
 tftp-server          x86_64          0.49-8.el6                 mybase           39 k
Installing for dependencies:
 xinetd               x86_64          2:2.3.14-40.el6            mybase          122 k

Transaction Summary
=======================================================================================
Install       2 Package(s)

Total download size: 161 k
Installed size: 317 k
Downloading Packages:
---------------------------------------------------------------------------------------
Total                                                  6.0 MB/s | 161 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 2:xinetd-2.3.14-40.el6.x86_64                                       1/2 
  Installing : tftp-server-0.49-8.el6.x86_64                                       2/2 
  Verifying  : 2:xinetd-2.3.14-40.el6.x86_64                                       1/2 
  Verifying  : tftp-server-0.49-8.el6.x86_64                                       2/2 

Installed:
  tftp-server.x86_64 0:0.49-8.el6                                                      

Dependency Installed:
  xinetd.x86_64 2:2.3.14-40.el6                                                        

Complete!
[root@localhost yum.repos.d]# 
      

  說明:TFTP伺服器就隻需要安裝tftp-server這個包就可以了,tftp這個包是用戶端工具,友善測試。我們裝好TFTP可以用rpm -ql tftp-server指令來檢視,這個包到底安裝了些什麼檔案,裝在什麼地方去了

[root@localhost yum.repos.d]# rpm -ql tftp-server
/etc/xinetd.d/tftp
/usr/sbin/in.tftpd
/usr/share/doc/tftp-server-0.49
/usr/share/doc/tftp-server-0.49/CHANGES
/usr/share/doc/tftp-server-0.49/README
/usr/share/doc/tftp-server-0.49/README.security
/usr/share/doc/tftp-server-0.49/README.security.tftpboot
/usr/share/man/man8/in.tftpd.8.gz
/usr/share/man/man8/tftpd.8.gz
/var/lib/tftpboot
[root@localhost yum.repos.d]# 
      

  說明:/etc/xined.d/tftp 這個就是tftp的配置檔案,因為tftp不是獨立的守護程序,它是瞬時守護程序,有超級守護程序xinetd管理,是以它的配置檔案在xinetd.d目錄下;/usr/sbin/in.tftpd 這個檔案就是tftp二進制主程式;/var/lib/tftpboot 這個目錄就是tftp伺服器的工作目錄,我們上傳下載下傳的檔案都是放在這個目錄下

  啟動tftp伺服器

[root@localhost ~]# chkconfig tftp on
[root@localhost ~]# chkconfig --list tftp
tftp            on
[root@localhost ~]# /etc/init.d/xinetd restart
Stopping xinetd:                                           [FAILED]
Starting xinetd:                                           [  OK  ]
[root@localhost ~]# ss -nul
State       Recv-Q Send-Q           Local Address:Port             Peer Address:Port 
UNCONN      0      0                            *:67                          *:*     
UNCONN      0      0                            *:69                          *:*     
UNCONN      0      0                    127.0.0.1:737                         *:*     
UNCONN      0      0                            *:55660                       *:*     
UNCONN      0      0                            *:111                         *:*     
UNCONN      0      0                            *:631                         *:*     
UNCONN      0      0                            *:679                         *:*     
UNCONN      0      0                           :::111                        :::*     
UNCONN      0      0                           :::49813                      :::*     
UNCONN      0      0                           :::679                        :::*     
[root@localhost ~]# 
      

  說明:TFTP預設工作在udp的69号端口,可以看到69号端口已經啟動。

  測試:我們在TFTP伺服器上放一些檔案,看能不能下載下傳下來,如果能,這我們的tftp伺服器就搭建的沒有問題

[root@localhost tmp]# echo "this is test file" >> f1
[root@localhost tmp]# ls
f1
[root@localhost tmp]# cp f1 /var/lib/tftpboot/
[root@localhost tmp]# ll /var/lib/tftpboot/
total 8
-rw-------. 1 root root 1591 Nov  3 21:06 anaconda-ks.cfg
-rw-r--r--. 1 root root   18 Nov  3 21:09 f1
[root@localhost tmp]# cd 
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  install.log         Music     Public     Videos
Desktop          Downloads  install.log.syslog  Pictures  Templates
[root@localhost ~]# tftp 172.16.56.5
tftp> get f1
tftp> q
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  f1           install.log.syslog  Pictures  Templates
Desktop          Downloads  install.log  Music               Public    Videos
[root@localhost ~]# cat f1
this is test file
[root@localhost ~]# 
      

  說明:我們搭建的TFTP伺服器器能夠下載下傳檔案,至此TFTP伺服器就搭建完畢。

  接下來我們還需要搭建一個基于HTTP的網絡yum倉庫

[root@localhost yum.repos.d]# yum install httpd -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Package httpd-2.2.15-59.el6.centos.x86_64 already installed and latest version
Nothing to do
[root@localhost yum.repos.d]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]
[root@localhost yum.repos.d]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir centos/6/os/x86_64 -pv
mkdir: created directory `centos'
mkdir: created directory `centos/6'
mkdir: created directory `centos/6/os'
mkdir: created directory `centos/6/os/x86_64'
[root@localhost html]# mount /dev/sr0 /var/www/html/centos/6/os/x86_64/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost html]# ls centos/6/os/x86_64/
CentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-Debug-6
EFI              Packages                  RPM-GPG-KEY-CentOS-Security-6
EULA             RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Testing-6
GPL              repodata                  TRANS.TBL
images           RPM-GPG-KEY-CentOS-6
[root@localhost html]# 
      

  說明:yum倉庫的搭建請參考https://www.cnblogs.com/qiuhom-1874/p/11487456.html 這裡不作過多解釋

  system-config-kickstart圖形工具生成kickstart應答檔案ks.cfg

  1、先安裝system-config-kickstart

[root@localhost ~]# yum install -y system-config-kickstart
      

  2、在圖形界面打開一個終端輸入system-config-kickstart啟動system-config-kickstart編輯器

Linux系統自動化安裝之pxe實作

  說明:上圖是基本配置頁面,裡面可以指定系統的語言,鍵盤布局,時區的設定,root密碼,安裝平台,安裝後是否重新開機,以什麼模式安裝。

Linux系統自動化安裝之pxe實作

  說明:上圖是安裝方法的配置,可以配置系統是新裝還是更新,安裝源,就是我們鏡像存放的位置。

Linux系統自動化安裝之pxe實作

  說明:上圖是bootloader選項配置,在此頁面可以配置bootloader安裝類型,grub密碼,boot loader安裝在那個位置,還可以寫核心參數。

Linux系統自動化安裝之pxe實作

  說明:上圖是分區資訊配置,在此頁面可以配置清除原有MBR,是否清除原有的分區資訊,是否初始化磁盤标簽,以及分區政策,以及raid的配置

Linux系統自動化安裝之pxe實作

  說明:此頁面是網絡配置頁面

Linux系統自動化安裝之pxe實作

  說明:此頁面是認證方法的配置,一般我們不該它的預設配置,它預設配置是本地驗證,也就是用/etc/passwd檔案裡的内容來驗證,當然也可以根據自己的實際情況去配置,可以支援ldap,nis等驗證

Linux系統自動化安裝之pxe實作

  說明:此頁面是防火牆和selinux配置頁面

Linux系統自動化安裝之pxe實作

  說明:此頁面是顯示配置,可以配置是否安裝圖形環境,是否第一次啟動時初始化

Linux系統自動化安裝之pxe實作

  說明:以上頁面上包的選擇配置頁面,我們需要裝什麼包就在裡面打對勾就好

Linux系統自動化安裝之pxe實作

  說明:此頁面是我們寫安裝前的腳本,我們希望系統安裝前需要幹什麼事可以用腳本的方式告訴它

Linux系統自動化安裝之pxe實作

  說明:此頁面是定制安裝後需要跑的腳本内容,如上,我們寫了一個yum倉庫的配置,系統安裝後自動就會有此倉庫配置

  配置好以上後我們找到檔案按鈕,然後點選裡面的save ,然後選擇位置,就可以把我們之前的選項生成一個kictstart檔案

Linux系統自動化安裝之pxe實作
Linux系統自動化安裝之pxe實作
[root@localhost Desktop]# cat ks.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="http://172.16.56.5/centos/6/os/x86_64"
# Root password
rootpw --plaintext admin
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone  Africa/Abidjan
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --location=mbr --password="admin"
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel 
# Disk partitioning information
part /home --fstype="ext4" --size=10240
part swap --fstype="swap" --size=2048
part / --fstype="ext4" --size=40960
part /data --fstype="ext4" --size=20480
part /boot --fstype="ext4" --size=500

%post
mkdir /etc/yum.repos.d/bak_repo
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak_repo 
cat > /etc/yum.repos.d/myrepo.repo<< EOF 
[mybase] 
name=mybase 
baseurl=http://172.16.56.5/centos/6/os/x86_64 
gpgcheck=0 
EOF 
%end 
%packages 
@base 
%end      

  至此我們用system-config-kickstart工具就制作好了我們的應答檔案。

  準備啟動相關檔案

[root@localhost tftpboot]# yum install -y syslinux
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package syslinux.x86_64 0:4.04-3.el6 will be installed
--> Processing Dependency: syslinux-nonlinux for package: syslinux-4.04-3.el6.x86_64
--> Running transaction check
---> Package syslinux-nonlinux.noarch 0:4.04-3.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package                    Arch            Version              Repository       Size
=======================================================================================
Installing:
 syslinux                   x86_64          4.04-3.el6           mybase          346 k
Installing for dependencies:
 syslinux-nonlinux          noarch          4.04-3.el6           mybase          584 k

Transaction Summary
=======================================================================================
Install       2 Package(s)

Total download size: 929 k
Installed size: 2.2 M
Downloading Packages:
---------------------------------------------------------------------------------------
Total                                                   26 MB/s | 929 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : syslinux-nonlinux-4.04-3.el6.noarch                                 1/2 
  Installing : syslinux-4.04-3.el6.x86_64                                          2/2 
  Verifying  : syslinux-4.04-3.el6.x86_64                                          1/2 
  Verifying  : syslinux-nonlinux-4.04-3.el6.noarch                                 2/2 

Installed:
  syslinux.x86_64 0:4.04-3.el6                                                         

Dependency Installed:
  syslinux-nonlinux.noarch 0:4.04-3.el6                                                

Complete!
[root@localhost tftpboot]# find / -name pxelinux.0
/usr/share/syslinux/pxelinux.0
[root@localhost tftpboot]#      

  說明:在centos7我需要安裝syslinux這個包,因為這個包裡有我們需要的pxelinux.0這個檔案,而在centos6上需要安裝syslinux-nolinux這個包,當然在centos6上安裝syslinux這個包,預設會把它依賴的syslinux-nolinux這個包給安裝上。是以我們在centos6上安裝syslinux也是可以的,隻不過我們要用find指令去查找才能知道pxelinux.0這個檔案的具體位置,rpm -ql syslinux裡是查不到的。當然我們推薦還是隻裝syslinux-nolinux這個包就好了。

[root@localhost tftpboot]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@localhost tftpboot]# pwd
/var/lib/tftpboot
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
[root@localhost tftpboot]# cp /misc/cd/images/pxeboot/{vmlinuz,initrd.img} . 
[root@localhost tftpboot]# cp /misc/cd/isolinux/{boot.msg,vesamenu.c32,splash.jpg} .
[root@localhost tftpboot]# tree
.
├── boot.msg
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
├── splash.jpg
├── vesamenu.c32
└── vmlinuz

1 directory, 6 files
[root@localhost tftpboot]#       

  說明:我們需要vmlinuz(壓縮的核心檔案),initrd.img(根檔案,存放系統啟動需要挂載的驅動),pxelinux.0(系統引導檔案,作用類似bootloader),boot.msg,vesamenu.c32,splash.jpg(這個圖檔就是我們裝系統時選擇怎麼安裝後面那個背景圖檔)

  準備啟動菜單檔案

[root@localhost tftpboot]# cp /misc/cd/isolinux/isolinux.cfg pxelinux.cfg/default
[root@localhost tftpboot]# cat pxelinux.cfg/default 
default vesamenu.c32
#prompt 1
timeout 60

display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.9!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000

label auto
  menu label ^Auto install  an  system
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=http://172.16.56.5/centos/6/ks/centos6.cfg
label local
  menu label Boot from ^local drive
  localboot 0xffff
[root@localhost tftpboot]# 
      

  說明:啟動菜單在CD光牒的ioslinux目錄下就有一個isolinux.cfg檔案,這個檔案定義了啟動菜單内容,和ks自動安裝腳本的位置。

  測試

  建立一個虛拟機,把網絡和pxe伺服器設定在同一區域網路裡,然後打開電源

Linux系統自動化安裝之pxe實作

  說明:啟動虛拟機時,尋找DHCP伺服器

Linux系統自動化安裝之pxe實作

  說明:找到DHCP伺服器後,顯示我們之前定制的菜單

Linux系統自動化安裝之pxe實作

  說明:加載vmlinuz和initrd.img檔案

Linux系統自動化安裝之pxe實作

  說明:自動安裝我們選擇需要裝的包

Linux系統自動化安裝之pxe實作

  說明:系統自動安裝完畢,自動重新開機後,登入系統的界面。至此pxe自動安裝系統環境我們就搭建完畢。

  以上是用pxe安裝centos6系統,接下來我們再說說安裝centos7系統

  pxe安裝centos7系統和安裝centos6系統流程和工作過程都是一樣的

  第一步安裝系統所需要的包,并把相應的服務設定為開機啟動

yum install httpd dhcp tftp-server syslinux -y
systemctl enable httpd dhcpd tftp
      

  說明:若pxe伺服器是centos7用以上指令

yum install httpd dhcp tftp-server syslinux -y
chkconfig httpd on
chkconfig dhcpd on
chkconfig tftp on
      

  說明:若pxe伺服器上centos6用以上指令

  第二步準備好yum源,并啟動httpd服務

mkdir /var/www/html/centos/7/os/x86_64/ -p    
mount /dev/sr1 /var/www/html/centos/7/os/x86_64/

/etc/init.d/httpd   start
      

  說明:在新加的光驅,要讓系統識别,我們要用 echo '- - -' > /sys/class/scsi_host/host2/scan(有的機子上是host0)指令更新一下,不然lsblk看不到對應的裝置,也就無法挂載

  第三步配置好dhcp伺服器,啟動dhcpd,tftp服務,這個和上面有一樣,這裡就不再羅嗦

  第四步準備ks檔案,并放到yum倉庫中

mkdir /var/www/html/centos/7/ks -p
cp centos7.cfg /var/www/html/centos/7/ks/
      

  說明:制作做ks檔案可以參照centos6上的做法,若pxe伺服器是centos7 要注意一點的是在我們選擇選擇包的界面有可能出現的是空白頁面無法選擇,若出現這種問題,我們需要把自己的yum源名字改成development就可以了,其他步驟同centos6一樣

  第五步準備相應的檔案

mkdir /var/lib/tftpboot/pxelinux.cfg/
cp /usr/share/syslinux/{menu.c32,pxelinux.0} /var/lib/tftpboot/
cp /var/www/html/centos/7/os/x86_64/ioslinux.cfg/{vmlinuxz,initrd.img} /var/lib/tftpboot/
cp /var/www/html/centos/7/os/x86_64/ioslinux.cfg/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default      

  第六步準備啟動菜單

vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 100
menu title PXE Install CentOS

label mini
  menu label ^Auto Install Mini CentOS 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.0.218/centos/7/ks/centos7.cfg
      

  至此pxe伺服器自動安裝centos7就搭建好了。測試和上面centos6是一樣的,這裡就不多闡述。

作者:Linux-1874

出處:https://www.cnblogs.com/qiuhom-1874/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀