pve下以lxc模式运行openwrt

本文是旁路路由模式,主路由模式 请浏览 : https://dev.leiyanhui.com/openwrt/lxc-mian-op/

pve 没有默认docker,需要单独安装。通常不推荐这样做。原因是那个尽量不动pve自身的原则。一般在大量使用容器的情况下,采用lxc 开启一个debian11,然后在里面安装docker。 或者干脆开一个 alpine虚拟机。

那么openwrt 因为要接管我们网络的网关,最好在一个独立的容器里面运行。 用虚拟机方式跑openwrt ,在内网没人下载东西的时候,它依旧会吃相当一部分cpu,所以还是用lxc方式合适,其性能就是你物理机的性能

另外lxc 没有docker的层概念,所以非常适合运行这种独立的服务!

# 获取openwrt的 rootfs

我这里是自己编译的,如果你有这个需求,可以联系我编译。也可以用官网版本(新手不建议,坑多)

# 代编译说明 直接拿到tar.gz包

https://dev.leiyanhui.com/openwrt/diy

# 原有镜像转lxc

# 转换为img

# qcow2/vmdk

如果原来镜像是 qcow2 vmdk

1
2
qemu-img convert -p -f qcow2 -O raw op.qcow2  op.img
qemu-img convert -p -f vmdk -O raw op.vmdk  op.img
# img.gz
1
gzip -d openwrt-01.10.2023-x86-64-generic-squashfs-combined.img.gz
# 正在运行的虚拟机openwrt转lxc

直接在pve web控制台点备份,选择 gzip格式 ,得到文件 /var/lib/vz/dump/vzdump-qemu-100-2023_02_03-12_29_56.vma.gz

在 local 备份里面 找到这个文件 点恢复 导入到一个新虚拟机 102

1
qemu-img convert -p -f qcow2 -O raw /var/lib/vz/images/102/vm-102-disk-0.qcow2  op.img

# 挂载和打包

1
2
3
4
5
6
7
cd /var/lib/vz/template/cache
mkdir op
root_partition=$((`fdisk -l op.img | grep .img2 | awk '{print $2}'` * 512))
mount -o loop,offset=$root_partition op.img /var/lib/vz/template/cache/op
cd op && tar zcf /var/lib/vz/template/cache/op.tar.gz * &&cd ..     # 打包至PVE中LXC模板路径
umount op && rm -rf op
#rm -rf 固件名称.img

如果img是正在运行的系统且使用的Overlay模式,会丢失Overlay内容(恢复默认了)。你需要在原来系统里面 导出 后覆盖过来。

# 上传lxc

上传到 local节点 ct模板 op.tar.gz 后面操作都基于local和这个文件名。

# 创建CT容器

直接在pve里面创建 会提示 unknown ID 'openwrt' in /etc/os-release file, trying fallback detection TASK ERROR: unable to create CT 2000 - unable to detect OS distribution 这样的错误,我们需要修改ostype,这在pve的webui里面是无法是实现的。 登录 pve shell 命令 创建 2000 使用上面的rootfs 磁盘和内存 cpu 也配置一下

1
pct create 2000 /var/lib/vz/template/cache/op.tar.gz --arch amd64 --hostname lxc-OpenWrt --rootfs local:2 --memory 2048 -swap 0 --cores 12 --ostype unmanaged --unprivileged 0 -net0 bridge=vmbr0,name=eth0

--rootfs local:2 代表放到 local 2g大小

你需要根据你的情况修改上面命令的部分内容,btrfs 的话路径是/var/lib/pve/local-btrfs/template/cache/

# 网络配置

创建 添加网卡

我这里是做旁路由使用,如果你做主路由用,还需要把映射 /dev/ppp 以及分配额外的网卡过来。 一般不建议 aio平台跑主路由哈。

# 启动

1
pct start 2000

# 其他操作

如果你是用的别人编译的 或者官网的,还需要去容器里面 修改一下ip,如果是定制版本的 直接就可以开始用了。

1
lxc-attach 2000 # 进入容器终端

# 其他常见问题

# luci-static 错误

部分固件提示:

1
2
3
4
Function statements require a function name
  in http://10.1.1.20/luci-static/resources/network.js?v=git-94eb2a9:?
  at http://10.1.1.20/luci-static/resources/luci.js?v=git-24.033.44588-94eb2a9:179:29
  at async Promise.all (index 4)

固件本身的问题 换固件。

# ssh无法登录

固件问题

# ipsec无法链接的问题

问题不明,最佳方案,单独一个lxc/docker 运行 ipsec 详情: https://dev.leiyanhui.com/pve/ipsec

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计