1.在编译openwrt之前,需要先安装依赖包,命令如下:

sudo apt-get install autoconf binutils bison bzip2 flex gawk gettext make libncurses5-dev patch unzip wget zlib1g curl libcurl3 openssl libssl-dev perl libperl-dev cpio libexpat1 gettext openssh-client subversion cmake libtool

2.然后新建用户

#adduser openwrt

3.再开始编译openwrt源码生成镜像文件

3.1.首先下载openwrt源码,
下载地址:https://github.com/openwrt/openwrt/tree/chaos_calmer或者使用git工具(要先装git工具),输入下列命令:

git clone git://git.openwrt.org/openwrt.git

参考:https://dev.openwrt.org/wiki/GetSource

这是我的openwrt的源码:https://github.com/qinfupingqq/openwrt

3.2.然后解压下载的文件

解压zip格式的压缩文件的话使用:

unzip openwrt***.zip

如果使用git联网下载就不需要解压。
3.3.然后进入openwrt文件夹

#cd openwrt

3.4.添加软件扩展包

./script/feeds update -a #更新所有附加的包(如:luci 等等)
./script/feeds install -a #安装所有更新的包

3.5.接着,测试编译环境

#make defconfig

3.6.进入定制界面,配置编译选项

make menuconfig #设置编译配置

(其中有的选项位置和图片不一样,应该是版本不同,耐心找找就能找到位置,主要在"base system" "kernel modules" "luci" "network" "utilities")

上面的参考是树莓派的配置且用来制作无线路由的,自己的需要根据自身需要选择。

这里需要注意
1."target system"要根据自己板子的实际来选择,否则编译出来的系统放到开发板上也是跑不起来的,如果"target system"中没有完全匹配的可以,在"subtarget"中选择,然后在"target profile"中选择开发板名称
例如,我的是树莓派3,就应该在"target system"选择"Broadcom BCM2708",在"subtarget"中选择"Broadcom BCM2710",在"target profile"中选择"raspberrypi3 B"
2.其他配置中,下面列出部分自己找到的说明:
我配置的选项是:(后面跟default表示,没有修改,选择默认的选项)

target system-----------------------------Broadcom BCM2708
  subtarget---------------------------------Broadcom BCM2710
  target profile----------------------------raspberrypi3 B(default)
  Global build settings---------------------compile with support for patented functionality(default)
					    enable shadow password support
					    enable support for prink
					    support for paging of anonymous memory
					    compile the kernel with debug filesystem enabled
					    ...
  base system-------------------------------wireless tools
  Luci:
    Applications----------------------------luci-app-samba
  kernel module
    block device------------------------------kmod-block2mtd(MTD:memory technology device内存技术设备)
    Cryptographic API modules-----------------kmod-crypto-ccm
					      kmod-crypto-cmac
    file systems------------------------------ext4
					      kmod-fs-ntfs
					      kmod-fs-vfat
    USB Supprot-------------------------------kmod-usb-ohci
					      kmod-usb-uhci
					      kmod-usb-storage-extras
					      kmod-usb2
					      mount-utils
					      ntfs-3g
					      block-mount
    video support-----------------------------kmod-video-core
					      kmod-video-uvc
    Wireless Drivers--------------------------kmod-ath
					      kmod-ath9k-common
					      kmod-ath9k-htc
					      kmod-brcmfmac
  network-------------------------------------netperf
					      wpa-supplicant
    SSH---------------------------------------openssh-sftp-server
  utilities-----------------------------------usbstils
    disc--------------------------------------fdisk
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

关于其中的配置的部分说明如下

