本文记录以下OpenWrt的入门知识,包括源码编译,make menuconfig配置,feeds包下载以及生成可以运行在VMWare上的镜像。

然后在VMWare上运行,并配置静态IP,然后可以使用SSH工具远程连接到OpenWrt系统中。

源码下载编译🍕

源码下载

$ git clone https://git.openwrt.org/openwrt/openwrt.git
Cloning into 'openwrt'...
remote: Enumerating objects: 6230, done.
remote: Counting objects: 100% (6230/6230), done.
remote: Compressing objects: 100% (2985/2985), done.
remote: Total 719334 (delta 3439), reused 5522 (delta 3025), pack-reused 713104
Receiving objects: 100% (719334/719334), 203.89 MiB | 211.00 KiB/s, done.
Resolving deltas: 100% (504427/504427), done.
Updating files: 100% (9117/9117), done.

更新下载feeds

会将每个feed下载到feeds/<feed_name>目录下,同时创建相应的feeds/<feed_name>.tmp目录存放索引文件、下载路径(location)等

blduan@ubuntu24:~/projects/open_source$ cd openwrt
blduan@ubuntu24:~/projects/open_source/openwrt$ ls
BSDmakefile  Config.in  Makefile   config              include  rules.mk  target     tools
COPYING      LICENSES   README.md  feeds.conf.default  package  scripts   toolchain
blduan@ubuntu24:~/projects/open_source/openwrt$ ./scripts/feeds update -a
Updating feed 'packages' from 'https://git.openwrt.org/feed/packages.git' ...
Cloning into './feeds/packages'...
remote: Enumerating objects: 7271, done.
remote: Counting objects: 100% (7271/7271), done.
remote: Compressing objects: 100% (5977/5977), done.
remote: Total 7271 (delta 255), reused 5704 (delta 160), pack-reused 0
Receiving objects: 100% (7271/7271), 14.22 MiB | 84.00 KiB/s, done.
Resolving deltas: 100% (255/255), done.
Updating feed 'luci' from 'https://git.openwrt.org/project/luci.git' ...
Cloning into './feeds/luci'...
remote: Enumerating objects: 10730, done.
remote: Counting objects: 100% (10730/10730), done.
remote: Compressing objects: 100% (4906/4906), done.
remote: Total 10730 (delta 3577), reused 7548 (delta 1046), pack-reused 0
Receiving objects: 100% (10730/10730), 9.40 MiB | 34.00 KiB/s, done.
Resolving deltas: 100% (3577/3577), done.
Updating files: 100% (5242/5242), done.
Updating feed 'routing' from 'https://git.openwrt.org/feed/routing.git' ...
Cloning into './feeds/routing'...
remote: Enumerating objects: 286, done.
remote: Counting objects: 100% (286/286), done.
remote: Compressing objects: 100% (231/231), done.
remote: Total 286 (delta 9), reused 91 (delta 0), pack-reused 0
Receiving objects: 100% (286/286), 120.37 KiB | 438.00 KiB/s, done.
Resolving deltas: 100% (9/9), done.
Updating feed 'telephony' from 'https://git.openwrt.org/feed/telephony.git' ...
Cloning into './feeds/telephony'...
remote: Enumerating objects: 273, done.
remote: Counting objects: 100% (273/273), done.
remote: Compressing objects: 100% (255/255), done.
remote: Total 273 (delta 5), reused 182 (delta 5), pack-reused 0
Receiving objects: 100% (273/273), 226.46 KiB | 402.00 KiB/s, done.
Resolving deltas: 100% (5/5), done.
Create index file './feeds/packages.index'
Checking 'true'... ok.
Checking 'false'... ok.
Checking 'working-make'... ok.
Checking 'case-sensitive-fs'... ok.
Checking 'proper-umask'... ok.
Checking 'gcc'... updated.
Checking 'working-gcc'... ok.
Checking 'g++'... updated.
Checking 'working-g++'... ok.
Checking 'ncurses.h'... ok.
Checking 'git'... updated.
Checking 'rsync'... updated.
Checking 'perl-data-dumper'... ok.
Checking 'perl-findbin'... ok.
Checking 'perl-file-copy'... ok.
Checking 'perl-file-compare'... ok.
Checking 'perl-thread-queue'... ok.
Checking 'perl-ipc-cmd'... ok.
Checking 'tar'... updated.
Checking 'find'... updated.
Checking 'bash'... updated.
Checking 'xargs'... updated.
Checking 'patch'... updated.
Checking 'diff'... updated.
Checking 'cp'... updated.
Checking 'seq'... updated.
Checking 'awk'... updated.
Checking 'grep'... updated.
Checking 'egrep'... updated.
Checking 'getopt'... updated.
Checking 'realpath'... updated.
Checking 'stat'... updated.
Checking 'gzip'... updated.
Checking 'unzip'... updated.
Checking 'bzip2'... updated.
Checking 'wget'... updated.
Checking 'install'... updated.
Checking 'perl'... updated.
Checking 'python'... updated.
Checking 'python3'... updated.
Checking 'python3-distutils'... ok.
Checking 'python3-stdlib'... ok.
Checking 'file'... updated.
Checking 'which'... updated.
Checking 'argp.h'... ok.
Checking 'fts.h'... ok.
Checking 'obstack.h'... ok.
Checking 'libintl.h'... ok.
Checking 'ldconfig-stub'... ok.
Collecting package info: done
Collecting target info: done
Create index file './feeds/luci.index'
Collecting package info: done
Collecting target info: done
Create index file './feeds/routing.index'
Collecting package info: done
Collecting target info: done
Create index file './feeds/telephony.index'
Collecting package info: done
Collecting target info: done
blduan@ubuntu24:~/projects/open_source/openwrt$ cd feeds/
blduan@ubuntu24:~/projects/open_source/openwrt/feeds$ ls -l
total 32
drwxrwxr-x 13 blduan blduan 4096 Nov 21 15:22 luci
lrwxrwxrwx  1 blduan blduan   21 Nov 21 15:24 luci.index -> luci.tmp/.packageinfo
lrwxrwxrwx  1 blduan blduan   20 Nov 21 15:24 luci.targetindex -> luci.tmp/.targetinfo
drwxrwxr-x  3 blduan blduan 4096 Nov 21 15:24 luci.tmp
drwxrwxr-x 18 blduan blduan 4096 Nov 21 15:17 packages
lrwxrwxrwx  1 blduan blduan   25 Nov 21 15:24 packages.index -> packages.tmp/.packageinfo
lrwxrwxrwx  1 blduan blduan   24 Nov 21 15:24 packages.targetindex -> packages.tmp/.targetinfo
drwxrwxr-x  3 blduan blduan 4096 Nov 21 15:24 packages.tmp
drwxrwxr-x 32 blduan blduan 4096 Nov 21 15:22 routing
lrwxrwxrwx  1 blduan blduan   24 Nov 21 15:24 routing.index -> routing.tmp/.packageinfo
lrwxrwxrwx  1 blduan blduan   23 Nov 21 15:24 routing.targetindex -> routing.tmp/.targetinfo
drwxrwxr-x  3 blduan blduan 4096 Nov 21 15:24 routing.tmp
drwxrwxr-x  6 blduan blduan 4096 Nov 21 15:22 telephony
lrwxrwxrwx  1 blduan blduan   26 Nov 21 15:24 telephony.index -> telephony.tmp/.packageinfo
lrwxrwxrwx  1 blduan blduan   25 Nov 21 15:24 telephony.targetindex -> telephony.tmp/.targetinfo
drwxrwxr-x  3 blduan blduan 4096 Nov 21 15:24 telephony.tmp
blduan@ubuntu24:~/projects/open_source/openwrt/feeds/packages.tmp$ ls -a
.  ..  .packageinfo  .targetinfo  info  location
blduan@ubuntu24:~/projects/open_source/openwrt/feeds/packages.tmp$ cat location
https://git.openwrt.org/feed/packages.git

