Docker上运行Lean大源码编译的OpenWRT

原文:https://openwrt.club/93.html#comments

准备工作

为了在之后可以在Docker中运行L大的OpenWRT,所以我们在编译固件的时候,需要选中如下选项,废话不多说上图:
BY.png

编译完成后,留下**openwrt-x86-64-generic-rootfs.tar.gz**文件备用。

宿主机Linux的基本配置

由于需要将Linux配置为宿主机,所以Linux上需要进行一些基本的设置。

  • 打开所需要使用的网卡的混杂模式

ip link set ens33 promisc on
ip link set ens34 promisc on
  • 加载pppoe内核模块

modprobe pppoe

Docker容器的网络配置

为 docker 创建 macvlan 模式的虚拟网卡,并关联到宿主机。涉及到ipv4,ipv6地址请自行修改

  • LAN 口
docker network create -d macvlan --subnet=172.16.60.0/24 --gateway=172.16.60.254 --ipv6 --subnet=fe80::/16 --gateway=fe80::1 -o parent=ens33 -o macvlan_mode=bridge openwrt-LAN
  • WAN 口
docker network create -d macvlan --subnet=192.168.0.0/24 --gateway=192.168.0.254 --ipv6 --subnet=fe81::/16 --gateway=fe81::1 -o parent=ens34 -o macvlan_mode=bridge openwrt-WAN

建立以及启动容器

  • 创建容器镜像
docker import openwrt-x86-64-generic-rootfs.tar.gz lean_openwrt
  • 启动容器
docker run -it -d --restart always --network openwrt-LAN --privileged --name openwrt lean_openwrt /sbin/init
  • 将第二块网卡的挂接到 openwrt
docker network connect openwrt-WAN openwrt

修改OpenWRT配置

  • 进入容器
docker exec -it openwrt /bin/sh
  • 编辑 /etc/config/network
config interface 'lan'
    option type 'bridge'
    option ifname 'eth0'  
    option proto 'static'
    option ipaddr '172.16.60.1'
    option netmask '255.255.255.0'
    option ip6assign '64'

config interface 'wan'
        option ifname 'eth1'  
        option proto 'dhcp'
        option ip6assign '64'
  • 重启OpenWRT网络服务
/etc/init.d/network restart

将OpenWRT作为宿主机的网关

由于容器网络采用 macvlan 的 bridge 模式,即使宿主机与容器在同一网段,相互之间也是无法通信的。为了解决这个问题,需利用多个 macvlan 接口之间是互通的原理,新建macvlan虚拟接口:

ip link add link ens33 vLAN type macvlan mode bridge   
ip addr add 172.16.60.253/24 brd + dev vLAN   
ip link set vLAN up
ip route del default                
ip route add default via 172.16.60.1 dev vLAN     

#设置宿主机的dns服务器为OpenWRT
echo "nameserver 172.16.60.1" > /etc/resolv.conf

将宿主机的网络配置加入到自启动脚本中

为了保证在Linux重启后,网络依然可以生效,我们将上一步中的配置加入到启动脚本(例如rc.local)。

剩下的事情就是OpenWRT的基本配置了,在这里就不在详述了。

# DOCKER  OpenWRT 

本文由:星际难民
实践,测试,整理发布.如需转载请注明地址 本文标题:Docker上运行Lean大源码编译的OpenWRT
地址:https://530503.xyz/articles/2021/01/11/1610699429539.html

评论

取消