编译openwrt全过程(超详细)

来源:互联网 发布:怎么关闭百度推荐 知乎 编辑:程序博客网 时间:2024/06/05 13:37

Openwrt 官方正式的发行版是已编译好了的映像文件(后缀名bin或trx、trx2),此映像文件可从Openwrt官方网站的下载页面中轻松获取到
为了参与测试或参与开发OpenWrt的工作;我们需要重新移植或者重新编译个一个openwrt,若想实现上述目的,其实很简单,按下述文字即可成功编译出一个您的Openwrt来。
准备工作
在开始编译Openwrt之前需要您做些准备工作;与其他编译过程一样,类似的编译工具和编译环境是必不可少的:
一个构建并编译OpenWrt映像的系统,简单说就是准备一个Linux或者类Unix操作系统(比如Ubuntu、Debian,MAC OS,FreeBSD等);
确保安装了所需的依赖关系库, (在debian系统中就是安装各种需要的软件包)
获取到OpenWrt源代码副本
首先, 开机登陆到开发机的操作系统(废话了)。不管是独立安装的或者是运行在虚拟机上的(Vmware 或者 Qemu),推荐使用Linux系统。
在 *BSD 或者是 Max OSX 系统上编译Openwrt也可行,但并不能保证可以成功编译。下文假定您使用的是Ubuntu操作系统可以到http://www.ubuntu.com/download/ubuntu/download下载最新版。

1,安装Ubuntu,安装过程就不说了,图形化安装,而且是中文版,连安装都搞不定的,可以关闭本页面了。可以安装在虚拟机里,也可以安装在物理机了,官方建议有条件的最好安装在物理机上,毕竟在虚拟机中编译Openwrt会产生无法预料的问题,所以建议您还是使用物理主机来编译以减少不必要的麻烦。
2,安装好后,在左面左边找到“系统设置”“屏幕”选项把“定时关闭”改成“从不”把锁定“关闭”在左上角“主面板”菜单的“搜索”中输入“te”找到“终端”拖至桌面,省去以后再找的过程,这个窗口是没有“应用”或“确定”之类的,直接关闭它就会保存。用惯WINDOWS的用户注意了。为什么要做这步呢? 因为整个编译过程中有些步骤要等一段时间的,老是自动启用屏幕保护程序,然后还要重新输密码才能退出,也是麻烦事。

3,打开桌面上的“终端”进入命令模式然后输入sudo –sH,注意大小写,然后输入装系统时设置的用户密码,就能切换到root(提示:输入密码的时候不会有*之类的显示的,是完全不显示,部分人不必惊慌)。切换到root是为了安装或升级部分必要的组件,有些组件需要root权限。
   注:如果你使用此方法切换root权限失败(提示sudo:-sh command no found)你使用一下命令
sudo passwd root

******(输入原先的root口令)

******(输入新的root口令,可以保持原样。我就保持的原样)

******(再次输入新的root口令)

su root
就切换到root权限了
4、切到root后,顺序输入:
apt-get install g++
apt-get install libncurses5-dev
apt-get install zlib1g-dev
apt-get install bison
apt-get install flex
apt-get install unzip
apt-get install autoconf
apt-get install gawk

apt-get install make
apt-get install gettext
apt-get install gcc
apt-get install binutils
apt-get install patch
apt-get install bzip2
apt-get install libz-dev
apt-get install asciidoc
apt-get install subversion

    每一行回车后都会马上检测安装或升级那个组件,官方WIKI用的是单一条命令,我分开了,因为这样更容易查看每一个组件的安装是否成功。一行命令的话,一大堆东西连续出来,懒得拖滚动条回头看了。一个个分开执行方便看一些。个人喜好了。当中最后面的asciidoc组件是需要下载400多MB东西的,慢慢等了。下载东西之前会有提示本次要下载多少M的东西,问是否的,按y然后回车让它下载就行了。提示:假如它提示找不到软件包的话,那麻烦你再看清楚命令中的软件名名称,因为有时是字母中间冒个数字1出来之类的,很容易错看成字母L。