每个feed具有一个索引文件,索引文件中保存了feed下每个软件包的详细信息(软件包名、版本号、使用的Makefile、依赖关系等等),示例如下:

blduan@ubuntu24:~/projects/open_source/openwrt/feeds/packages.tmp$ more .packageinfo
Source-Makefile: feeds/packages/admin/atop/Makefile

Package: atop
Version: 2.11.0-r2
Depends: +libc +zlib +libncurses +glib2
Conflicts:
Menu-Depends:
Provides:
Section: admin
Category: Administration
Repository: base
Title: System and process monitor for Linux
Maintainer: Toni Uhlig <matzeton@googlemail.com>
Source: atop-2.11.0.tar.gz
License: GPL-2.0-or-later
LicenseFiles: COPYING
CPE-ID: cpe:/a:atop_project:atop
URL: https://www.atoptool.nl/
Type: ipkg
Description:   Atop is an ASCII full-screen performance monitor for
  Linux that is capable of reporting the activity of all
  processes (even if processes have finished during the
  interval), daily logging of system and process activity
  for long-term analysis, highlighting overloaded system
  resources by using colors, etcetera. At regular
  intervals, it shows system-level activity related to the
  CPU, memory, swap, disks (including LVM) and network
  layers, and for every process (and thread) it shows e.g.
  the CPU utilization, memory growth, disk utilization,
  priority, username, state, and exit code. In combination
  with the optional kernel module netatop, it even shows
  network activity per process/thread.
@@
...

安装feeds

这些feed在安装前其软件包在OpenWrt构建时是不被包含在内的。

