PVE一键优化脚本

https://github.com/ivanhao/pvetools

PVE 换源

# 注释企业源
/etc/apt/sources.list.d/pve-no-subscription.list
deb https://mirrors.ustc.edu.cn/proxmox/debian/pve bookworm pve-no-subscription

# PVE 软件源更换
nano /etc/apt/sources.list
deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware/etc/apt/sources.list

#pve 8+ ceph
if [ -f /etc/apt/sources.list.d/ceph.list ]; then CEPH_CODENAME=`ceph -v | grep ceph | awk '{print $(NF-1)}'`; source /etc/os-release; echo "deb https://mirrors.ustc.edu.cn/proxmox/debian/ceph-$CEPH_CODENAME $VERSION_CODENAME no-subscription" > /etc/apt/sources.list.d/ceph.list; fi

# 更新测试一下
apt update
# 更新升级
apt update && apt dist-upgrade
# 更新cpu微码
apt install iucode-tool
# 微码地址
https://mirrors.tuna.tsinghua.edu.cn/deepin/pool/non-free/i/intel-microcode/
dpkg -i xx.deb

修改网络环境

修改ip
nano /etc/network/interfaces
修改启动显示的ip地址
nano /etc/issue

虚拟内存使用量

cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
永久性修改:
在/etc/sysctl.conf 文件里添加如下参数:
vm.swappiness=10
free -h //查询
swapoff -a //释放
swapon -a  //启用
取消swap
vi /etc/fstab #注释swap
lvremove /dev/pve/swap
lvextend -l +100%FREE -r pve/root
创建swap
dd if=/dev/zero of=/dev/pve/swap bs=1M count=16384 #16G虚拟内存
chmod 0600 /dev/pve/swap #权限
mkswap /dev/pve/swap #创建
swapon /dev/pve/swap #激活

PVE虚拟机删除LOCAL-LVM分区

lvremove pve/data
lvextend -l +100%FREE -r pve/root
进入到pve的后台操作,数据中心--存储--local-lvm--移除

修改 CT (LXC 容器) 源

# 备份 APLInfo.pm 文件
cp /usr/share/perl5/PVE/APLInfo.pm /usr/share/perl5/PVE/APLInfo.pm_back

# 使用清华源替换官方源
sed -i 's|http://download.proxmox.com|https://mirrors.tuna.tsinghua.edu.cn/proxmox|g' /usr/share/perl5/PVE/APLInfo.pm

# 重启 PVE 服务
systemctl restart pveproxy.service

删除订阅弹窗

# 修改 JS 源码
sed -Ezi.bak "s/(Ext.Msg.show\(\{\s+title: gettext\('No valid sub)/void\(\{ \/\/\1/g" /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js 

# 重启 PVE 服务
systemctl restart pveproxy.service

PVE 插件安装

#CPU主板等温度
apt install lm-sensors -y
sensors-detect --auto

#硬盘温度
apt install hddtemp -y
#查看硬盘温度
hddtemp /dev/sd?
#因为我们后面需要放到 Web 界面显示,所以需要改这个软件 Web 端的权限:
chmod +s /usr/sbin/hddtemp
#在Glances中显示硬盘温度
systemctl enable hddtemp
systemctl status hddtemp
#开启功能
/etc/default/hddtemp
编辑该文件并将 RUN_DAEMON 更改为 true
然后修复sudo systemctl restart hddtemp

#硬盘smart
apt-get install smartmontools
smartctl -s on -a /dev/sda
smartctl -A /dev/sda

#Docker安装
curl -fsSL https://get.docker.com -o get-docker.sh
./get-docker.sh
apt install docker.io -y
systemctl enable docker

#MQTT
apt-get install -y mosquitto-clients
#MQTT定时配置
*/3 * * * * mosquitto_pub -h ip -u 用户名-P 密码 -t wky/temp -m `sensors | grep -E 'temp1' | cut -c16-19`

