#!/bin/bash
# 配置管理IP
#vi /etc/sysconfig/network-scripts/ifcfg-ens192
# 配置pxe IP 192.168.0.1/24 不能更换成其他IP
#vi /etc/sysconfig/network-scripts/ifcfg-ens224
# 配置主机名 pxe-server
#hostname pxe-server
#echo pxe-server > /etc/hostname
# 创建存放iso的目录 /mnt/iso
#mkdir -p /mnt/iso
# 下载最新版 CentOS6 和 CentOS7 iso文件(随着版本的更新,镜像网站将停止老版本的下载支持,需要自己搞定iso下载)
#wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD1.iso
#wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD2.iso
#wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-7-x86_64-Everything-1908.iso
############### 上面的部分手工执行 #####################
# 配置阿里云YUM源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装EPEL
yum install -y epel-release
# 配置阿里云EPEL源
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装常用软件
yum install -y vim wget lftp net-tools bash-completion jq git sysstat lrzsz
# 禁用防火墙
systemctl stop firewalld && systemctl disable firewalld
# 关闭SELinux
setenforce 0 && getenforce
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 优化SSH访问速度
echo 'UseDNS no' >> /etc/ssh/sshd_config
systemctl restart sshd.service
echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config
# 定义操作系统密码
passwd='cnblogs.C0M'
# 定义Cobbler Server 地址
server=192.168.0.1
# 定义TFTP Server地址
next_server=$server
# DHCP定义:网络地址
subnet=192.168.0.0
# DHCP定义:掩码
netmask=255.255.255.0
# DHCP定义:网关
gateway=$server
# DHCP定义:DNS
dns=$server
# DHCP定义:地址段起
ip_start=192.168.0.101
# DHCP定义:地址段止
ip_stop=192.168.0.200
# 安装cobbler及相关服务
yum install -y cobbler cobbler-web pykickstart dhcp
# 设置cobbler动态修改配置,比改配置文件要方便
sed -i 's/allow_dynamic_settings: 0/allow_dynamic_settings: 1/' /etc/cobbler/settings
# 启动相关服务,并设置为开机自启动,因为dhcp还没有配置,暂时先不启动
systemctl start cobblerd.service && systemctl enable cobblerd.service
systemctl start httpd.service && systemctl enable httpd.service
systemctl start rsyncd.service && systemctl enable rsyncd.service
systemctl start tftp.socket && systemctl enable tftp.socket
systemctl enable dhcpd.service
# DHCP配置:网络地址
sed -i 's@192.168.1.0@'$subnet'@' /etc/cobbler/dhcp.template
# DHCP配置:掩码
sed -i 's@255.255.255.0@'$netmask'@' /etc/cobbler/dhcp.template
# DHCP配置:网关
sed -i 's@192.168.1.5;@'$gateway';@' /etc/cobbler/dhcp.template
# DHCP配置:DNS
sed -i 's@192.168.1.1;@'$dns';@' /etc/cobbler/dhcp.template
# DHCP配置:地址段起
sed -i 's@192.168.1.100@'$ip_start'@' /etc/cobbler/dhcp.template
# DHCP配置:地址段止
sed -i 's@192.168.1.254@'$ip_stop'@' /etc/cobbler/dhcp.template
# 设置cobbler server对外能访问的ip
cobbler setting edit --name=server --value=$server
# 设置next_server,即tftp的服务器地址
cobbler setting edit --name=next_server --value=$server
# 设置有cobbler自动管理dhcp
cobbler setting edit --name=manage_dhcp --value=1
# 在tftp配置文件中设置启用tftp
sed -i '14s/yes/no/' /etc/xinetd.d/tftp
# 在线从cobbler官网下载 boot loaders
cobbler get-loaders
# cobbler配置完毕,执行下面命令重新加载配置,并自动配置相关服务
cobbler sync
# 创建centos ISO挂载目录
mkdir -p /mnt/centos/centos6
mkdir -p /mnt/centos/centos7
# 挂载centos ISO
mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/centos/centos6
mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/centos/centos7
# 创建centos ks文件
echo '
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# System keyboard
keyboard us
# System language
lang en_US
# Root password
rootpw --plaintext '$passwd'
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all
# Use network installation
url --url=$tree
# Disk partitioning information
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="ext4" --grow --size=1
%packages
@base
%end
' > /var/lib/cobbler/kickstarts/ks6.cfg
echo '
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# System keyboard
keyboard us
# System language
lang en_US
# Root password
rootpw --plaintext '$passwd'
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all
# Use network installation
url --url=$tree
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1
%packages
@base
%end
' >/var/lib/cobbler/kickstarts/ks7.cfg
# cobbler导入centos
cobbler import --name=centos610 --path=/mnt/centos/centos6 --kickstart=/var/lib/cobbler/kickstarts/ks6.cfg
cobbler import --name=centos77 --path=/mnt/centos/centos7 --kickstart=/var/lib/cobbler/kickstarts/ks7.cfg
echo -e "Cobbler部署完毕,可以自动安装centos6.10 和centos7.7。\n另外还可以使用Cobbler Web进行管理 \n https://$server/cobbler_web \n 用户名 cobbler \n 密码 cobbler"