国产服务器操作系统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模式

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个搭建无人值守系统安装服务器的示例代码: 1. 配置 DHCP 服务 在 DHCP 服务器上,创建一个新的 DHCP 配置文件 `/etc/dhcp/dhcpd.conf`,并添加以下内容: ``` subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.100 192.168.1.200; option domain-name-servers 8.8.8.8; option routers 192.168.1.1; option broadcast-address 192.168.1.255; default-lease-time 600; max-lease-time 7200; next-server 192.168.1.2; filename "pxelinux.0"; } ``` 这个配置文件指定了 DHCP 服务器的 IP 地址池(192.168.1.100 到 192.168.1.200),DNS 服务器地址(8.8.8.8),网关地址(192.168.1.1),并将默认租约时间设置为 10 分钟。 注意,这个配置文件中的 `next-server` 和 `filename` 属性指定了 PXE 服务器的 IP 地址和启动文件名。在这个示例中,我们将 PXE 服务器的 IP 地址设置为 `192.168.1.2`,将启动文件名设置为 `pxelinux.0`。 2. 配置 TFTP 服务 在 PXE 服务器上,安装 TFTP 服务器软件,并将启动文件 `pxelinux.0` 复制到 TFTP 服务器的根目录下。 3. 配置 Kickstart 文件 创建一个 Kickstart 文件 `ks.cfg`,并将其放置在 PXE 服务器的根目录下。以下是一个示例 Kickstart 文件的内容: ``` # Install OS instead of upgrade install # Use graphical installer graphical # Set up network network --bootproto=dhcp --device=eth0 # Set up root password rootpw --iscrypted $6$SALT$HASH # Configure timezone timezone Asia/Shanghai --utc # Configure partitioning autopart --type=lvm # Configure packages to install %packages @^minimal @core kexec-tools %end ``` 这个 Kickstart 文件将使用图形界面安装 CentOS 操作系统,并自动配置网络、设置 root 密码、设置时区、使用 LVM 分区,以及安装一些基本的软件包。 注意,这个 Kickstart 文件中的 `--bootproto` 参数指定了使用 DHCP 自动配置网络。这个参数的值应该与 DHCP 配置文件中的 `bootproto` 属性保持一致。 4. 配置 PXE 引导文件 创建一个 PXE 引导文件 `pxelinux.cfg/default`,并添加以下内容: ``` default ks prompt 0 timeout 300 label ks kernel vmlinuz append initrd=initrd.img ks=http://192.168.1.2/ks.cfg ``` 这个 PXE 引导文件将使用 `vmlinuz` 和 `initrd.img` 内核文件,以及之前创建的 Kickstart 文件 `ks.cfg` 来启动自动安装程序。 注意,这个 PXE 引导文件中的 `ks` 标签指定了使用 Kickstart 文件自动安装系统,`kernel` 和 `initrd` 属性指定了内核文件,`append` 属性指定了 Kickstart 文件的 URL。 5. 启动 PXE 客户端 现在,启动一个 PXE 客户端并让其从网络引导。在启动过程中,客户端将自动从 DHCP 服务器获取 IP 地址,并下载启动文件 `pxelinux.0`。一旦下载完成,客户端将执行这个启动文件,并自动安装操作系统。 注意,整个过程是无人值守的,只需要启动客户端并等待自动安装完成即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河 静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值