## 0.必要条件 在上一篇文章 [02.PVE初始化配置](./02.PVE初始化配置.md) 中,已初始化了 PVE 系统,接下来需要对 PVE 系统进一步调整。 在 PVE 系统调整之前,请确认必要的软件包已经安装完成,本文后续命令均在 SSH 终端下执行。 Debian 系统默认编辑器为 `nano` ,推荐将其更换为 `neovim` ,关于 [Neovim](https://neovim.io/) 的基础使用方法,请参阅:[Neovim 基础教程](https://cn.bing.com/search?q=Neovim+%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B) 。 ```bash ## 修改系统默认编辑器(选择 nvim 所在条目,即可将 neovim 设为默认编辑器) $ update-alternatives --config editor #### 系统默认编辑器示例输出 There are 3 choices for the alternative editor (providing /usr/bin/editor). Selection Path Priority Status ------------------------------------------------------------ * 0 /bin/nano 40 auto mode 1 /bin/nano 40 manual mode 2 /usr/bin/nvim 30 manual mode 3 /usr/bin/vim.tiny 15 manual mode Press to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/bin/nvim to provide /usr/bin/editor (editor) in manual mode ``` ## 1.系统时区 如果在安装 PVE 系统时选错了时区,导致系统时间和北京时间不一致,可以执行以下命令修正。 若输出结果和北京时间一致,则代表修改正确。 ```bash ## 修改系统时区 $ timedatectl set-timezone Asia/Shanghai ## 检查系统时间 $ date -R #### 系统时间示例输出 Wed, 27 Aug 2025 15:12:21 +0800 ``` Debian 系统常用 `systemd-timesyncd.service` 来同步时间,而 PVE 系统使用 `chrony.service` 来同步时间。 为了使用国内的 NTP 服务器,需要对 `chrony.service` 进行配置,执行以下命令。 ```bash ## 编辑 chrony 配置文件 $ editor /etc/chrony/chrony.conf ``` 在编辑器对话框中,将 `pool 2.debian.pool.ntp.org iburst` “注释” 掉,并添加国内的 NTP 服务器。 ```bash ## chrony 配置项 # Use Debian vendor zone. # pool 2.debian.pool.ntp.org iburst ## 在这行前面增加注释符 # 来注释 # Use Custom vendor zone. pool ntp.aliyun.com iburst pool ntp.tencent.com iburst pool cn.pool.ntp.org iburst ``` 保存该配置文件后,需重启 `chrony.service` ,并再次检查系统 NTP 服务器地址。 ```bash ## 重启 chrony.service $ systemctl restart chrony.service ## 检查系统 NTP 服务器 $ chronyc sources -V #### 系统 NTP 服务器示例输出 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 17 0 -875us[-2496us] +/- 22ms ^- 106.55.184.199 2 6 17 3 -1331us[-1331us] +/- 50ms ^- time.neu.edu.cn 2 6 17 8 +935us[ +935us] +/- 23ms ^- 119.28.206.193 2 6 65 5 +33us[ +33us] +/- 65ms ^- electrode.felixc.at 2 6 17 11 -1320us[-1320us] +/- 126ms ^- dns1.synet.edu.cn 1 6 17 13 -44us[ -44us] +/- 22ms ``` ## 2. CPU 调度器 安装 `linux-cpupower` 后,需检查 CPU 当前调度器。 ```bash ## 检查 CPU 当前调度器 $ cpupower -c all frequency-info #### 设备 CPU - J4125 示例输出 analyzing CPU 0: driver: intel_cpufreq CPUs which run at the same hardware frequency: 0 CPUs which need to have their frequency coordinated by software: 0 maximum transition latency: 20.0 us hardware limits: 800 MHz - 2.70 GHz available cpufreq governors: conservative ondemand userspace powersave performance schedutil current policy: frequency should be within 800 MHz and 2.70 GHz. The governor "performance" may decide which speed to use within this range. current CPU frequency: Unable to call hardware current CPU frequency: 2.60 GHz (asserted by call to kernel) boost state support: Supported: yes Active: yes #### 设备 CPU - N100 示例输出 analyzing CPU 0: driver: intel_pstate CPUs which run at the same hardware frequency: 0 CPUs which need to have their frequency coordinated by software: 0 maximum transition latency: Cannot determine or is not supported. hardware limits: 800 MHz - 3.30 GHz available cpufreq governors: performance powersave current policy: frequency should be within 800 MHz and 3.30 GHz. The governor "performance" may decide which speed to use within this range. current CPU frequency: Unable to call hardware current CPU frequency: 2.00 GHz (asserted by call to kernel) boost state support: Supported: yes Active: yes ``` 这里面主要关注两个点: - `driver` :CPU 调频驱动方案,例如 `intel_cpufreq` 或 `intel_pstate` - `governor` :调频策略中使用的调度器,例如 `performance` CPU 驱动一般不建议手动调整,而 `governor` 后面的参数表示 CPU 当前调度器设置。 接下来,需要了解 CPU 支持的调度器有哪些,执行以下命令。 ```bash ## 检查 CPU 调度器支持情况 $ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors #### 设备 CPU - J4125 示例输出 conservative ondemand userspace powersave performance schedutil #### 设备 CPU - N100 示例输出 performance powersave ``` 根据 CPU 所使用的驱动不同,可选调度器也不同,至于每种调度器有什么优劣,欢迎大家深度挖掘。 - CPU 驱动为 `intel_cpufreq` 时,推荐使用 `schedutil` 调度器。 - CPU 驱动为 `intel_pstate` 时,推荐使用 `powersave` 调度器。 本文使用 `powersave` 调度器创建 `cpupower` 的配置文件。 ```bash ## 创建 cpupower 默认配置文件 $ editor /etc/default/cpupower ``` 在配置文件中输入以下内容,并保存。 ```bash # This configuration file is customized by fox, # Optimize system CPU governors. CPUPOWER_START_OPTS="frequency-set -g powersave" CPUPOWER_STOP_OPTS="frequency-set -g performance" ``` 进一步创建 `cpupower` 服务配置文件,以满足系统自动化设置需求。 ```bash ## 创建 cpupower.service 配置文件 $ editor /etc/systemd/system/cpupower.service ``` 在服务配置文件中输入以下内容,并保存。 ```bash # This configuration file is customized by fox, # Optimize for cpupower systemd service. [Unit] Description=Apply cpupower configuration ConditionVirtualization=!container After=syslog.target [Service] Type=oneshot EnvironmentFile=/etc/default/cpupower ExecStart=/usr/bin/cpupower $CPUPOWER_START_OPTS ExecStop=/usr/bin/cpupower $CPUPOWER_STOP_OPTS RemainAfterExit=yes [Install] WantedBy=multi-user.target ``` 由于修改了服务项,需要执行以下命令进行重载。 ```bash ## 服务重载 $ systemctl daemon-reload ``` 执行以下命令让 `cpupower` 服务开机自启动。 ```bash ## 设置 cpupower 服务开机自启 $ systemctl enable cpupower.service ``` 修改完成后,需重启 PVE 服务器,并再次查看 CPU 调度器,检验配置文件是否生效。 这里提供两个额外命令,方便实时查看 CPU 当前频率和温度状况。 ```bash ## 查看 CPU 当前频率 $ watch cat /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq ## 查看内部温度 $ watch -d sensors ``` ## 3. PVE 定时重启 有时需要让 PVE 服务器周期性的定时重启,则可执行以下命令。 参数表示每月 `1` 、 `16` 号的 `02:30` 执行系统重启命令。 ```bash ## 查看系统定时任务 $ crontab -l ## 编辑系统定时任务,编辑器选择 nano $ crontab -e ``` 在配置文件末尾,增加以下内容。 ```bash ## 定时任务配置项 30 2 1,16 * * /usr/sbin/reboot ``` ## 4. PVE 自动更新 ### 4.1.系统定时器 配置系统自动更新之前,需检查系统当前定时器状态。 后续将手动调整该定时器的时间,使其每 `5` 天的 `01:30` 进行触发。 ```bash ## 检查系统定时器 $ systemctl status apt-daily-upgrade.timer #### 系统定时器示例输出 ● apt-daily-upgrade.timer - Daily apt upgrade and clean activities Loaded: loaded (/usr/lib/systemd/system/apt-daily-upgrade.timer; enabled; preset: enabled) Active: active (waiting) since Wed 2025-08-27 13:41:10 CST; 1h 37min ago Invocation: 26ebb62a4f6545998522fff8c4104b3e Trigger: Thu 2025-08-28 06:14:37 CST; 14h left Triggers: ● apt-daily-upgrade.service Aug 27 13:41:10 node01 systemd[1]: Started apt-daily-upgrade.timer - Daily apt upgrade and clean activities. ``` ### 4.2.配置更新策略 执行以下命令,启用系统自动更新。 执行命令后,使用 “左右” 方向键进行选择,“回车” 键进行确认。 ```bash ## 配置自动更新策略 $ dpkg-reconfigure -plow unattended-upgrades ## 选择 “是” ``` 进一步调整 `20auto-upgrades` 配置文件。 ```bash ## 进入 apt 的配置目录 $ cd /etc/apt/apt.conf.d ## 编辑 20auto-upgrades 配置文件 $ editor /etc/apt/apt.conf.d/20auto-upgrades ``` 清空当前全部配置项后,输入以下内容,并保存。 配置文件中,用来控制更新周期的参数为 `APT::Periodic::Unattended-Upgrade` ,`10` 表示更新周期为 `10` 天。 ```bash ## 系统更新周期配置项 APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "10"; APT::Periodic::AutocleanInterval "1"; APT::Periodic::CleanInterval "1"; ``` 进一步调整 `50unattended-upgrades` 配置文件。 ```bash ## 编辑 50unattended-upgrades 配置文件 $ editor /etc/apt/apt.conf.d/50unattended-upgrades ``` 配置文件中,被修改的参数解释如下: - 启用了 Debian `trixie-updates` 相关更新。 - 增加并启用 PVE 自有仓库的更新。 - 增加并启用 PVE Ceph 仓库的更新,请按需启用。 - 自动修复被打断的 Dpkg 安装。 - 自动移除无用的的内核包。 - 自动移除因更新而出现的无用依赖包。 - 自动移除以前的无用依赖包。 - 自动重启:开启。 - 自动重启时间:`02:30` 。 因为该配置文件很长,完整的配置文件可查看 [pve_50unattended_upgrades.conf](./src/pve/pve_50unattended_upgrades.conf) 以便对比。 ```bash ## 删除以下行前面的注释符 // ,代表启用 "origin=Debian,codename=${distro_codename}-updates"; ## 添加 PVE 系统更新项目 "origin=Proxmox,codename=${distro_codename},label=Proxmox Debian Repository"; ## 按需添加 PVE Ceph 更新项目 "origin=Proxmox,codename=${distro_codename},label=Proxmox Ceph 19 Squid Debian Repository"; ## 在配置文件末尾增加以下内容,代表启用,并调整参数 Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; Unattended-Upgrade::Automatic-Reboot "true"; Unattended-Upgrade::Automatic-Reboot-Time "02:30"; ``` ### 4.3.重设触发器 系统自动更新配置文件修改完成后,需要重设自动更新定时器,执行以下命令。 完整的配置文件可查看 [pve_apt_daily_upgrade.conf](./src/pve/pve_apt_daily_upgrade.conf) 以便对比。 ```bash ## 配置系统定时器 $ systemctl edit apt-daily-upgrade.timer ``` 根据配置文件中的提示,在中间空白处填入以下内容。 ```bash ## 定时器配置项 [Timer] OnCalendar= OnCalendar=01:30 RandomizedDelaySec=0 ``` 设置完成后,重启自动更新触发器。 ```bash ## 重启触发器 $ systemctl restart apt-daily-upgrade.timer ## 再次检查触发器状态 $ systemctl status apt-daily-upgrade.timer #### 系统自动更新触发器示例输出 ● apt-daily-upgrade.timer - Daily apt upgrade and clean activities Loaded: loaded (/usr/lib/systemd/system/apt-daily-upgrade.timer; enabled; preset: enabled) Drop-In: /etc/systemd/system/apt-daily-upgrade.timer.d └─override.conf Active: active (waiting) since Wed 2025-08-27 15:23:29 CST; 8s ago Invocation: e38354f06d284894b084a52e1f19af73 Trigger: Thu 2025-08-28 01:30:00 CST; 10h left Triggers: ● apt-daily-upgrade.service Aug 27 15:23:29 node01 systemd[1]: Stopping apt-daily-upgrade.timer - Daily apt upgrade and clean activities... Aug 27 15:23:29 node01 systemd[1]: Started apt-daily-upgrade.timer - Daily apt upgrade and clean activities. ``` ## 5.硬件直通 ### 5.1.修改 Grub 参考官方文档 [qm_pci_passthrough](https://pve.proxmox.com/pve-docs/pve-admin-guide.html#qm_pci_passthrough) 和 [Pci passthrough](https://pve.proxmox.com/wiki/Pci_passthrough) 开启 PVE 硬件直通功能。 使用终端工具登录到 PVE 服务器,编辑系统 `Grub` 的配置文件 `/etc/default/grub` 。 ```bash ## 编辑 Grub 配置文件 $ editor /etc/default/grub ``` 在编辑器对话框中修改 `GRUB_CMDLINE_LINUX_DEFAULT` 参数,注意命令中间的空格。 ```bash ## Intel 处理器添加参数 GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt" ``` 根据官方文档的说明,AMD 处理器下硬件直通功能将会自动打开,否则需要手动修改 `Grub` 配置文件。 ```bash ## AMD 处理器添加参数 GRUB_CMDLINE_LINUX_DEFAULT="quiet amd_iommu=on iommu=pt" ``` 修改并保存后,需要更新系统 `Grub` 。 ```bash ## 更新系统 Grub $ update-grub ``` ### 5.2.添加内核模块 编辑 **内核模块** 配置文件,增加必要的系统模块,执行以下命令。 ```bash ## 编辑系统配置文件 $ editor /etc/modules-load.d/10-server-modules.conf ``` 在配置文件中输入以下内容,并保存。 ```bash # This configuration file is customized by fox, # Optimize vfio related modules at system boot. vfio vfio_iommu_type1 vfio_pci ``` 执行以下命令更新 `initramfs` ,更新完成后,建议重启 PVE 服务器。 ```bash ## 更新 initramfs $ update-initramfs -u -k all ``` ### 5.3.检查硬件直通 PVE 服务器重启完成后,再次使用终端工具登录,并执行以下命令检查硬件直通状态。 主要查看 `IOMMU` 、 `Directed I/O` 或 `Interrupt Remapping` 的启用状态。 ```bash ## 检查系统硬件直通状态 $ dmesg | grep -e DMAR -e IOMMU -e AMD-Vi #### 设备 CPU - J4125 示例输出 [ 0.008356] ACPI: DMAR 0x000000007953E000 0000A8 (v01 INTEL GLK-SOC 00000003 BRXT 0100000D) [ 0.008406] ACPI: Reserving DMAR table memory at [mem 0x7953e000-0x7953e0a7] [ 0.055618] AMD-Vi: Unknown option - 'on' [ 0.220916] DMAR: Host address width 39 [ 0.220919] DMAR: DRHD base: 0x000000fed64000 flags: 0x0 [ 0.220939] DMAR: dmar0: reg_base_addr fed64000 ver 1:0 cap 1c0000c40660462 ecap 9e2ff0505e [ 0.220944] DMAR: DRHD base: 0x000000fed65000 flags: 0x1 [ 0.220954] DMAR: dmar1: reg_base_addr fed65000 ver 1:0 cap d2008c40660462 ecap f050da [ 0.220960] DMAR: RMRR base: 0x000000794a9000 end: 0x000000794c8fff [ 0.220963] DMAR: RMRR base: 0x0000007b800000 end: 0x0000007fffffff [ 0.220967] DMAR-IR: IOAPIC id 1 under DRHD base 0xfed65000 IOMMU 1 [ 0.220970] DMAR-IR: HPET id 0 under DRHD base 0xfed65000 [ 0.220973] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping. [ 0.223069] DMAR-IR: Enabled IRQ remapping in x2apic mode [ 0.493005] DMAR: No ATSR found [ 0.493008] DMAR: No SATC found [ 0.493010] DMAR: dmar0: Using Queued invalidation [ 0.493018] DMAR: dmar1: Using Queued invalidation [ 0.494092] DMAR: Intel(R) Virtualization Technology for Directed I/O ``` 检查系统 `IOMMU` 分组,执行以下命令。 ```bash ## 检查 IOMMU group $ find /sys/kernel/iommu_groups/ -type l #### 设备 CPU - J4125 示例输出 /sys/kernel/iommu_groups/17/devices/0000:04:00.0 /sys/kernel/iommu_groups/7/devices/0000:00:13.3 /sys/kernel/iommu_groups/15/devices/0000:02:00.0 /sys/kernel/iommu_groups/5/devices/0000:00:13.0 /sys/kernel/iommu_groups/13/devices/0000:00:1f.0 /sys/kernel/iommu_groups/13/devices/0000:00:1f.1 /sys/kernel/iommu_groups/3/devices/0000:00:0f.0 /sys/kernel/iommu_groups/11/devices/0000:00:17.0 /sys/kernel/iommu_groups/11/devices/0000:00:17.3 /sys/kernel/iommu_groups/11/devices/0000:00:17.1 /sys/kernel/iommu_groups/11/devices/0000:00:17.2 /sys/kernel/iommu_groups/1/devices/0000:00:00.0 /sys/kernel/iommu_groups/18/devices/0000:05:00.0 /sys/kernel/iommu_groups/8/devices/0000:00:14.0 /sys/kernel/iommu_groups/16/devices/0000:03:00.0 /sys/kernel/iommu_groups/6/devices/0000:00:13.2 /sys/kernel/iommu_groups/14/devices/0000:01:00.0 /sys/kernel/iommu_groups/4/devices/0000:00:12.0 /sys/kernel/iommu_groups/12/devices/0000:00:1c.0 /sys/kernel/iommu_groups/2/devices/0000:00:0e.0 /sys/kernel/iommu_groups/10/devices/0000:00:15.0 /sys/kernel/iommu_groups/0/devices/0000:00:02.0 /sys/kernel/iommu_groups/9/devices/0000:00:14.1 ``` ## 6. BTRFS 调整 **额外说明:** 1. 本节专为 `BTRFS` 单盘 `RAID0`(条带模式)安装的 PVE 系统设计,使用其他安装模式时,请跳过此节。 2. `BTRFS` 文件系统当前仍为技术预览状态,请谨慎操作。 3. 有关在 PVE 中使用 `BTRFS` 的详情,请参阅 [Proxmox VE - BTRFS](https://pve.proxmox.com/wiki/BTRFS) 。 安装 PVE 时,若使用了 `BTRFS` 单盘 `RAID0` 的安装模式,系统默认未启用 swap 和 zstd 压缩,需要手动开启。 通常情况下,内存与 swap 的 **推荐** 比例为 `1:1` 。本机具有 `16GB` 内存,因此设置 `16GB` swap 空间。 执行以下命令,在 `BTRFS` 文件系统中创建子卷,并配置激活 swapfile 。 ```bash ## 创建用于存放交换文件的子卷 $ btrfs subvolume create /swap ## 在子卷中创建 16GB 的交换文件 $ btrfs filesystem mkswapfile --size 16g --uuid clear /swap/swapfile ## 激活交换文件 $ swapon /swap/swapfile ``` 此时还需进一步修改系统的 `fstab` 配置文件,以启用 `BTRFS` 的 zstd 压缩功能并确保 swap 在系统启动时自动激活。 ```bash ## 编辑 fstab 配置文件 $ editor /etc/fstab ``` `fstab` 为系统关键配置文件,直接影响系统启动,修改此文件时,请注意以下几点: - 仅修改根目录 `/` 对应的挂载选项,添加 `compress=zstd` 参数。 - 在文件末尾新增一行,添加 swap 的自动挂载。 - 请 **不要** 修改其余配置参数,尤其是设备的唯一标识符( `UUID` ),切勿修改。 修改完成后,示例如下。 ```bash #### 系统 fstab 示例配置 # UUID= / btrfs defaults,compress=zstd 0 1 UUID= /boot/efi vfat defaults 0 1 proc /proc proc defaults 0 0 /swap/swapfile none swap defaults 0 0 ``` ## 7.系统清理 PVE 系统配置完成后,可执行以下命令,对系统进行清理。 ```bash ## 清理系统软件包 $ apt clean && apt autoclean && apt autoremove --purge ## 清理系统缓存 $ bash -c 'find /var/cache/apt/ /var/lib/apt/lists/ /tmp/ -type f -print -delete' ## 清理系统日志 $ bash -c 'find /var/log/ -type f -print -delete' ## 清理命令历史记录文件 $ rm -rvf ~/.bash_history && history -c ``` 至此 PVE 的系统调整已经完成。