Linux PXE自動化安裝centos6,centos7系統

  一、PXE是什麼?html

  pxe是Preboot Excution Environment的縮寫,是intel公司研發,基於client/server的網絡模式,支持遠程主機經過網絡從遠端服務器下載鏡,並由此支持經過網絡啓動操做系統的預啓動執行環境。linux

  二、PXE的工做原理ios

它的工做流程是這樣的:vim

  一、客戶端向PXE 服務器上的DHCP發送ip地址請求消息,DHCP檢測到客戶端是否合法(主要是檢測客戶端的網卡MAC地址)若是合法則返回客戶端的ip地址,同時將啓動文件pxelinx.0位置信息一併傳給客戶端;centos

  二、客戶端拿到IP地址和存放啓動文件服務器(tftp服務器)地址後,它就會用向tftp服務器發送獲取pxelinux.0的請求消息,tftp服務器接收到消息後,再向客戶端發送pxelinux.0文件的大小,試探客戶端是否滿意,當tftp接收到客戶端發回的贊成大小信息後,tftp服務器就正式向客戶端發送pxelinux.0文件;bash

  三、客戶端收到tftp發送過來的pxelinux.0文件後,客戶端會再向tftp服務器發送針對本機的配置信息文件(在TFTP服務器的pxelinux.cfg目錄下),TFTP服務器將配置文件發送給客戶端後,繼而客戶端根據配置文件執行後續的操做;服務器

  四、客戶端向TFTP服務器發送請求Linux內核信息,tftp接收到消息後將內核文件發送給客戶端,客戶端接收到內核文件後,又向tftp發送獲取根文件請求的信息,當tftp服務器收到根文件請求信息後,就把根文件發送給客戶端;網絡

  五、客戶端此時有了Linux內核文件和根文件後,客戶端嘗試啓動Linux內核;app

  六、客戶端下載安裝源文件,讀取自動化安裝腳本(ks.cfg)dom

從上面的流程咱們能夠知道,咱們首先要配置好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) <centos-6-key@centos.org>
 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的6七、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倉庫的搭建請參考http://www.javashuo.com/article/p-zkqqiusx-bk.html 這裏不做過多解釋

system-config-kickstart圖形工具生成kickstart應答文件ks.cfg

一、先安裝system-config-kickstart

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

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

 

說明:上圖是基本配置頁面,裏面能夠指定系統的語言,鍵盤佈局,時區的設定,root密碼,安裝平臺,安裝後是否重啓,以什麼模式安裝。

說明:上圖是安裝方法的配置,能夠配置系統是新裝仍是更新,安裝源,就是咱們鏡像存放的位置。

說明:上圖是bootloader選項配置,在此頁面能夠配置bootloader安裝類型,grub口令,boot loader安裝在那個位置,還能夠寫內核參數。

說明:上圖是分區信息配置,在此頁面能夠配置清除原有MBR,是否清除原有的分區信息,是否初始化磁盤標籤,以及分區策略,以及raid的配置

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

說明:此頁面是認證方法的配置,通常咱們不應它的默認配置,它默認配置是本地驗證,也就是用/etc/passwd文件裏的內容來驗證,固然也能夠根據本身的實際狀況去配置,能夠支持ldap,nis等驗證

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

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

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

說明:此頁面是咱們寫安裝前的腳本,咱們但願系統安裝前須要幹什麼事能夠用腳本的方式告訴它

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

配置好以上後咱們找到文件按鈕,而後點擊裏面的save ,而後選擇位置,就能夠把咱們以前的選項生成一個kictstart文件

[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]# 

  說明:啓動菜單在光盤的ioslinux目錄下就有一個isolinux.cfg文件,這個文件定義了啓動菜單內容,和ks自動安裝腳本的位置。

測試

新建一個虛擬機,把網絡和pxe服務器設置在同一局域網裏,而後打開電源

說明:啓動虛擬機時,尋找DHCP服務器

說明:找到DHCP服務器後,顯示咱們以前定製的菜單

說明:加載vmlinuz和initrd.img文件

說明:自動安裝咱們選擇須要裝的包

 說明:系統自動安裝完畢,自動重啓後,登陸系統的界面。至此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

  說明:在新加的光驅,要讓系統識別,咱們要用scandisk命令更新一下,否則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是同樣的,這裏就很少闡述。

相關文章
相關標籤/搜索