一 PXE简介
PXE: Preboot Excution Environment 预启动执行环境 Intel公司研发 基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载 映像,并由此支持通过网络启动操作系统
PXE可以引导和安装Windows,linux等多种操作系统
PXE,就是预启动执行环境,是一种引导启动的方式。
这种协议一般由两部分构成,一部分是服务器端,一个是客户端。
简单来说,我们通过这种方式可以自己创建一个“安装源”,
在安装系统的时候只要能找到这个“源”便可以实现系统的安装。
1.1 PXE工作原理
Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检
测Client的网卡MAC地址), 如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的
位置信息一并传送给Client
Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client
发送pxelinux.0大小信息,试探Client 是否满意,当TFTP收到Client发回的同意大小信息之后,
正式向Client发送pxelinux.0
Client执行接收到的pxelinux.0文件
Client向TFTP Server发送针对本机的配置信息文件(在TFTP 服务的pxelinux.cfg目录下),
TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client
Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统
Client启动Linux内核
Client下载安装源文件,读取自动化安装脚本
二 CentOS 7 PXE自动化安装
2.1 安装前准备
关闭防火墙和SELINUX,DHCP服务器静态IP
2.2 安装软件包
**yum install httpd tftp-server dhcp syslinux system-config-kickstart
xinetd **
#启动http
systemctl enable httpd
systemctl start httpd
#创建磁盘文件夹
mkdir /var/www/html/centos/7
#镜像挂载
mount /dev/sr0 /var/www/html/centos/7
2.3 准备kickstart文件
#复制/root目录下的anaconda-ks.cfg文件加以修改**
mkdir /var/www/html/ks
cp -a /root/anaconda-ks.cfg /var/www/html/ks/centos7.cfg```powershell
chmod +r /var/www/html/ks/centos7.cfg # "这里的文件需要加读权限,非常重要"
#进行修改/var/www/html/ks/centos7.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url=ftp://192.168.233.150//centos/7/ # 指明yum源的路径
# Use graphical install
text # 将cdrom修改为text,我们不是基于光盘安装的,我们是基于字符界面安装
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate # --bootproto必须是dhcp获取,--onboot=on
network --hostname=centos7.test.com #主机名
# Root password
#密码为123456
rootpw --iscrypted $6$LLxl4n9dUaNn9Z5E$aMae1s6w9S0IFUq5C7K8Mi1FIKrOmINApegn2eMuevfHM5lYTpORznsQfu38a5H8Aje9hl3c9KziPJInYm71I/
# System services
###关闭无关的服务
services --disabled="chronyd"
selinux --disabled
firewall --disabled
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#普通账户密码
user --name=zgj -password=$6$LLxl4n9dUaNn9Z5E$aMae1s6w9S0IFUq5C7K8Mi1FIKrOmINApegn2eMuevfHM5lYTpORznsQfu38a5H8Aje9hl3c9KziPJInYm71I/ --iscrypted --gecos="zgj"
# X Window System configuration information
xconfig --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
zerombr # 添加zerombr ,表示清除旧磁盘上原有的mbr,新磁盘安装可不写
clearpart --all # 清除所有分区信息,新磁盘安装可不写
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=2048 # 分区表信息,如果你想添加分区,可按照该格式添加
part / --fstype="xfs" --ondisk=sda --size=15000
part /boot --fstype="xfs" --ondisk=sda --size=1024
reboot # 安装完成之后重启
%packages # 安装软件包
@^graphical-server-environment
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@hardware-monitoring
@input-methods
@internet-browser
@multimedia
@print-client
@x11
kexec-tools
autofs # 安装autofs服务包
%end
%addon com_RedHat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
%post # 安装后脚本,我们配置了yum源
systemctl enable autofs
mkdir /etc/yum.rpeps.cd/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end
#centos6系统配置 (和上面的centos7的其他配置一样)
#version=DEVEL
install
text # 基于字符界面安装
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$hfb25YOYZDU3YZTl$VxTkHGGJGGBbr59OPnY5kTJzvJ9hb9NRwrh5FMHLIAlXh9VQ74PYoK7QzPWYN0zaJrm3mv/IP0fDkHxFglNi6/
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
url --url=http://192.168.233.150/centos/6/ # yum源的路径
selinux --disabled # 关闭selinux
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
zerombr # 清除mbr
clearpart --all # 清除分区信息
reboot # 安装完成后重启
part /boot --fstype=ext4 --size=1000 # 分区信息
part / --fstype=ext4 --size=15000
part swap --size=2048
#repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100 # 注释掉此行
%packages
@base
@core
@debugging
@basic-desktop
@desktop-debugging
@desktop-platform
@directory-client
@fonts
@general-desktop
@graphical-admin-tools
@input-methods
@internet-applications
@internet-browser
@java-platform
@kde-desktop
@legacy-x
@network-file-system-client
@office-suite
@print-client
@remote-desktop-clients
@server-platform
@server-policy
@workstation-policy
@x11
mtools
pax
python-dmidecode
oddjob
wodim
sgpio
genisoimage
device-mapper-persistent-data
abrt-gui
qt-mysql
samba-winbind
certmonger
pam_krb5
krb5-workstation
xterm
xorg-x11-xdm
libXmu
rdesktop
%end
%post # 安装后脚本,安装完系统后向做什么配置,都可以写在%post...%end中间
mkdir /etc/yum.repos.d/bak
rm -rf /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end
2.4 配置tftp服务
systemctl enable tftp.socket
systemctl start tftp.socket
2.5 配置DHCP服务
2.51 vim /etc/dhcp/dhcpd.conf
option domain-name "example.com";
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.233.0 netmask 255.255.255.0
{
range 192.168.100.1 192.168.233.200;
filename "pxelinux.0";
next-server 192.168.233.150;
}
2.5.2 启动dhcp
systemctl enable dhcpd
systemctl start dhcpd
2.6 准备相关文件
mkdir /var/lib/tftpboot/pxelinux.cfg/
mkdir /var/lib/tftpboot/contos/7 -p
cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot
cp /var/www/html/centos/7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot
cp /var/www/html/centos/7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
文件列表如下:
/var/lib/tftpboot/
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│ └── default
└── vmlinuz
2.7 准备启动菜单
Vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title PXE INSTALL MENU
label auto menu label Auto Install CentOS 7
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.233.150/ks/centos7.cfg
label manual
menu label Manual Install CentOS 7
kernel vmlinuz
append initrd=initrd.img
inst.repo=http://192.168.233.150/centos/7
label local
menu default
menu label ^Boot from local drive
localboot 0xffff
2.8 在另一台测试机上进行自动安装
出现错误: TFTP open timeout
原因: iptables限制或者selinux开启导致
最后,再次安装,安装成功。yes!