一波未平,一波又起
在pve直通失败后,一周内我就搜索到lxc在宿主机显示影像的方法(https://blog.simos.info/discussion-on-running-x11-applications-from-within-lxd-system-containers/ , https://blog.simos.info/running-x11-software-in-lxd-containers/ ),但是信息非常零散。但是管它呢,我已经一周没看视频了,没有好看视频的滋润,我都要枯竭了,于是就装了Debian又装了lxd。
我一会说lxc,一会说lxd,是不是很奇怪。没错,我一开始也不习惯。lxc应该是linux container的简称吧,我不知道,不过应该是吧,外国人肯定会把linux container缩写成lxc。那我为什么在下文又频繁的使用“lxc容器”这词汇,原因很简单,在官网他们就是这样写的 (https://linuxcontainers.org/lxc/introduction/ )。而lxd是linux container和virtual machine的管理器。
整个过程不是非常顺利。lxd在Debian有2种安装方法,一是用apt装,或是用snap装。我是跟着 https://documentation.ubuntu.com/lxd/en/latest/installing/ 这用snap装的。装完lxd然后在initialize的时候,用lxd init,提示找不到lxd命令,明明装了却提示没有,只好发动浏览器搜索,发现snapd的路径没加进去,额,这不是应该在安装过程中自动加进去的吗。加好路径,initialize完后,新建个lxc容器,然后这容器却连不上网,你们知道为什么吗,因为我还在宿主机上装了docker,然后docker和lxd的网络冲突了。好好好,删了docker继续,大不了我在lxc容器里装docker。新建个lxc容器,装上docker,然后新建docker容器时,又提醒我没有足够权限,不让我启动。搜了下,要在lxc容器中使用docker,需要给lxc容器nest container的权限。给完权限,启动lxc容器,有网络了,接着在浏览器登录下portainer设置其它docker容器,总之先看下这lxc容器的ip,啥,这ip咋是10.x.x.x网段的,我的lan的网段应该是192.168.x.x才对。然后我又去搜索,要在局域网内访问lxc容器的方法有(https://blog.simos.info/how-to-make-your-lxd-containers-get-ip-addresses-from-your-lan-using-a-bridge/ , https://linuxcontainers.org/incus/docs/main/reference/devices_proxy/ ) :ipvlan, macvlan, bridged, routed和proxy,哈,和docker好像,又不大一样。接下来又花了几天的,又看了一堆英语的说明文,看的我都要吐了,这些程序员就不能把文档写的让一般人士更容易明白的吗,就不能据几个完整的例子吗。算了,鉴于我的小主机不会经常移动,采用bridged方案。在看完( https://wiki.debian.org/BridgeNetworkConnections )后,我依然不明白怎么搞,然后又去看了(https://docs.openvswitch.org/en/latest/tutorials/ ),我脑子更像浆糊了。明白了看官方文档是看不懂的情况下,又看了一堆散文才勉强明白。搞完网络后,又去访问lxd的webui,这又是什么鬼啊。
前面不是说是用lxd吗,为什么这里变成了incus?因为在去年在canonical(Ubuntu母公司)的指导下lxd脱离了lxc,然后又继续作死:
https://discuss.linuxcontainers.org/t/important-notice-for-lxd-users-image-server/18479
incus和pve对比,好处就是可以更方便的管理Linux container, 更方便创建unprivileged container,支持多个Linux distribution。
坏处就是官方文档比traefik还操蛋,webui真是半成品,基本等于是从lxd那搬来的,而且没有优化过,而且还是用client side certificate authentication,然后自带的网络是nat,这些等下就能明白。
我演示下从头开始安装的过程,我的OS是Debian 12,没有安装GUI。

新装的系统得更新,把源换成国内的会快很多。
想要转码就必需要non-free。
https://wiki.debian.org/HardwareVideoAcceleration
“For Intel, it's split generationally, and into free and non-free drivers. The non-free drivers are necessary to encode media while the free drivers can only decode. ”
sudo su
mv /etc/apt/sources.list /etc/apt/sources.list.old
echo -e "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmwarendeb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmwarendeb https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware" > /etc/apt/sources.list
apt update
apt full-upgrade -y
因为我装debian时用的语言是中文,为了正确显示中文,得装字体。
apt install fonts-noto-cjk fonts-noto-cjk-extra -y
装解码和转码的驱动。
apt install intel-media-va-driver-non-free -y
如果是8代以下不含8代的,装这个:
apt install i965-va-driver-shaders -y
举个例子,j4125应该是Gemini Lake ,9.5代的。
https://trac.ffmpeg.org/wiki/Hardware/QuickSync
没错,现在开始才要安装incus。没错,我们还是继续用root权限。
https://linuxcontainers.org/incus/docs/main/installing/
apt install curl gpg btrfs-progs -y
curl -fsSL https://pkgs.zabbly.com/key.asc | gpg --show-keys --fingerprint
mkdir -p /etc/apt/keyrings/
curl -fsSL https://pkgs.zabbly.com/key.asc -o /etc/apt/keyrings/zabbly.asc
sh -c 'cat < /etc/apt/sources.list.d/zabbly-incus-stable.sources
Enabled: yes
Types: deb
URIs: https://pkgs.zabbly.com/incus/stable
Suites: $(. /etc/os-release && echo ${VERSION_CODENAME})
Components: main
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/zabbly.asc
EOF'
apt update
apt install incus incus-ui-canonical -y
终于安装完了,但是还没结束。可以把用户加进incus-admin组里,以后就不用天天用sudo了。
adduser 用户名 incus-admin
因为我觉得sudo用户用gui会有安全风险,现在建个没有sudo权限,但是可以使用gui的用户,什么名字都行,我就随便取个guiuser 。
useradd -m -G input,audio,video,incus-admin -s /bin/bash -u 1100 guiuser
passwd guiuser
我不知道init这过程怎么解释,不过按我做的干就行了。要是没看见在:后我输了什么,就表示我按的是回车。
incus admin init
Would you like to use clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Name of the storage backend to use (btrfs, dir) [default=btrfs]:
Create a new BTRFS pool? (yes/no) [default=yes]:
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:
Size in GiB of the new loop device (1GiB minimum) [default=5GiB]: 50
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=incusbr0]:
What IPv4 address should be used? (CIDR subnet notation, ¡°auto¡± or ¡°none¡±) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, ¡°auto¡± or ¡°none¡±) [default=auto]:
Would you like the server to be available over the network? (yes/no) [default=no]: yes
Address to bind to (not including port) [default=all]:
Port to bind to [default=8443]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]:
Would you like a YAML "init" preseed to be printed? (yes/no) [default=no]:
好了,现在注销,然后重新登录,总之就是不停exit就是了。
登录后加lxc镜像的国内源。
incus remote add tuna https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public
先建个叫fristContainer的容器玩玩。
incus launch tuna:debian/12 fristContainer
首先在宿主机上输这条命令看ip:
ip a
然后再在宿主机上输这条命令看容器的ip:
incus exec fristContainer -- ip a
对比这2个ip,就会明白,它们不在同个网段。你lan的设备在父网段,容器在子网段,所以除这台宿主机外的设备ping不到容器是非常正常的,现在能ping到就有假。

