文章目錄
一、簡介
1、什麼是PXE
- PXE,全名Pre-boot Execution Environment,預啓動執行環境
- PXE客戶端(client)這個術語是指機器在PXE啓動過程中的角色。一個PXE客戶端可以是一臺服務器、筆記本電腦或者其他裝有PXE啓動代碼的機器(我們電腦的網卡)。
2、PXE的工作過程
推薦看下:
注意:
1. 後面實操應該實時查看系統日誌,結合這個圖進行分析理解!
2. 上面不論是dhcp還是其他服務我們都部署在一個虛擬機上!
二、實現網絡手工安裝
1、系統環境準備
目的:實現爲數百臺空白服務器,安裝操作系統!
# 關閉防火牆
systemctl stop firewalld
# 關閉selinux
sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config # 重啓
getenforce
# 虛擬機網卡說明
本機需要兩塊網卡,一塊網卡爲NAT模式,一塊網卡爲LAN區段
配置yum源:
cd /etc/yum.repos.d/
rm -f *
mount /dev/sr0 /mnt
vim /etc/yum.repos.d/base.repo
'
name=base
baseurl=file:///mnt/
gpgcheck=0
enabled=1
'
2、安裝配置dhcp服務
// 本機的內網網卡ip爲192.168.1.201
yum install dhcp -y
[root@linux-node1 ~]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.1 192.168.1.200;
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
next-server 192.168.1.201;
filename "/pxelinux.0";
}
// 注:dhcp服務器應該和需要安裝操作系統的虛擬機在一個內網環境中,且沒有其他dhcp服務器的干擾!
systemctl restart dhcpd
3、安裝配置tftp服務
yum install tftp-server -y
[root@linux-node1 ~]# vim /etc/xinetd.d/tftp
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot // 指定目錄,保持默認,不用修改
disable = no // 由原來的yes改爲no
per_source = 11
cps = 100 2
flags = IPv4
}
systemctl start tftp.socket
systemctl start tftp.service
netstat -luntp|egrep '69|67'
4、安裝配置httpd服務
yum install httpd -y
sed -i '96i ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf
systemctl restart httpd
mkdir /var/www/html/CentOS-7.6
cp -a /mnt/* /var/www/html/CentOS-7.6/
# 注意還有兩個隱藏文件沒有拷貝過去:
cp /mnt/.discinfo /var/www/html/CentOS-7.6/
cp /mnt/.treeinfo /var/www/html/CentOS-7.6/
# 前面將centos7的鏡像文件掛載到了/mnt下,我們需要把鏡像文件複製到httpd的發佈目錄來,後面虛擬機回自己找httpd來下載
curl http://192.168.1.201/CentOS-7.6/ # 看下httpd是否成功!
5、配置PXE啓動程序
(1)PXE引導配置
syslinux是一個功能強大的引導加載程序,而且兼容各種介質。SYSLINUX是一個小型的Linux操作系統,它的目的是簡化首次安裝Linux的時間,並建立修護或其它特殊用途的啓動盤。如果沒有找到pxelinux.0這個文件,可以安裝一下。
yum install syslinux -y
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp -a /var/www/html/CentOS-6.7/isolinux/* /var/lib/tftpboot/
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cp /var/www/html/CentOS-6.7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
(2)PXE配置文件default解析
[root@linux-node1 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
label linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=http://192.168.1.201/CentOS-7.6/ quiet net.ifnames=0 biosdevname=0
// 其中"net.ifnames=0 biosdevname=0"這兩個內核啓動參數是爲了讓網卡名稱爲ethN,而不是默認的eno16777728這樣的隨機名稱。
6、測試手工網絡安裝操作系統
因爲我電腦是mac本,這裏聽說本機的內網網卡需要用lan區段,但是我這個vmware fusion好像並沒有這個網絡模式,因此我採用了僅主機模式,但是到這裏出現了問題!這個只能等以後有機會了,再補充完善吧!
(1)新建空白虛擬機
(2)啓動虛擬機
三、實現kickstart自動化安裝
1、ks.cfg詳解
ks.cfg
文件組成大致分爲3段
- 命令段
鍵盤類型,語言,安裝方式等系統的配置,有必選項和可選項,如果缺少某項必選項,安裝時會中斷並提示用戶選擇此項的選項
- 軟件包段
- %packages
- @groupname:指定安裝的包組
- package_name:指定安裝的包
- -package_name:指定不安裝的包
在安裝過程中默認安裝的軟件包,安裝軟件時會自動分析依賴關係。
- 腳本段(可選)
1. %pre:安裝系統前執行的命令或腳本(由於只依賴於啓動鏡像,支持的命令很少)
2. %post:安裝系統後執行的命令或腳本(基本支持所有命令)
2、編寫ks.cfg文件
# 先生成一個密碼備用
[root@linux-node1 ~]# grub-crypt
Password:123456
Retype password:123456
$6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
[root@linux-node1 ~]# mkdir /var/www/html/ks_config
[root@linux-node1 ~]# vim /var/www/html/ks_config/CentOS-7.6-ks.cfg
# Kickstart Configurator for CentOS 7.6 by yao zhang
install
url --url="http://192.168.1.201/CentOS-7.6/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post
wget -O /tmp/optimization.sh http://192.168.1.201/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end
3、開機優化腳本
[root@linux-node1 ~]# vim /var/www/html/ks_config/optimization.sh
#!/bin/bash
##############################################################
# File Name: /var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2015-12-03 15:23:08
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=192.168.1.201
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
echo "Http server is bad!"
exit 1
}
# Defined result function
function Msg(){
if [ $? -eq 0 ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined IP function
function ConfigIP(){
Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print $6}'`
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.0.0.$Suffix
PREFIX=24
GATEWAY=10.0.0.2
DNS1=10.0.0.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
END
Msg "config eth0"
}
# Defined Yum source Functions
function yum(){
YumDir=/etc/yum.repos.d
[ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori}
wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
[ -f "/etc/issue" ] && >/etc/issue
Msg "Hide issue"
[ -f "/etc/issue.net" ] && > /etc/issue.net
Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
[ -f "/etc/security/limits.conf" ] && {
echo '* - nofile 65535' >> /etc/security/limits.conf
Msg "open files"
}
}
# Defined Kernel parameters Functions
function kernel(){
KernelDir=/etc
[ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
Msg "Kernel config"
}
# Defined System Startup Services Functions
function boot(){
for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"`
do
chkconfig $oldboy off
done
Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
Msg "Time Synchronization"
}
# Defined main Functions
function main(){
ConfigIP
yum
HideVersion
openfiles
kernel
boot
Time
}
main
4、整合編輯default配置文件
# 最精簡配置
[root@linux-node1 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default ks
prompt 0
label ks
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.1.201/ks_config/CentOS-7.6-ks.cfg ksdevice=eth0 # 告訴安裝程序ks.cfg文件在哪裏
# ksdevice=eth0代表當客戶端有多塊網卡的時候,要實現自動化需要設置從eth1安裝,不指定的話,安裝的時候系統會讓你選擇,那就不叫全自動化了。
5、無人值守自動安裝
打開系統電源,出去喝杯水。過會回來,系統就以經裝好了._