Base system—> <*> block-mount (用于自动挂载的工具)
  Kernel modules—> Filesystems—>  <*> kmod-fs-ext4 (ext4分区格式支持组件)
  Kernel modules—> Native Language Support—>  <*> kmod-nls-utf8 (编码格式)
  Kernel modules—> Sound Support—>  <*> kmod-sound-core (声卡)
  Kernel modules—> Sound Support—>  <*> kmod-usb-audio (声卡)
  Kernel modules—> USB Support—>  <*> kmod-usb-cm109 (声卡)
  Kernel modules—> USB Support—>  <*> kmod-usb-serial (串口)
  Kernel modules—> USB Support—>  <*> kmod-usb-serial-ch341 (ch341)
  Kernel modules—> USB Support—>  <*> kmod-usb-serial-cp210x (cp210x)
  Kernel modules—> USB Support—>  <*> kmod-usb-serial-ftdi (ftdi)
  Kernel modules—> USB Support—>  <*> kmod-usb-storage (u盘)
  Kernel modules—> USB Support—>  <*> kmod-usb-storage-extras (u盘)
  Kernel modules—> Video Support—>  <*> kmod-video-core (视频)
  Kernel modules—> Video Support—>  <*> kmod-video-uvc (视频)
  Kernel modules—> Video Support—>  <*> kmod-video-videobuf2 (视频)
  Libraries—> <*> alsa-lib (视频)
  LuCI设置界面,其中Applications
  luci-app-ahcp..................................... LuCI Support for AHCPd
  luci-app-ddns..........动态域名.......... Dynamic DNS configuration module
  luci-app-diag-core......................... LuCI Diagnostics Tools (Core)
  luci-app-diag-devinfo....磁盘信息.... LuCI Diagnostics Tools (Device Info)
  luci-app-epg-oscam.......卫星电视.......... epg-oscam configuration module
  luci-app-filebrowser......文件浏览............... File browser tool module
  luci-app-firewall.......防火墙.... Firmware and Portforwarding application
  luci-app-freifunk-policyrouting.......... Policy routing for mesh traffic 
  luci-app-guide...........应用向导.............. guide configuration module
  luci-app-hd-idle.........磁盘休眠......... Hard Disk Idle Spin-Down module
  luci-app-ipid.................................. ipid configuration module 
  luci-app-lqtapifoss.......................................... Lantiq voip 
  luci-app-mentohust.......校园认证................. mentohust confi
  luci-app-meshwizard...... Shellscript based wizard to setup mesh networks 
  luci-app-mldonkey......mldonkey下载服务..... mldonkey configuration module
  luci-app-mmc-over-gpio................ MMC-over-GPIO configuration module 
  luci-app-multiwan............ LuCI Support for the OpenWrt MultiWAN agent 
  luci-app-n2n.................................... n2n configuration module 
  luci-app-nfsd........................ Network Shares - NFS SERVERS module 
  luci-app-ntpc.............. NTP time synchronisation configuration module 
  luci-app-nwan..............多拨服务      ....... nwan configuration module
  luci-app-openvpn................................ LuCI Support for OpenVPN 
  luci-app-p2pblock.......... LuCI Support for the Freifunk P2P-Block addon
  luci-app-p910nd........打印服务............ p910nd - Printer server module
  luci-app-polipo........................ LuCI Support for the Polipo Proxy 
  luci-app-pppoeserver.................... pppoeserver configuration module
  luci-app-pptpd................................ pptpd configuration module
  luci-app-qos..................... Quality of Service configuration module 
  luci-app-qosv4........QOS平衡服务.............. qosv4 configuration module
  luci-app-radvd.................................... LuCI Support for Radvd 
  luci-app-samba.........共享服务.... Network Shares - Samba SMB/CIFS module
  luci-app-siitwizard............. SIIT IPv4-over-IPv6 configuration wizard 
  luci-app-splash......................... Freifunk DHCP-Splash application 
  luci-app-statistics.......................... LuCI Statistics Application 
  luci-app-tinyproxy............... Tinyproxy - HTTP(S)-Proxy configuration 
  luci-app-transmission.......BT下载器.... transmission configuration module
  luci-app-upnp................. Universal Plug & Play configuration module 
  luci-app-ushare.....流媒体服务....... uShare - UPnP A/V & DLNA Media Server
  luci-app-vnstat.................................. LuCI Support for VnStat   
  luci-app-voice-core........................... LuCI Voice Software (Core) 
  luci-app-vsftpd..........FTP服务.............. vsftpd configuration module
  luci-app-webcam.............................. webcam configuration module 
  luci-app-webshell.......WEB命令控制台........ webshell configuration module
  luci-app-wol..........网络唤醒............... LuCI Support for Wake-on-LAN
  luci-app-wshaper................................ LuCI Support for wshaper
  Multimedia—> <*> mjpg-streamer (视频)
  Utilities—> Filesystems—>  <*> badblocks (u盘)
  Utilities—>  <*> alsa-utils-tests (声卡)
  luci-i18n-chinese //luci中文支持
  luci-app-ushare    //媒体服务
  luci-app-ddns    //ddns
  luci-app-qos    //qos
  luci-app-hd-idle //usb硬盘自动休眠
  luci-app-samba    //samba
  luci-app-upnp    //upnp端口映射
  ushare        //媒体服务
  usbutils    //usb工具如lsusb
  nfs-kernel-server    //NFS文件服务器
  fdisk
  e2fsprogs //分区管理工具
  kmod-usb-storage    //USB存储设备支持
  kmod-fs-ntfs    //ntfs,只读
  kmod-fs-vfat    //fat,fat32文件系统支持
  ntfs-3g        //可读写NTFS支持
  kmod-fs-ext4    //ext2,ext3,ext4
  kmod-nls-utf8    //文件系统语言支持
  kmod-fs-nfs-common
  kmod-fs-nfs    //NFS
  //摄像头
  kmod-video-core        //视频支持
  kmod-video-gspca-core
  kmod-video-uvc    //免驱摄像头
  kmod-i2c-core
  kmod-input-core
  motion        //监控软件
  block-mount    //自动挂载,可在luci的系统->挂载点查看相关信息