编辑后端服务文件

vim /usr/share/perl5/PVE/API2/Nodes.pm
#搜索 pveversion 位置,加入下面 3 行代码:
PVE::pvecfg::version_text();
$res->{cpusensors} = `lscpu | grep MHz`;# 添加此行以获取 CPU 频率
$res->{cpu_temperatures} = `sensors`;  # 添加此行以获取 CPU 与主板温度
$res->{hdd_temperatures} = `hddtemp /dev/sd?`;  # 添加此行以获取硬盘温度

编辑前端 JS 文件

vim /usr/share/pve-manager/js/pvemanagerlib.js
#搜索到 widget.pveNodeStatus 位置,修改 height: 300,每多一个硬盘,那么这个数据得增加 20,大家这里可以一个个尝试,我暂定修改为:460
#继续搜索 pveversion ,添加 2 个 item,根据自己上面显示的 CPU 核心数自行修改,我的是 8 核心,改成如下:
{
    itemId: 'MHz',
    colspan: 2,
    printBar: false,
    title: gettext('CPU频率'),
    textField: 'cpusensors',
    renderer:function(value){
        const f0 = value.match(/CPU MHz.*?([\d]+)/)[1];
        const f1 = value.match(/CPU min MHz.*?([\d]+)/)[1];
        const f2 = value.match(/CPU max MHz.*?([\d]+)/)[1];
        return `CPU实时: ${f0} MHz | 最小: ${f1} MHz | 最大: ${f2} MHz `
    }
},
{
    itemId: 'cpu_temperatures',
    colspan: 2,
    printBar: false,
    title: gettext('CPU温度'),
    textField: 'cpu_temperatures',
    renderer:function(value){
        const cpu = value.match(/Package id 0.*?\+([\d\.]+)Â/)[1];
        const c0 = value.match(/Core 0.*?\+([\d\.]+)Â/)[1];
        const c1 = value.match(/Core 1.*?\+([\d\.]+)Â/)[1];
        const c2 = value.match(/Core 2.*?\+([\d\.]+)Â/)[1];
        const c3 = value.match(/Core 3.*?\+([\d\.]+)Â/)[1];
        return `CPU:${cpu} °C (核心 0: ${c0} °C , 核心 1: ${c1} °C , 核心 2: ${c2} °C , 核心 3: ${c3} °C)`
    }
},
{
    itemId: 'hdd_temperatures',
    colspan: 2,
    printBar: false,
    title: gettext('硬盘温度'),
    textField: 'hdd_temperatures',
    renderer: function(value) {
        value = value.replaceAll('Â', '',);
        return value.replaceAll('\n', '<br>');
    }
}

重启面板服务

systemctl restart pveproxy

定时任务

/var/spool/cron/crontabs/root
crontab -e
CTRL+O 保存 CTRL+X 退出
重启
service cron restart
service cron status

PCIE设备名称更新

update-pciids
pve无网络
rm /usr/share/misc/pci.ids 
wget -P /usr/share/misc/ http://pci-ids.ucw.cz/v2.2/pci.ids

开启 IOMMU 直通

/etc/default/grub
#黑苹果
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt pcie_acs_override=downstream,multifunction nofb textonly nomodeset video=efifb:off"
#混杂
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt pcie_acs_override=downstream video=efifb:off"
#正常
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on"
#更新生效
update-grub
reboot

添加 VFIO 模块

echo "vfio" >> /etc/modules
echo "vfio_iommu_type1" >> /etc/modules
echo "vfio_pci" >> /etc/modules
echo "vfio_virqfd" >> /etc/modules
echo "coretemp" >> /etc/modules #风扇调速
echo "nct6775" >> /etc/modules #风扇调速
update-initramfs -u -k all
reboot

硬盘的直通

ls /dev/disk/by-id
qm set 107 -sata1 /dev/disk/by-id/ata-TOSHIBA_MG06ACA10TE_6970A01YFKQE