安装过程则是将这些软件包软链接到package目录下,之后则可以通过make package/feeds/<feed_name>/<package_name>/compile进行构建,同时也能够在make menuconfig中看到。

blduan@ubuntu24:~/projects/open_source/openwrt$ ./scripts/feeds install -a
Collecting package info: done
Collecting target info: done
Installing all packages from feed packages.
Installing package '2to3' from packages
Installing package 'python3' from packages
...
blduan@ubuntu24:~/projects/open_source/openwrt$
blduan@ubuntu24:~/projects/open_source/openwrt$ ls -l package/feeds/
# 下面是4个feed,其中是每个feed下软件包的符号链接
total 60
drwxrwxr-x 2 blduan blduan 12288 Nov 21 15:44 luci
drwxrwxr-x 2 blduan blduan 36864 Nov 21 15:44 packages
drwxrwxr-x 2 blduan blduan  4096 Nov 21 15:44 routing
drwxrwxr-x 2 blduan blduan  4096 Nov 21 15:44 telephony
blduan@ubuntu24:~/projects/open_source/openwrt$ ls -l package/feeds/packages/
# package feed下软件包的软连接
total 56
lrwxrwxrwx 1 blduan blduan 40 Nov 21 15:44 2to3 -> ../../../feeds/packages/lang/python/2to3
lrwxrwxrwx 1 blduan blduan 43 Nov 21 15:44 python3 -> ../../../feeds/packages/lang/python/python3
...

编译配置

配置Target X86: 配置Target X86

配置Target Image(VMDK): 配置Target Image(VMDK)

配置Web UI: 配置Web UI

配置完成之后会在代码根目录生成.config文件。

blduan@ubuntu24:~/projects/open_source/openwrt$ ls -l .config
-rw-rw-r-- 1 blduan blduan 261986 Nov 23 04:02 .config
blduan@ubuntu24:~/projects/open_source/openwrt$ cat .config | grep X86
CONFIG_FFMPEG_X86ASM=y
blduan@ubuntu24:~/projects/open_source/openwrt$ cat .config | grep VMDK
CONFIG_VMDK_IMAGES=y

下载开源组件

截至到这儿为止,所有的软件包的源码都还没有下载到本地,下载信息则保存在对应软件包的Makefile文件中。因此,接下来首先要将软件包的源码下载到本地。

至于具体要下载哪些软件包,则取决于是否在make menuconfig是否配置。

软件包的源码会下载到dl目录,同时会解压到build_dir/target*目录下

可以使用命令make download -j$(nproc) V=s进行下载。

开始编译

编译命令:make -j$(nproc) V=s

编译完成之后的目录结构:其中bin, build_dir, staging_dir, tmp是编译产生的,并且编译的镜像在bin目录下。

blduan@ubuntu24:~/projects/open_source/openwrt$ ll
total 672
drwxrwxr-x 20 blduan blduan   4096 Nov 29 13:54 ./
drwxrwxr-x 19 blduan blduan   4096 Nov 21 14:43 ../
-rw-rw-r--  1 blduan blduan 261986 Nov 23 04:02 .config
-rw-rw-r--  1 blduan blduan 256590 Nov 23 03:58 .config.old
drwxrwxr-x  3 blduan blduan   4096 Nov 21 15:00 .devcontainer/
drwxrwxr-x  8 blduan blduan   4096 Nov 21 15:00 .git/
-rw-rw-r--  1 blduan blduan      8 Nov 21 15:00 .gitattributes
drwxrwxr-x  4 blduan blduan   4096 Nov 21 15:00 .github/
-rw-rw-r--  1 blduan blduan    362 Nov 21 15:00 .gitignore
drwxrwxr-x  2 blduan blduan   4096 Nov 21 15:00 .vscode/
-rw-rw-r--  1 blduan blduan    106 Nov 21 15:00 BSDmakefile
-rw-rw-r--  1 blduan blduan    284 Nov 21 15:00 COPYING
-rw-rw-r--  1 blduan blduan    668 Nov 21 15:00 Config.in
drwxrwxr-x  2 blduan blduan   4096 Nov 21 15:00 LICENSES/
-rw-rw-r--  1 blduan blduan   4233 Nov 21 15:00 Makefile
-rw-rw-r--  1 blduan blduan   4296 Nov 21 15:00 README.md
drwxr-xr-x  4 blduan blduan   4096 Nov 29 13:56 bin/
drwxr-xr-x  6 blduan blduan   4096 Nov 29 13:54 build_dir/
drwxrwxr-x  2 blduan blduan   4096 Nov 21 15:00 config/
drwxr-xr-x  2 blduan blduan   4096 Nov 29 14:24 dl/
drwxrwxr-x 10 blduan blduan   4096 Nov 21 15:43 feeds/
-rw-rw-r--  1 blduan blduan    442 Nov 21 15:00 feeds.conf.default
drwxrwxr-x  3 blduan blduan   4096 Nov 21 15:00 include/
drwxrwxr-x 12 blduan blduan   4096 Nov 21 15:44 package/
-rw-r--r--  1 blduan blduan    227 Nov 29 13:54 private-key.pem
-rw-r--r--  1 blduan blduan    178 Nov 29 13:54 public-key.pem
-rw-rw-r--  1 blduan blduan  16947 Nov 21 15:00 rules.mk
drwxrwxr-x  4 blduan blduan   4096 Nov 21 15:00 scripts/
drwxrwxr-x  7 blduan blduan   4096 Nov 29 14:32 staging_dir/
drwxrwxr-x  7 blduan blduan   4096 Nov 21 15:00 target/
drwxrwxr-x  3 blduan blduan  20480 Nov 29 14:34 tmp/
drwxrwxr-x 12 blduan blduan   4096 Nov 21 15:00 toolchain/
drwxrwxr-x 74 blduan blduan   4096 Nov 21 15:00 tools/

