找回密码
 加入我们
搜索
      
查看: 620|回复: 7

[网络] 以ucg-fiber为例,在unifios上使用容器安装openwrt,硬路由的性能+软路由的多功能

[复制链接]
发表于 2025-7-4 14:41 | 显示全部楼层 |阅读模式
本帖最后由 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发行版,都能使用这个容器

  1. nspawn-container 通常指的是使用 systemd-nspawn 工具创建的轻量级容器。systemd-nspawn 是 systemd 项目的一部分,用于快速创建和管理基于 Linux 命名空间(namespace)的容器,提供进程、文件系统和网络的隔离,类似于轻量级虚拟机或 Docker 容器。

  2. 关键特性
  3. 基于命名空间的隔离:

  4. 使用 Linux 内核的命名空间(PID、网络、挂载、UTS 等)实现隔离。

  5. 比传统虚拟机更轻量(无需虚拟化硬件)。

  6. 集成 systemd:

  7. 容器内可运行完整的 systemd 初始化系统(可选)。

  8. 支持将容器作为 systemd 单元(systemctl start/stop 管理)。

  9. 镜像兼容性:

  10. 可直接从目录、磁盘镜像或压缩包(如 .tar.xz)启动。

  11. 支持主流 Linux 发行版(Debian、Arch、Fedora 等)的根文件系统。

  12. 网络配置:

  13. 支持多种模式:主机网络、虚拟以太网对(veth)、桥接网络等。
复制代码


配置步骤
1. 安装组件
在unifios的web界面开启ssh,ssh登录unifios(账号root/密码为web界面登录密码)。
  1. apt -y install systemd-container
复制代码


2.创建容器
推荐存储目录问/data、/opt。假如安装了ssd,ssd会自动格式化为ext4并挂载,挂载目录为/volume1。ucg-fiber自身的剩余存储空间大概为6G左右,有条件的话就使用ssd,轻量使用之用ucg-fiber的emmc也可以。这里就以ssd为例

  1. mkdir -p /volume1/machines/openwrt
复制代码


然后到https://images.linuxcontainers.org/下载openwrt的arm64镜像包,"rootfs.tar.xz",大概3m左右。连不上话找找国内各大学的镜像源。
然后解压到上面创建的目录
  1. tar -xf rootfs.tar.xz -C /volume1/machines/openwrt
复制代码


3.配置容器
为了方便容器启动,创建容器软链接
  1. mkdir -p /var/lib/machines
  2. ln -s /volume1/machines/openwrt /var/lib/machines/
复制代码


网络接口推荐使用macvlan。试过veth,和ucg-fiber的offload有冲突,在容器内会掉包,速度会很慢。使用macvaln会有一个小问题,host和容器会互相访问不了,这是macvlan本身的特性决定的,不过可以通过其他手段解决。

配置网络脚本
  1. #!/bin/bash
  2. # This script will create a macvlan bridge interface to allow communication
  3. # between container networks and host networks.
  4. # An interface called brX.mac will be created, where X = $VLAN configured below.
  5. # The interface will be assigned an IP of $IPV4_GW, and $IPV6_GW configured below.
  6. # Routes will be added for the container IP $IPV4 and $IPV6.
  7. # Script is based on 10-dns.sh from unifios-utilities.

  8. ## CONFIGURATION VARIABLES

  9. # VLAN ID network container will be on. This VLAN has to first be configured as a
  10. # network in Unifi Network settings with a unique IP/subnet. Do not use the same
  11. # IP in the unifi network settings as you will use below for IPV4_IP or IPV4_GW.
  12. VLAN=0

  13. # IP addresses of container.
  14. IPV4_IP="192.168.1.253"
  15. # Gateway IP address of macvlan interface. IP above should be in this subnet.
  16. IPV4_GW="192.168.1.254/24" #这个地址不要和ucg-fiber本身的地址起冲突,可以使用同网段的其他空闲地址。

  17. # Set this to the interface(s) on which you want DNS TCP/UDP port 53 traffic
  18. # re-routed through this container. Separate interfaces with spaces.
  19. # This is useful when runinng a DNS service, like Adguard Home
  20. # e.g. "br0" or "br0 br1" etc.
  21. FORCED_INTFC=""

  22. ## END OF CONFIGURATION

  23. # set VLAN bridge promiscuous
  24. ip link set "br${VLAN}" promisc on

  25. # create macvlan bridge and add IPv4 IP
  26. ip link add "br${VLAN}.mac" link "br${VLAN}" type macvlan mode bridge
  27. ip addr add "${IPV4_GW}" dev "br${VLAN}.mac" noprefixroute

  28. # set macvlan bridge promiscuous and bring it up
  29. ip link set "br${VLAN}.mac" promisc on
  30. ip link set "br${VLAN}.mac" up

  31. # add IPv4 route to container
  32. ip route add "${IPV4_IP}/32" dev "br${VLAN}.mac"

  33. #启动容器,注意容器名匹配。
  34. 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里创建脚本目录
  1. mkdir -p /volume1/on_boot.d