5、上述安装完成后,这一步很重要,就是执行exit(小写)命令退出root。如果不退出root的话,那后面会出现晕人的情况,下一步是下载源码,以root身份下载源码后,在后续操作中有些会提示不能以root身份操作,但源码已经以root身份下载了,所有文件的“所有者”都是root,退出root后再操作,又会提示没有权限,因为文件是root的,普通用户无法访问,结果只能用命令修改文件所有者,比较麻烦。所以这里就要退出root,以普通用户身份执行下面的下载源码命令。其实一开始安装组件的时候可以在命令前加上sudo来达到临时用root身份,执行完后又回到普通权限的目的,但对于新手来说,不必过于追求这些了,有个可行的方法就凑合着先用吧。
6 ,关于Openwrt的源代码下载,途径有二,一是通过 subversion ,一是通过 git,建议使用 subversion(SVN) ,因为Openwrt主要以 svn 来维护Openwrt系统的版本。另外,请注意Openwrt中不同的分支版本,一种是最新版但不是最稳定,也就是trunk版,一种是相对稳定版,backfire版。
    下述代码即举例说明了通过svn从brandkfire获得backfire源代码(此版本意思是从trunk分支的补丁也在backfire版本中了,即包含修复补丁):
mkdir OpenWrt/
cd OpenWrt/
svn co svn://svn.openwrt.org/openwrt/branches/backfire(回车后等待下载)

注解: 上述svn命令将在当前目录创建一个 OpenWrt/backfire/ 子目录,此目录包含此命令获取到的源代码。您也可以通过下述命令,下载不含修复补丁的backfire的原版源码:
svn co svn://svn.openwrt.org/openwrt/tags/backfire_10.03

使用开发版源代码
当前的开发版本分支(trunk)已包含最新的实验补丁。此分支或许还突破了Openwrt原来所不支持的硬件设备的限制哦,惊喜的同时也有风险存在。因此,编译trunk版,慎之~
mkdir OpenWrt/
cd OpenWrt/
svn co svn://svn.openwrt.org/openwrt/trunk/(回车后等待下载)

7,开始准备编译
首先跟进并更新源代码
因Openwrt的源代码随时都会变动,故此命令将确保您所获取得到的源码的最新性。

如果你用的是backfire版本的源码:
cd OpenWrt/backfire/
svn up
./scripts/feeds update -a
./scripts/feeds install -a
如果你用的是trunk版本的源码:
cd OpenWrt/trunk/
svn up
./scripts/feeds update -a
./scripts/feeds install -a
下一步是检查编译环境,若可进行编译则生成默认配置:
make defconfig
若defconfig回显提示缺少软件包或编译库等依赖,则按提示安装所缺软件包或库等即可,不难的,细心点就行。
Menuconfig
Menuconfig is a text-based tool which handles the selection of target, packages to include in the build, kernel options

etc.
make menuconfig
8,编译固件
万事具备,只欠东风,通过下面简单的make命令来编译:
make
在多核电脑中编译
具有多核CPU处理器的电脑进行编译,使用下述参数可令编译过程加速。 常规用法为 <您cpu处理器的数目 + 1> – 例如使用3进程来编译 (即双核CPU), 命令及参数如下:
make -j 3

后台编译
If you intend to use your system while building, you can have the build process only use idle I/O and CPU capacity like this (dualcore CPU):
ionice -c 3 nice -n 20 make -j 2

编译简单的基本的软件包
When developing or packaging software for OpenWrt, it is convenient to be able to build only the package in question (e.g. with package cups):
make package/cups/compile V=99

For a package in the feeds it looks like this:
make package/feeds/packages/ndyndns/compile V=99


编译错误
如果因某种不知道的原因而编译失败,下面有种简单的方法来得知编译到底错在哪里了:
make V=99 2>&1 |tee build.log |grep -i error
The above saves a full verbose copy of the build output (with stdout piped to stderr) in build.log and only shows errors on
the screen.
举例说明:
ionice -c 3 nice -n 20 make -j 2 V=99 CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 \
  |tee build.log |egrep -i '(warn|error)'
