国产服务器操作系统PXE安装脚本 可重复执行(rc08版本)
执行效果如下:
#!/bin/bash
#Date:2023/12/25
#Func:一键部署pxe服务器
#Author:Zhanghaodong
#Version:2023.12.25.05
#Note:仅适用x86架构uefi安装
# 1.此脚本可多次重复执行。
# 2.如遇到某个服务异常退出,检查响应状态码排错后,再次执行脚本即可。
# 3.此脚本仅仅适用于CRITC-HongAn-Server-x86_64-V1.0-rc08.iso或CRITC-HongAn-Server-aarch-V1.0-rc08.iso
# 4.在填写以下参数时,请注意:x86架构可用uefi或leagcy引导,其对应的filename分别是grubx64.efi和pxelinux.0
# arm只能用uefi引导且filename只能是grubaa64.efi
#Manually Writing Basic Information
PXE_SERVER="11.0.1.181"
ISO="/root/CRITC-HongAn-Server-x86_64-V1.0-rc08.iso"
CLIENT_ARCH="x86" #x86 or arm
CLIENT_BOOT_MODE="uefi" #uefi or leagcy
#PXE_FILE_NAME="pxelinux.0" #x86下uefi对应的grubx64.efi,leagcy对应pxelinux.0;arm对应的只有grubaa64.efi
ROOT_PASSWORD="qwer1234!@#$"
#Dhcp Config Information
SUBNET="11.0.1.0" #子网
NETMASK="255.255.255.0" #掩码
GATEWAY="11.0.1.1" #网关
RANGE_IP_LOW="11.0.1.200" #ip范围
RANGE_IP_HIGH="11.0.1.230" #ip范围
function Log {
local log_level=$1
local log_info=$2
local line=$3
local script_name=$(basename $0)
case ${log_level} in
"INFO")
echo -e "\033[32m$(date "+%Y-%m-%d %T.%N") [INFO]: ${log_info}\033[0m";;
"WARN")
echo -e "\033[33m$(date "+%Y+%m+%d %T.%N") [WARN]: ${log_info}\033[0m";;
"ERROR")
echo -e "\033[31m$(date "+%Y-%m-%d %T.%N") [ERROR ${script_name} ${FUNCNAME[1]}:$line]: ${log_info}\033[0m";;
*)
echo -e "${@}"
;;
esac
}
function MOUNT_ISO {
[ -d /tmpmnt ] || mkdir /tmpmnt
umount /tmpmnt
mount -o loop ${ISO} /tmpmnt &> /dev/null
if [ $? -eq 0 ];then
Log INFO "${ISO}镜像已挂载到/tmpmnt目录下!"
else
Log ERROR "${ISO}镜像挂载失败!"
exit 2
fi
}
function STOP_FIREWALLD {
iptables -F && Log INFO "防火墙规则已清空!" || Log WARN "防火墙规则清空失败,请重试!"
systemctl stop firewalld && systemctl disable firewalld &> /dev/null && Log INFO "防火墙及SELINUX已关闭!"
#本系统seLinux缺省disabled,无需做修改操作
#sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
}
function CONFIG_LOCAL_YUM {
[ -d /etc/yum.repos.d/repo_bak ] || mkdir /etc/yum.repos.d/repo_bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_bak
cat > /etc/yum.repos.d/local.repo <<-EOF
[local]
name = local media for ${ISO}
baseurl = file:///tmpmnt
gpgcheck = 0
enabled = 1
EOF
Log INFO "本地YUM源配置完成!"
}
function CONFIG_HTTPD {
rpm -qa | grep httpd &>/dev/null || yum -y install httpd &> /dev/null
[ -d /var/www/html/hy ] && rm -rf /var/www/html/hy
[ -d /var/www/html/ks ] && rm -rf /var/www/html/ks
mkdir /var/www/html/{hy,ks}
chmod -R 755 /var/www/html/{hy,ks}
Log INFO "正在拷贝ISO文件到/var/www/html/hy,大约需1分钟左右......"
rsync -a /tmpmnt/ /var/www/html/hy
if [ $? -eq 0 ];then
Log INFO "ISO文件拷贝完毕!"
else
Log ERROR "ISO文件拷贝失败,请检查后重试!"
exit 3
fi
systemctl restart httpd
if [ $? -eq 0 ];then
Log INFO "重新启动HTTPD服务完毕!"
systemctl enable httpd &>/dev/null
else
Log ERROR "启动HTTPD服务异常,请检查后重试!"
exit 4
fi
}
function CONFIG_KS {
cat > /tmp/rootpw.py <<-EOF
#!/usr/bin/python2
import crypt
print(crypt.crypt("$ROOT_PASSWORD"))
EOF
ROOTPW="rootpw --iscrypted `python2 /tmp/rootpw.py | sed 's/\$/\\\$/g' | sed 's/.$//'`"
rm -rf /tmp/rootpw.py
cat > /var/www/html/ks/hy-ks.cfg <<-EOF
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
install
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --disabled
ignoredisk --only-use=nvme0n1
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8
eula --agreed
#skipx 安装完成后开机不启动图形
#skipx
# Network information
network --bootproto=static --device=ens33 --ip=11.0.1.23 --netmask=255.255.255.0 --gateway=11.0.1.1 --nameserver=114.114.114.114,8.8.8.8 --noipv6
network --bootproto=dhcp --device=eno1 --onboot=off --ipv6=auto --no-activate
network --bootproto=dhcp --device=eno2 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=eno3 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=eno4 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=ens1f0 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=ens1f1 --onboot=off --ipv6=auto
network --hostname=localhost.localdomain
# Root password
${ROOTPW}
# System services
#services --enabled="ntpd" #注意此处如果设置失败,将会在安装完之后报错,导致pxe自动安装程序无法进行
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
#user --name=hy --password=.CvWNQ/ --iscrypted --gecos="hy"
# X Window System configuration information
xconfig --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=nvme0n1
# Partition clearing information
clearpart --all --initlabel
autopart --type=lvm
#######################################################################
#分区情况案例如下
# Disk partitioning information
#标准分区如下操作
#part swap --asprimary --fstype="swap" --ondisk=nvme0n1 --size=4096
#part /boot --asprimary --fstype="xfs" --ondisk=nvme0n1 --size=1024
#part /boot/efi --asprimary --fstype="vfat" --ondisk=nvme0n1 --size=1024
#part / --asprimary --fstype="xfs" --grow --ondisk=nvme0n1 --size=1 #将剩下的分区都给/
##########################################################################################
#lvm分区如下
#后续待更新
###################################
%packages
#@^Server with UKUI GUI
@^Minimal Install
wget
chrony
kexec-tools
%end
%post
systemctl distable chronyd
systemctl disable firstboot-graphical.service
%end
#%addon com_redhat_kdump --enable --reserve-mb='auto'
%addon ADDON_placeholder --enable --reserve-mb=1024M
%end
EOF
}
function CONFIG_DHCP_FILENAME {
rpm -qa | grep dhcp &> /dev/null|| yum -y install dhcp &>/dev/null
case ${CLIENT_BOOT_MODE} in
"uefi")
case ${CLIENT_ARCH} in
"x86")
filename=grubx64.efi;;
"arm")
filename=grubaa64.efi;;
"*")
Log ERROR "您填写的${CLIENT_ARCH}不符合${CLIENT_BOOT_MODE}引导,请检查后重试!"
exit 7;;
esac;;
"leagcy")
case ${CLIENT_ARCH} in
"x86")
filename=pxelinux.0;;
"arm")
Log ERROR "您填写的${CLIENT_ARCH}不符合${CLIENT_BOOT_MODE}引导,请检查后重试!"
exit 8;;
"*")
Log ERROR "您填写的${CLIENT_ARCH}不符合${CLIENT_BOOT_MODE}引导,请检查后重试!"
exit 9;;
esac;;
"*")
Log ERROR "客户端引导方式只能是UEFI或LEAGCY模式,请检查后重试!"
exit 10;;
esac
}
function CONFIG_DHCP {
rpm -qa | grep dhcp &> /dev/null|| yum -y install dhcp &>/dev/null
cat > /etc/dhcp/dhcpd.conf <<-EOF
ddns-update-style interim;
ignore client-updates;
filename "${filename}";
next-server ${PXE_SERVER};
subnet ${SUBNET} netmask ${NETMASK} {
option routers ${GATEWAY};
option subnet-mask ${NETMASK};
range dynamic-bootp ${RANGE_IP_LOW} ${RANGE_IP_HIGH};
default-lease-time 21600;
max-lease-time 43200;
}
EOF
systemctl restart dhcpd
if [ $? -eq 0 ];then
Log INFO "DHCP服务已正常启动!"
systemctl enable dhcpd &>/dev/null
else
Log ERROR "DHCP服务异常,请检查参数是否正确!"
exit 3
fi
}
function CONFIG_TFTP_X86_ARM_UEFI_GRUB_CFG {
rpm -qa | grep -q tftp || yum install tftp -y > /dev/null
rpm -qa | grep -q tftp-server || yum install tftp-server -y > /dev/null
#rpm -qa | grep -q xinetd || yum install xinetd -y > /dev/null
cat > /etc/xinetd.d/tftp <<-EOF
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
rm -rf /var/lib/tftpboot/*
cp -rf /tmpmnt/images/pxeboot/* /var/lib/tftpboot/
cp -rf /tmpmnt/EFI/BOOT/grub*.efi /var/lib/tftpboot/
chmod -R 755 /var/lib/tftpboot
cat > /var/lib/tftpboot/grub.cfg <<-EOF
set default="0"
function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}
load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2
set timeout=10
### BEGIN INSTALL CRITC-HongAn-Server ###
menuentry 'PXE INSTALL FOR Install CRITC-HongAn-Server-x86_64-V1.0-rc08' --class red --class gnu-linux --class gnu --class os {
set root=(tftp,${PXE_SERVER})
linux /vmlinuz ip=dhcp inst.repo=http://${PXE_SERVER}/hy inst.ks=http://${PXE_SERVER}/ks/hy-ks.cfg
initrd /initrd.img
}
EOF
systemctl restart tftp && Log INFO "TFTP服务启动完毕!"
if [ $? -eq 0 ];then
Log INFO "TFTP服务已正常启动!"
systemctl enable tftp &>/dev/null
else
Log ERROR "TFTP服务异常,请检查后重试!"
exit 4
fi
}
function CONFIG_TFTP_X86_LEAGCY_GRUB_CFG {
rpm -qa | grep -q tftp || yum install tftp -y > /dev/null
rpm -qa | grep -q tftp-server || yum install tftp-server -y > /dev/null
#rpm -qa | grep -q xinetd || yum install xinetd -y > /dev/null
cat > /etc/xinetd.d/tftp <<-EOF
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
rm -rf /var/lib/tftpboot/*
cp -rf /tmpmnt/isolinux/* /var/lib/tftpboot/
cp -rf /tmpmnt/images/pxeboot/* /var/lib/tftpboot/
[ -d /var/lib/tftpboot/pxelinux.cfg ] || mkdir /var/lib/tftpboot/pxelinux.cfg &> /dev/null
rm -rf /var/lib/tftpboot/pxelinux.cfg/*
chmod -R 755 /var/lib/tftpboot
cat > /var/lib/tftpboot/pxelinux.cfg/default <<-EOF
default vesamenu.c32
timeout 100
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 CRITC-HongAn-Server-x86_64-V1.0-rc08
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
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label http
menu label ^PXE INSTALL FOR CRITC-HongAn-Server-x86_64-V1.0-rc08
menu default
kernel vmlinuz
append initrd=initrd.img ip=dhcp inst.repo=http://${PXE_SERVER}/hy inst.ks=http://${PXE_SERVER}/ks/hy-ks.cfg
menu end
EOF
cp -rf /tmpmnt/Packages/syslinux-nonlinux-* /tmp
rm -rf /tmp/usr &> /dev/null
rpm2cpio /tmp/syslinux-nonlinux-* | cpio -idm --quiet -D /tmp/
cp -rf /tmp/usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
chmod -R 755 /var/lib/tftpboot
systemctl restart tftp && Log INFO "TFTP服务启动完毕!"
if [ $? -eq 0 ];then
Log INFO "TFTP服务已正常启动!"
systemctl enable tftp &>/dev/null
else
Log ERROR "TFTP服务异常,请检查后重试!"
exit 4
fi
}
function CHOOSE_BOOT_MODE {
case ${CLIENT_BOOT_MODE} in
"uefi")
CONFIG_TFTP_X86_ARM_UEFI_GRUB_CFG;;
"leagcy")
CONFIG_TFTP_X86_LEAGCY_GRUB_CFG;;
"*")
Log ERROR "CLIENT_BOOT_MODE选择有误,请重试!"
exit 5;;
esac
}
function MAIN {
MOUNT_ISO
STOP_FIREWALLD
CONFIG_LOCAL_YUM
CONFIG_DHCP_FILENAME
CONFIG_DHCP
CHOOSE_BOOT_MODE
CONFIG_HTTPD
CONFIG_KS
}
MAIN
######################
#状态码错误排查
#2:镜像挂载失败
#3:拷贝ISO文件到/var/www/html/hy下失败
#4:启动HTTPD服务异常
#5:选择CLIENT_BOOT_MODE有误
#6:选择PXE_FILE_NAME有误
#7:您填写的${CLIENT_ARCH}不符合UEFI引导,请检查后重试!
#8:您填写的${CLIENT_ARCH}不符合LEAGCY引导,请检查后重试!
#9:您填写的${CLIENT_ARCH}不符合LEAGCY引导,请检查后重试!此处用户填写的是非x86
#10:${CLIENT_BOOT_MODE}填写错误有误,此处只能UEFI或LEAGCY模式