#!/bin/bash
if [[ $# != 1 ]];then
echo "input a kickstart file"
exit 1
fi
#关闭防火墙
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
#关闭selinux
setenforce 0
sed -r -i 's@(^SELINUX=).*@\1disabled@' /etc/selinux/config
#安装包
yum clean all &> /dev/null
yum install -y httpd dhcp tftp-server syslinux net-tools &> /dev/null
#准备程序包下载源
mkdir -pv /var/www/html/centos/7 &> /dev/null
echo "/dev/sr0 /var/www/html/centos/7 iso9660 defaults 0 0" >> /etc/fstab
umount /dev/sr0
mount -a > /dev/null
#准备kickstart文件
mkdir -pv /var/www/html/ksdir/7 &> /dev/null
cp $1 /var/www/html/ksdir/7/
#配置dhcp
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
subnet=$(route -n | grep "eth0" | cut -d" " -f1)
netmask=$(route -n | grep "eth0" | awk '{print $3}')
iprange=$(route -n | grep "eth0" | cut -d" " -f1 | sed -r 's@(.*)[^.]+$@\1@')
IP=$(ip a | egrep "inet.*eth0" | awk -F/ '{print $1}' | awk '{print $2}')
cat > /etc/dhcp/dhcpd.conf <<END
subnet $subnet netmask $netmask {
range ${iprange}10 ${iprange}100;
next-server $IP;
filename "pxelinux.0";
}
END
eval "sed -r -i 's/([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}/$IP/' /var/www/html/ksdir/7/`basename $1`"
#启动服务
systemctl start dhcpd httpd tftp.socket &> /dev/null
systemctl enable dhcpd httpd tftp.socket &> /dev/null
#准备pxe相关文件
mkdir -pv /var/lib/tftpboot/pxelinux.cfg &> /dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot
cp /var/www/html/centos/7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot
cat > /var/lib/tftpboot/pxelinux.cfg/default <<END
default menu.c32
timeout 600
menu title CentOS7 PXE Install
label PXE centos7
menu default
menu label Install ^Mini CentOS 7
kernel vmlinuz
append initrd=initrd.img ks=http://$IP/ksdir/7/`basename $1`
label local
menu label Boot from ^local drive
localboot 0xffff
menu end
END
当网卡名是eth0时可直接使用,kickstart文件自备,可以用system-config-kickstart生成kickstart文件,属于system-config-kickstart
ksvalidator:kickstart文件语法检查工具,属于pykickstart包