#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2021-02-08
#FileName:install_pxe_centos8.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
. /etc/init.d/functions
#系统镜像位置确定
os_file(){
#请设置系统镜像位置写绝对路径,否则无法安装
os_centos6_file=/root/CentOS-6.10-x86_64-bin-DVD1.iso
os_centos7_file=/root/CentOS-7-x86_64-Everything-2003.iso
os_centos8_file=/root/CentOS-8.2.2004-x86_64-dvd1.iso
if [ ! -e $os_centos6_file ];then
action "centos6镜像文件不存在" false;exit
fi
if [ ! -e $os_centos7_file ];then
action "centos8镜像文件不存在" false;exit
fi
if [ ! -e $os_centos8_file ];then
action "centos8镜像文件不存在" false;exit
fi
}
#镜像挂载
os_mount(){
mkdir -p /var/www/html/centos/{6,7,8}/x86_64/
mount $os_centos6_file /var/www/html/centos/6/x86_64/ &>/dev/null
mount $os_centos7_file /var/www/html/centos/7/x86_64/ &>/dev/null
mount $os_centos8_file /var/www/html/centos/8/x86_64/ &>/dev/null
action "系统镜像挂载成功"
}
#准备内核启动文件
centos_isolinux(){
mkdir -p /var/lib/tftpboot/centos{6,7,8}
cp /var/www/html/centos/6/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6/
cp /var/www/html/centos/7/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/
cp /var/www/html/centos/8/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8/
cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
cp /var/www/html/centos/8/x86_64/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg/
cat <<EOF >/var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title PXE INSTALL MENU
label linux8
menu label Auto Install Centos Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://${IP}/ks/ks-centos8.cfg
label linux7
menu label Auto Install Centos Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://${IP}/ks/ks-centos7.cfg
label linux6
menu label Auto Install Centos Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://${IP}/ks/ks-centos6.cfg
label manual
menu label ^Manual Install CentOS Linux 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img inst.repo=http://${IP}/centos/8/x86_64/
label rescue
menu label ^Rescue a CentOS Linux system 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img inst.repo=http://${IP}/centos/8/x86_64/ rescue
label local
menu default
menu label ^Boot from local drive
localboot 0xffff
EOF
action "PXE启动文件准备完成"
}
#安装
install_pxe(){
os_file
yum -y install wget dhcp-server tftp-server httpd syslinux-nonlinux
if [ $? = 0 ];then
action "安装成功"
else
action "安装错误,请检查YUM源" false ; exit
fi
systemctl enable --now httpd.service tftp.service &>/dev/null
systemctl is-active tftp.service &>/dev/null && action "TFTP服务启动成功" || action "TFTP服务启动失败" false
systemctl is-active httpd.service &>/dev/null && action "HTTP服务启动成功" || action "HTTP服务启动失败" false
dhcp_conf_file
os_mount
ks_file
centos_isolinux
action "PXE服务安装完成,如无法正常自动部署系统请检查脚本中键入的参数是否错误"
action "系统部署后root密码默认123456"
}
#DHCP配置文件设置
dhcp_conf_file(){
echo -e "\033[1;31m以下输入的参数没有限制请正确输入,否则会导致dhcp无法启动!\033[0m"
read -p "请输入dhcp分配的网络地址:" NETWORK
read -p "请输入子网掩码:" MASK
read -p "请输入地址池范围(2个地址使用空格分割):" start stop
read -p "请输入网关:" GATEWAY
read -p "请输入本机IP地址:" IP
cat <<EOF >/etc/dhcp/dhcpd.conf
option domain-name "zhangzhuo.ltd";
option domain-name-servers 180.76.76.76,223.6.6.6;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet ${NETWORK} netmask ${MASK} {
range ${start} ${stop};
option routers ${GATEWAY};
next-server ${IP};
filename "pxelinux.0";
}
EOF
systemctl enable --now dhcpd &>/dev/null
systemctl is-active dhcpd.service &>/dev/null && action "DHCP服务启动成功" || action "DHCP服务启动失败请检查配置文件参数是否正确" false
}
#ks应答文件创建
ks_file(){
mkdir /var/www/html/ks
read -p "请输入本机IP地址:" IP
cat <<EOF >/var/www/html/ks/ks-centos6.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$NxU3dKmV\$fRJv0NpyNJv6sQSK2SEh//
# System language
lang en_US.UTF-8
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
authconfig --enableshadow --passalgo=sha512
# Firewall configuration
firewall --disabled
# Network information
network --onboot yes --bootproto=dhcp --device=eth0 --noipv6
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://${IP}/centos/6/x86_64"
# System bootloader configuration
bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024
%post
%end
%packages
@development
@mail-server
%end
EOF
cat <<EOF >/var/www/html/ks/ks-centos7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$NxU3dKmV\$fRJv0NpyNJv6sQSK2SEh//
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://${IP}/centos/7/x86_64"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024
%post
%end
%packages
@development
@mail-server
%end
EOF
cat <<EOF >/var/www/html/ks/ks-centos8.cfg
#version=RHEL8
ignoredisk --only-use=sda
# Partition clearing information
# Use graphical install
text
zerombr
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
# Use CDROM installation media
url --url="http://${IP}/centos/8/x86_64"
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Network information
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted \$6\$q76Q7kMy.HLaa0Jc\$pN1iBqDhYaNZNqLxV.8Qz8QwxgRYB3NXt7/1ikn4buFXhI8GrYUj3hzYhN5Bdw5LzmmZAbXEg9KNx8cC67J73.
# Run the Setup Agent on first boot
firstboot --enable
# Do not configure the X Window System
skipx
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024
%packages
@^minimal-environment
vim
tree
wget
curl
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
%end
EOF
action "ks应答文件准备完毕"
}
install_pxe
注意事项:
- 使用脚本前需要先准备 centos6,7,8 系统镜像并写入到脚本变量中 os_centos{6.7.8}_file 中,否则无法运行
- 脚本需要在 centos8 系统中使用
- 执行完毕后可以同时部署 centos6,7,8 系统
- ks 应答文件可以自己进行自定义
- 脚本执行过程中需要键入的参数需正确填写否则会导致服务无法启动尤其 dhcp 服务
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于