本文共 6642 字,大约阅读时间需要 22 分钟。
#运行脚本前提 拥有一个 ios 镜像文件
#此脚本 我使用的是 rhel-server-7.2-x86_64-dvd.iso 这个镜像
#我使用的是KVM虚拟机 将此镜像挂载到光驱上 也就是 /dev/cdrom
#如果不想使用 /dev/cdrom 也可以将你所要安装操作系统的镜像 拷贝到自己定义的文件中
#然后 用自己定义的这个文件 替换本脚本中的 /dev/cdrom
#具体可以 sed -i "/\/dev\/cdrom/s/\/dev\/cdrom/${在这个写你自己定义的文件目录}/" 此脚本名
#!/bin/bash
#获取当前机器IP地址
IP=`ifconfig | head -2 | tail -1 | awk '{print $2}'`
#获取当前机器MASK地址
MASK=`ifconfig | head -2 | tail -1 | awk '{print $4}'`
MASK1=`echo $MASK | awk -F. '{print $1}'`
if [ $MASK1 -ne 0 ];then
IP1=`echo $IP | awk -F. '{print $1}'`
MIN1=`echo $IP | awk -F. '{print $1}'`
MAX1=`echo $IP | awk -F. '{print $1}'`
else
IP1=0
MIN1=1
MAX1=254
fi
MASK2=`echo $MASK | awk -F. '{print $2}'`
if [ $MASK2 -ne 0 ];then
IP2=`echo $IP | awk -F. '{print $2}'`
MIN2=`echo $IP | awk -F. '{print $2}'`
MAX2=`echo $IP | awk -F. '{print $2}'`
else
IP2=0
MIN2=1
MAX2=254
fi
MASK3=`echo $MASK | awk -F. '{print $3}'`
if [ $MASK3 -ne 0 ];then
IP3=`echo $IP | awk -F. '{print $3}'`
MIN3=`echo $IP | awk -F. '{print $3}'`
MAX3=`echo $IP | awk -F. '{print $3}'`
else
IP3=0
MIN3=1
MAX3=254
fi
MASK4=`echo $MASK | awk -F. '{print $4}'`
if [ $MASK4 -ne 0 ];then
IP4=`echo $IP | awk -F. '{print $4}'`
MIN4=`echo $IP | awk -F. '{print $4}'`
MAX4=`echo $IP | awk -F. '{print $4}'`
else
IP4=0
MIN4=1
MAX4=254
fi
#NETIP为要配置DHCP的网络号
NETIP="$IP1.$IP2.$IP3.$IP4"
#MINIP 为要配置DCHP范围最小IP
MINIP="$MIN1.$MIN2.$MIN3.$MIN4"
#MAXIP 为要配置DCHP范围最大IP
MAXIP="$MAX1.$MAX2.$MAX3.$MAX4"
systemctl stop firewalld.service
setenforce 0
#配置本地YUM源
echo -en "正在配置本地YUM源......\t\t"
creatyum(){
echo "/dev/cdrom /mnt/ iso9660 defaults 0 0" >> /etc/fstab
mount -a &> /dev/null
n=`ls /mnt/$Date | wc -l`
if [ $n -eq 0 ];then
echo '请挂载光盘文件!!!'
exit
fi
rm -rf /etc/yum.repos.d/*
echo "[dvd]
name=dvd
baseurl=file:///mnt/
gpgcheck=0
" > /etc/yum.repos.d/dvd.repo
yum clean all &> /dev/null
}
yum repolist &> ${Date}.txt
a=`cat ${Date}.txt | tail -1 | sed 's/[,]//' | awk '{print $2}'`
rm -rf ${Date}.txt
if [ -z `echo "$a" | grep -e 's/[^0-9]//'` ];then
creatyum
elif [ $a -eq 0 ];then
creatyum
fi
echo -e "\e[32;1m[OK]\e[0m"
#部署DHCP服务器
echo -en "正在部署DHCP服务器......\t\t"
yum -y install dhcp >& /dev/null
echo '
subnet $NETIP netmask $MASK {
range $MINIP $MAXIP;
option domain-name-servers $IP;
option routers $IP;
default-lease-time 600;
max-lease-time 7200;
next-server $IP;
filename "pxelinux.0";
}' > /etc/dhcp/dhcpd.conf
sed -i "s/\$IP/$IP/" /etc/dhcp/dhcpd.conf
sed -i "s/\$MASK/$MASK/" /etc/dhcp/dhcpd.conf
sed -i "s/\$NETIP/$NETIP/" /etc/dhcp/dhcpd.conf
sed -i "s/\$MINIP/$MINIP/" /etc/dhcp/dhcpd.conf
sed -i "s/\$MAXIP/$MAXIP/" /etc/dhcp/dhcpd.conf
systemctl restart dhcpd>& /dev/null
systemctl enable dhcpd>& /dev/null
echo -e "\e[32;1m[OK]\e[0m"
#搭建TFTP服务
echo -en "正在搭建TFTP服务......\t\t"
yum -y install tftp-server.x86_64>& /dev/null
systemctl restart tftp>& /dev/null
systemctl enable tftp>& /dev/null
echo -e "\e[32;1m[OK]\e[0m"
#部署pxelinux.0文件
echo -en "正在部署pxelinux.0文件......\t\t"
yum -y install syslinux-4.05-12.el7.x86_64 >& /dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg/ >& /dev/null
cp /mnt/isolinux/initrd.img /mnt/isolinux/vmlinuz /mnt/isolinux/vesamenu.c32 /var/lib/tftpboot/
echo -e "\e[32;1m[OK]\e[0m"
#部署default默认的菜单文件
echo -en "正在部署default默认的菜单文件......\t\t"
yum -y install vsftpd >& /dev/null
systemctl restart vsftpd >& /dev/null
systemctl enable vsftpd >& /dev/null
mkdir /var/ftp/rhel7 >& /dev/null
mount /dev/cdrom /var/ftp/rhel7 >& /dev/null
echo -e "\e[32;1m[OK]\e[0m"
#搭建vsftpd服务,共享应答文件
echo -en "正在搭建vsftpd服务,共享应答文件......\t\t"
echo '
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$Vl9/l0oR$ZM8s/NJIjlPSGy6P0cMMb.
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="ftp://$IP/rhel7"
# System language
lang zh_CN
# Firewall configuration
firewall --disabled
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Network information
network --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part swap --fstype="swap" --size=1024
part / --fstype="xfs" --grow --size=1
%post --interpreter=/bin/bash
rm -rf /etc/yum.repos.d/*
echo "
[ftp]
name=ftp
baseurl=ftp://$IP/rhel7
enabled=1
gpgcheck=0
" > /etc/yum.repos.d/ftp.repo
%end
%packages
@base
%end
' > /var/ftp/ks.cfg
sed -i "s/\$IP/$IP/" /var/ftp/ks.cfg
echo -e "\e[32;1m[OK]\e[0m"
#部署default默认的菜单文件
echo -en "正在部署default默认的菜单文件......\t\t"
echo '
default vesamenu.c32
timeout 600
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 PXE SERVER
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 RHEL
kernel vmlinuz
append initrd=initrd.img ks=ftp://$IP/ks.cfg
' > /var/lib/tftpboot/pxelinux.cfg/default
sed -i "s/\$IP/$IP/" /var/lib/tftpboot/pxelinux.cfg/default
chmod 644 /var/lib/tftpboot/pxelinux.cfg/default
echo -e "\e[32;1m[OK]\e[0m"
echo -e "PXE服务部署成功\t\t"