PXE(Preboot Excution Environment): 预启动执行环境,Intel公司研发,基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统。
PXE可以引导和安装Windows,linux等多种操作系统
PXE工作原理:
准备一台主机作为网络环境内的dhcp服务器、tftp服务器、http服务器,在此主机上配置PXE环境,为网络中的其它主机提供自动化系统安装服务。
root&Centos7: ~#systemctl disable firewalld.service
#禁止防火墙开机自启动
root&Centos7: ~#systemctl stop firewalld.service
#关闭防火墙
root&Centos7: ~#iptables -vnL
root&Centos7: ~#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#禁用SElinux策略
root&Centos7: ~# grep SELINUX=disabled /etc/selinux/config
#确定SELINUX=disabled,已经将SElinux策略禁用
root&Centos7: ~#setenforce 0
#设置SELinux当前状态为Permissive
root&Centos7: ~# getenforce
#获取SELinux当前状态
root&Centos7: ~# nmcli connection show
#查看网卡名
NAME UUID TYPE DEVICE
ens32 af3fea87-cb81-4a24-8ae4-afb54afa551c 802-3-ethernet ens32
root&Centos7: ~#nmcli connection modify "ens32" ipv4.addresses 192.168.10.43/24 ipv4.gateway 192.168.10.1
#设置ens32网卡的IP地址为静态地址
root&Centos7: ~#yum install -y dhcp tftp-server httpd syslinux
#一次性安装完成dhcp tftp-server httpd syslinux四个软件包
root&Centos7: ~#systemctl enable httpd.service
root&Centos7: ~#systemctl start httpd.service
root&Centos7: ~#ss -ntl |grep 80
root&Centos7: ~#mkdir -pv /var/www/html/centos/{6,7}
root&Centos7: ~#mount /dev/sr0 /var/www/html/centos/6/
root&Centos7: ~#mount /dev/sr1 /var/www/html/centos/7/
root&Centos7: ~#ls /var/www/html/centos/{6,7}
root&Centos7: ~#mkdir /var/www/html/ksdir/
root&Centos7: ~#cp -p /root/anaconda7-ks.cfg /var/www/html/ksdir/ks7-1.cfg
root&Centos7: ~#cp -p /root/anaconda6-ks.cfg /var/www/html/ksdir/ks6-1.cfg
root&Centos7: ~#ls /var/www/html/ksdir/
root&Centos7: ~#chmod +r /var/www/html/ksdir/*.cfg
#为应答文件增加读权限,此步骤很重要,请确保执行。
root&Centos7: ~#vim /var/www/html/ksdir/ks7-1.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
url --url=http://192.168.10.43/centos/7 #指定安装使用的yum源路径
# Use graphical install
#graphical
text #使用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=ens32 --onboot=on --ipv6=auto --activate
network --hostname=localhost.localdomain
# Root password #设置root密码
rootpw --iscrypted $6$KvKpJji3uieQkGBS$5s1fuvxc0WcOW77438w.bLZJwTxV8afFC.NL6X0zCfRj8pfrldm37lKgR5iSsY.z9pNd7Q9rxvqfIU0O1CUnX.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --ntpservers=0.centos.pool.ntp.org,1.centos.pool.ntp.org,2.centos.pool.ntp.org,3.centos.pool.ntp.org
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
#clearpart --none --initlabel
zerombr #安装时自动清空MBR
clearpart --all #清空全部分区
reboot #安装完成后自动重启
# Disk partitioning information #分区信息
part swap --fstype="swap" --ondisk=sda --size=2048
part /app --fstype="xfs" --ondisk=sda --size=51200
part / --fstype="xfs" --ondisk=sda --size=51200
part /boot --fstype="xfs" --ondisk=sda --size=1024
%packages #要安装的软件包
@^minimal
@core
chrony
kexec-tools
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 #安装完成后要执行的脚本
systemctl enable autofs
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end
root&Centos7: ~#vim /var/www/html/ksdir/ks6-1.cfg
# Kickstart file automatically generated by anaconda.
#version=DEVEL
install
#cdrom
url --url=http://192.168.10.43/centos/6
text
reboot
zerombr
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$fo5PVsYpQzE2RC..$vZ2FT3sHNJBR2aopg8uzWWM2.59BykYcelOD7rBryYUuYpeNKpqAneREqaaO4x3btGdBJGbc4vHvjwaGLBGVG1
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc 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
#clearpart --none
clearpart --all
part /boot --fstype=ext4 --size=1000
part / --fstype=ext4 --size=50000
part /app --fstype=ext4 --size=50000
part swap --size=2000
#repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100
%packages
@core
@server-policy
@workstation-policy
%end
root&Centos7: ~#yum install system-config-kickstart -y
root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks7-1.cfg
root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks6-1.cfg
root&Centos7: ~#systemctl enable tftp.socket
root&Centos7: ~#systemctl start tftp.socket
root&Centos7: ~#ss -nul|grep 69
默认dhcp配置文件内没有配置信息,可以使用配置示例文件将配置文件覆盖后进行修改,dhcp服务必须在更改配置文件后才能启动。dhcp配置文件必须要dhcp服务器所在网段的地址池。
root&Centos7: ~#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
#制作配置文件
root&Centos7: ~#vim /etc/dhcp/dhcpd.conf
#编辑配置文件
# DHCP server to understand the network topology.
subnet 192.168.10.0 netmask 255.255.255.0 { #设置地址段
range 192.168.10.50 192.168.10.100; #设置地址池
option routers 192.168.10.1; #设置网关
next-server 192.168.10.43; #提供引导文件的服务器ip ,用于网络安装操作系统时
filename "pxelinux.0"; #指明引导文件名称
}
root&Centos7: ~#systemctl enable dhcpd
#将dhcp服务设置为开机自启动
root&Centos7: ~#systemctl start dhcpd
#启动dhcp服务
root&Centos7: ~#ss -nul|grep 67
#检查确认dhcp服务已经启动
root&Centos7: ~#mkdir /var/lib/tftpboot/pxelinux.cfg/
root&Centos7: ~#mkdir /var/lib/tftpboot/centos{6,7}
root&Centos7: ~#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
root&Centos7: ~#cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos7
root&Centos7: ~#cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos6
root&Centos7: ~#cp /misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
root&Centos7: ~#tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│ ├── initrd.img
│ └── vmlinuz
├── centos7
│ ├── initrd.img #伪文件系统文件
│ └── vmlinuz #内核文件
├── menu.c32 #纯文本菜单
├── pxelinux.0 #引导文件,相当于grub。
└── pxelinux.cfg
└── default #启动菜单文件
root&Centos7: ~#vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title CentOS Linux PXE Install
label centos7 #自动安装centos7
menu label Auto Install CentOS Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://192.168.10.43/ksdir/ks7-1.cfg #应答文件所在路径
label centos6 #自动安装centos6
menu label Auto Install CentOS Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://192.168.10.43/ksdir/ks6-1.cfg
label manual7 #手动安装centos7
menu label ^Manual Install CentOS Linux 7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img inst.repo=http://192.168.10.43/centos/7
label manual6 #手动安装centos6
menu label Manual ^Install CentOS Linux 6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img inst.repo=http://192.168.10.43/centos/6
label local #本地硬盘启动
menu default #默认启动项
menu label Boot from ^local drive
localboot 0xffff
menu end
使用网络引导启动,下图是启动后的安装界面:
注意:使用虚拟机安装时,centos7内存要大于1024M,否则无法成功安装。