# PVE New Setup in One Page

本文的内容是对 PVE 创建的新虚拟机应做什么样的配置。

## ~~直接对模板进行的文件操作~~

此类操作已经集成在 vendor-data 中。

### Setup DNS

编辑文件：`/etc/systemd/resolved.conf`

然后重启服务：`sudo systemctl restart systemd-resolved.service`

### 网络优化：开启 BBR 并调整窗口大小

在 `/etc/sysctl.conf` 中添加以下内容。

```none
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
net.ipv4.tcp_rmem=8192 262144 2147483647
net.ipv4.tcp_wmem=4096 16384 2147483647
net.ipv4.tcp_adv_win_scale=-2
```

### apt 自动更新

*暂时不进行此修改。*

```bash
sudo nano /etc/apt/apt.conf.d/20auto-upgrades
```

```bash
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
###
Unattended-Upgrade::Remove-Unused-Dependencies "true"; 
Unattended-Upgrade::Automatic-Reboot "true"; 
Unattended-Upgrade::Automatic-Reboot-WithUsers "false"; 
Unattended-Upgrade::Automatic-Reboot-Time "20:00"; 
```

### CPU、内存热插拔

在 Options 中开启 CPU 和内存热插拔，然后在 Hardware 中 CPU Sockets 和 Cores 设置为与 Host 相同并开启 NUMA，用 vCPU 设置控制核数。

#### CPU 热插拔

文件 `/lib/udev/rules.d/80-hotplug-cpu.rules`

```none
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}="1"
```

内存热插似乎不需要特别的设置。

在文件 `/etc/default/grub` 中添加内核参数 `memhp_default_state=online movable_node` 后内存热拔也会失败，似乎是默认设置下 Linux 不会把内存移动到其他内存块中，所以热拔失败，正在调查解决方法。

<https://pve.proxmox.com/wiki/Hotplug_(qemu_disk,nic,cpu,memory)>

<https://forum.proxmox.com/threads/cpu-hotplug-not-working.59337/>

<https://docs.kernel.org/admin-guide/mm/memory-hotplug.html#phases-of-memory-hotunplug>

## 创建实例后在 PVE 进行的操作

### 克隆模板后：配置

* CPU 大小
* 内存大小
* 硬盘大小
* Cloud-init
  * 用户名和密码
  * 公钥
  * IP 地址
  * DNS
* 定期备份

### 开机前：创建备份

方便还原回初始状态，无需再次配置

## 开机后在实例内进行的操作

### ~~Mirrors~~

直接修改映像中的地址可能会被 cloud-init 重置，所以需要手动修改

<https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/>

### zram (X)

由于 host 也有 zram，guest 中不宜设置 zram，以免影响 host 内存压缩效果。

当环境适合开始 zram 时，使用配置文件更改 zram 大小和压缩算法。

此外，在 `/etc/sysctl.conf` 中增加交换优先级，如果使用的不是 zstd 压缩算法，可以适当减小（如减小到 90）

```none
vm.swappiness=180
```

### ~~guest-agent~~

```bash
sudo apt install qemu-guest-agent
sudo systemctl enable --now qemu-guest-agent
```

### DDNS


1. CF 添加 NS
2. HE 添加 DDNS
3. 配置script
4. 配置crontab

```bash
#!/bin/sh
curl -4 "https://dyndlc.b11p.com:x@dyn.dns.he.net/nic/update?hostname=dyndlc.b11p.com"
curl -6 "https://dyndlc.b11p.com:x@dyn.dns.he.net/nic/update?hostname=dyndlc.b11p.com"
```

```none
*/10 * * * * ~/he_ddns.sh
```

### tailscale

<https://tailscale.com/download/linux>

然后更改端口（`/etc/default/tailscaled`）

### Docker & IPv6 NAT

```bash
export DOWNLOAD_URL="https://mirrors.tuna.tsinghua.edu.cn/docker-ce"
# 如您使用 curl
curl -fsSL https://get.docker.com/ | sudo -Esh
# 如您使用 wget
wget -O- https://get.docker.com/ | sudo -Esh
```

```bash
sudo nano /etc/docker/daemon.json
```

```json
{
    "ipv6": true,
    "fixed-cidr-v6": "fdxx:xxxx:xxxx::/64",
    "experimental": true,
    "ip6tables": true
}
```

```bash
sudo usermod -aG docker $USER
docker network create --ipv6 --subnet fdxx:xxxx:xxxx:1::/64 backend
```

### acme.sh

```bash
curl https://get.acme.sh | sh -s email=bleatingsheep@live.cn
export CF_Token="xx"
export CF_Zone_ID=d84a7f1dc670026115edd51d40b9a43b
acme.sh --set-default-ca --server letsencrypt
acme.sh --issue --dns dns_cf -k ec-256 -d xx.b11p.com
mkdir ~/cert
acme.sh --install-cert --ecc -d xx.b11p.com --fullchain-file ~/cert/xx.b11p.com.crt --key-file ~/cert/xx.b11p.com.key
```

## 故障排除

### sshd 服务没有启动

Ubuntu 容器可能出现此问题，解决方法是重新安装 openssh-server。

```bash
sudo apt-get purge openssh-server
sudo apt-get install openssh-server
```

---

**Documents**

- [vendor-data Ubuntu](https://notes.bleatingsheep.org/s/7eab7555-2e23-44e2-820d-3dc72427ab3e/doc/vendor-data-ubuntu-n1lvpuJYhT)
- [vendor-data Fedora](https://notes.bleatingsheep.org/s/7eab7555-2e23-44e2-820d-3dc72427ab3e/doc/vendor-data-fedora-7NyMpeG5SO)