安装与初始化 Incus 环境

1. 安装存储后端

安装 BTRFS 和 LVM 的工具包,确保 Incus 初始化时能检测并启用高级存储后端(如 BTRFS),从而支持高效的快照、克隆和卷管理功能,避免回退到基础的 dir 模式。

sudo apt update
sudo apt install btrfs-progs lvm2

安装完成后,可通过 modprobe btrfsvgchange -ay 加载模块验证支持。

2. 使用 apt 安装 Incus

在 Ubuntu 24.04 系统中,可通过 apt 快速安装 Incus:

sudo apt install incus

安装完成后,验证 Incus 版本。本文测试环境使用 Incus 6.0.0 LTS:

sudo incus version

3. 默认配置初始化

运行以下命令以交互方式完成 Incus 默认初始化,所有提示可直接按 Enter 接受默认选项。

注意:在使用 WSL2 镜像网络模式(Mirrored Networking)时,初始化可能失败。它将 Windows 主机网络适配器的 DNS/DHCP 端口(UDP 53/67/68)镜像到 WSL 实例中,导致本地 dnsmasq 无法绑定这些端口,即使本地服务已暂停——镜像的“幽灵”绑定会报告 “Address already in use”。

sudo incus admin init

初始化过程会自动创建:

  • 基于 BTRFS 的存储池(默认名称 default);
  • 本地网络桥接接口 incusbr0(自动分配 IPv4/IPv6 地址,支持 NAT);
  • 基础 Incus 服务配置,包括自动图像更新。

该过程无需额外硬件或磁盘分区,非常适合快速搭建测试或开发环境。初始化完成后,即可创建系统容器或虚拟机。

序号问题文本默认值含义与影响
1Would you like to use clustering? (yes/no) [default=no]no是否启用 Incus 集群(多节点高可用)。默认单节点即可,支持后期扩展。
2Do you want to configure a new storage pool? (yes/no) [default=yes]yes是否创建存储池,Incus 依赖至少一个存储池,建议保留默认。
3Name of the new storage pool [default=default]default存储池名称,默认名便于管理。
4Name of the storage backend to use (btrfs, dir, lvm) [default=btrfs]btrfs存储后端类型选择。btrfs 支持高效快照和克隆,适合大多数场景;dir 简单但功能有限;lvm 适合现有 LVM 环境。选择影响存储性能和功能,后续可更改。
5Create a new BTRFS pool? (yes/no) [default=yes]yes是否创建新的 BTRFS 存储池(仅当选择 btrfs 时出现)。默认创建新池,便于快速启动;no 可用于现有 BTRFS 子卷。
6Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]no是否使用现有空闲块设备(如磁盘或分区)作为 BTRFS 池源。默认 no,使用循环设备(loop device)模拟;yes 需要指定设备路径,提高性能但需手动管理硬件。
7Size in GiB of the new loop device (1GiB minimum) [default=30GiB]30GiB新循环设备的大小(仅当不使用现有设备时)。默认 30GiB 适合小型部署;可调整以匹配可用磁盘空间,过小会导致存储不足,后续扩展需手动。
8Would you like to create a new local network bridge? (yes/no) [default=yes]yes创建本地网络桥接 incusbr0,支持 NAT 和桥接主机网络。
9What should the new bridge be called? [default=incusbr0]incusbr0桥接接口名称,默认 incusbr0,便于识别。
10What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]autoIPv4 地址配置,auto 自动分配子网(如 10.0.4.1/24),none 禁用 DHCP/NAT。
11What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]autoIPv6 地址配置,auto 自动分配 ULA 子网(如 fd42:…),none 禁用。
12Would you like the server to be available over the network? (yes/no) [default=no]no是否允许远程访问 Incus API,默认本地仅用,支持安全 TLS 配置。
13Would you like stale cached images to be updated automatically? (yes/no) [default=yes]yes是否启用自动图像更新,保持镜像最新,减少手动维护。
14Would you like a YAML “init” preseed to be printed? (yes/no) [default=no]no是否输出 YAML 预种子文件,便于非交互重复初始化,默认否。

4. 修改存储池大小

Incus loopback 存储池支持 动态扩容(不支持缩小)

