## 什么是 PVE
Proxmox VE(Proxmox Virtual Environment)是一种开源的服务器虚拟化管理平台,它结合了 KVM 虚拟机(VMs)、容器,以及软件定义存储和网络功能,提供一个完整的虚拟化解决方案。在 Proxmox VE 上可以创建和管理多个虚拟机,这些虚拟机可以用于各种应用,如数据中心虚拟化、服务器整合、应用测试、和灾难恢复等。
PVE 虚拟机的主要特点包括:
1. 多种虚拟化技术支持:支持基于 KVM 的虚拟机以及基于 LXC 的容器虚拟化。
2. 集群管理:可以轻松管理多服务器集群,提供高可用性和负载均衡。
3. 易于使用的界面:提供一个基于 web 的管理界面,便于用户配置和管理虚拟机。
4. 备份和恢复:内置的备份工具可以帮助用户备份和恢复虚拟机,确保数据安全。
5. 资源监控:提供资源使用情况的监控,包括 CPU、内存、网络和磁盘使用情况。
## 更换 PVE 源
1. 保存脚本文件 `update-sources.sh`
```Shell
#!/bin/bash
dir="/etc/apt/sources.list.d/"
file="/etc/apt/sources.list"
if [ -d "$dir" ]; then
echo "Deleting directory $dir..."
rm -rf "$dir"
echo "Directory deleted."
else
echo "Directory $dir does not exist. Skipping deletion."
fi
echo "Replacing content of $file..."
echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > "$file"
echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> "$file"
echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware" >> "$file"
echo "deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main" >> "$file"
echo "deb https://mirrors.ustc.edu.cn/proxmox/debian bookworm pve-no-subscription" >> "$file"
echo "Content replaced."
```
2. 然后执行
```Shell
$ chmod +x update-sources.sh && ./update-sources.sh
$ apt update
$ apt dist-upgrade -y
```
## 更改网络为 DHCP
修改 `/etc/network/interfaces` 文件,找到对应的网卡修改为 `dhcp`,例如:
```
iface vmbr0 inet dhcp
bridge-ports enp2s0
bridge-stp off
bridge-fd 0
```
## SR-IOV 虚拟化核显
1. 首先设置 BIOS,以 NUC13 机器为例
```
1. 开机画面按 F2 进入 BIOS
2. Advanced > Video > Primary Display > 选择 IGFX,确保核显优先作为主显示设备(核显+独显建议改成 IGFT,否则可能无法识别核显)
3. Boot > Secure Boot > 选择 Disabled,解决由于内核模块签名验证失败或驱动签名无效
4. 按 F10 保存
```
3. 开启 SR-IOV 硬件的虚拟化,将核显实例数量设置为3,最大虚拟化实例数设定为7。
```shell
$ sed -i '/^GRUB_CMDLINE_LINUX_DEFAULT/c\GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt pcie_acs_override=downstream i915.enable_guc=3 i915.max_vfs=7"' /etc/default/grub
$ echo -e "vfio\nvfio_iommu_type1\nvfio_pci\nvfio_virqfd" | tee -a /etc/modules
$ update-grub
$ update-initramfs -u -k all
$ reboot
```
3. 检查显卡是否直通成功
```shell
$ lspci | grep VGA
```
4. 更新 i915 SR-IOV 驱动
```shell
$ apt install build-* dkms git sysfsutils -y
$ apt install proxmox-headers-$(uname -r) proxmox-kernel-$(uname -r)
$ cd ~
$ git clone https://github.com/strongtz/i915-sriov-dkms.git
$ cd ~/i915-sriov-dkms
$ dkms add .
$ dkms install -m i915-sriov-dkms -v $(cat VERSION) --force
$ lspci | grep VGA # 请记录当前的核显ID,修改下行的ID后执行下面的代码。
$ echo "devices/pci0000:00/0000:00:02.0/sriov_numvfs = 3" > /etc/sysfs.conf
$ reboot
```
5. 检查是否虚拟化成功
```shell
$ cat /sys/bus/pci/devices/0000:00:02.0/sriov_totalvfs # 返回 7
$ cat /sys/bus/pci/devices/0000:00:02.0/sriov_numvfs # 返回 3
$ lspci | grep VGA
```
提示:在 PVE 系统中,出现错误提示 “No /etc/kernel/proxmox-boot-uuids found, skipping ESP sync” 通常与引导配置相关。可以通过以下步骤重置 `proxmox-boot-tool` 配置
```shell
umount /boot/efi # 卸载 EFI 引导分区
lsblk # 查看磁盘分区信息
proxmox-boot-tool init /dev/nvme1n1p2 # 初始化指定的引导分区
mount /dev/nvme1n1p2 /boot/efi # 重新挂载 EFI 引导分区
proxmox-boot-tool refresh # 刷新引导配置
update-initramfs -u -k all # 更新初始内存盘
cat /etc/kernel/proxmox-boot-uuids # 查看引导配置文件是否正常
proxmox-boot-tool status # 检查当前引导状态是否正常
```
## 双显卡独显直通
```
$ vim /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt initcall_blacklist=sysfb_init pcie_acs_override=downstream"
$ update-grub
$ vim /etc/modules
vfio
vfio_iommu_type1
vfio_pci
vfio_virqfd
$ reboot
$ dmesg | grep -e DMAR -e IOMMU
验证是否有:
DMAR: IOMMU enabled
$ dmesg | grep remapping
验证是否有:
[ 0.161756] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[ 0.163274] DMAR-IR: Enabled IRQ remapping in x2apic mode
$ vim /etc/modprobe.d/pve-blacklist.conf
blacklist nvidiafb
blacklist nouveau
blacklist nvidia
$ vim /etc/modprobe.d/blacklist.conf
blacklist snd_hda_intel
$ vim /etc/modprobe.d/kvm.conf
options kvm ignore_msrs=1 report_ignored_msrs=0
$ lspci -nn | grep VGA
$ lspci -n -s 01:00
$ lspci -n -s 3e:00
$ vim /etc/modprobe.d/vfio.conf
options vfio-pci ids=10de:2684,10de:22ba,10de:2486,10de:228b
$ update-initramfs -u -k all
$ reboot
$ lspci -nnk
```
## 挂载新磁盘
PVE 中将挂载新磁盘,并创建 LVM-Thin 存储池,完整流程如下
1. 选择节点 > 磁盘 > 选择磁盘(/dev/nvme0n1) > 擦除硬盘
2. 创建物理卷
```shell
$ pvcreate /dev/nvme0n1
```
3. 加入卷组
```shell
# 加入现有的卷组
$ vgextend pve /dev/nvme0n1
# 或者创建新的卷组
# $ vgcreate vg_data /dev/nvme0n1
```
4. 创建逻辑卷
```shell
# 查看卷组剩余大小
$ vgs
# 创建 Thin Pool 逻辑卷 (LVM-Thin 精简池)
$ lvcreate -L 1.8T --thinpool data2 pve --chunksize 64K -Zy
```
5. 数据中心 > 存储 > 添加 LVM-Thin > 选择 data2 精简池 > 添加
## SMB 备份
PVE 中创建一个 SMB 共享存储用于虚拟机备份,完整流程如下
1. 安装 CIFS 工具
```shell
$ apt update
$ apt install cifs-utils
```
2. 创建挂载点
```shell
$ mkdir -p /mnt/pve-backup
$ mount -t cifs //192.168.22.2/pve-backup /mnt/pve-backup -o username=xxx,password=123456
$ df -h | grep /mnt/pve-backup
```
3. 自动挂载,编辑 `/etc/fstab` 文件,添加如下内容:
```
//192.168.22.2/pve-backup /mnt/pve-backup cifs username=user,password=123456 0 0
```
4. PVE管理后台 > 数据中心 > 存储 > 添加目录:
```
ID: pve-backup
目录: /mnt/pve-backup
内容: VZDump备份文件
```
5. 之后就可以在数据中心中设置自动备份任务,或手动选择虚拟机创建备份
## PVE 导入 img 磁盘映像
1. PVE管理后台 > 选择节点 > local > ISO镜像 > 上传 img 文件
2. PVE Shell:
```shell
$ qm importdisk 100 /var/lib/vz/template/iso/immortalwrt-24.10.0-rc3-x86-64-generic-squashfs-combined-efi.img local-lvm
```
## PVE 导入 QCOW2 磁盘映像
1. 可以通过 SCP 或 SFTP 将 `xxx.qcow2` 上传到 `/var/lib/vz/images/` 目录下
2. PVE Shell:
```shell
$ qm importdisk 101 /var/lib/vz/images/xxx.qcow2 local-lvm
```
## PVE 创建 LXC 容器开启 TUN
编辑 `/etc/pve/lxc/{ID}.conf` 文件,添加如下配置
```
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
```
## 常见问题
### PVE 无法找到显卡导致的安装失败
在 NUC13 + RTX 4090 机器上安装 PVE 时,提示错误:`Cannot run in framebuffer mode. Please specify busIDs for all framebuffer devices`,解决步骤如下
1. 按 Ctrl + Alt + F2 查看安装时遇到的错误(如上提示的错误信息)
2. 按 Ctrl + Alt + F1 返回控制台
3. 查看显卡的 PCI 设备信息,获取设备编号:`01:00.0`
```shell
$ lspci | grep VGA
# 01:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
```
4. 创建显卡驱动配置文件
```shell
$ mkdir -p /usr/share/X11/xorg.conf.d/
$ vi /usr/share/X11/xorg.conf.d/driver-nvidia.conf
```
5. 保存如下内容(将 `01:00.0` 去掉前导零,得到正确的 BusID `PCI:1:0:0`)
```
Section "Device"
Identifier "Card0"
Driver "fbdev" # 如果你已安装 NVIDIA 驱动,可以改为 "nvidia"
BusID "PCI:1:0:0"
EndSection
```
6. 按 Ctrl + D 退出重启
### 通电自动启动
通常在 PVE 服务器和数据中心,建议开启通电自动启动功能,以便在电源故障恢复后实现无人值守操作,以 NUC13 机器为例
```
1. 开机画面按 F2 进入 BIOS
2. Power > Secondary Power Settings > After Power Failure > 选择 Power On
3. 按 F10 保存
```
## 参考
- https://pve.sqlsec.com/
- https://v2rayssr.com/pve.html
- https://forum.proxmox.com/threads/generic-solution-when-install-gets-framebuffer-mode-fails.111577/