The above saves a full verbose copy of the build output (with stdout piped to stderr) in build.log and outputs only
warnings and errors while building using only background resources on a dual core CPU.
一键编译
即使用脚本来编译Openwrt固件。许多朋友编译Openwrt是用的脚本来编译的,详见: https://forum.openwrt.org/viewtopic.php?id=28267
生成的固件在哪
编译成功后所生成的固件文件位于bin目录下,可用如下命令查看:
cd bin/
ls */
Cleaning Up
You might need to clean your build environment every now and then. The following make targets are useful for that job:
Clean
Cleaning the trunk/ directory after the build process is simple and only requires 'make clean'. It deletes contents of
'bin' and 'build_dir' directories.
See CAUTION below
make clean
CAUTION: Make sure to move your compiled OpenWrt images to another location before issuing this command since it erases the contents of the "bin" directory (where the compiled OpenWrt images live).
Dirclean
If you want to clean in addition to the build directories also the cross-compile tools (in toolchain directory), use the
'dirclean' make, which does the same as 'clean' and also deletes 'staging_dir' and 'toolchain' contents. 'Dirclean' is your
basic "Full clean" operation.
See CAUTION below
make dirclean

CAUTION: Make sure to move your compiled OpenWrt images to another location before issuing this command.
Distclean
If complete cleaning of the build environment is required, then all it takes is 'make distclean'. It practically nukes
everything you have compiled or configured and also deletes all downloaded feeds contents and package sources. Making
distclean means quite a lot downloading afterwards, so it should not be used lightly

'Dirclean' is enough for most cases.
See CAUTION below
make distclean

CAUTION: In addition to all else, this will erase your build configuration (.config), your toolchain and all other sources.
Use with care!
There are numerous other functionalities in the OpenWrt build system, but the above should have covered some of the fundamentals.
Installing OpenWrt
In comparison to the actual building of the desired image(s), the installation of the OpenWrt image can sometimes be a
little, or more, of a challenge. It can be as simple as using the tools supplied by the manufacturer of the hardware (web
interface or recovery tools), over manually uploading a new firmware via tftp and rebooting, to doing extensive hardware
modifications with the intent of enabling JTAG access. Though, generally installation is made possible via the bootloader
(Redboot, Uboot etc.) or simple copying onto a CompactFlash-card – all depending on the specific device.
如果你的英语不怎么样你还是用谷歌翻译吧
make 命令简单说明
  make V=99
  V=99表示输出详细的debug信息

  make world
  表示编译所有

  make j=2 V=99
  如是多核CPU,加j=2 选项理论上能加快编译速度
  make -j 2 V=99可加快编译速度(不推荐使用)

  只清除某个模块
  make package/qos/clean

  只编译某个模块
  make package/qos/clean
  make package/qos/compile
  make package/qos/install

  make V=99 | tee compile_v1.0.0.log
  混合 tee 命令将编译信息放到compile_v1.0.0.log文件中方便查看。

x86的编译要点
 考虑到x86平台使用的特点,以及 OpenWrt 本身,可参考以下要点
 1. 编译所有文件系统支持,包括ext2/ext3/ext4,还有NTFS,甚至是LVM;
 2. 加入一个文本编辑器,例如vim或者nano,因为日常使用中需要用文本编辑器修改各种配置文件;
 3. 加入所有关于无线网卡的驱动,模块,各种支持程序,
 4. 一些其他常见应用--蓝牙、加密、PGP、SSL、SSH、VPN、USB支持、3G上网卡、
 5. 编译时要到网上下载相关源码,所以可以日后下载的包尽量不选择编译;
 6. 编译好的文件在当前目录的bin文件夹下,如果是brcm-2.4,则 ls bin/brcm-2.4/ 就可以看到很多熟悉的文件了。

参考资料
  如何给 Openwrt 提交补丁
  https://dev.openwrt.org/wiki/SubmittingPatches

  Openwrt 源代码的下载方式
  https://dev.openwrt.org/wiki/GetSource

  构造编译环境
  http://wiki.openwrt.org/doc/howto/buildroot

  构造编译环境-老的文档
  http://downloads.openwrt.org/docs/buildroot-documentation.html

  本文赖以生存的基础-官方wiki中的编译教程,我翻译了编译一文中的小部分。
  http://wiki.openwrt.org/doc/howto/build

  下文有详细的编译方面说明
  http://downloads.openwrt.org/kamikaze/docs/openwrt.html

 

 

0 0
原创粉丝点击