查看当前存储池状态:

incus storage info default

示例输出:

SIZE: 30.00GiB
USED: 0B

扩容存储池:

在 Incus 中,当你执行 incus storage set default size=100GiB 扩容 loop-backed(循环设备支持)的存储池(如 btrfs 或 lvm 后端)时,不会立即占用主机磁盘的全部 100GiB 空间。只有当你向存储池中写入数据(例如创建容器、上传镜像、存储文件)时,文件系统才会按需分配实际磁盘块。

sudo incus storage set default size=100GiB

验证修改:

incus storage info default

示例输出:

SIZE: 100.00GiB

运行系统容器

创建容器(示例容器名 u2404):

sudo incus launch images:ubuntu/24.04 u2404

进入容器:

sudo incus exec u2404 -- bash

启动容器:

sudo incus start u2404

停止容器:

sudo incus stop u2404

删除容器:

sudo incus delete u2404

查看所有容器:

sudo incus list

查看容器详细信息:

sudo incus info u2404

容器断网修复

1. 问题症状

Incus 容器(如 Ubuntu 24.04)内运行 apt update 失败,常见错误包括:

  • IPv6:connect (101: Network is unreachable)
  • IPv4:connection timed out(超时)。
  • 结果:仓库索引下载失败(Ign/Err/W),但使用旧缓存报告 “All packages are up to date”。

2. 根本原因

Ubuntu 24.04 默认 iptables FORWARD 链策略为 ACCEPT,但安装 Docker 后,其网络栈会自动设置为 DROP 以隔离容器。这阻断 Incus 桥接(incusbr0)出站流量转发,即使 NAT (MASQUERADE) 已配置(仅修改源地址)。

3. 临时修复

在主机(非容器内)运行:

sudo iptables -P FORWARD ACCEPT
sudo iptables -A FORWARD -i incusbr0 -j ACCEPT
sudo iptables -A FORWARD -o incusbr0 -j ACCEPT

立即验证:容器内 apt update,应成功下载索引。

4. 持久化配置

使用 netfilter-persistent 服务确保重启后规则生效:

  1. 安装:

    sudo apt update && sudo apt install netfilter-persistent iptables-persistent -y
    
  2. 保存规则:

    sudo netfilter-persistent save
    
  3. 启用服务:

    sudo systemctl enable netfilter-persistent
    sudo systemctl start netfilter-persistent
    
  • IPv6 支持:若需,添加 sudo ip6tables -P FORWARD ACCEPT 等规则后重新保存。
  • WSL 注意:规则在 WSL 重启后持久;Windows 重启需重新加载。

5. 验证与注意

  • 检查规则:sudo iptables -L FORWARD -v -n(Policy: ACCEPT,且有 incusbr0 规则)。
  • 容器测试:sudo incus exec u2404 -- apt update && apt upgrade -y(无错误)。
  • 安全:全局 ACCEPT 风险低(Incus 隔离),但可结合 UFW:sudo ufw allow in on incusbr0。若 UFW 启用,优先用其规则。
  • 回滚:sudo netfilter-persistent flush 清规则。

此修复兼容 Incus + Docker 共存,适用于生产/测试环境。

容器 SSH 连接

1. 进入容器

sudo incus exec u2404 -- bash

2. 系统更新

apt update
apt upgrade -y

3. 安装 OpenSSH 服务

apt install openssh-server -y