在Proxmox VE中实现网卡名和MAC地址绑定

touch /etc/systemd/network/10-persistent-net.link

[Match]
MACAddress=30:5a:3a:06:ec:6d

[Link]
Name=enp9s0

/etc/init.d/udev force-reload

修改启动配置
/etc/network/interfaces

iface enp9s0 inet manual

auto vmbr0
iface vmbr0 inet static
    address 10.10.10.254/24
    gateway 10.10.10.253
    bridge-ports enp9s0
    bridge-stp off
    bridge-fd 0

多个网卡绑定
11-persistent-net.link

virtIO镜像

https://github.com/virtio-win/virtio-win-pkg-scripts

安装iperf

apt-get install iperf3 -y
#启动服务端
iperf3 -s

无法关闭虚拟机

rm /var/lock/qemu-server/lock-102.conf #102是你的虚拟机编号
qm stop 102

openwrt

#下载
https://blog.wxhbts.pro/usr/uploads/2023/04/1350062531.zip
chmod 777 ./img2kvm
./img2kvm istoreos-21.02.3-2023040712-x86-64-squashfs-combined.img.gz 107

用mkcert工具自签基于内网IP的证书

https://github.com/FiloSottile/mkcert
mkcert.exe -install 192.168.1.1
打开PVE后台,按如下步骤上传证书,选择从文件上传证书,将xxxxx.key.pem文件上传到第一个私钥栏,将xxxxx.pem文件上传到凭证链栏
#获取根证书
mkcert -CAROOT

PVE嵌套虚拟化

cat /sys/module/kvm_intel/parameters/nested
N 未开启 Y 开启
modprobe -r kvm_intel
modprobe kvm_intel nested=1
#重启生效
echo "options kvm_intel nested=1" >> /etc/modprobe.d/modprobe.conf
#群辉配置
vi /etc/pve/nodes/pve/qemu-server/100.conf 
#添加
args: -cpu 'kvm64,enforce,+kvm_pv_eoi,+vmx,+kvm_pv_unhalt,+lahf_lm,+sep'

AMD显卡直通

GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt"
quiet amd_iommu=on iommu=pt pcie_acs_override=downstream
update-grub
防止虚拟机崩溃影响宿主机的参数:
echo "options vfio_iommu_type1 allow_unsafe_interrupts=1" > /etc/modprobe.d/iommu_unsafe_interrupts.conf
echo "options kvm ignore_msrs=1 report_ignored_msrs=0" > /etc/modprobe.d/kvm.conf
屏蔽显卡驱动:
echo "blacklist radeon" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist amdgpu" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist nouveau" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist nvidia" >> /etc/modprobe.d/pve-blacklist.conf
echo "blacklist nvidiafb" >> /etc/modprobe.d/pve-blacklist.conf
安装编译vendor-reset驱动需要的依赖(在网卡部分安装过则可以跳过)

apt install pve-headers-$(uname -r)
apt install git dkms build-essential

下载vender-reset驱动并编译
git clone https://github.com/gnif/vendor-reset.git
cd vendor-reset
dkms install .

#将驱动添加到modules管理中
echo "vendor-reset" >> /etc/modules
update-initramfs -u

#重启后运行
dmesg | grep vendor
#如果有输出,则表示安装成功
[    8.851280] vendor_reset_hook: installed
#关闭日志功能
systemctl mask systemd-journald
systemctl restart systemd-journald.service
#系统自带日志
vim /etc/rsyslog.conf
service syslog restart

磁盘清理

du -ah --max-depth=1
find . -type f -size +100M -print0 | xargs -0 du -h
sudo lsof |grep delete
结束进程

SSD硬盘缓存加速

apt install fio
顺序读
fio ./test -direct=1 -rw=read -iodepth=1 -ioengine=psync -bs=4M -size=2G -numjobs=1 -runtime=10 -group_reporting -name=name

