标签 openwrt 下的文章

openwrt 代码框架分析

这次讲讲openwrt的结构.

1.代码上来看有几个重要目录package, target, build_root, bin, dl....
---build_dir/host目录是建立工具链时的临时目录
---build_dir/toolchain-是对应硬件的工具链的目录
---staging_dir/toolchain-
则是工具链的安装位置
---target/linux/目录里面是各个平台(arch)的相关代码
---target/linux//config-3.10文件就是配置文件了
---dl目录是'download'的缩写, 在编译前期,需要从网络下载的数据包都会放在这个目录下,这些软件包的一个特点就是,会自动安装在所编译的固件中,也就是我们make menuconfig的时候,为固件配置的一些软件包。如果我们需要更改这些源码包,只需要将更改好的源码包打包成相同的名字放在这个目录下,然后开始编译即可。编译时,会将软件包解压到build_dir目录下。
---而在build_dir/目录下进行解压,编译和打补丁等。
---package目录里面包含了我们在配置文件里设定的所有编译好的软件包。默认情况下,会有默认选择的软件包。在openwrt中ipk就是一切, 我们可以使用

$ ./scripts/feeds update来对软件包进行更新.
$ ./scripts/feeds search nmap 查找软件包'nmap'
 Search results in feed ’packages’: 
nmap       Network exploration and/or security auditing utility 
$ ./scripts/feeds install nmap 安装'nmap'这个软件
$ make package/symlinks  //估计意思是更新软件源之类的

---bin目录下生成了很多bin文件,根据不同的平台来区分。另外bin//package目录,里面有很多ipk后缀的文件,都是package目录下的源码在build_dir目录下编译后的生成的结果。

2.新建自己的packages
对于自己新建的package,而这个package又不需要随固件一起安装,换句话说,就是可以当做一个可选软件包的话。我们可以利用我们的SDK环境来单独编译,编译后会生成一个ipk的文件包。然后利用 opkg install xxx.ipk 来安装这个软件。

下面具体说下,如何编译一个helloword的软件包。
(1)首先,编写helloworld程序
编写helloworld.c

/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <unistd.h>
int main(void)
{
     printf("Hell! O' world, why won't my code compile?\n\n");
     return 0;
}

编写Makefile文件

