执行脚本部署PXE安装环境
# ./pxe.sh
脚本内容如下
#!/bin/bash
yum -y install httpd tftp-server xinetd dhcp syslinux net-tools wget
DEVICE=ens32
ADDRESS=`ifconfig $DEVICE |grep netmask |awk '{print $2}'`
NETMASK=`ifconfig $DEVICE |grep netmask |awk '{print $4}'`
GATEWAY=`nmcli connection show $DEVICE |grep IP4.GATEWAY |awk '{print $2}'`
DNS=`echo $(nmcli connection show $DEVICE |grep IP4.DNS |awk '{print $2}') |awk '{gsub(/ /,","); print $0}'`
SUBNET=`nmcli connection show $DEVICE |grep IP4.ROUTE |grep -v 0.0.0.0/0 |awk '{print $4}' |awk -F '/' '{print $1}'`
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp
sed -i 's|/var/www/html|/pxe/www/html|g' /etc/httpd/conf/httpd.conf
mkdir -p /pxe/www/html/{ks,centos-7}
wget https://mirrors.ustc.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-2003.iso
mount CentOS-7-x86_64-Minimal-2003.iso /media
cp -rvf /media/* /pxe/www/html/centos-7
rm -rf CentOS-7-x86_64-Minimal-2003.iso
## Configuration dhcp server
cat > /etc/dhcp/dhcpd.conf <<- EOF
ddns-update-style interim;
ignore client-updates;
allow booting;
allow bootp;
subnet $SUBNET netmask $NETMASK {
next-server $ADDRESS;
filename "pxelinux.0";
option routers $GATEWAY;
option subnet-mask $NETMASK;
option domain-name-servers $DNS;
range dynamic-bootp ${SUBNET%.*}.200 ${SUBNET%.*}.250;
}
EOF
ln -s /var/lib/tftpboot /
mkdir -p /var/lib/tftpboot/pxelinux.cfg/
## copy file
cp /pxe/www/html/centos-7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
cp /pxe/www/html/centos-7/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/
cp /usr/share/syslinux/{vesamenu.c32,pxelinux.0} /var/lib/tftpboot/
chmod 644 /var/lib/tftpboot/pxelinux.cfg/default
## Configuration tftpboot
cat > /tftpboot/pxelinux.cfg/default <<- EOF
default vesamenu.c32
timeout 150
display boot.msg
# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13
# Border Area
menu color border * #00000000 #00000000 none
# Selected item
menu color sel 0 #ffffffff #00000000 none
# Title bar
menu color title 0 #ff7ba3d0 #00000000 none
# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none
# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none
# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none
# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none
# Help text
menu color help 0 #ffffffff #00000000 none
# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none
# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
menu label ^Install CentOS 7
menu default
kernel vmlinuz
append initrd=initrd.img inst.repo=http:///centos-7 quiet ks=http:///ks/centos-ks.cfg
menu separator # insert an empty line
# utilities submenu
menu begin ^Troubleshooting
menu title Troubleshooting
label vesa
menu indent count 5
menu label Install CentOS 7 in ^basic graphics mode
text help
Try this option out if you're having trouble installing
CentOS 7.
endtext
kernel vmlinuz
append initrd=initrd.img inst.repo=http:///centos-7 ks=http:///ks/centos-ks.cfg xdriver=vesa nomodeset quiet
label rescue
menu indent count 5
menu label ^Rescue a CentOS system
text help
If the system will not boot, this lets you access files
and edit config files to try to get it booting again.
endtext
kernel vmlinuz
append initrd=initrd.img inst.repo=http:///centos-7 ks=http:///ks/centos-ks.cfg rescue quiet
label memtest
menu label Run a ^memory test
text help
If your system is having issues, a problem with your
system's memory may be the cause. Use this utility to
see if the memory is working correctly.
endtext
kernel memtest
menu separator # insert an empty line
label local
menu label Boot from ^local drive
localboot 0xffff
menu separator # insert an empty line
menu separator # insert an empty line
label returntomain
menu label Return to ^main menu
menu exit
menu end
EOF
cat > /pxe/www/html/ks/centos-ks.cfg <<- EOF
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Install OS instead of upgrade
install
# Use network installation
url --url=http://$ADDRESS/centos-7
# Use text install
text
# Run the Setup Agent on first boot
firstboot --disable
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 --noipv6 --activate
network --hostname=localhost.localdomain
# Root password
rootpw landzf.cn
# System services
services --enabled="chronyd"
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --enabled --http --ssh
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=1024
part pv.157 --size=1 --grow
volgroup centos pv.157
logvol swap --fstype="swap" --size=2048 --name=swap --vgname=centos
logvol / --fstype="xfs" --size=1 --grow --name=root --vgname=centos
reboot
%packages
@^minimal
@core
chrony
%end
%addon com_redhat_kdump --disable --reserve-mb=auto
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS-Base.repo <<- eof
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-\$releasever - Base
#mirrorlist=https://mirrors.ustc.edu.cn/?release=\$releasever&arch=\$basearch&repo=os&infra=\$infra
baseurl=https://mirrors.ustc.edu.cn/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-\$releasever - Updates
#mirrorlist=https://mirrors.ustc.edu.cn/?release=\$releasever&arch=\$basearch&repo=updates&infra=\$infra
baseurl=https://mirrors.ustc.edu.cn/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras
#mirrorlist=https://mirrors.ustc.edu.cn/?release=\$releasever&arch=\$basearch&repo=extras&infra=\$infra
baseurl=https://mirrors.ustc.edu.cn/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus
#mirrorlist=https://mirrors.ustc.edu.cn/?release=\$releasever&arch=\$basearch&repo=centosplus&infra=\$infra
baseurl=https://mirrors.ustc.edu.cn/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
eof
yum -y install vim bash-completion
%end
%pre
parted -s /dev/sda mklabel gpt
%end
EOF
sed -i 's|\$|\\$|g' /pxe/www/html/ks/centos-ks.cfg
setenforce 0
firewall-cmd --permanent --add-service=dhcp --add-service=http --add-service=tftp
firewall-cmd --reload
systemctl enable dhcpd httpd xinetd tftp
systemctl restart dhcpd httpd xinetd tftp