顺序写
fio ./test -direct=1 -rw=write -iodepth=1 -ioengine=psync -bs=4M -size=2G -numjobs=1 -runtime=10 -group_reporting -name=name

查看硬盘
fdisk -l 
apt install bcache-tools
机械
make-bcache -B /dev/sdd
固态
make-bcache -C /dev/sdg

获取缓存盘的cset.uuid
bcache-super-show /dev/sdc
echo f21736ce-5e86-4c61-88b8-569a43b38b76 > /sys/block/bcache0/bcache/attach

如果你的SSD足够强大,可以不跟踪,减少跟踪的开销
echo 0 > /sys/fs/bcache/f21736ce-5e86-4c61-88b8-569a43b38b76/congested_read_threshold_us
echo 0 > /sys/fs/bcache/f21736ce-5e86-4c61-88b8-569a43b38b76/congested_write_threshold_us

参数调优
echo writeback > /sys/block/bcache0/bcache/cache_mode

1024*1024表示1MB,超过1MB的数据不会进入缓存盘,也就是仅加速小文件(默认4MB)。调整为0加速全部读写,但不建议使用,非常影响固态寿命
echo $((1024*1024)) > /sys/block/bcache0/bcache/sequential_cutoff
echo 0 > /sys/block/bcache0/bcache/sequential_cutoff
调整writeback缓存比例,0-40可以选择
echo 40 > /sys/block/bcache0/bcache/writeback_percent
预读缓存
echo "8192" > /sys/block/sdc/queue/read_ahead_kb
格式化
mkfs.ext4 /dev/bcache0
mount /dev/bcache0 /mnt/pve/HDD3T/
umount /mnt/pve/apple

查看信息
ls /sys/fs/bcache/

查看缓存
cat /sys/block/bcache0/bcache/dirty_data
命中率
cat /sys/block/bcache0/bcache/stats_total/cache_hit_ratio
查看缓存信息
cat /sys/class/block/bcache0/bcache/writeback_rate_debug

删除缓存盘
echo 7db6b0b0-073f-48b6-89b2-68ad8bf2ce95 > /sys/block/bcache0/bcache/detach
注销缓存盘
echo 1 > /sys/fs/bcache/7db6b0b0-073f-48b6-89b2-68ad8bf2ce95/unregister 
停用后端磁盘
echo 1 >/sys/block/bcache2/bcache/stop

查看UUID挂载磁盘
lsblk -lf
挂载硬盘
/etc/fstab
/dev/bcache0 /mnt/aa ext4 defaults 0 0

UUID为bcache0的
如 bcache0  ext4 1.0 bbc03a76-9bd0-41f4-81de-1a3aa77339dc 1.3T 48% /mnt/pve/HDD3T
UUID=18d5fb9b-e480-4bde-83a0-191abc475158 /mnt/pve/HDD2T ext4 defaults 0 0

风扇转速调节

/sys/class/hwmon/hwmon2/pwm1_enable #1为手动5为自动
/sys/class/hwmon/hwmon2/pwm1 #echo 144 > pwm1 最高255满速

#依赖文件
sudo apt-get install jq

#GPU调节程序
/usr/bin/ #文件目录

#!/bin/bash

# 这里是注释,请不要修改这部分,脚本没设置错误机制
# 请按照下面demo进行修改填写,出错了请自行解决。
#
# "interval"  : "5",       // 调整一次风扇速度间隔时间为5
# "hwmon"     : "4",       // 第4组,hwmon4
# "pwm_chan"  : "1",       // hwmon中第1个通道 hwmon4/pwm1
# "fan_chan"  : "1",       // 某些主板这里并不是和pwm通道对应的
# "fan_min"   : "10",      // 风扇启动时候最小转速为10%
# "fan_max"   : "70",      // 风扇正常调速时最高转速70%
# "tmp_start" : "45",      // 当显卡温度达到此(45度)温度,风扇启动
# "tmp_ratio" : "2",       // 当显卡温度每升高1度,风扇转速提高2%
# "tmp_high"  : "82"       // 当显卡温度达到此(82度)温度,风扇全速运转(100%)

