wei/sh/init.sh
2026-06-26 10:10:40 +08:00

266 lines
8.7 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 设置脚本在遇到错误时立即退出
set -e
[ "$(id -u)" -ne 0 ] && { echo "必须以 root 用户身份运行此脚本。"; exit 1; }
# 检查 tuned-adm 命令是否存在,如果存在则执行
if command -v tuned-adm &> /dev/null; then
tuned-adm profile network-latency
else
echo "Warning: The 'tuned-adm' command is not available."
fi
# EPB not enabled on this platform
#x86_energy_perf_policy performance
###################### core ###################
echo "# 开始检查内核参数..."
# limits.conf
declare -r LIMITS_CONF_APPEND='
* soft core unlimited
* hard core unlimited
'
if ! grep -qE '\* soft core unlimited|\* hard core unlimited' /etc/security/limits.conf; then
echo "$LIMITS_CONF_APPEND" >> /etc/security/limits.conf
else
echo "The limits have already been set in /etc/security/limits.conf"
fi
ulimit -c unlimited
###################### rc.local ###################
echo "# 开始检查rc.local..."
# rc.local
if [ ! -f "/etc/rc.d/rc.local" ]; then
echo "Error: /etc/rc.d/rc.local file does not exist. Aborting."
exit 1
fi
chmod +x /etc/rc.d/rc.local
ETHTOOL_COMMANDS=(
"ethtool -G p1p1 rx 2048 tx 2048"
"ethtool -G ens1f0 rx 2048 tx 2048"
"ethtool -C ens1f0 rx-usecs 0 rx-usecs-irq 0 adaptive-rx off"
"ethtool -C p1p2 rx-usecs 0 rx-usecs-irq 0 adaptive-rx off"
)
# 检查 rc.local 文件中是否已包含相应行,如果没有则追加
for cmd in "${ETHTOOL_COMMANDS[@]}"; do
if ! grep -Fxq "$cmd" /etc/rc.d/rc.local; then
echo "$cmd" >> /etc/rc.d/rc.local
fi
done
###################### GRUB ###################
## TODO: 这个需要手工操作
echo "# 开始检查GRUB..."
# 设置GRUB 引导参数
# 读取当前GRUB_CMDLINE_LINUX值
CURRENT_CMDLINE=$(grep GRUB_CMDLINE_LINUX /etc/default/grub | cut -d '"' -f2)
# 获取系统CPU核心数
CPU_COUNT=$(nproc)
# 要检查和添加的配置项
ADDITIONAL_OPTS="ipv6.disable=1 selinux=0 isolcpus=1-$((CPU_COUNT-1)) nohz_full=1-$((CPU_COUNT-1)) transparent_hugepage=never default_hugepagesz=1G hugepagesz=1G hugepages=32 intel_idle.max_cstate=0 processor.max_cstate=0 idle=poll intel_iommu=off nosoftlockup mce=ignore_ce nmi_watchdog=0 pcie_aspm=off nohz=off audit=0"
# 需要检查的配置项列表排除isolcpus和nohz_full
CHECK_OPTS="ipv6.disable=1 selinux=0 transparent_hugepage=never default_hugepagesz=1G hugepagesz=1G hugepages=32 intel_idle.max_cstate=0 processor.max_cstate=0 idle=poll intel_iommu=off nosoftlockup mce=ignore_ce nmi_watchdog=0 pcie_aspm=off nohz=off audit=0"
# 检查并添加配置项
for opt in $CHECK_OPTS; do
if [[ ! $CURRENT_CMDLINE =~ $opt ]]; then
CURRENT_CMDLINE+=" $opt"
fi
done
# 特别处理 isolcpus 和 nohz_full
if [[ ! $CURRENT_CMDLINE =~ isolcpus= ]]; then
CURRENT_CMDLINE+=" isolcpus=1-$((CPU_COUNT-1))"
fi
if [[ ! $CURRENT_CMDLINE =~ nohz_full= ]]; then
CURRENT_CMDLINE+=" nohz_full=1-$((CPU_COUNT-1))"
fi
# 更新GRUB_CMDLINE_LINUX
#awk -v cmdline="${CURRENT_CMDLINE}" 'BEGIN{FS="=";OFS="="} $1 == "GRUB_CMDLINE_LINUX" {$2 = "\""cmdline"\""} 1' /etc/default/grub | tee /etc/default/grub > /dev/null
# 重启grub配置
#update-grub
####################### 大页检查 ########################
echo "# 开始检查大页..."
# 检查 HugePages 是否启用
HUGE_PAGES_TOTAL=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}')
if [ "$HUGE_PAGES_TOTAL" -eq 0 ]; then
echo "Error: HugePages are not enabled or configured properly."
else
echo "HugePages are enabled with a total of $HUGE_PAGES_TOTAL pages."
fi
# 检查 hugetlbfs 是否挂载
if ! mount | grep -q hugetlbfs; then
echo "Error: hugetlbfs is not mounted."
else
echo "hugetlbfs is correctly mounted."
fi
# 如果 HugePages 没有启用或 hugetlbfs 没有挂载,进行配置
if [ "$HUGE_PAGES_TOTAL" -eq 0 ] || ! mount | grep -q hugetlbfs; then
echo "Configuring HugePages..."
# 更新内核参数
grubby --update-kernel=ALL --args="default_hugepagesz=1G hugepagesz=1G hugepages=4"
# 检查配置是否成功
if grubby --info=ALL | grep -q "default_hugepagesz=1G"; then
echo "HugePages configuration updated successfully."
# 尝试挂载 hugetlbfs
mkdir -p /dev/hugepages
mount -t hugetlbfs nodev /dev/hugepages
if mount | grep -q hugetlbfs; then
echo "hugetlbfs mounted successfully."
else
echo "Error: Failed to mount hugetlbfs."
fi
else
echo "Error: Failed to update HugePages configuration."
fi
fi
####################### 检查虚拟网卡 ########################
echo "# 开始检查并删除虚拟网卡..."
set +e
systemctl stop libvirtd.service 2>/dev/null
systemctl disable libvirtd.service 2>/dev/null
set -e
# 检查 virbr0 网桥是否存在
if ifconfig virbr0 &> /dev/null; then
echo "virbr0 exists."
# 如果存在,可以在此处添加删除 virbr0 的命令
# 因为 libvirt 会在需要时自动重建它
# 可以考虑使用 libvirt 的工具来管理网络,例如 'virsh net-destroy default'
else
echo "virbr0 does not exist."
fi
####################### selinux ########################
echo "# 开始关闭SELINUX..."
# 检查 SELinux 当前的状态
current_selinux_status=$(grep ^SELINUX= /etc/selinux/config | awk -F "=" '{print $2}' | tr -d ' ')
# 如果 SELinux 不是 disabled 状态,则进行修改
if [ "$current_selinux_status" != "disabled" ]; then
# 备份原来的配置文件
cp /etc/selinux/config /etc/selinux/config.bak
# 使用 sed 命令更新 SELINUX 参数为 disabled
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
echo "SELinux has been set to disabled."
else
echo "SELinux is already disabled."
fi
####################### 网络低延迟 ########################
echo "# 开始设置网络低延迟..."
cd /proc/sys/net/ipv4
sysctl -w net.core.rmem_default=134217728
sysctl -w net.core.rmem_max=134217728
sysctl -w net.core.wmem_default=134217728
sysctl -w net.core.wmem_max=134217728
sysctl -w net.ipv4.udp_mem="134217728 134217728 268435456"
sysctl -w net.ipv4.udp_rmem_min=134217728
sysctl -w net.ipv4.udp_wmem_min=134217728
sh -c 'echo "net.ipv4.tcp_low_latency = 0" >> /etc/sysctl.conf'
sh -c 'echo "vm.nr_hugepages=4096" >> /etc/sysctl.conf'
sh -c 'echo "net.core.rmem_default=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.core.rmem_max=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.core.wmem_default=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.core.wmem_max=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.ipv4.udp_mem=134217728 134217728 268435456" >> /etc/sysctl.conf'
sh -c 'echo "net.ipv4.udp_rmem_min=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.ipv4.udp_wmem_min=134217728" >> /etc/sysctl.conf'
sh -c 'echo "net.core.netdev_max_backlog=4000" >> /etc/sysctl.conf'
sysctl -p /etc/sysctl.conf
####################### 关闭服务 ########################
echo "# 开始关闭各种无用的服务..."
set +e
systemctl set-default multi-user.target
systemctl disable firewalld.service
systemctl stop firewalld.service
systemctl stop libvirtd.service
systemctl disable libvirtd.service
systemctl stop iptables.service
systemctl stop firewalld.service
systemctl stop irqbalance.service
systemctl stop abrt-ccpp.service
systemctl stop abrt-oops.service
systemctl stop abrt-xorg.service
systemctl stop abrtd.service
systemctl stop alsa-state.service
systemctl stop cups.service
systemctl stop ModemManager.service
systemctl stop postfix.service
systemctl disable firewalld.service
systemctl disable iptables.service
systemctl disable irqbalance.service
systemctl disable alsa-state.service
systemctl disable cups.service
systemctl disable ModemManager.service
systemctl disable postfix.service
systemctl stop cpuspower
systemctl stop firewalld
#chkconfig NetworkManager off
#chkconfig --del NetworkManager
set -e
# 检查 tuned-adm 命令是否存在,如果存在则执行
if command -v tuned-adm &> /dev/null; then
tuned-adm profile network-latency
else
echo "Warning: The 'tuned-adm' command is not available."
fi
####################### SSH配置 ########################
echo "# 开始配置SSH服务..."
ssh_options="ClientAliveInterval 30
ClientAliveCountMax 10"
# 检查这些行是否已存在于 sshd_config 文件中
if ! grep -qxF "[[:space:]]*ClientAliveInterval 30" /etc/ssh/sshd_config ||
! grep -qxF "[[:space:]]*ClientAliveCountMax 10" /etc/ssh/sshd_config; then
# 如果不存在,则添加到文件的末尾
echo -e "$ssh_options" >> /etc/ssh/sshd_config
echo "Options added to /etc/ssh/sshd_config"
else
echo "Options already present in /etc/ssh/sshd_config"
fi
# 重启 SSH 服务
systemctl restart sshd
####################### SSH配置 ########################
echo "# 开始添加用户boyi_trader..."
useradd boyi_trader
echo "DONE! PLS RESTART!"