编译带 OpenClash 的 OpenWrt 固件

编译并配置带 OpenClash 的 OpenWrt 固件。

OpenWrt 是开源的、基于 Linux 的软路由系统,而 OpenClash 一个运行在 OpenWrt 上的 Clash 客户端,兼容Shadowsocks、Vmess、Trojan、Snell 等协议,根据灵活的规则配置策略代理,把 OpenClash 当网关做透明代理,该网段下的所有的设备无需设置,即可自动实现科学上网。

网上有很多修改过的 OpenWrt 固件,但集成 OpenClash 的固件比较少,本文将记录一下编译纯净的 OpenWrt 固件,并集成 OpenClash。

前言

  • 系统我使用的 Debian 10
  • 由于会下载大量国外数据,建议国内用户全程在科学上网环境下编译。
  • 编译时会产生大量临时数据,建议至少预留20G的空闲空间和4G以上内存

开始

使用root用户安装所需软件依赖

1
2
3
4
5
6
7
apt update

apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python3-distutils python3-setuptools python3-dev rsync subversion \
swig time xsltproc zlib1g-dev curl

以下操作全部以非root用户执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## 切换到用户根目录
cd ~

## 拉取OpenWrt
git clone https://github.com/openwrt/openwrt

## 下载OpenClash
wget https://github.com/vernesong/OpenClash/archive/master.zip

## 解压
unzip master.zip

## 复制OpenClash软件包到OpenWrt
cp -r OpenClash-master/luci-app-openclash openwrt/package

cd openwrt
./scripts/feeds update -a
./scripts/feeds install -a

输入make menuconfig进入交互式配置界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Target System (x86)  --->   目标系统(x86)
Subtarget (x86_64) ---> 子目标(x86_64)
Target Profile (Generic) --->目标配置文件(通用)
Target Images ---> 保存目标镜像的格式
Global build settings ---> 全局构建设置
Advanced configuration options (for developers) ---- 高级配置选项(适用于开发人员)
Build the OpenWrt Image Builder 构建OpenWrt图像生成器
Build the OpenWrt SDK 构建OpenWrt SDK
Package the OpenWrt-based Toolchain 打包基于OpenWrt的工具链
Image configuration --->图像配置
Base system ---> 基本系统
Administration ---> 管理
Boot Loaders ---> 引导加载程序
Development ---> 开发
Extra packages ---> 额外包
Firmware --->固件
Fonts --->字体
Kernel modules ---> 内核模块
Languages --->语言
Libraries ---> 图书馆
LuCI ---> LuCI
Mail ---> 邮件
Multimedia --->多媒体
Network --->网络
Sound ---> 声音
Utilities --->实用程序
Xorg --->Xorg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## 选择系统(以 x86_64 为例)
Target System -> x86
Subtarget -> x86_64

## 选择固件的文件系统
## https://openwrt.org/docs/techref/filesystems
Target Images -> squashfs

## 选择构建X86_X64的GRUB固件
Target Images -> Build GRUB images (Linux x86 or x86_64 host only)

## 选择更小的压缩格式固件,方便复制
Target Images -> GZip images

## 修改软件包可用空间,默认安装会占用100M左右,建议修改扩大,为后续安装其他软件打基础
Target Images -> Root filesystem partition size

## 添加web界面(y键选择n键排除)
LuCI > Collections -> Luci

## 添加兼容性依赖
LuCI > Modules -> luci-compat

## 添加中文
LuCI > Modules -> Translations -> Chinese Simplified

## 添加openclash
LuCI > Applications -> luci-app-openclash

## 添加主题
LuCI -> Themes

## 添加wget
Nerwork -> File Transfer -> wget-ssl

## 添加kmod-tun,TUN模式必须
Kernel modules -> Network Support -> kmod-tun

## 排除dnsmasq,由于默认会安装dnsmasq-full,这里需要排除dnsmasq,否则会冲突报错。
Base system -> dnsmasq

开始编译

1
2
3
4
5
6
7
## -j8: 使用8个内核,V=s: 为故障排除目的提供更详细的信息

## 下载dl库(国内请尽量进行科学上网)
make -j8 download V=s

## 开始编译
make -j4 V=s

编译时间很长,一般要30分钟以上
编译完成后的固件xx-squashfs-combined.img.gzopenwrt/bin/targets/x86/64/目录下

二次更新编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## 切换到用户根目录
cd ~

## 删除旧的openclash文件
rm -rf master.zip && rm -rf OpenClash-master && rm -rf openwrt/package/luci-app-openclash

## 重新下载OpenClash
wget https://github.com/vernesong/OpenClash/archive/master.zip

## 解压
unzip master.zip

## 复制OpenClash软件包到OpenWrt
cp -r OpenClash-master/luci-app-openclash openwrt/package

cd openwrt

## 更新openwrt
git pull

./scripts/feeds update -a
./scripts/feeds install -a

## 配置
make menuconfig

## 下载dl库(国内请尽量进行科学上网)
make -j8 download V=s

## 开始编译
make -j4 V=s

ProxmoxVE安装OpenWrt

新建虚拟机,记住VM ID,这里以300为例;操作系统选择不使用任何介质;系统默认;硬盘随便分配1G,稍后需要手动删除的;CPU、内存、网络自行配置。
将编译好的 OpenWrt 固件放入 PVE 根目录。

1
2
3
4
5
6
7
cd ~

## 解压固件
gunzip xx-squashfs-combined.img.gz

## 转换成 PVE 磁盘格式,注意其中的300替换为你的VM ID
qm importdisk 300 ./openwrt.img local-lvm

转换完,在虚拟机硬件界面应该会多出一个未使用磁盘。将创建虚拟机时分配的磁盘分离删除,选择转换的 OpenWrt 磁盘点击编辑添加。