N1盒子ubuntu下LXC容器实现openwrt

网络 · 2022-04-18 · 4049 人浏览
N1盒子ubuntu下LXC容器实现openwrt

之前在N1下armbian中用docker实现了openwrt,为什么要折腾LXC容器的openwrt:

  1. ipv6的支持问题,docker这点支持度不高,lxc能更好适应ipv6 only的vps和移动较低的ipv6丢包。
  2. LXC容器的隔离性以及挂载备份都更方便,无非config修订和文件夹打包。

操作在N1 ubuntu实现,理论debian一样,x86的机器一般直接利用PVE虚拟机里的LXC,不用以下手工,N1没有。另外方法也可以同时建立其他容器,方便调试,rootfs路径自由也可以避免N1 emmc空间小的问题。除了openwrt,我也测试lxc了ubuntu和debian:
lxc-top

安装软件

apt install lxc bridge-utils

host主机网桥设置

将eth0单网口设置为网桥,lxc自带的lxcbr0内部网桥 10.0.3.1,无法实现旁路网关
将原有网卡/etc/network/interfaces固定地址注释,改为br0。

#iface eth0 inet static
#address 192.168.0.251
#netmask 255.255.255.0
#gateway 192.168.0.1
#dns-nameservers 1.1.1.1 8.8.8.8


auto br0
iface br0 inet static
        address 192.168.0.251
        netmask 255.255.255.0
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_stp off
        bridge_maxwait 5
        dns-nameservers 1.1.1.1 8.8.8.8

重启网卡/etc/init.d/networking restart

配置文件准备

在lxc默认文件夹/var/lib/lxc/下新建openwrt文件夹(也可以建在别处,但注意config中lxc.rootfs.path),然后建立config文件和rootfs文件夹。
编辑/var/lib/lxc/openwrt/config,复制以下内容,按需注释。

lxc.include = /etc/lxc/default.conf
lxc.include = /usr/share/lxc/config/common.conf
lxc.include = /usr/share/lxc/config/userns.conf
lxc.arch = armvirt
lxc.uts.name = openwrt
lxc.start.auto = 1
lxc.start.delay = 1
lxc.rootfs.path = /var/lib/lxc/openwrt/rootfs
lxc.mount.auto = proc:mixed sys:ro cgroup:mixed

# lan interface
lxc.net.0.link = br0

# lxc.cgroup.memory.limit_in_bytes = 512M
# lxc.cgroup.cpuset.cpus = "0,1"

# 挂载host的文件夹
# lxc.mount.entry = /mnt/usb mnt/usb none rw,bind,create=dir 0 0

# vpn tun启用
lxc.mount.entry = /dev/net/tun dev/net/tun none rw,bind,create=file 0 0

镜像固件

将下载的rootfs.tar.gz解压至rootfs文件夹

tar zxvf rootfs.tar.gz -C /var/lib/lxc/openwrt/rootfs/

各版本x64或arm64的openwrt rootfs可在网上下载或云编译,如果有的作者只提供img文件,如xxx-squashfs-rootfs.img.gz,则需要 apt install squashfs-tools工具解压教程
也可以用我云编译0416版本进行测试,一个只有bp和adguard的 https://molezz.lanzouj.com/iFTvD03g1dba 密码:6sf6。 之后发现bp不支持v6,换回pw+adg的组合: https://molezz.lanzouj.com/ipvs804081ed

至此,config文件和rootfs文件夹完成, 日常可直接备份文件夹(先stop或者freeze容器)
启动容器lxc-start openwrtlxc-attach openwrt进入容器修改密码和ip等,生效后即可登录网页版。

一般命令

# 查看网桥信息,veth的既lxc容器的,eth0是物理卡
root@N1:~#  brctl show

    br0      eth0
             vethQby
# 重启容器
root@N1:~#    lxc-stop openwrt && lxc-start openwrt

# 冻结容器,解冻容器,可用于文件夹打包时或脚本
lxc-freeze openwrt
lxc-unfreeze openwrt

# 容器信息
root@N1:~#    lxc-info openwrt
Name:           openwrt
State:          RUNNING
PID:            4910
IP:             192.168.0.251
IP:             2401:8a1e:6a65:321::a
CPU use:        191.45 seconds
BlkIO use:      700.00 KiB
Memory use:     126.52 MiB
KMem use:       4.74 MiB
Link:           vethQby
 TX bytes:      11.01 MiB
 RX bytes:      16.82 MiB
 Total bytes:   27.84 MiB

同样也可以安装各种其他lxc容器,如debian,centos等
rootfs镜像官方: https://uk.lxd.images.canonical.com/images/
高阶用法snapsot,config可参考官网和以下

参考链接

https://www.how2shout.com/how-to/how-to-install-and-use-lxc-linux-containers-on-ubuntu.html
https://yunfwe.cn/2019/09/23/2019/LXC%20Linux%E7%B3%BB%E7%BB%9F%E5%AE%B9%E5%99%A8/

lxc openwrt ubuntu N1

Buy Me a Coffee

支付宝 支付宝
微信 微信
取消回复
  1. Fred.Chen 2022-04-29

    感谢你的分享,能否分享一下,当前Molezz使用的俄罗斯主机的网址?给你的邮箱发邮件,好像被拦截了!

    1. molezz (作者)  2022-04-29
      @Fred.Chen

      https://justhost.ru。 现在已不用了。 oracle云

Theme Jasmine by Kent Liao