# build helloworld executable when user executes "make"
helloworld: helloworld.o
        $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
        $(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
        rm *.o helloworld

在这两个文件的目录下,执行make 应该可以生成helloworld的可执行文件。执行helloworld后,能够打印出“Hell!O' world, why won't my code compile?”。 这一步,主要保证我们的源程序是可以正常编译的。

下面我们将其移植到OpenWRT上。
(2)将OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2解压
tar –xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
(3)进入SDK

cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1

可以看到里面的目录结构跟我们之前source的目录结构基本相同,所需要编译的软件包,需要放置在package目录下
(4)在package目录下创建helloworld目录

cd package
mkdir helloworld
cd helloworld

(5)创建src目录,拷贝 helloworld文件

mkdir src
cp /home/wrt/test/helloworld.c src
cp /home/wrt/test/Makefile src

(6)在helloworld目录下创建Makefile文件
这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。

touch Makefile
vim Makefile

Makefile文件模板内容如下:

##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1


# This specifies the directory where we're going to build the program. 
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk

# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
        SECTION:=utils
        CATEGORY:=Utilities
        TITLE:=Helloworld -- prints a snarky message
endef


# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
        If you can't figure out what this program does, you're probably
        brain-dead and need immediate medical attention.
endef

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        $(CP) ./src/* $(PKG_BUILD_DIR)/
endef


# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one


# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
        $(INSTALL_DIR) $(1)/bin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef


# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

(7)返回到SDK的根目录
执行make进行编译
编译过程会在build_dir目录下完成
编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_bcm47xx.ipk
(8)上传helloworld_1_bcm47xx.ipk
上传helloworld_1_bcm47xx.ipk至路由器
执行# opkg install helloworld_1_bcm47xx.ipk
然后输入hello然后按Tab键,发现openwrt中已经有helloworld可执行命令。
执行 helloworld命令来查看程序的效果。

Hell! O' world, why won't my code compile?

本文章由http://www.wifidog.pro/2015/08/17/openwrt-%E6%A1%86%E6%9E%B6.html整理编辑,转载请注明出处

在Openwrt中使用vsftpd作为FTP服务器

opkgupdate
opkginstall vsftpd
ln -s   /etc/init.d/vsftpd/etc/rc.d/S50vsftpd     #建立快速启动,如果是固件集成vsftpd,则跳过该步骤;

防火墙开启21端口,如果有需要,可以进行端口转发

vim /etc/vsftpd.conf

做出如下修改

chown_uploads=YES                #上传后更改文件所属权
chown_username=root            #上传后更改文件所属权
ftp_username=nobody           #定义匿名用户名
anonymous_enable=YES                  #允许匿名用户访问
anon_upload_enable=YES     #允许匿名上传
anon_mkdir_write_enable=YES     #允许匿名创建目录
anon_root=/mnt/anymous   #匿名用户根目录
anon_max_rate=512000       #匿名用户限速
local_enable=YES                           #允许本地用户登录(必须)
write_enable=YES                          #允许上传
local_umask=022                            #允许上传
check_shell=NO
local_root=/                           #本地用户主目录(必须)

修改文件权限

chown root:nogroup /mnt/anymous
chmod 557 /mnt/anymous

启动vsftpd

/etc/init.d/vsftpd restart

杀掉vsftpd进程

kill-9 PID

本文章由http://www.wifidog.pro/2015/08/13/openwrt-ftp.html整理编辑,转载请注明出处

openwrt ubus (OpenWrt micro bus 架构)

为了在OpenWrt中提供守护进程和应用程序间的通讯,开发了ubus项目工程。它包含了守护进程、库以及一些额外的帮助程序。

核心部分是ubusd守护进程,它提供了其他守护进程将自己注册以及发送消息的接口。因为这个,接口通过使用Unix socket来实现,并使用TLV(type-length-value)消息。

为了简化软件的开发,可以使用已有的libubus库来使用ubus(连接ubus)。

每个守护进程在自己的名称空间中注册自有的路径。每个路径可以提供多个带有不定数量参数的方法,方法可以通过消息回复调用。

代码在LGPL 2.1授权方法下发布,你可以通过git在git://nbd.name/luci2/ubus.git或通过http在http://nbd.name/gitweb.cgi?p=luci2/ubus.git;a=summary获取。 ubus从r28499起被包含在OpenWrt中。

ubus命令行工具

ubus可以和ubusd服务器交互(和当前所有已经注册的服务). 它对研究和调试注册的命名空间以及编写脚本非常有用。对于调用带参数和返回信息的方法,它使用友好的JSON格式。下面是它的命令说明。

list
缺省列出所有通过RPC服务器注册的命名空间:

root@uplink:~# ubus list
network
network.device
network.interface.lan
network.interface.loopback
network.interface.wan
root@uplink:~#

如果调用时包含参数-v,将会显示指定命名空间更多方法参数等信息:

root@uplink:~# ubus -v list network.interface.lan
'network.interface.lan' @099f0c8b
    "up": {  }
    "down": {  }
    "status": {  }
    "prepare": {  }
    "add_device": { "name": "String" }
    "remove_device": { "name": "String" }
    "notify_proto": {  }
    "remove": {  }
    "set_data": {  }
root@uplink:~#

call
调用指定命名空间中指定的方法,并且通过消息传递给它:

root@uplink:~# ubus call network.interface.wan status
{
    "up": true,
    "pending": false,
    "available": true,
    "autostart": true,
    "uptime": 86017,
    "l3_device": "eth1",
    "device": "eth1",
    "address": [
        {
            "address": "178.25.65.236",
            "mask": 21
        }
    ],
    "route": [
        {
            "target": "0.0.0.0",
            "mask": 0,
            "nexthop": "178.25.71.254"
        }
    ],
    "data": {

    }
}
root@uplink:~#

消息参数必须是有效的JSON字符串,并且携带函数所要求的键及值:

root@uplink:~# ubus call network.device status '{ "name": "eth0" }'
{
    "type": "Network device",
    "up": true,
    "link": true,
    "mtu": 1500,
    "macaddr": "c6:3d:c7:90:aa:da",
    "txqueuelen": 1000,
    "statistics": {
        "collisions": 0,
        "rx_frame_errors": 0,
        "tx_compressed": 0,
        "multicast": 0,
        "rx_length_errors": 0,
        "tx_dropped": 0,
        "rx_bytes": 0,
        "rx_missed_errors": 0,
        "tx_errors": 0,
        "rx_compressed": 0,
        "rx_over_errors": 0,
        "tx_fifo_errors": 0,
        "rx_crc_errors": 0,
        "rx_packets": 0,
        "tx_heartbeat_errors": 0,
        "rx_dropped": 0,
        "tx_aborted_errors": 0,
        "tx_packets": 184546,
        "rx_errors": 0,
        "tx_bytes": 17409452,
        "tx_window_errors": 0,
        "rx_fifo_errors": 0,
        "tx_carrier_errors": 0
    }
}
root@uplink:~#

listen
设置一个监听socket并观察进入的事件:

root@uplink:~# ubus listen &
root@uplink:~# ubus call network.interface.wan down
{ "network.interface": { "action": "ifdown", "interface": "wan" } }
root@uplink:~# ubus call network.interface.wan up
{ "network.interface": { "action": "ifup", "interface": "wan" } }
{ "network.interface": { "action": "ifdown", "interface": "he" } }
{ "network.interface": { "action": "ifdown", "interface": "v6" } }
{ "network.interface": { "action": "ifup", "interface": "he" } }
{ "network.interface": { "action": "ifup", "interface": "v6" } }
root@uplink:~# 

send
发送一个事件提醒:

root@uplink:~# ubus listen &
root@uplink:~# ubus send foo '{ "bar": "baz" }'
{ "foo": { "bar": "baz" } }
root@uplink:~# 

本文章由http://www.wifidog.pro/2015/08/10/openwrt-ubus%E6%9E%B6%E6%9E%84.html整理编辑,转载请注明出处

OpenWrt Buildroot – About 编译过程

OpenWrt Buildroot is a set of Makefiles and patches that allows users to easily generate both a cross-compilation toolchain and a root filesystem for embedded systems. It is a heavily modified Buildroot. The cross-compilation toolchain uses uClibc, a tiny C standard library.

A compilation toolchain is the set of tools used to compile code for your system. It consists of:

◾ a compiler (in our case, gcc / deb: gcc)
◾ binary utils like assembler and linker (in our case, binutils / deb: binutils )
◾ a C standard library (for example GNU Libc, uClibc or dietlibc).

Using a PC, the compilation toolchain runs on an x86 processor and generates code for a x86 processor. On most Linux systems, the compilation toolchain uses the GNU libc as C standard library. It is called the "host compilation toolchain", and the machine it is running on is called the "host system". The compilation toolchain is provided by the distribution, and has nothing to do with OpenWrt Buildroot.

Embedded systems use a different processor and require a cross-compilation toolchain - a compilation toolchain that runs on a host system but that generates code for a target system (and target processor's instruction set architecture (ISA)). For example, if your host system uses x86 and your target system uses MIPS32, the regular compilation toolchain of your host runs on x86 and generates code for x86, while the cross-compilation toolchain runs on x86 and generates code for MIPS32.

While it is possible to manually configure and compile your own software, OpenWrt Buildroot automates this process to work on the instruction set architecture of most embedded systems.

While the OpenWrt Buildroot was designed for developers, inexperienced users can also use it to easily build their own custom firmware!

The OpenWrt Makefile has its own syntax, different from the conventional Makefile of Linux make tool. The OpenWrt Makefile defines the meta information of the package, where to download the package, how to compile, where to install the compiled binaries, etc. See How to Build OpenWrt Application Package for more detail.

OpenWrt Buildroot – Features
◾ Makes it easy to port software
◾ Uses kconfig (Linux Kernel menuconfig) for configuration of features
◾ Provides integrated cross-compiler toolchain (gcc, ld, …)
◾ Provides abstraction for autotools (automake, autoconf), cmake, scons
◾ Handles standard download, patch, configure, compile and packaging workflow
◾ Provides a number of common fixups for badly behaving packages

OpenWrt Buildroot – Make Targets
◾ Offers a number of high level make targets for standard package workflows
◾ Targets always in the format "component/name/action", e.g. "toolchain/gdb/compile" or "package/mtd/install"
◾ Prepare a package source tree: package/foo/prepare
◾ Compile a package: package/foo/compile
◾ Clean a package: package/foo/clean

OpenWrt Buildroot – Build sequence

  1. tools – automake, autoconf, sed, cmake
  2. toolchain/binutils – as, ld, …
  3. toolchain/gcc – gcc, g++, cpp, …
  4. target/linux – kernel modules
  5. package – core and feed packages
  6. target/linux – kernel image
  7. target/linux/image – firmware image file generation

Patch management
◾ Many packages will not work as-is and need patches to work on the target or to even compile
◾ OpenWrt Buildroot integrates quilt for easy patch management
◾ Turn package patches into quilt series: make package/foo/prepare QUILT=1
◾ Update patches from modified series: make package/foo/update
◾ Automatically rebase patches after an update: make package/foo/refresh

Packaging considerations
◾ Main objective is small memory and size footprint
◾ Features that make no sense on embedded systems get disabled through configure or are patched out
◾ Packages must be compilable regardless of the host system, should be self contained
◾ Shipped "configure" scripts are often faulty or unusable in a cross-compile setting, autoreconf or patching needed
◾ Build variants and kconfig includes allow for configurable compile-time settings
◾ There is no standard way for porting software, in many cases it "just works" but often the package build process needs tweaks

本文章由http://www.wifidog.pro/2015/08/07/openwrt-%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B.html整理编辑,转载请注明出处