4. SSH 配置(覆盖 /etc/ssh/sshd_config

# 极简 SSH 配置,支持密码登录
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
UsePAM yes
PasswordAuthentication yes
KbdInteractiveAuthentication yes
PermitRootLogin prohibit-password
SyslogFacility AUTH
LogLevel INFO
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
Subsystem sftp /usr/lib/openssh/sftp-server
AcceptEnv LANG LC_*
PrintMotd no

重载 systemd 并启动 SSH:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

5. 创建新用户并赋予 sudo 权限

adduser username
usermod -aG sudo username
su - username
sudo whoami  # 输出应为 root

6. 查看容器 IP

ip a

一般在 incusbr0 网段,如:10.153.171.206

7. 主机 SSH 连接

ssh username@<container_ip>
# 示例:
ssh touken@10.153.171.206

容器端口映射

1. SSH 映射示例

sudo incus config device add u2404 sshproxy proxy listen=tcp:0.0.0.0:2222 connect=tcp:127.0.0.1:22

登录主机 2222 端口即可访问容器 SSH:

ssh touken@127.0.0.1 -p 2222

2. HTTP 映射示例

sudo incus config device add u2404 webproxy proxy listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:80

访问 Web 服务:

http://主机IP:8080

3. 查看已映射端口

sudo incus config device show u2404

示例输出:

sshproxy:
  connect: tcp:127.0.0.1:22
  listen: tcp:0.0.0.0:2222
  type: proxy

4. 删除端口映射

sudo incus config device remove u2404 sshproxy

宿主机目录挂载

1. 添加路径映射

sudo incus config device add <容器名> <设备名称> disk source=<主机路径> path=<容器路径> readonly=<true|false>

示例:主机 /home/touken/data → 容器 /mnt/data 可写

sudo incus config device add u2404 mydata disk source=/home/touken/data path=/mnt/data

2. 查看挂载路径

sudo incus config device show u2404

输出示例:

mydata:
  path: /mnt/data
  source: /home/touken/data
  type: disk

3. 删除路径映射

sudo incus config device remove u2404 mydata

容器内挂载点消失,但主机目录不受影响。

容器 GPU 透传

注意:宿主机必须已正确安装 NVIDIA 驱动,并且 /dev/nvidia\* 存在。


1. 给容器分配 全部 GPU

向容器添加全部可用 GPU:

sudo incus config device add u2404 allgpu gpu

检查容器内是否出现 /dev/nvidia*

sudo incus exec u2404 -- ls -l /dev | grep nvidia

示例输出:

crw-rw-rw- 1 root root 195, 254 Dec  3 04:55 nvidia-modeset
crw-rw-rw- 1 root root 510,   0 Dec  3 04:55 nvidia-uvm
crw-rw-rw- 1 root root 510,   1 Dec  3 04:55 nvidia-uvm-tools
crw-rw-rw- 1 root root 195,   0 Dec  3 04:55 nvidia0
crw-rw-rw- 1 root root 195,   1 Dec  3 04:55 nvidia1
crw-rw-rw- 1 root root 195,   2 Dec  3 04:55 nvidia2
crw-rw-rw- 1 root root 195,   3 Dec  3 04:55 nvidia3
crw-rw-rw- 1 root root 195,   4 Dec  3 04:55 nvidia4
crw-rw-rw- 1 root root 195,   5 Dec  3 04:55 nvidia5
crw-rw-rw- 1 root root 195,   6 Dec  3 04:55 nvidia6
crw-rw-rw- 1 root root 195,   7 Dec  3 04:55 nvidia7
crw-rw-rw- 1 root root 195, 255 Dec  3 04:55 nvidiactl

如需移除全部 GPU:

sudo incus config device remove u2404 allgpu

2. 按 PCI 地址 指定 GPU

首先查看 GPU 的 PCI 地址:

lspci | grep -i 'vga\|3d\|display'

示例输出:

03:00.0 VGA compatible controller: ASPEED Technology, Inc. ASPEED Graphics Family (rev 41)
1d:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
1e:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
1f:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
20:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
21:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
22:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
23:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
24:00.0 VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)

例如分配 PCI 地址为 1d:00.0 的 GPU:

sudo incus config device add u2404 gpu1 gpu pci=1d:00.0

添加多块 GPU:

sudo incus config device add u2404 gpu2 gpu pci=1e:00.0
sudo incus config device add u2404 gpu3 gpu pci=1f:00.0

移除某个 GPU:

sudo incus config device remove u2404 gpu1

3. 安装容器内部的 NVIDIA 用户态库

容器(Ubuntu 24.04)需要安装与宿主机驱动 主版本号一致nvidia-utils

在宿主机执行 nvidia-smi 可查看主版本号,例如 550/560/580

sudo incus exec u2404 -- bash -c "
apt update &&
apt install -y nvidia-utils-580
"

安装完成后测试:

sudo incus exec u2404 -- nvidia-smi

若 GPU 透传正确,现在应能看到正常输出。

更多推荐