1201 字
6 分钟
将PVE的温度与公告信息添加到Linux虚拟机中
1. 在PVEwebui的系统概要中显示温度
使用脚本一键完成
wget -O /root/showtempcpufreq.sh https://raw.githubusercontent.com/a904055262/PVE-manager-status/main/showtempcpufreq.sh && chmod +x /root/showtempcpufreq.sh && /root/showtempcpufreq.sh2. 将PVE的温度信息同步到Linux虚拟机中
2.1. 前提提要
PVE版本:9.0.5 虚拟机系统:Ubuntu server 24.04.3 LTS 虚拟机机型:q35(为必须,否则将无法挂载温度文件到虚拟机)
2.2 在 PVE 宿主机上定时获取 CPU 温度
2.2.1. 创建共享目录并设置权限
在 PVE 主机上执行:
# 创建目录(按需修改路径)mkdir -p /srv/virtfs/sharedtemp
# 设置所有者与权限(允许宿主写、虚拟机读)chown root:root /srv/virtfs/sharedtempchmod 755 /srv/virtfs/sharedtemp各位可以自行修改目录的位置,记得这里修改了,后面关于位置的信息也要进行修改
2.2.2. 安装并启动sensors
apt updateapt install lm-sensors -ysensors-detect --autosensors执行完 sensors 命令之后应该可以看到这样的输出
root@pve:~# sensorscoretemp-isa-0000Adapter: ISA adapterPackage id 0: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 0: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 1: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 2: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 3: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 4: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 5: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 6: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 8: +29.0°C (high = +90.0°C, crit = +100.0°C)Core 9: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 10: +31.0°C (high = +90.0°C, crit = +100.0°C)Core 11: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 12: +32.0°C (high = +90.0°C, crit = +100.0°C)Core 13: +29.0°C (high = +90.0°C, crit = +100.0°C)Core 16: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 17: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 18: +31.0°C (high = +90.0°C, crit = +100.0°C)Core 19: +31.0°C (high = +90.0°C, crit = +100.0°C)Core 20: +29.0°C (high = +90.0°C, crit = +100.0°C)Core 21: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 22: +29.0°C (high = +90.0°C, crit = +100.0°C)Core 24: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 25: +29.0°C (high = +90.0°C, crit = +100.0°C)Core 26: +30.0°C (high = +90.0°C, crit = +100.0°C)Core 27: +31.0°C (high = +90.0°C, crit = +100.0°C)Core 28: +31.0°C (high = +90.0°C, crit = +100.0°C)Core 29: +32.0°C (high = +90.0°C, crit = +100.0°C)
pch_lewisburg-virtual-0Adapter: Virtual devicetemp1: +45.0°C
coretemp-isa-0001Adapter: ISA adapterPackage id 1: +37.0°C (high = +90.0°C, crit = +100.0°C)Core 0: +37.0°C (high = +90.0°C, crit = +100.0°C)Core 1: +37.0°C (high = +90.0°C, crit = +100.0°C)Core 2: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 3: +34.0°C (high = +90.0°C, crit = +100.0°C)Core 4: +34.0°C (high = +90.0°C, crit = +100.0°C)Core 5: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 6: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 8: +34.0°C (high = +90.0°C, crit = +100.0°C)Core 9: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 10: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 11: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 12: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 13: +37.0°C (high = +90.0°C, crit = +100.0°C)Core 16: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 17: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 18: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 19: +38.0°C (high = +90.0°C, crit = +100.0°C)Core 20: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 21: +34.0°C (high = +90.0°C, crit = +100.0°C)Core 22: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 24: +33.0°C (high = +90.0°C, crit = +100.0°C)Core 25: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 26: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 27: +36.0°C (high = +90.0°C, crit = +100.0°C)Core 28: +35.0°C (high = +90.0°C, crit = +100.0°C)Core 29: +35.0°C (high = +90.0°C, crit = +100.0°C)
power_meter-acpi-0Adapter: ACPI interfacepower1: 288.00 W (interval = 1.00 s)2.2.3. 编写采集脚本
创建脚本 /usr/local/bin/update_cpu_temp.sh
#!/bin/bash
# 可调校正值CORRECTION=0
# 获取 CPU Package 温度TEMP=$(sensors 2>/dev/null | grep -m1 -E "Package id|Package" | awk '{print $4}')if [ -z "$TEMP" ]; then TEMP=$(sensors 2>/dev/null | grep -m1 'Core 0' | awk '{print $3}')fiTEMP_NUM=$(echo "$TEMP" | sed 's/[^0-9.]//g')if [ -z "$TEMP_NUM" ]; then TEMP_NUM=0; fiADJUSTED_TEMP=$(echo "$TEMP_NUM + $CORRECTION" | bc)ADJUSTED_TEMP_FMT=$(printf "%.1f°C" "$ADJUSTED_TEMP")
# 获取功率POWER=$(sensors 2>/dev/null | grep -m1 'power1' | awk '{print $2}')POWER_NUM=$(echo "$POWER" | sed 's/[^0-9.]//g')if [ -z "$POWER_NUM" ]; then POWER_NUM=0; fiPOWER_FMT=$(printf "%.1fW" "$POWER_NUM")
# 时间戳DATE=$(date -Is)
# 写入文件echo "$DATE CPU:$ADJUSTED_TEMP_FMT POWER:$POWER_FMT" > /srv/virtfs/sharedtemp/cpu_temp.txtecho "$DATE CPU:$ADJUSTED_TEMP_FMT POWER:$POWER_FMT" >> /srv/virtfs/sharedtemp/cpu_temp.log给权限
chmod +x /usr/local/bin/update_cpu_temp.sh创建写入目录
mkdir -p /srv/virtfs/sharedtempchmod 777 /srv/virtfs/sharedtemp定期执行脚本(使用 systemd timer)
创建 service 单元 /etc/systemd/system/update-cpu-temp.service
[Unit]Description=Update CPU temp file
[Service]Type=oneshotExecStart=/usr/local/bin/update_cpu_temp.sh创建 timer `/etc/systemd/system/update-cpu-temp.timer
[Unit]Description=Run update-cpu-temp every minute
[Timer]OnCalendar=*:0/1Persistent=trueUnit=update-cpu-temp.service
[Install]WantedBy=timers.target启动并启用定期执行:
systemctl daemon-reloadsystemctl enable --now update-cpu-temp.timer验证宿主机端写入是否正常
# 等几秒后查看文件cat /srv/virtfs/sharedtemp/cpu_temp.txttail -n 20 /srv/virtfs/sharedtemp/cpu_temp.log2.3 创建并挂载
2.3.1 创建并添加VirtIO FS
- 在PVEwebui中选择
数据中心,找到存储,添加一个目录;ID可以随意填写,目录为 在PVE中创建共享目录所使用的文件夹(我这里就是/srv/virtfs/sharedtemp这个目录) - 点击虚拟机,现将虚拟机关机,再选择
硬件,点击添加,选择VirtIofs,点击目录ID之后会让选择,这里我们选择刚才填写的ID, 然后点击添加即可;再点击虚拟机下的选项,将QEMU Guest Agent设为启用 - 打开虚拟机,依次执行一下命令,安装
QEMU Guest Agent
apt-get install qemu-guest-agentsudo systemctl start qemu-guest-agentsudo systemctl enable qemu-guest-agent2.3.2 在虚拟机中添加挂载点
sudo mkdir -p /mnt/sharedtempsudo mount -t virtiofs sharedtemp /mnt/sharedtemp挂载后可以测试:
cat /mnt/sharedtemp/cpu_temp.txt你应该能看到这样的输出
2025-11-14T00:04:35+08:00 CPU:32.0°C POWER:283.0W至此,整个过程就这样结束了
将PVE的温度与公告信息添加到Linux虚拟机中
https://blog.goodnightan.com/posts/pve-vm-temperature-display/