通过PXE自动安装操作系统
脚本下载链接
#!/bin/bash
#
#********************************************************************
#Author: chenjiahao
#QQ: 1938191576
#Date: 2022-10-18
#FileName: PXE.sh
#URL: https://www.placjh.com
#Description: The deploy script
#Copyright (C): 2022 All rights reserved
#********************************************************************
#关闭selinux
setenforce 0
grep 'SELINUX=enforcing' /etc/selinux/config || sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#关闭firewalld
systemctl status firewalld && systemctl disable --now firewalld
#安装所需软件
yum -y install tftp-server xinetd dhcp syslinux vsftpd
#配置tftp
sed -n '/wait/ s/yes/no/p' /etc/xinetd.d/tftp
sed -n '/disable/ s/yes/no/p' /etc/xinetd.d/tftp
#配置DHCP
cat >> /etc/dhcp/dhcpd.conf <<-EOF
subnet 100.100.129.0 netmask 255.255.255.0 {
range 100.100.129.50 100.100.129.100;
option domain-name-servers 114.114.114.114;
option routers 100.100.129.1;
# option broadcast-address 10.5.5.31;
default-lease-time 600;
max-lease-time 7200;
next-server 100.100.129.6;
filename "pxelinux.0";
}
EOF
#配置FTP 默认目录位置到 /mnt/ftp
echo 'anon_root=/mnt/ftp' >> /etc/vsftpd/vsftpd.conf
mkdir /mnt/ftp
#启动服务
systemctl enable --now tftp xinetd dhcpd vsftpd
#拷贝PXE启动引导器pxelinux.0 到 TFTP 目录下
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#挂载iso文件到 /mnt/ftp/centos/下,并设置开机自动挂载
mkdir -p /mnt/ftp/centos/centos7
mount /mnt/iso/CentOS-7-x86_64-Minimal-2009.iso /mnt/ftp/centos/centos7
echo 'mount /mnt/iso/CentOS-7-x86_64-Minimal-2207-02.iso /mnt/ftp/centos/centos7' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
#拷贝光盘中isolinux目录下所有文件到 TFTP目录
cp /mnt/ftp/centos/centos7/isolinux/* /var/lib/tftpboot/
mkdir -p /var/lib/tftpboot/pxelinux.cfg
#创建PXE核心配置文件
cat > /var/lib/tftpboot/pxelinux.cfg/default <<-EOF
default vesamenu.c32
timeout 600
display boot.msg
# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Install My Linux
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13
# Border Area
menu color border * #00000000 #00000000 none
# Selected item
menu color sel 0 #ffffffff #00000000 none
# Title bar
menu color title 0 #ff7ba3d0 #00000000 none
# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none
# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none
# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none
# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none
# Help text
menu color help 0 #ffffffff #00000000 none
# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none
# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
# Command prompt text
label boot from local
menu label Boot From Local Disk
localboot 0xffff
label linux
menu label Install CentOS 7 Automatic
menu default
kernel vmlinuz
append initrd=initrd.img ks=ftp://100.100.129.6/ks/centos7.cfg
label linux
menu label Install CentOS 7 Manual
kernel vmlinuz
append initrd=initrd.img method=ftp://100.100.129.6/centos/centos7
EOF
#生成Kickstart.cfg 文件
mkdir -p /mnt/ftp/ks
cat > /mnt/ftp/ks/centos7.cfg <<-EOF
#安装或升级
install
#不使用图形界面
text
#默认语言
lang en_US.UTF-8
#默认时区
timezone Asia/Shanghai
#默认键盘类型
keyboard us
#ROOT密码
rootpw --iscrypted \$1\$lIVnUmpC\$Z9QV08Xu0s5zYr4TYDtmC/
#创建用户
user --name=chen --groups=dev --iscrypted --password="\$1\$qrN2.HI3\$m1e1rJK.I2EVR.4ClRx261" --shell=bash
#安装完毕重启
reboot
#关闭selinux
selinux --disabled
#关闭firewalld
firewall --disabled
#系统引导装载程序配置
bootloader --location=mbr
#系统授权信息
auth --useshadow --passalgo=sha512
#初始化无效分区表
zerombr
#删除原有分区
clearpart --all
#系统分区
part /boot --fstype="xfs" --size=1024
part / --fstype="ext4" --grow --size=1
#通过网络安装
url --url="ftp://100.100.129.6/centos/centos7"
#指定安装程序包
%packages
@^minimal
%end
#安装完成后执行的命令
%post --interpreter=/bin/bash
yum -y install wget
mkdir /etc/yum.repo.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repo.d/backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
%end
EOF