以ucg-fiber为例,在unifios上使用容器安装openwrt,硬路由的性能+软路由的多功能
本帖最后由 waio0 于 2025-7-4 15:06 编辑前言
在ucg-fiber上,终于有了硬件offload,这就使得cpu有了很多空闲性能用来做其他的事情。所有使用unifios的机型其实都可以使用容器,但是没有offload的机型可能会影响性能。
这里只是以openwrt为例怎么使用容器,因为op有web界面,配置会简单方便很多,但是容器的权限不一定有,要自己设置,不保证所有功能都能用。假如熟悉Linux,或者已经有相关程序的配置文件,使用debian容器来跑程序更为稳妥。
所有的相关教程均来自gayhub,这里只是搬运。
容器的选择
docker是容器,容器不是docker!!!
unifios 3.x版本之后,原本可以使用的podman就不能用了,内核的相关配置都没有使能,那么docker也是不能用的。愿意修改编译内核的话,可以在gayhub上面找到相关教程。
查询ds之后,发现lxc的内核需求也是有一些不满足的,所以lxc也用不了,那么理论上incus也是用不了的。愿意修改编译内核的话,可以在gayhub上面找到相关教程。
不过还有一个比较小众的容器是能用的——nspawn-container。只要是使用systemd的linux发行版,都能使用这个容器
nspawn-container 通常指的是使用 systemd-nspawn 工具创建的轻量级容器。systemd-nspawn 是 systemd 项目的一部分,用于快速创建和管理基于 Linux 命名空间(namespace)的容器,提供进程、文件系统和网络的隔离,类似于轻量级虚拟机或 Docker 容器。
关键特性
基于命名空间的隔离:
使用 Linux 内核的命名空间(PID、网络、挂载、UTS 等)实现隔离。
比传统虚拟机更轻量(无需虚拟化硬件)。
集成 systemd:
容器内可运行完整的 systemd 初始化系统(可选)。
支持将容器作为 systemd 单元(systemctl start/stop 管理)。
镜像兼容性:
可直接从目录、磁盘镜像或压缩包(如 .tar.xz)启动。
支持主流 Linux 发行版(Debian、Arch、Fedora 等)的根文件系统。
网络配置:
支持多种模式:主机网络、虚拟以太网对(veth)、桥接网络等。
配置步骤
1. 安装组件
在unifios的web界面开启ssh,ssh登录unifios(账号root/密码为web界面登录密码)。
apt -y install systemd-container
2.创建容器
推荐存储目录问/data、/opt。假如安装了ssd,ssd会自动格式化为ext4并挂载,挂载目录为/volume1。ucg-fiber自身的剩余存储空间大概为6G左右,有条件的话就使用ssd,轻量使用之用ucg-fiber的emmc也可以。这里就以ssd为例
mkdir -p /volume1/machines/openwrt
然后到https://images.linuxcontainers.org/下载openwrt的arm64镜像包,"rootfs.tar.xz",大概3m左右。连不上话找找国内各大学的镜像源。
然后解压到上面创建的目录
tar -xf rootfs.tar.xz -C /volume1/machines/openwrt
3.配置容器
为了方便容器启动,创建容器软链接
mkdir -p /var/lib/machines
ln -s /volume1/machines/openwrt /var/lib/machines/
网络接口推荐使用macvlan。试过veth,和ucg-fiber的offload有冲突,在容器内会掉包,速度会很慢。使用macvaln会有一个小问题,host和容器会互相访问不了,这是macvlan本身的特性决定的,不过可以通过其他手段解决。
配置网络脚本
#!/bin/bash
# This script will create a macvlan bridge interface to allow communication
# between container networks and host networks.
# An interface called brX.mac will be created, where X = $VLAN configured below.
# The interface will be assigned an IP of $IPV4_GW, and $IPV6_GW configured below.
# Routes will be added for the container IP $IPV4 and $IPV6.
# Script is based on 10-dns.sh from unifios-utilities.
## CONFIGURATION VARIABLES
# VLAN ID network container will be on. This VLAN has to first be configured as a
# network in Unifi Network settings with a unique IP/subnet. Do not use the same
# IP in the unifi network settings as you will use below for IPV4_IP or IPV4_GW.
VLAN=0
# IP addresses of container.
IPV4_IP="192.168.1.253"
# Gateway IP address of macvlan interface. IP above should be in this subnet.
IPV4_GW="192.168.1.254/24" #这个地址不要和ucg-fiber本身的地址起冲突,可以使用同网段的其他空闲地址。
# Set this to the interface(s) on which you want DNS TCP/UDP port 53 traffic
# re-routed through this container. Separate interfaces with spaces.
# This is useful when runinng a DNS service, like Adguard Home
# e.g. "br0" or "br0 br1" etc.
FORCED_INTFC=""
## END OF CONFIGURATION
# set VLAN bridge promiscuous
ip link set "br${VLAN}" promisc on
# create macvlan bridge and add IPv4 IP
ip link add "br${VLAN}.mac" link "br${VLAN}" type macvlan mode bridge
ip addr add "${IPV4_GW}" dev "br${VLAN}.mac" noprefixroute
# set macvlan bridge promiscuous and bring it up
ip link set "br${VLAN}.mac" promisc on
ip link set "br${VLAN}.mac" up
# add IPv4 route to container
ip route add "${IPV4_IP}/32" dev "br${VLAN}.mac"
#启动容器,注意容器名匹配。
machinectl start openwrt
这个脚本需要注意的一点,假如是使用ucg-fiber默认的网络,VLAN填0;假如是使用其他vlan网络,VLAN就填创建的vlan数字。ucg-fiber的默认网络vlan是1,但是自动创建的br是br0;其他vlan网络,使用的vlan是什么,创建的br就是br$vlan。例如在web界面创建了一个vlan号是2,那么自动创建的br就是br2。只有默认的vlan1是br0。
使能开机自动配置容器网络,可以使用systemd,也可以使用脚本。这里以systemd+脚本为例。
在/volume1里创建脚本目录
mkdir -p /volume1/on_boot.d
将上面的网络脚本放到这个目录里,命名为10-setup-network.sh
创建一个systemd service,/etc/systemd/system/fiber-boot.service
Description=Run On Startup unifios 3.x and above
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=1
Type=oneshot
ExecStart=bash -c 'mkdir -p /volume1/on_boot.d && find -L /volume1/on_boot.d -mindepth 1 -maxdepth 1 -type f -print0 | sort -z | xargs -0 -r -n 1 -- sh -c '\''if test -x "$0"; then echo "%n: running $0"; "$0"; else case "$0" in *.sh) echo "%n: sourcing $0"; . "$0";; *) echo "%n: ignoring $0";; esac; fi'\'
RemainAfterExit=true
WantedBy=multi-user.target
创建好之后执行systemctl enable fiber-boot.service
这个systemd service会自动搜索/volume1/on_boot.d下的sh脚本,并执行。
将配置好的网络添加到容器。
mkdir -p /etc/systemd/nspawn
vim /etc/systemd/nspawn/openwrt.nspawn
Boot=on
ResolvConf=off
MACVLAN=br0
配置容器openwrt静态地址。
/volume1/machines/openwrt/etc/config/network
lan的iface填写mv-br0,这是systemd container的规则,mv代表macvlan,brX就填写对应的设置。本例中使用br0,假如是使用其他vlan的就使用对用的vlan号。
静态地址填写10-setup-network.sh中的IPV4_IP,网关使用10-setup-network.sh中的IPV4_GW,要对应一致。
设置好之后,
systemctl start fiber-boot.service
然后就可以使用浏览器打开openwrt界面了。
没检测openwrt需要开启哪些权限,可能使用上会有问题,这个需要自行解决。
我自己使用只安装了debian 12的rootfs,在debian中安装mosdns,traefik,postgresql少数几个端口号和unifios起冲突的。端口不冲突的二进制文件可以直接在unifios上运行,毕竟unifios也是debian。
systemd container也是可以跑docker镜像的,有点类似于routeos运行容器一样,需要把镜像包下载解压到一个目录,然后使用systemd-nspawn命令来启动。
最后一步
unifios固件升级之后,/var和/usr会被删除重置,要重新安装容器组件。用一个小脚本解决。
mkdir -p /volume1/dpkg && cd /volume1/dpkg
apt download systemd-container libnss-mymachines debootstrap arch-test
vim /volume1/on_boot.d/0-setup-system.sh
#!/bin/bash
# This script installs systemd-container if it's not installed.
# Also links any containers from /volume1/machines to /var/lib/machines.
set -e
if ! dpkg -l systemd-container | grep ii >/dev/null; then
if ! apt -y install systemd-container debootstrap; then
yes | dpkg -i /volume1/dpkg/*.deb
fi
fi
mkdir -p /var/lib/machines
for machine in $(ls /volume1/machines/); do
if [ ! -e "/var/lib/machines/$machine" ]; then
ln -s "/volume1/machines/$machine" "/var/lib/machines/"
machinectl enable $machine
machinectl start $machine
fi
done
PS on_boot.d下的所有脚本都要用执行权限!! macvlan的另外一种接法,能够与主机通讯
在web界面创建一个vlan2
brctl delif br2 eth5.2 #可以是任何一个lan口的vlan接口
ip addr del 192.168.100.1/24 dev br2 #ip 为web界面创建vlan时填写的网关ip
ip link add mv-vlan2 link eth5.2 type macvlan mode bridge
ip addr add 192.168.100.1/24 dev mv-vlan2
ip link set mv-vlan2 up
ip link add mv-mosdns link eth5.2 type macvlan mode bridge docker、docker 镜像、dcoker 容器 这是三个概念
所以,docker 是容器 也是不对的 qhdxy 发表于 2025-7-4 14:58
docker、docker 镜像、dcoker 容器 这是三个概念
所以,docker 是容器 也是不对的 ...
嗯,你说的对。 楼主太牛逼了!这绝对是硬核贴! 收藏
老铁如果能研究一下ubnt的古董xg server的系统
再上面安装最新的ubnt软件,
xg server扩展一下设备卡
系统更新,
那就秒杀一切ubnt设备去 sanna 发表于 2025-7-5 10:08
收藏
老铁如果能研究一下ubnt的古董xg server的系统
再上面安装最新的ubnt软件,
作为穷屌丝,玩不起这么高贵的机器呃[流泪]。网上稍微看了一下,x86的机器,能够使用dpkg命令安装包,不负责任的猜一下,系统应该也是debian系的,只要能连ssh,就和debian一样玩就行了。 waio0 发表于 2025-7-5 11:39
作为穷屌丝,玩不起这么高贵的机器呃。网上稍微看了一下,x86的机器,能够使用dpkg命令安装包,不负责任 ...
官方介绍
UniFi 应用服务器基于 Ubuntu 操作系统,可保障所有业务程序的长期稳定运行,内置 UniFi 和 UniFi 视频软件,可集中管理您的 UniFi 设备。 楼主,问个问题,ucg-fiber用PPPOE上网,支持fullcone/NAT1不?
页:
[1]