一键部署pxe环境

系统:Centos6.5

环境:VMware Workstation12

#!/bin/bash
# Please prepare CentOS ISO image first
# root password:123456
# initialization
ip=192.168.126.11
dhcp_start=192.168.126.100
dhcp_end=192.168.126.200
subnet=192.168.126.0
netmask=255.255.255.0
broadcast=192.168.126.255
swap_size=2048


# Mount CentOS ISO
[ -e /dev/cdrom -a -e /dev/cdrom1 ]||{
echo "please insert iso image";exit
}

mkdir -p /pxe/{centos6,centos7,ks}
mount /dev/cdrom  /pxe/centos6 &>/dev/null
mount /dev/cdrom1 /pxe/centos7 &>/dev/null
echo "/dev/cdrom   /pxe/centos6  iso9660  defaults 0 0" >>/etc/fstab
echo "/dev/cdrom1  /pxe/centos7  iso9660  defaults 0 0" >>/etc/fstab

# Install NFS
echo "NFS Installing..."
yum install nfs-utils -y &>/dev/null
echo "/pxe/centos6 *(ro)" >> /etc/exports
echo "/pxe/centos7 *(ro)" >> /etc/exports
echo "/pxe/ks *(ro)" >> /etc/exports
service rpcbind restart &>/dev/null
service nfs restart &>/dev/null
chkconfig rpcbind on
chkconfig nfs on
[ `ps -ef|grep nfsd|wc -l` -gt 1 ]&& echo "NFS is ready!"|| {
 echo "NFS error";exit
}

# Install TFTP
echo "TFTP Installing..."
yum install xinetd tftp-server -y &>/dev/null
sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp
sed -i '/server_args/c\server_args	= -t -s -v /var/lib/tftpboot' /etc/xinetd.d/tftp
service xinetd restart &>/dev/null
chkconfig xinetd on
[ `ss -anu|grep :69|wc -l` -gt 0 ]&& echo "TFTP is ready!"||{
echo "TFTP error";exit
}

# Prepare Boot File
echo "syslinux Installing..."
yum install syslinux -y &>/dev/null
mkdir -p /var/lib/tftpboot/centos{6..7}
\cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
\cp /pxe/centos6/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos6
\cp /pxe/centos7/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos7
mkdir -p /var/lib/tftpboot/pxelinux.cfg

cat >/var/lib/tftpboot/pxelinux.cfg/default <<-EOF
default menu.c32
timeout 600
menu title Welcome to CentOS Automatic Install
label local
  menu label Boot from local drive
  localboot 0xffff
label centos6.5
  menu label Install CentOS 6.5 Minimal
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks=nfs:$ip:/pxe/ks/centos6.cfg
label centos7.3
  menu label Install CentOS 7.3 Minimal
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.ks=nfs:$ip:/pxe/ks/centos7.cfg
EOF
echo "The boot file is ready!"

# Install DHCP
echo "DHCP Installing..."
yum install dhcp -y &>/dev/null
cat >/etc/dhcp/dhcpd.conf <<-EOF
next-server $ip;
filename "/var/lib/tftpboot/pxelinux.0";
allow bootp;
allow booting;
subnet $subnet netmask $netmask {
  range $dhcp_start $dhcp_end;
  option domain-name-servers $ip;
  option routers $ip;
  option broadcast-address $broadcast;
}
EOF
service dhcpd restart &>/dev/null
chkconfig dhcpd on
[ `ss -anu|grep :67|wc -l` -gt 0 ]&& echo "DHCP is ready!"||{
echo "DHCP error";exit
}

# Prepare ks file

# centos6 
echo "ks file preparing..."
cat > /pxe/ks/centos6.cfg <<-EOF
firewall --disabled
install
nfs --server=$ip --dir=/pxe/centos6
rootpw --iscrypted \$1\$PWGYEp1D\$UmqHNIm./L8dLSsF4owc30
auth  --useshadow  --passalgo=sha512
text
keyboard us
lang zh_CN
selinux --disabled
skipx
logging --level=info
reboot
timezone  Asia/Shanghai
network  --bootproto=dhcp --device=eth0 --onboot=on
bootloader --append="quiet" --location=mbr
zerombr
clearpart --all  
part /boot --asprimary --fstype="ext4" --size=200      
part pv.008002 --size=1 --grow
volgroup vg --pesize=4096 pv.008002
logvol swap --fstype=swap --name=swap --vgname=vg --size=$swap_size
logvol / --fstype=ext4 --name=root --vgname=vg --size=1 --grow
%packages --nobase
@Core
%end
%post
sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i '/UseDNS/c\UseDNS no' /etc/ssh/sshd_config
%end
EOF

# centos7
\cp /pxe/ks/{centos6.cfg,centos7.cfg}
sed -i '/^nfs/s/centos6/centos7/' /pxe/ks/centos7.cfg
sed -i 's/ext4/xfs/;s/--size=200/--size=500/' /pxe/ks/centos7.cfg
sed -i '$a\%addon com_redhat_kdump --disable --reserve-mb='auto'\n%end' /pxe/ks/centos7.cfg 

echo "ks file is ready!"
echo "Installation is complete!!!"

 

posted @ 2018-07-21 21:06  fcing  阅读(574)  评论(0编辑  收藏  举报