这不像docker一样给你port mapping,然后你lan的设备就能访问到docker container。其实还是可以的,我们可以用proxy,不过要是这么干,管理防火墙是很辛苦的,看完这网页就能明白:
https://linuxcontainers.org/incus/docs/main/reference/devices_proxy/
所以要学proxmox,用bridge给容器分配lan的ip。在Linux,bridge有2个应用可以选bridge-utils和openvswich-switch。bridge-utils比较简单,proxmox默认就是用这个。openvswich-switch很复杂,但是性能还高,据说cdn都用这个。
ovs bridge性能的确很高,我用手机Wi-Fi测速可以跑到900多兆。

我不想再看ovs的文档,所以你们看我做的就是了。重点来了,如果你只有无线网卡,请不要用bridge,因为WPA验证会拒绝1个NIC有多个ip。
https://wiki.debian.org/BridgeNetworkConnections#Bridging_with_a_wireless_NIC
用无线网卡的可以用这方法,像docker一样port mapping。我能帮到的只有这些了。
incus config device add containerName proxyName proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80
顺便说下,要是只有1个网口,在设置时肯定会断网,设置完了才有网络。
sudo su
apt install openvswitch-switch -y
首先得看网口名字。
ip a
你会看到有个网口会跟着类似这个的“inet 192.168.1.3/24 brd 192.168.1.255 scope global”的内容,这就是用来上网的网口。要是你有多个物理网口,就用另一个来当bridge。没有的话,就用这个吧,反正可以跑900多兆,差不多算千兆了。要是真看不懂,那得好好学完linux基础再继续。
创建个叫ovsbr0的bridge:
ovs-vsctl add-br ovsbr0
ovs-vsctl show
然后这个bridge真的需要物理网口才能和外界沟通,所以得把bridge和物理网口接起来。要是你有多个物理网口,上网那网口不要加进bridge,最好是另外用一个。不过要是只有一个,那就加进去吧,加进去那瞬间,这电脑就会断网。这里假设加进去的网口叫eno1。
ovs-vsctl add-port ovsbr0 eno1
多网口的只要把bridge和物理网口接起来就行了,要想知道怎么让lxc容器获得从路由器那分配的ip,得等我教完单网口先。
单网口的继续看。这里我们创建个叫host的虚拟网口给宿主机,而且还有加进刚刚建的bridge里。放心,现在还是没有网络。注意,这里2条命令和在了一起,你要是想给虚拟网口取新名字,得把2个host都改了。
ovs-vsctl add-port ovsbr0 host -- set Interface host type=internal
在没安装gui的情况下,debian不会自动设置网络,所以得手动。
nano /etc/network/interfaces
allow-hotplug eno1
iface eno1 inet manual
auto host
allow-hotplug host
iface host inet dhcp
#如果路由器是用dhcp6分配ipv6的话,这样写:iface host inet6 dhcp
#如果路由器是用 SLAAC 分配ipv6的话,像下面一样写
iface host inet6 auto
然后我好像重启network service后,eno1还残留着ip,bridge不需要ip,先关了再开,再重启network service就好了。锵锵,网络连上了。
ip link set eno1 down
ip link set eno1 up
service networking restart
现在不论多网口,还是单网口的都好了,现在给lxc容器接上bridge。
为了方便管理和重复使用,我们用profile,创建个叫ovs的profile。
incus profile copy default ovs
incus profile edit ovs
没错,只要把跳出来的界面中的东西清空,再把这原原本本黏贴上去就行了,完全不需要更改。因为distrobuilder创建的容器都会用eth0当网口,我们只需要覆盖旧的就行了。
config: {}
description: Let containers get ip from router
devices:
eth0:
name: eth0
nictype: bridged
parent: ovsbr0
type: nic
name: ovs
这里示范下把ovs profile加进前面新建的 fristContainer里。
incus profile add fristContainer ovs
现在fristContainer没有网络,得重启。
incus restart fristContainer
然后看看fristContainer的ip。
incus exec fristContainer -- ip a
现在就能看到容器的ip是和你其它设备一样从路由器那获得的了。
要是想从创建容器那时就把ovs profile加进去的话,顺序很重要,ovs必须要在default 后面,而且必须要有default,除非你加个带储存的profile:
incus launch tuna:debian/12 secondContainer -p default -p ovs
你要是想让一个容器多个接口,那新建个profile,把eth0改成其它的,再加进容器,然后像上面单网口设置网络一样搞就行了。
现在来说奇葩的webui。
首先在浏览器输入https://192.168.1.3:8443,把 192.168.1.3换成自己的。然后一进去就会提醒是典型的自签证书的提醒,同意就是了,然后又跳出来提醒证书相关的,接着同意。
然后按create a new certificate。

再按generate。

如果你用imac的话,就必需设密码。

然后把crt和pfx都载下来。

接着把crt复制到宿主机里,不管用什么方式。假设这crt 叫incus-ui.crt,存在于你的home目录里,接着在宿主机里运行这个:
incus config trust add-certificate $HOME/incus-ui.crt
然后在你访问这网页的电脑,假设你用火狐,跟着import进行。去about:preferences#privacy的证书那。

点“查看证书”,点“您的证书”,点“导入”,导入的是pfx,然后确定。关掉浏览器,然后再开浏览器,进https://192.168.1.3:8443,然后选择刚刚导入的证书,你就成功了。是不是非常麻烦。

作者声明本文无利益相关,欢迎值友理性交流,和谐讨论~

Hi喵帕斯
校验提示文案
xianba
校验提示文案
随风飘的空气
校验提示文案
Hi喵帕斯
校验提示文案
随风飘的空气
校验提示文案
xianba
校验提示文案