复制代码

将上面的网络脚本放到这个目录里,命名为10-setup-network.sh

创建一个systemd service,/etc/systemd/system/fiber-boot.service
  1. [Unit]
  2. Description=Run On Startup unifios 3.x and above
  3. Wants=network-online.target
  4. After=network-online.target
  5. StartLimitIntervalSec=500
  6. StartLimitBurst=1

  7. [Service]
  8. Type=oneshot
  9. 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'\'
  10. RemainAfterExit=true

  11. [Install]
  12. WantedBy=multi-user.target
复制代码


创建好之后执行
  1. systemctl enable fiber-boot.service
复制代码

这个systemd service会自动搜索/volume1/on_boot.d下的sh脚本,并执行。

将配置好的网络添加到容器。
  1. mkdir -p /etc/systemd/nspawn
  2. vim /etc/systemd/nspawn/openwrt.nspawn
复制代码

  1. [Exec]
  2. Boot=on
  3. ResolvConf=off

  4. [Network]
  5. 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,要对应一致。

设置好之后,
  1. 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会被删除重置,要重新安装容器组件。用一个小脚本解决。
  1. mkdir -p /volume1/dpkg && cd /volume1/dpkg
  2. apt download systemd-container libnss-mymachines debootstrap arch-test
复制代码

  1. vim /volume1/on_boot.d/0-setup-system.sh
复制代码

  1. #!/bin/bash
  2. # This script installs systemd-container if it's not installed.
  3. # Also links any containers from /volume1/machines to /var/lib/machines.

  4. set -e

  5. if ! dpkg -l systemd-container | grep ii >/dev/null; then
  6.     if ! apt -y install systemd-container debootstrap; then
  7.         yes | dpkg -i /volume1/dpkg/*.deb
  8.     fi
  9. fi

  10. mkdir -p /var/lib/machines
  11. for machine in $(ls /volume1/machines/); do
  12.     if [ ! -e "/var/lib/machines/$machine" ]; then
  13.         ln -s "/volume1/machines/$machine" "/var/lib/machines/"
  14.         machinectl enable $machine
  15.         machinectl start $machine
  16.     fi
  17. done
复制代码


PS on_boot.d下的所有脚本都要用执行权限!!
 楼主| 发表于 2025-7-4 14:57 | 显示全部楼层
macvlan的另外一种接法,能够与主机通讯
在web界面创建一个vlan2
  1. brctl delif br2 eth5.2 #可以是任何一个lan口的vlan接口
  2. ip addr del 192.168.100.1/24 dev br2 #ip 为web界面创建vlan时填写的网关ip
  3. ip link add mv-vlan2 link eth5.2 type macvlan mode bridge
  4. ip addr add 192.168.100.1/24 dev mv-vlan2
  5. ip link set mv-vlan2 up
  6. ip link add mv-mosdns link eth5.2 type macvlan mode bridge
复制代码
发表于 2025-7-4 14:58 | 显示全部楼层
docker、docker 镜像、dcoker 容器 这是三个概念

所以,docker 是容器 也是不对的
 楼主| 发表于 2025-7-4 15:07 | 显示全部楼层
qhdxy 发表于 2025-7-4 14:58
docker、docker 镜像、dcoker 容器 这是三个概念

所以,docker 是容器 也是不对的 ...

嗯,你说的对。
发表于 2025-7-4 16:03 | 显示全部楼层
楼主太牛逼了!这绝对是硬核贴!
发表于 2025-7-5 10:08 来自手机 | 显示全部楼层
收藏
老铁如果能研究一下ubnt的古董xg server的系统
再上面安装最新的ubnt软件,
xg server扩展一下设备卡
系统更新,
那就秒杀一切ubnt设备去
 楼主| 发表于 2025-7-5 11:39 | 显示全部楼层
sanna 发表于 2025-7-5 10:08
收藏
老铁如果能研究一下ubnt的古董xg server的系统
再上面安装最新的ubnt软件,

作为穷屌丝,玩不起这么高贵的机器呃。网上稍微看了一下,x86的机器,能够使用dpkg命令安装包,不负责任的猜一下,系统应该也是debian系的,只要能连ssh,就和debian一样玩就行了。
发表于 2025-7-5 14:07 来自手机 | 显示全部楼层
waio0 发表于 2025-7-5 11:39
作为穷屌丝,玩不起这么高贵的机器呃。网上稍微看了一下,x86的机器,能够使用dpkg命令安装包,不负责任 ...

官方介绍

UniFi 应用服务器基于 Ubuntu 操作系统,可保障所有业务程序的长期稳定运行,内置 UniFi 和 UniFi 视频软件,可集中管理您的 UniFi 设备。
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-7-6 01:34 , Processed in 0.009285 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

快速回复 返回顶部 返回列表