PXE 自动部署CentOS

9 篇文章 0 订阅
4 篇文章 0 订阅
#!/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

#  为 rc.local文件添加执行权限,该文件在CentOS7中默认没有执行权限
chmod +x /etc/rc.d/rc.local

####    自动化部署 DHCP 服务

#  安装 DHCP 服务
yum install -y dhcp

#  配置 DHCP 服务
echo '
subnet 192.168.0.0 netmask 255.255.255.0 {
    range 192.168.0.100 192.168.0.200 ;
    next-server 192.168.0.1 ;
    filename "pxelinux.0" ;
}
' > /etc/dhcp/dhcpd.conf

#  启动 DHCP 服务,并设置开机自启动
systemctl restart dhcpd
systemctl enable dhcpd

####    自动化部署 TFTP 服务

#  安装 TFTP 服务
yum install -y tftp-server

#  配置 TFTP 服务
sed -i '14s/yes/no/' /etc/xinetd.d/tftp

#  启动 TFTP 服务,并设置开机自启动
systemctl restart tftp
systemctl enable tftp

####    自动化部署 FTP 服务

#  安装 FTP 服务
yum install -y vsftpd

#  配置 FTP 默认目录位置到 /mnt/ftp
mkdir -p /mnt/ftp
echo 'anon_root=/mnt/ftp' >> /etc/vsftpd/vsftpd.conf

#  启动 FTP 服务,并设置开机自启动
systemctl restart vsftpd
systemctl enable vsftpd

####    自动化配置 PXE 核心配置部分

#  安装 syslinux
yum install -y syslinux

#  拷贝 pxelinux.0 到 TFTP 目录下
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

#  挂载 CentOS6.10 iso到 /mnt/ftp/centos/centos610,并设置开机自动挂载
mkdir -p /mnt/ftp/centos/centos610
mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/ftp/centos/centos610
echo 'mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/ftp/centos/centos610' >> /etc/rc.d/rc.local

#  挂载 CentOS7.7 iso到 /mnt/ftp/centos/centos77,并设置开机自动挂载
mkdir -p /mnt/ftp/centos/centos77
mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/ftp/centos/centos77
echo 'mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/ftp/centos/centos77' >> /etc/rc.d/rc.local

#  拷贝 CentOS7.7 光盘中 isolinux 目录下所有文件到 TFTP目录
cp /mnt/ftp/centos/centos77/isolinux/* /var/lib/tftpboot/

#  拷贝 CentOS6.10 光盘中引导文件到 TFTP目录,因为与默认CentOS7的重名,需要改下名字
cp /mnt/ftp/centos/centos610/isolinux/initrd.img /var/lib/tftpboot/initrd6.img
cp /mnt/ftp/centos/centos610/isolinux/vmlinuz /var/lib/tftpboot/vmlinuz6

#  创建PXE核心配置文件
mkdir -p  /var/lib/tftpboot/pxelinux.cfg
cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

#  设置PXE工具标题:CentOS PXE Install Tools
sed -i 's/menu title CentOS.*/menu title CentOS PXE Install Tools/' /var/lib/tftpboot/pxelinux.cfg/default

#  删掉默认光盘启动菜单内容
sed -i '61,200d' /var/lib/tftpboot/pxelinux.cfg/default

#  全新启动菜单:
#    Boot From Local Disk        默认值,从本地磁盘启动,防止误装机
#    Install CentOS 7.7  Automatic    自动执行 CentOS7.7 最小化安装
#    Install CentOS 7.7  Manual    手动安装 CentOS7.7
#    Install CentOS 6.10 Automatic    自动执行 CentOS6.10 最小化安装
#    Install CentOS 6.10 Manual    手动安装 CentOS6.10
echo '
label boot from local
  menu label Boot From Local Disk
  menu default
  localboot 0xffff

label linux
  menu label Install CentOS 7.7  Automatic
  kernel vmlinuz
  append initrd=initrd.img ks=ftp://192.168.0.1/ks/centos7.cfg

label linux
  menu label Install CentOS 7.7  Manual
  kernel vmlinuz
  append initrd=initrd.img method=ftp://192.168.0.1/centos/centos77

label linux
  menu label Install CentOS 6.10 Automatic
  kernel vmlinuz6
  append initrd=initrd6.img ks=ftp://192.168.0.1/ks/centos6.cfg

label linux
  menu label Install CentOS 6.10 Manual
  kernel vmlinuz6
  append initrd=initrd6.img method=ftp://192.168.0.1/centos/centos610

menu separator # insert an empty line

' >> /var/lib/tftpboot/pxelinux.cfg/default

#  创建 Kickstart.cfg 文件ftp目录
mkdir -p /mnt/ftp/ks

#  生成 CentOS7.7 Kickstart.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 cnblogs.C0M
# 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="ftp://192.168.0.1/centos/centos77"
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

%packages
@base
%end
' > /mnt/ftp/ks/centos7.cfg

#  生成 CentOS6.10 Kickstart.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 cnblogs.C0M
# 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="ftp://192.168.0.1/centos/centos610"
# Disk partitioning information
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="ext4" --grow --size=1

%packages
@base
%end
' > /mnt/ftp/ks/centos6.cfg

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值