blduan@ubuntu24:~/projects/open_source/openwrt/bin/targets/x86/generic$ ls -lh
total 100M
-rw-r--r-- 1 blduan blduan  955 Nov 29 13:55 config.buildinfo
-rw-r--r-- 1 blduan blduan  395 Nov 29 13:55 feeds.buildinfo
-rw-r--r-- 1 blduan blduan  11M Nov 29 14:35 openwrt-x86-generic-generic-ext4-combined-efi.img.gz                                                            -rw-r--r-- 1 blduan blduan  11M Nov 29 14:35 openwrt-x86-generic-generic-ext4-combined-efi.vmdk.gz
-rw-r--r-- 1 blduan blduan  11M Nov 29 14:35 openwrt-x86-generic-generic-ext4-combined.img.gz
-rw-r--r-- 1 blduan blduan  11M Nov 29 14:35 openwrt-x86-generic-generic-ext4-combined.vmdk.gz
-rw-r--r-- 1 blduan blduan 4.8M Nov 29 14:34 openwrt-x86-generic-generic-ext4-rootfs.img.gz
-rw-r--r-- 1 blduan blduan 5.3M Nov 29 14:34 openwrt-x86-generic-generic-kernel.bin
-rw-r--r-- 1 blduan blduan 4.7M Nov 29 14:34 openwrt-x86-generic-generic-rootfs.tar.gz
-rw-r--r-- 1 blduan blduan 9.8M Nov 29 14:34 openwrt-x86-generic-generic-squashfs-combined-efi.img.gz
-rw-r--r-- 1 blduan blduan  11M Nov 29 14:35 openwrt-x86-generic-generic-squashfs-combined-efi.vmdk
-rw-r--r-- 1 blduan blduan 9.6M Nov 29 14:34 openwrt-x86-generic-generic-squashfs-combined.img.gz
-rw-r--r-- 1 blduan blduan 9.8M Nov 29 14:35 openwrt-x86-generic-generic-squashfs-combined.vmdk
-rw-r--r-- 1 blduan blduan 4.0M Nov 29 14:34 openwrt-x86-generic-generic-squashfs-rootfs.img.gz
-rw-r--r-- 1 blduan blduan 4.3K Nov 29 14:35 openwrt-x86-generic-generic.manifest
drwxr-xr-x 3 blduan blduan 4.0K Nov 29 14:35 packages
-rw-r--r-- 1 blduan blduan 3.5K Nov 29 14:36 profiles.json
-rw-r--r-- 1 blduan blduan 1.8K Nov 29 14:36 sha256sums
-rw-r--r-- 1 blduan blduan   18 Nov 29 13:55 version.buildinfo

VMWare安装

  1. 新建虚拟机,选择自定义模式,稍后安装操作系统。
  2. 选择客户机操作系统版本。 客户机操作系统版本
  3. 然后一直默认,直到选择现有虚拟磁盘。 选在现有虚拟磁盘 选择现有磁盘文件
  4. 安装完成。 OpenWrt安装完成
  5. 启动OpenWrt虚拟机。 启动OpenWrt虚拟机
  6. 进入OpenWrt系统。 OpenWrt系统
  7. 配置和主机同网段的IP(192.168.100.129)或者使用动态IP。
# uci set network.lan.ipaddr=192.168.100.129
# uci commit
# /etc/init.d/network restart

设置静态IP

访问OpenWrt

  1. ssh远程到openwrt系统中。 ssh远程连接到openwrt中
  2. 通过Web访问OpenWrt系统。 通过Web访问OpenWrt系统 通过Web访问OpenWrt系统2

参考文章🍟

[OpenWrt Wiki] The OpenWrt source code

[OpenWrt Wiki] OpenWrt Feeds

vmware安装openwrt | 包含代码下载,编译,安装,配置和使用 - whilewell - 博客园