network中wpa-supplicant和wpa-psk的区别
WPA-PSK是指Pre-Shared Key,就是用户已经有了一个Key,通过这个Key来进行WPA认证,不需要认证服务器。如果不是PSK的话,需要认证服务器来产生Key。
WPA和WPA-PSK采用了相同的加密机制,其区别仅在于WPA-PSK的认证机制只有简单的一般密码,而非针对用户特定的身份认证。虽然这种一般密码式的方法存在被暴力破解的可能性,但是,与WEP相比,无线网络的安全性仍然大大强化。
从技术角度看,WPA主要解决了WEP在共享密钥上的漏洞,添加了用户级的认证措施。Wi-Fi联盟给出的WPA定义即为:
  WPA = 802.1X + EAP + TKIP + MIC
WPA只是在802.11i正式推出之前的Wi-Fi企业联盟的安全标准,802.11i,该标准通过使用CCM (Counter-Mode/CBC-MAC)认证方式和AES(Advanced Encryption Standard)加密算法,更进一步加强了无线局域网的安全和对用户信息的保护。

以上是配置中的选项的部分解释,备注一下。
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
3.7.最后是编译的过程,如下:

注意:编译的时候尽量使用root用户,否则,可能出现没有创建某个文件的权限,没有删除某个文件的权限等问题,此时,即便使用chmod改变所有文件的权限也行不通,因为,里面部分文件是临时创建的,创建出来的文件可能会没有权限。

make download #提前下载需要编译软件的源代码(可以跳过)
make V=99 #V=99表示显示编译详情(便于调试编译错误)
#等待编译完成,编译完成后所有的输出镜像和ipk包会在openwrt/bin目录下。
注意:部分安装包可能会很难下载,可以FQ下载下来,然后放到openwrt/dl目录下边。

下面给出地址,里面有我安装时需要的安装包:

https://github.com/qinfupingqq/dl

下面是我编译出来的镜像(我的是树莓派3):

链接:http://pan.baidu.com/s/1jI21Ll0 密码:nbwc

编译过程有几个问题需要注意
1.you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)
解决方法
输入命令:export FORCE_UNSAFE_CONFIGURE=1
2.编译的时候尽量使用root用户,否则,可能出现没有创建某个文件的权限,没有删除某个文件的权限等问题,此时,即便使用chmod改变所有文件的权限也行不通,因为,里面部分文件是临时创建的,创建出来的文件可能会没有权限。
3.编译到后面,会有一个选择是否安装Broadcom BCM2835 camera interface driver的选项,此时要选N,如果选择Y,就会遇到

config.status: error: cannot find input file: `po/Makefile.in.in'
Makefile:198: recipe for target '/home/openwrt/openwrt/build_dir/target-arm_arm1176jzf-s+vfp_musl-1.1.16_eabi/transmission-mbedtls/transmission-2.92+git/.configured_yyyyyyyy' failed
make[3]: *** [/home/openwrt/openwrt/build_dir/target-arm_arm1176jzf-s+vfp_musl-1.1.16_eabi/transmission-mbedtls/transmission-2.92+git/.configured_yyyyyyyy] Error 1
make[3]: Leaving directory '/home/openwrt/openwrt/feeds/packages/net/transmission'
package/Makefile:196: recipe for target 'package/feeds/packages/transmission/compile' failed
make[2]: *** [package/feeds/packages/transmission/compile] Error 2
make[2]: Leaving directory '/home/openwrt/openwrt'
package/Makefile:193: recipe for target '/home/openwrt/openwrt/staging_dir/target-arm_arm1176jzf-s+vfp_musl-1.1.16_eabi/stamp/.package_compile' failed
make[1]: *** [/home/openwrt/openwrt/staging_dir/target-arm_arm1176jzf-s+vfp_musl-1.1.16_eabi/stamp/.package_compile] Error 2
make[1]: Leaving directory '/home/openwrt/openwrt'
/home/openwrt/openwrt/include/toplevel.mk:192: recipe for target 'world' failed
make: *** [world] Error 2

等问题,配置失败导致缺乏文件。
4.如果编译成功,也生成了镜像,但是写到板子上却无法启动,就要检查前面选择树莓派时的配置("target system")是否正确。
5.如果烧写到树莓派启动发现系统是read-only filesystem时,就要检查启动时,文件系统有没有问题,可能是镜像损坏了,重新写一个系统就能解决。

启动openwrt

整个编译过程结束后,可以在bin/brcm2708的目录下找到编译成功的镜像,使用工具烧写到SD卡上,然后插入到树莓派上通电就可以启动了。

参考博客:

https://wiki.openwrt.org/toh/raspberry_pi_foundation/raspberry_pi#boot_log_raspberry_pi_3