cfg_path="/etc/gpufan-ctrl/config.json"

cfg_interval=$(jq -r '.interval' $cfg_path)
cfg_hwmon=$(jq -r '.hwmon' $cfg_path)
cfg_pwm_chan=$(jq -r '.pwm_chan' $cfg_path)
cfg_fan_chan=$(jq -r '.fan_chan' $cfg_path)
cfg_fan_min=$(jq -r '.fan_min' $cfg_path)
cfg_fan_max=$(jq -r '.fan_max' $cfg_path)
cfg_tmp_start=$(jq -r '.tmp_start' $cfg_path)
cfg_tmp_ratio=$(jq -r '.tmp_ratio' $cfg_path)
cfg_tmp_high=$(jq -r '.tmp_high' $cfg_path)

# control path
ctrl_enable="/sys/class/hwmon/hwmon${cfg_hwmon}/pwm${cfg_pwm_chan}_enable"
ctrl_path="/sys/class/hwmon/hwmon${cfg_hwmon}/pwm${cfg_pwm_chan}"
fan_input="/sys/class/hwmon/hwmon${cfg_hwmon}/fan${cfg_fan_chan}_input"
if [ -e "$ctrl_enable" ]; then
    echo "1" > $ctrl_enable
fi

if [ ! -e "$ctrl_path" ]; then
    echo "pwm control path not found! please make sure driver&config work correct!"
    exit -1
fi

if [ ! -e "$fan_input" ]; then
    echo "fan input path not found! please make sure driver&config work correct!"
    exit -1
fi

# fan input
ctrl_value="0"

# control logical
echo "| GPU Temp | Fan Speed | Fan RPM |"
while true; do
    curr_tmp=$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader)
    if [ $curr_tmp -le $cfg_tmp_start ]; then
        ctrl_value="0"
    elif [ $curr_tmp -gt $cfg_tmp_high ]; then
        ctrl_value="100"
    else
        ctrl_value=$(( (($curr_tmp - $cfg_tmp_start) * $cfg_tmp_ratio) + $cfg_fan_min ))
    fi

    abs_ctrl_value=$(( (ctrl_value * 2) + (ctrl_value >> 1) ))
    echo $abs_ctrl_value > $ctrl_path
    fan_value=$(cat $fan_input)

    printf "| %7.4s° | %8.4s%% | %7.4s |\n" "$curr_tmp" "$ctrl_value" "$fan_value"
    sleep $cfg_interval
done

#HDD配置
hddtemp-lt /dev/sdg | grep -o '[0-9]*$' | awk '{print $1}'

config.json配置文件
{
    "interval"  : "5", 
    "hwmon"     : "4",
    "pwm_chan"  : "1",
    "fan_chan"  : "1",
    "fan_min"   : "10",
    "fan_max"   : "70",
    "tmp_start" : "45",
    "tmp_ratio" : "2",
    "tmp_high"  : "82"
}

#自启服务
/etc/systemd/system/gpufan-ctrl.service

[Unit]
Description=gpu fan control
After=network.target

[Service]
ExecStart=/usr/bin/gpufan-ctrl > /dev/null
Restart=always

[Install]
WantedBy=multi-user.target

#启动服务
systemctl enable gpufan-ctrl.service
systemctl start gpufan-ctrl.service #开机自启
卸载
systemctl stop gpufan-ctrl.service
systemctl disable gpufan-ctrl.service
rm -f /etc/systemd/system/gpufan-ctrl.service
systemctl daemon-reload
Last modification:August 4, 2024
如果觉得我的文章对你有用,请随意赞赏