#!/usr/bin/env bash
# 文件名: kickstart_deploy_auto.sh

ipaddr=10.0.0.247
gateway=10.0.0.254
network=10.0.0.0
range="10.0.0.1 10.0.0.100"
pxe_dir=/data/os/centos7
boot_dir=/var/lib/tftpboot
iso_path=/data/iso/CentOS-7-x86_64-Minimal-1511.iso
repo_url=http://mirrors.163.com/.help/CentOS7-Base-163.repo

[[ ! -e ${iso_path} ]] && echo "The iso file[${iso_path}] does not exist, exit!" && exit 1

mkdir -p ${pxe_dir} ${boot_dir}/pxelinux.cfg ${boot_dir}/centos7

yum -y install syslinux xinetd tftp-server dhcp httpd

cp /usr/share/syslinux/pxelinux.0 ${boot_dir}

sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp

cat >/etc/dhcp/dhcpd.conf<<EOF
option domain-name "vqiu.cn";
option domain-name-servers dhcp.vqiu.cn;
filename        "pxelinux.0";
next-server     ${ipaddr};
default-lease-time 600;
max-lease-time 7200;
authoritative;

subnet ${network} netmask 255.255.255.0 {
    range dynamic-bootp ${range};
    option broadcast-address 10.0.0.255;
    option routers ${gateway};
}
EOF

mount -t iso9660 -o loop ${iso_path} ${pxe_dir}
\cp ${pxe_dir}/images/pxeboot/vmlinuz ${boot_dir}/centos7
\cp ${pxe_dir}/images/pxeboot/initrd.img ${boot_dir}/centos7
\cp /usr/share/syslinux/menu.c32 ${boot_dir}
\cp /usr/share/syslinux/chain.c32 ${boot_dir}

cat >${boot_dir}/pxelinux.cfg/default<<EOF
timeout 100
default menu.c32
menu title ########## PXE Boot Menu ##########
label 1
   menu label ^1) Boot from PXE Server for AUTO Install CentOS 7
   kernel centos7/vmlinuz
   append initrd=centos7/initrd.img ks=http://${ipaddr}/ks/centos7-ks.cfg

label 2
   menu label ^2) Boot from PXE Server for Manual Install CentOS 7
   kernel centos7/vmlinuz
   append initrd=centos7/initrd.img method=http://${ipaddr}/centos7 devfs=nomount

label 3
   menu label ^3) Boot from local drive
   localboot
EOF

\rm /etc/httpd/conf.d/*
mkdir /var/www/html/ks
cat >/var/www/html/ks/centos7-ks.cfg<<EOF
#version=DEVEL
auth --enableshadow --passalgo=sha512
url --url=http://${ipaddr}/centos7/
firstboot --enable
ignoredisk --only-use=sda
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
network --bootproto=dhcp --ipv6=auto --activate --hostname=localhost.localdomain

# Root password
rootpw --iscrypted \$6\$310uHXrlaTVY/bFk\$ZeUMXI.kT61A63bUS/uokaXpSPyKg4sHg0qjpSf5ihkHexEiXWIZuKIrGRSxe1W8MTc10J85vgpuXpMBTt3za1
#rootpw --plaintext 123456
selinux --disabled
services --enabled="chronyd"
services  --disabled=postfix

timezone Asia/Shanghai --isUtc
bootloader --location=mbr --boot-drive=sda --append="rhgb quiet net.ifnames=0 biosdevname=0"

# initialize all partition tables
zerombr
clearpart --all --initlabel

# partitioning
part /boot --fstype="xfs" --ondisk=sda --size=500 --fsoptions="noatime,nodiratime"
part swap --fstype="swap" --ondisk=sda --size=4096
part /  --fstype="xfs" --ondisk=sda --grow

repo --name=base --baseurl=${repo_url}

# Reboot after install
reboot
%packages
@core
%end
EOF

cat >/etc/httpd/conf.d/pxeboot.conf<<EOF
Alias /centos7 ${pxe_dir}
<Directory ${pxe_dir}>
    Options Indexes FollowSymLinks
    # IP address you allow to access

    Require ip 127.0.0.1 ${network}/24
</Directory>
EOF

systemctl restart xinetd dhcpd httpd
systemctl enable xinetd dhcpd httpd
