QuietHeart's Site

小米路由器DIY


目录

原文参考:小米路由器DIY

曾经还以为自己是一枚成长迅速的米粉,但是渐渐却发现,我不适合做为米粉。

尽管购买过红米手机、但是我现在觉得目前使用魅蓝更好用。

尽管之前购买了小米路由、小蚁摄像机、小米插排……现在也正在使用着,

但这并不表示我忠爱小米,只能说我是一个小米“受害者”,宣传的力量真是不可忽视。

我相信小米也有它自己的好处,但是真的觉得它不适合我,

在这台路由器上如此折腾,只是不得不使用已经花钱用买过的东西,很大程度上也可能是因为对Linux的兴趣。

这个文章没有贬低小米的意思,更无意与人争执,只是最开始表达一些个人的情绪,不喜者,可以略过。

分享此文出来供喜欢和不喜欢小米的朋友们参考,以及自己记录。

通过此文,可以让专业和有些专业背景的朋友们了解如下内容:

  • 如何在特定平台上进行交叉编译,实现源码跨平台运行
  • 如何在特定平台上搭建自己的云存储体系
  • 如何在特定平台上搭建 php/nginx 提供web服务
  • 如何在特定平台上搭建 mysql 数据库
  • 如何在特定平台上搭建基于 git 的管理代码仓库服务
  • 其它,待更新

这里的特定平台,小米路由器上实践,在其它 openwrt 或者 linux 系统上,应当能够类似实现。

接下来主要介绍在小米路由器上面讲述有关这些方面的问题。

1 小米路由器ssh开启

开启ssh访问,小米路由器需升级到开发版0.5.28及以上,小米路由器mini需升级到开发版0.3.84及以上。注意:稳定版不支持。

1.1 烧写开发板

1.1.2 烧写:

U盘刷机步骤:
1、准备一个系统格式为FAT或FAT32的U盘;
2、到小米路由器的官网miwifi.com下载用来进行刷机的ROM包;
重要的事情再说三遍:U盘刷机会清空硬盘上的数据!会清空硬盘上的数据!!会清空硬盘上的数据!!!
3、将下载好的ROM包放到U盘的根目录下,并命名为“miwifi.bin”;
4、对于指示灯为红色闪烁或者红色常亮的情况,直接将U盘插入路由器的USB接口,用细长的工具按住reset,待路由器指示灯变为黄灯闪烁时,松开reset;对于指示灯为其他状态的情况,需要先断开路由器的电源后,将U盘插入路由器的USB接口,用细长的工具按住reset,接通电源,待路由器指示灯变为黄灯闪烁时,松开reset
5、大约3-5分钟后,指示灯变成黄色常亮状态时可以拔掉U盘,此时刷机已经完成正在重启,稍等片刻路由器指示灯变蓝就可以正常使用了。
如果在刷机过程中,指示灯黄灯闪烁几秒后变为黄灯常亮状态,说明此次U盘刷机过程失败,请检查以上的刷机过程并重新进行刷机操作。

1.1.3 总结:

  1. 下载rom
  2. 拷贝到u盘
  3. 路由器断电、插入u盘(fat32格式)
  4. 按住reset上电
  5. 等待黄灯闪烁,松开reset
  6. 一直等待,直至蓝灯。

1.2 烧写ssh

1.2.1 网址:MiWiFi开放平台

1.2.3 烧写:

工具包使用方法:小米路由器需升级到开发版0.5.28及以上,小米路由器mini需升级到开发版0.3.84及以上。注意:稳定版不支持。
    请将下载的工具包bin文件复制到U盘(FAT/FAT32格式)的根目录下,保证文件名为miwifi_ssh.bin;
    断开小米路由器的电源,将U盘插入USB接口;
    按住reset按钮之后重新接入电源,指示灯变为黄色闪烁状态即可松开reset键;
    等待3-5秒后安装完成之后,小米路由器会自动重启,之后您就可以尽情折腾啦 :)

1.2.4 总结:

  1. 下载rom
  2. 拷贝到u盘
  3. 路由器断电、插入u盘(fat32格式)
  4. 按住reset上电
  5. 等待黄灯闪烁,松开reset
  6. 一直等待,直至蓝灯。

1.3 访问文件系统权限

开启ssh后无法获得系统文件夹写入权限,只能读写外接硬盘的内容。这对有时候系统级别的配置不太方便。

这主要是因为挂载的时候,以只读方式挂载了相应的目录,执行下述命令即可:

$mount -o remount -rw /

1.4 免密ssh

实质将本地机器的 ~/.ssh/id_rsa.pub 内容拷贝追加到小米路由器中的: /etc/dropbear/authorized_keys.

小米路由器的ssh是dropbear,配置路径不是正常机器下的 .ssh/, 而是 /etc/dropbear

你通过 ssh-copy-id xiaomi, 只是将本地的public key拷贝到小米路由器上的 /root/.ssh/authorized_keys, 这样是不行的,需要将其转移到 /etc/dropbear 中。

1.5 ssh相关配置

修改文件: /etc/config/dropbear, 比如我将ssh默认端口改成:10022,如下:

root@XiaoQiang:/etc/config# cat dropbear 
config dropbear
        option PasswordAuth 'on'
        option RootPasswordAuth 'on'
#       option Port         '22'
        option Port         '10022'
        option GatewayPorts 'on'
        #option BannerFile   '/etc/banner'

然后再设置防火墙,就能导致外网使用 10022 访问小米路由器ssh了: /etc/config/firewall 添加类似如下:

config rule 'mywanssh'
	option src 'wan'
	option proto 'tcp'
	option target 'ACCEPT'
	option dest_port '10022'
	option name ''\''ssh wan accept tcp port 22001'\'''

2 小米路由器防火墙配置

2.1 开启端口

使用WinSCP软件进入192.168.31.1 端口22,用户名 root 密码 admin 文件协议改为SCP进入,进入后打开/etc/config/firewall
这个文件的尾部增加:

    config rule 'httpdwan'      
    	option src 'wan'     
    	option dest_port '8088'
    	option proto 'tcp'
    	option target 'ACCEPT'
    	option name ''\''httpd wan accept tcp port 8088'\'''

保存后,SSH到路由的22端口执行 /etc/init.d/firewall restart 重启防火墙。

参考:小米路由器防火墙开启端口,让外部网络访问你的小米路由

2.2 端口转发

修改 /etc/config/firewall

# port redirect of remapped ssh port (22001) on wan
config redirect 'myssh'
option src wan
option src_dport 10022
option dest lan
option dest_port 22
option proto tcp

重启防火墙 /etc/init.d/firewall restart

参考 小米路由器mini如何设置外网访问ssh

2.3 禁用端口

默认端口不对外开放。

3 使用opkg在小米路由器软件管理

小米路由器系统是openwrt系统,该系统基于linux,使用 opkg 工具实现软件管理,该工具类似ubuntu中的 apt-get 或者centeros 中的 yum 工具。

目前的现状其实是:找不到适合xiaomi路由器架构的合适的软件源,通过openwrt的所有软件源中相应交叉工具链的信息,发现很难找到和小米路由器相符的软件。这样的结果是,即使使用某一个软件源,强行配置将对应软件安装到小米路由器中,也无法运行。 最稳妥的方式还是使用小米路由器提供的交叉编译工具链,通过源代码交叉编译要使用的软件包,这需要一些专业技术(例如如何通过autotools 配置交叉编译选项等等), 所以对于小米路由器,普通用户其实也很难自由的安装所需软件。下面给出的信息,也算为遇到相关问题的同仁提供一个参考。

使用opkg管理软件的大致方式如下

3.1 配置软件源地址

配置文件为 /etc/opkg.conf, 下面是一个配置文件完整的例子:

#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm4709/R2D/packages
#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm63xx/generic/packages
src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages
#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/omap4/generic/packages
dest root /data
dest ram /tmp
lists_dir ext /data/var/opkg-lists
option overlay_root /data

arch all 1
arch noarch 1
arch brcm4709 10
arch brcm47xx 100
arch brcm53xx 110
arch brcm63xx 120
arch omap4 130

这里的含义不言而喻,最关键的还是最开始列出的地址信息,指明了软件源中所包括的软件从哪里下载。这里为 http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages 。其它为安装时的路径和架构信息,用到之时自然会明白。

3.2 用opkg在小米路由器(openwrt)上进行软件管理

opkg 使用方法类似 ubuntu下的 apt-get, 具体如下:

  • 更新软件源 当我们修改软件元配置文件 /etc/opkg.conf 中的软件源信息之后,需要更新本地软件源索引,才能继续后面的使用。命令如下:

    #opkg update
    
  • 查看当前系统支持安装的架构

    $opkg print-architecture
    

    只有符合这里列出的架构的软件包,才能被安装。

  • 直接下载 *.ipk 安装 我们可以直接下载相应的 *.ipk 文件进行安装, 例如安装screen

    $wget -c http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages/screen_4.0.3-3_brcm47xx.ipk
    #opkg install screen_4.2.1-2_ralink.ipk
    

    这里, ipk 文件的地址,通过软件源配置文件可以猜到。

  • 通过软件源配置文件中的软件源库进行安装 配置好软件源库,并且更新后,不用将 .ipk 文件下载,直接可以如下安装:

    #opkg install libgcc
    #opkg install ./libc_0.9.33.2-1_brcm47xx.ipk 
    #opkg install ./screen_4.0.3-3_brcm47xx.ipk
    

    安装之后,工具会在 /data/usr/ 相关的子目录中。

  • 查找软件源库中是否存在某软件 假设我们查找是否存在包含 screen 字样的软件,可如下:

    #opkg find screen
    
  • 查看软件源库中所有的软件

    #opkg list
    
  • 查看已经安装的软件

    #opkg list-installed
    
  • 卸载

    #opkg remove --autoremove screen
    #opkg remove --autoremove --force-removal-of-essential-packages libc
    #opkg remove --autoremove --force-removal-of-essential-packages libgcc
    

    这里对screen这样的普通程序,卸载之时可以加上 --autoremove 同时卸载掉无用的依赖。而对于 libc 这样的特殊库,需要再加额外选项才能强制卸载。

  • 更多卸载的命令可以查看帮助信息:

    $opkg
    

3.3 软件管理中遇到的问题

3.3.1 找不到软件,则需更换软件源

我们想安装某软件之前,只需找到包含该软件的相应软件源地址后,填入 opkg.conf 中即可,例如你可能会找到如下的源:

#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm4709/R2D/packages
#src/gz attitude_adjustment http://downloads.openwrt.org.cn/PandoraBox/ralink/packages/packages
#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages
#src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm63xx/generic/packages
src/gz attitude_adjustment http://downloads.openwrt.org/snapshots/trunk/bcm53xx/generic/packages/packages

使用其中一个即可。软件源中实际的软件包文件都是*.ipk格式,是事先用交叉编译方式编译好的二进制程序。

3.3.2 无法安装,则需配置架构信息

如果无法安装,可能是由于架构不匹配导致,需要通过命令 opkg print-architecture 查看当前系统支持的架构,再根据被安装的安装包名称,确定需支持的架构。

如果当前系统不支持被安装软件包所需支持的架构,那么则修改/etc/opkg.conf 添加类似如下:

arch all 100
arch brcm4716 200
arch brcm47xx 300
arch bcm53xx 310

这里安装的软件包为 screen_4.0.3-3_brcm47xx.ipk, 通过名字可知其架构为 brcm47xx; 而添加的 arch brcm47xx 与该软件包的架构匹配。 添加之后,所以即可安装成功。

注意: 安装成功不代表可以成功运行,参见后面。

修改 /etc/opkg.conf 的操作会修改 opkg print-architecture 默认配置。根据列出的结果,依次按照优先级匹配相应的内容。

3.3.3 安装成功,无法运行,需要更换二进制文件,使之支持当前路由器 cpu 架构

前面配置好架构之后虽然能够安装软件,但是不一定能够运行。这里仍以 screen 为例,如果仍然无法运行 screen 。则需要确认一下其二进制文件中的cpu架构信息。

  1. 将安装好的二进制文件 screen, 以及小米路由器上可运行的原生的 busybox 二进制文件拷贝到linux上,用 file 查看其架构是否一样。 这里如下,

    screen 中的架构信息为:

    $file screen
    screen: ELF 32-bit LSB executable, MIPS, MIPS32 version 1, dynamically linked (uses shared libs), corrupted section header size
    

    busybox 中的架构信息为:

    busybox: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
    

    可见 screen 无法运行,是由于实际cpu架构不一致问题(注意,这里的架构不是前面配置的架构,而是运行所需的实际架构),导致无法运行。

  2. 解决方法有二:
    • 直接用小米官网上提供的小米路由器对应的交叉编译工具链,结合相应软件(screen)源代码,编译出一个可执行的程序。
    • 在openwrt相关软件源网站中,寻找能够和当前主机架构一致(或最接近)的架构的软件源。
  1. 寻找匹配架构的软件源 这里不讲交叉编译的方法(事实上交叉编译的方法各个软件各不相同)。讲述当软件无法运行之时,如何找到可以运行的软件。
    • 首先,需要确定当前小米路由器上的 busybox 支持的cpu架构。 如前所述,通过 file 命令,可以看到是 ARM, version 1 (SYSV),... 。只有二进制文件中的信息和此相一致之时,才可能运行得起来。
    • 其次,确认配置文件中,软件源中软件所支持的cpu架构。 当然,我们可以先配置好架构信息,将软件安装上,然后类似前面,通过 file 命令查看相应的软件程序所支持的架构。但是这样做,太麻烦了,每检查一个软件源,就需要安装该源的软件,并且查看。其实有更简单的方法。

      我们前面配置中使用的是如下软件源: src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages, 所以到 http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/ 中,看到如下类似的链接:

      Parent directory/	-	-
      packages/	-	09-Jul-2014 08:05
      OpenWrt-ImageBuilder-brcm47xx-for-linux-i486.tar.bz2	355705874	03-Apr-2013 08:39
      OpenWrt-SDK-brcm47xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2	629822123	03-Apr-2013 08:39
      OpenWrt-Toolchain-brcm47xx-for-mipsel-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2
      

      这里,注意 xxxToolchainxxx, 由此可知,之前软件源配置中指定的软件源是mipsel相关的架构(这也和前面我们通过file命令中看到的结果一致),和我们的arm不一致。

      用类似的方法可以找到合适的架构,比如我们看到 http://downloads.openwrt.org/attitude_adjustment/12.09/omap4/generic/ 中的信息:

      OpenWrt-Toolchain-omap4-for-arm_v7-a-gcc-4.6-linaro_uClibc-0.9.33.2_eabi.tar.bz2
      

      发现这个软件源采用的arm架构,大致上和我们小米路由器上显示的 busybox 的架构更为接近,所以这个软件源中的软件可能会在小米路由器上运行起来。当然这里只是一个例子,还需实践一下(重新配置软件源、添加omap4的架构配置、更新软件源、安装软件、运行),才能确定。

4 小米路由器交叉编译环境配置

配置好小米交叉编译环境,所有的软件基本上从源码级别上原生即可编译生成,无需上网寻找各种人家做好的软件包。是最理想安装软件的方法。

  1. 下载

    首先到官网网站: 小米开放平台 下载交叉编译工具链。根据自己的路由器型号选择其中的 "插件开发文档" 链接。

    $mkdir -p /home/miracle/work/1_inbox/xiaomirouter/test
    $cd /home/miracle/work/1_inbox/xiaomirouter/test
    $wget -c http://bigota.miwifi.com/xiaoqiang/sdk/tools/package/sdk_package.zip
    
  2. 设置交叉编译环境

    $unzip sdk_package.zip
    $export PATH=/home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/bin:$PATH
    

    这里设置交叉编译环境是为了让源代码通过该交叉编译工具进行编译,这样生成的可执行文件才符合小米路由器cpu的架构,能够在小米路由器上执行。而pc上直接编译生成的可执行文件只能在pc上执行(因为pc和小米路由器的cpu架构不同)。

之后的内容,全部基于该交叉编译工具链进行。从源代码配置、编译、安装生成可运行的软件可执行文件,许多开源软件,基本都是利用GNU Auto tools工具进行,遵循 configure, make, make install 三部曲。

5 小米路由器上交叉编译一些常见库和环境

这里列出一些交叉编译时,需要的常见库,后面交叉编译一些工具时,可能会用到这里的内容。

5.1 交叉编译安装zlib

$wget -c http://zlib.net/zlib-1.2.8.tar.gz
$tar -xzvf zlib-1.2.8.tar.gz
$cd zlib-1.2.8
$export CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$./configure  -static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop 
$make 
$make install

5.2 交叉编译openssl

5.2.1 交叉编译php时, openssl 的编译过程

$wget -c http://www.openssl.org/source/openssl-1.0.2g.tar.gz
$tar -xzvf openssl-1.0.2g.tar.gz 
$cd openssl-1.0.2g/
$./Configure shared no-asm --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop os/compiler:arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install

5.2.2 其他编译 openssl 方式供参考

$wget -c http://www.openssl.org/source/openssl-1.0.2g.tar.gz
$tar -xzvf openssl-1.0.2g.tar.gz
$export CROSS_COMPILE=arm-xiaomi-linux-uclibcgnueabi-
$cd openssl-1.0.2g
./config no-asm shared --prefix=$CUSTOM_PREFIX/openssl-1.0.2g
$vim Makefile
...
#SHARED_LDFLAGS=-m64
...
#CC= gcc
CC= arm-xiaomi-linux-uclibcgnueabi-gcc
#CFLAG= -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -O3 -Wall
CFLAG= -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DOPENSSL_NO_KRB5 -DL_ENDIAN -O3 -Wall
DEPFLAG= -DOPENSSL_NO_EC_NISTP_64_GCC_128 -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_LIBUNBOUND -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SCTP -DOPENSSL_NO_SSL_TRACE -DOPENSSL_NO_SSL2 -DOPENSSL_NO_STORE -DOPENSSL_NO_UNIT_TEST -DOPENSSL_NO_WEAK_SSL_CIPHERS
PEX_LIBS= 
EX_LIBS= -ldl
EXE_EXT= 
ARFLAGS= 
#AR= ar $(ARFLAGS) r
AR= arm-xiaomi-linux-uclibcgnueabi-ar $(ARFLAGS) r
#RANLIB= /usr/bin/ranlib
RANLIB= arm-xiaomi-linux-uclibcgnueabi-ranlib
#NM= nm
NM= arm-xiaomi-linux-uclibcgnueabi-nm
PERL= /usr/bin/perl
TAR= tar
TARFLAGS= --no-recursion
MAKEDEPPROG= gcc
LIBDIR=lib
...
$make depend
$make
$make install

http://blog.csdn.net/zy799894671/article/details/22289039

5.3 交叉编译适用于小米路由的go环境

5.3.1 下载

因为交叉编译,需要用go本身来编译go,所以要有一个运行在PC上的二进制go程序,和待编译的go源码

$wget -c https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz
$wget -c https://storage.googleapis.com/golang/go1.8.1.src.tar.gz
$tar -xzvf go1.8.1.linux-amd64.tar.gz
$mv go /home/miracle/myinstalled
$tar -xzvf go1.8.1.src.tar.gz

5.3.2 配置环境

注:由于小米路由的CPU BCM4709不支持硬件浮点数,所以如果需要使用GO来编译GO程序,需要将GOARM 设置为5(默认为6)

交叉编译路径具体配置这里省略,具体参考:小米路由器交叉编译环境配置

下面是配置过程

$export PATH=/home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/bin:$PATH

$export GOROOT=/home/miracle/work/1_inbox/xiaomirouter/test/go
$export GOOS=linux
$export GOARCH=arm
$export GOARM=5
$CC_FOR_TARGET=arm-xiaomi-linux-uclibcgnueabi-gcc
$CXX_FOR_TARGET=arm-xiaomi-linux-uclibcgnueabi-g++
$export GOROOT_BOOTSTRAP=/home/miracle/myinstalled/go

5.3.3 编译

$cd go/src
$./make.bash

5.3.4 部署

编译之后, go/bin/linux_arm 里面的内容即为需要的内容。

$cd go/bin
$tar -czvf linux_arm.tgz linux_arm
scp linux_arm.tgz xiaomi:xxx
$scp linux_arm.tgz  xiaomi:/userdisk/data/myinstalled

5.3.5 测试

小米路由器上:

$tar -xzvf linux_arm.tgz
$mkdir go
$mv linux_arm go/bin
$mkdir gopackage
$export GOROOT=/userdisk/data/myinstalled/go
$export GOPATH=//userdisk/data/myinstalled/gopackage
$export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
$go version
go version go1.8.1 linux/arm

6 在小米路由器上搭建基于LNMP的Owncloud云平台

Mega Sync、Box、DropBox、苹果icloud、微软Sky Driver、Google的GDriver、腾讯微云、百度云、新浪微盘、坚果云、金山快盘、七牛、115网盘、华为网盘、Ubuntu One、

这里介绍如何在小米路由器上搭建自己的个人云存储系统。

在此之前,需要注意云存储和云备份是有区别的。在参考英文原文: Cloud services: How do they compare?(译者/刘旭坤) 中提到:

  1. 云存储的目的是方便地在不同设备之间共享文件,并不意味着一定会在电脑或手机上创建备份。如果发生误删,可能文件永远也找不回来了。
  2. 云备份则不同,如果本地的文件删除了在云端还有。

这里主要侧重的是云存储功能的建立。

如果单纯是为了云存储功能的搭建,采用许多可用的服务即可,例如:ftp、webdav等等。这里采用云平台,是考虑到日后扩展而准备的。

Web Server:云存储一般都基于Web Server后端,对于Web服务器,考虑到在小米路由器上搭建个人云,也可能会在将来基于此搭建个人站点,这里主要需求是:开源、免费、稳定、以及资源占用少、并发度高、流行度高(比较容易找到学习资料)。比较著名的Web Server有Apache、Tomcat、nginx、lighthttpd……对比众多服务器以及需求之后,选择nginx做为服务器。

云平台的选择:对于云,这里的主要需求是,开源、核心功能免费、扩展性强(扩展功能可以收费)。比较流行的个人云平台工具如:seafile、own cloud、SparkleShare、WonderBox等等,在对比了许多的介绍和需求之后,选择Own Cloud做为云平台。

由于Own Cloud使用php完成,综上,搭建Own cloud平台,可基于LNMP环境(Linux+Nginx+Mysql+Php)进行。搭建好的Own Cloud云平台,由于其本身的可扩展特点,具有的功能除传统网盘(如百度云、DropBox)外,还能支持webdav服务,以及自行添加插件App,比如OwnNote(基于Owncloud的云笔记)、tasks(基于Owncloud的任务管理)等等,这些应用可在Owncloud第三方插件提供处找到,也可自己基于Owncloud的开放接口开发出来。

由于小米路由器Cpu架构与传统PC机不同,所以这里先介绍在PC上搭建LNMP环境以及安装Own Cloud的方法,了解到与平台无关的核心过程后,然后再具体介绍在小米路由器上进行。

6.1 PC上搭建Owncloud

PC上的Owncloud在两个系统上进行过,即:Ubuntu和Center OS。除个别依赖库外,尽量采用源代码编译的方式进行,这样的过程比较“通用”,在各个平台上过程大同小异。过程大致如下:

6.1.1 安装openssl

下载: $wget -c http://www.openssl.org/source/openssl-1.0.2g.tar.gz

配置、编译、安装如下:

$tar -xzvf openssl-1.0.2g.tar.gz && cd openssl-1.0.2g
$./config --prefix=/usr/local/develop/openssl-1.0.2g
$sudo make depend
$sudo make 
$sudo make install

6.1.2 安装php所需依赖

6.1.2.1 在 Ubuntu 12.04 上:
$sudo apt-get install -y libxml2
$sudo apt-get install -y libxml2-dev
$sudo apt-get install -y libxml2-utils
$sudo apt-get install -y libcurl3
$sudo apt-get install -y libcurl
$sudo apt-get install -y libcurl4-gnutls-dev
$sudo apt-get install -y libjpeg-dev
$sudo apt-get install -y libpng-dev
$sudo apt-get install -y libmcrypt-dev
$sudo apt-get install -y libfreetype6
$sudo apt-get install -y libfreetype6-dev
6.1.2.2 在 Center OS 上:
$sudo yum install libxml2
$sudo yum install libxml2-devel
$sudo yum install libcurl
$sudo yum install libcurl-devel
$sudo yum install libjpeg-devel
$sudo yum install libpng-devel
$sudo yum install libfreetype-devel

另外在Center OS上对mysql的安装与启动如下:

$yum install mariadb-server mariadb-libs mariadb
$systemctl start mariadb.service

6.1.3 编译、安装php

下载:http://php.net/get/php-5.6.19.tar.bz2/from/a/mirror

配置、编译、安装如下:

$./configure --prefix=/usr/local/develop/ --with-config-file-path=/usr/local/develop/etc --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl=/usr/local/develop/openssl-1.0.2g --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo
$sudo make
$sudo make install

另外注: 如果遇到如下错误:

ext/openssl/.libs/xp_ssl.o: In function `php_select_crypto_method':
/home/miracle/myinstalled/php-5.6.19/ext/openssl/xp_ssl.c:947: undefined reference to `SSLv3_client_method'
/home/miracle/myinstalled/php-5.6.19/ext/openssl/xp_ssl.c:947: undefined reference to `SSLv3_server_method'

那么编译安装如下:

$sudo make EXTRA_CFLAGS=-DOPENSSL_NO_SSL3

6.1.4 安装nginx所需依赖

下载:

$wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
$wget http://apache.opencas.org//apr/apr-util-1.5.4.tar.gz
$wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.bz2

配置、编译、安装如下:

$cd apr-1.5.2
$sudo ./configure --prefix=/usr/local/develop/apr-1.5.2
$sudo make
$sudo make install

$cd apr-util-1.5.4
$sudo ./configure --prefix=/usr/local/develop/apr-util-1.5.4 --with-apr=/usr/local/develop/apr-1.5.2
$sudo make
$sudo make install

$cd pcre-8.38
$sudo ./configure --prefix=/usr/local/develop/pcre-8.38 --with-apr=/usr/local/develop/apr-1.5.2 --with-apr-util=/usr/local/develop/apr-util-1.5.4
$sudo make
$sudo make install

实践发现,其实只需要下载 pcre-8.38, 解压,后面编译 nginx 的时候,直接指定这个目录即可。不用对 pcre-8.38 进行编译。

6.1.5 编译、安装nginx

下载: wget http://nginx.org/download/nginx-1.8.1.tar.gz

配置、编译、安装:

$./configure --prefix=/usr/local/develop/ --with-openssl=`pwd`/../openssl-1.0.2g --with-pcre=`pwd`/../pcre-8.38 --with-http_ssl_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-threads
$sudo make
$sudo make install

6.1.6 Mysql的root帐号建立

  1. 启动mysqld服务

    #service mysqld start
    

    当然,可以用 stop 参数停止,用 restart 参数重启mysqld服务。

  1. 设置mysql管理员密码

    #mysqladmin -u root password '123'
    

    这里首先启动mysqld服务,然后设置mysql数据库root帐号密码,密码为123。 数据库安装好后,我们应该为mysql数据库创建一个管理帐号。执行这个命令之后, mysql数据库的管理员是root,密码是123(实践发现,不让设置成root之外的名字,应该有其他的方法)。

  2. 使用root用户登录mysql

    #mysql --user=root --password=123 
    或者
    #mysql -uroot -p123 
    或者
    #mysql -uroot -p 
    <手动输入密码>
    

    这里,假设root用户的密码是123,最后一个命令会提示你额外输入密码。这样会有 mysql> 提示符号,提示你和mysql进行交互。操作这些命令的时候,应该把mysqld服务器打开。注意,如果是连接到另外的机器上,则需要加入一个参数 -h 机器IP。

至此,mysql服务确认完毕。

6.1.7 php的配置和运行

6.1.7.1 对mysql的配置

为了能够安装owncloud,需要做php对mysql支持的配置,将源代码解压包中的 php.ini-development 拷贝到安装好的目录中( /usr/local/develop/php-5.6.19/etc ),命名为 php.ini 修改如下: pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

注意,这个mysql默认的socket路径,通过 ls /var/run/mysqld 发现是这个路径,也可能是别的路径,可以通过在 /etc/mysql/my.cnf 中查看到。

如果不进行这个配置,后面搭建好owncloud网站之后,设置的时候可能会出现类似如下错误:

ownCloud is not installed - only a limited number of commands are available
Error while trying to create admin user: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory
 ->
6.1.7.2 运行php

运行 ./php-fpm.sh start

停止 ./php-fpm.sh stop

php-fpm.sh 脚本如下(根据自己的情况,将开头定义的 $PHP_PATH 等变量做相应修改):

$cat /etc/init.d/php-fpm.sh 
#!/bin/sh
#
PHP_PATH=/usr/local/develop
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm
DAEMON=$PHP_PATH/sbin/$NAME
CONFIGFILE=$PHP_PATH/etc/php-fpm.conf
PIDFILE=$PHP_PATH/var/run/$NAME.pid

rh_start() {
  $DAEMON -c $PHP_PATH/etc -y $CONFIGFILE || echo -n " already running"
}

rh_stop() {
  killall $NAME
  #kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
         exit 3
        ;;
esac
exit 0

6.1.8 ssl key文件的生成

因为要使用https方式访问搭建的网站,需要生成ssl的认证文件,在后面配置nginx之时会用到。

$openssl genrsa -des3 -out server.key 1024
$openssl req -new -key server.key -out server.csr
$cp server.key server.key.org
$openssl rsa -in server.key.org -out server.key
$openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

6.1.9 nginx的配置和运行

6.1.9.1 运行脚本

先给出运行nginx的脚本,后面会用到(根据自己的情况,将开头定义的 $NGINX_BIN 等变量做相应修改)。

$cat /etc/init.d/nginx.sh
#!/bin/sh

NGINX_BIN=/usr/local/develop/sbin/nginx
NGINX_CONF_FILE="/usr/local/develop/conf/nginx.conf"
NGINX_PREFIX="/usr/local/develop"

start() {
        #mkdir -p /var/log/nginx
        #mkdir -p /var/lib/nginx
        $NGINX_BIN -p $NGINX_PREFIX -c $NGINX_CONF_FILE
        return $?
}

stop() {
        $NGINX_BIN -p $NGINX_PREFIX -s stop
        return $?
}

reload() {
        $NGINX_BIN -p $NGINX_PREFIX -s reload
        return $?
}

shutdown() {
        $NGINX_BIN -p $NGINX_PREFIX -s quit
}

case "$1" in
    start)
        start && echo "Start success" || echo "Start fail."
        ;;
    stop)
        stop && echo "Stop success" || echo "Stop fail."
        ;;
    reload)
        reload && echo "Reload success" || echo "Reload fail."
        ;;
    shutdown)
        shutdown
        ;;
    *)
        echo $"Usage: $0 {start|stop|reload|shutdown}"
        exit 2
esac
6.1.9.2 测试网站
  1. 先写一个测试的php脚本,用来检测是否正确。

    $mkdir -p ~/nginx
    $cd ~/nginx
    $vim index.php
    <?php
        const CONSTANT = 'Hello World';
        echo CONSTANT;
        phpinfo();   
    ?>
    
  2. 在nginx上配置测试站点

    $cd /usr/local/develop/nginx-1.8.1/conf/
    $vim nginx.conf
    ...前面省略...
    server {
            listen       18080;
              server_name localhost;
    
              location / {
                         root   /home/miracle/nginx;
                         index  index.php index.html;
            }
              location ~ \.php$ {
                         fastcgi_pass   127.0.0.1:9000;
                         fastcgi_index  index.php;
                         fastcgi_param  SCRIPT_FILENAME  /home/miracle/nginx$fastcgi_script_name;
                         include        fastcgi_params;
              }
    }
    ....后面省略...
    $/etc/init.d/nginx.sh start
    
  3. 访问站点

    经过前面配置之后,打开浏览器,输入 http://localhost:18080 若打开对应的测试站点并运行 index.php 脚本成功,说明配置成功。

6.1.10 搭建owncloud网站

对前面LNMP环境建立并且测试完成之后,搭建owncloud网站其主要工作就是修改 nginx.conf 配置文件了。在owncloud官网上有一个对nginx的配置说明,这里给出当前实践过,并且好用的配置。不做过多解释,根据自身情况,做相应修改应当不难。

6.1.10.1 OwnCloud相关软件和文档获取
  1. 服务端:

    $wget -c https://download.owncloud.org/community/owncloud-8.2.2.tar.bz2
    
  2. 客户端 客户端有用于PC的Linux、Window,也有用于手机IOS、Android版本。IOS版本的OwnCloud客户端竟然是收费的,不过没有关系,搭建好的Owncloud支持webdav,可以随便寻找一款文件同步工具(比如foldersync等)实现客户端的功能,具体可在后面列出的参考资料中寻找。

    官方客户端下载网址是:

    https://software.opensuse.org/download/package?project=isv:ownCloud:desktop&package=owncloud-client
    
  3. 文档 文档的下载和在线版本,可在如下网站获取: https://doc.owncloud.org/
6.1.10.2 配置用于OwnCloud的Nginx配置选项
$cat /usr/local/develop/nginx-1.8.1/conf/nginx.conf
...前面省略
###myown cloud with ssl
        upstream php-handler {
                server 127.0.0.1:9000;
#server unix:/var/run/php5-fpm.sock;
        }

        server {
                listen 18000;
                server_name 172.16.246.39;
# enforce https
                return 301 https://$server_name$request_uri;
        }
        server {
                listen 443 ssl;
                server_name 172.16.246.39;
                ssl_certificate /usr/local/develop/nginx-1.8.1/conf/server.crt;
                ssl_certificate_key /usr/local/develop/nginx-1.8.1/conf/server.key;
# Add headers to serve security related headers
                add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
                add_header X-Content-Type-Options nosniff;
                add_header X-Frame-Options "SAMEORIGIN";
                add_header X-XSS-Protection "1; mode=block";
                add_header X-Robots-Tag none;
                add_header X-Download-Options noopen;
                add_header X-Permitted-Cross-Domain-Policies none;

# Path to the root of your installation
                root /media/sda6/mycloud/owncloud;
# set max upload size
                client_max_body_size 1G;
                fastcgi_buffers 64 4K;
# Disable gzip to avoid the removal of the ETag header
                gzip off;
# Uncomment if your server is build with the ngx_pagespeed module
# This module is currently not supported.
#pagespeed off;
                index index.php;
                error_page 403 /core/templates/403.php;
                error_page 404 /core/templates/404.php;
                rewrite ^/.well-known/carddav /remote.php/dav/ permanent;
                rewrite ^/.well-known/caldav /remote.php/dav/ permanent;
# The following 2 rules are only needed for the user_webfinger app.
# Uncomment it if you're planning to use this app.
#rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
#rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

                location = /robots.txt {
                        allow all;
                        log_not_found off;
                        access_log off;
                }

                location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
                        deny all;
                }

                location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
                        deny all;
                }

                location / {
                        rewrite ^/remote/(.*) /remote.php last;
                        rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
                        try_files $uri $uri/ =404;
                }

                location ~ \.php(?:$|/) {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param PATH_INFO $fastcgi_path_info;
                        fastcgi_param HTTPS on;
                        fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
#fastcgi_pass   127.0.0.1:9000;
                                fastcgi_pass   php-handler;
                        fastcgi_intercept_errors on;
                }

                location ~* \.(?:css|js)$ {
                        add_header Cache-Control "public, max-age=7200";
# Add headers to serve security related headers
                        add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
                        add_header X-Content-Type-Options nosniff;
                        add_header X-Frame-Options "SAMEORIGIN";
                        add_header X-XSS-Protection "1; mode=block";
                        add_header X-Robots-Tag none;
                        add_header X-Download-Options noopen;
                        add_header X-Permitted-Cross-Domain-Policies none;
# Optional: Don't log access to assets
                        access_log off;
                }

# Optional: Don't log access to other assets
                location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
                        access_log off;
                }
        }
...后面省略

6.1.11 安装Owncloud

具体安装过程以及期间遇到的问题,可参考文档进行,下面给出大致过程。

6.1.11.1 设置OwnCloud的安装目录

先将Owncloud解压,然后设置文件权限。需要设置owncloud中data和config目录等的用户和组为 www-data 。在文档中具体介绍了为什么要这样设置。这里简化起见,设置之后,再将权限设置为777,并不是最优方案。

$tar -xjvf owncloud-8.2.2.tar.bz2
$mv owncloud-8.2.2 /media/sda6/mycloud/owncloud
$cd /media/sda6/mycloud
$sudo chown -R www-data.www-data owncloud
$sudo chmod 777 -R owncloud
$mkdir oc_data
$sudo chown -R www-data.www-data oc_data
$sudo chmod 777 -R oc_data
6.1.11.2 配置Owncloud添加管理员账户
  1. 基本配置

    我们可以根据前面的 nginx.conf 设置,直接使用 https://localhost 访问,进入Owncloud初始设置界面。为便于描述,这里直接用命令行方式对其进行设置,效果相同。

    命令实质是运行owncloud中的 occ php脚本,运行之时要以 www-data 身份进行(不同系统,可能为不同的http名称,参考官方文档)。如下:

    $ cd /media/sda6/mycloud/owncloud
    $ sudo -u www-data php occ maintenance:install --database "mysql" --database-name "ownclouddb" --database-user "root" --database-pass "<之前设置的mysql数据库root密码>" --admin-pass "<owncloud 的管理员admin 账户密码>" --data-dir "/media/sda6/mycloud/oc_data"
    

    上述命令是设置owncloud采用哪种数据库、使用的管理员账户是什么,以及数据存放的位置。运行之后,我们便可以登录 https://localhost 进行访问了。期间创建的文件数据都存放在 media/sda6/mycloud/oc_data 中,能够直接浏览。

  2. 额外配置

    如上配置之后,可以本地访问owncloud,但是如果想要实现远程其它机器也能访问,还需修改 owncloud/config/config.php 如下: 将

    array (
        0 => 'localhost',
        ),
    

    修改为:

    array (
        0 => 'localhost',
        1 => '112.74.19.38',
        ),
    

    这里, 112.74.19.38 是本地机器对外的ip地址,也可使用DNS域名。

  3. 其它
    1. openssl认证安装
      $ openssl req -new -x509 -days 730 -nodes -out "server.crt" -newkey rsa:2048 -keyout "server.key"
      Generating a 2048 bit RSA private key
      ..+++
      ..............................................................+++
      writing new private key to 'server.key'
      -----
      You are about to be asked to enter information that will be incorporated
      into your certificate request.
      What you are about to enter is what is called a Distinguished Name or a DN.
      There are quite a few fields but you can leave some blank
      For some fields there will be a default value,
      If you enter '.', the field will be left blank.
      -----
      Country Name (2 letter code) [AU]:CN
      State or Province Name (full name) [Some-State]:GD
      Locality Name (eg, city) []:SZ
      Organization Name (eg, company) [Internet Widgits Pty Ltd]:QCY
      Organizational Unit Name (eg, section) []:Public
      Common Name (e.g. server FQDN or YOUR name) []:QuietHeart
      Email Address []:quiet_heart000@126.com
      
    2. 可支持 webdav,
      #yum install davfs2
      $sudo mount -t davfs https://localhost/remote.php/webdav/ owncloud_webdav/
      Please enter the username to authenticate with server
      https://localhost/remote.php/webdav/ or hit enter for none.
        Username: public
      Please enter the password to authenticate user public with server
      https://localhost/remote.php/webdav/ or hit enter for none.
        Password:  
      /sbin/mount.davfs: the server certificate does not match the server name
      /sbin/mount.davfs: the server certificate is not trusted
        issuer:      Public, QCY, SZ, GD, CN
        subject:     Public, QCY, SZ, GD, CN
        identity:    QuietHeart
        fingerprint: 45:39:d4:81:81:35:c1:a0:35:ad:74:ca:8a:ee:84:55:d3:d0:74:70
      You only should accept this certificate, if you can
      verify the fingerprint! The server might be faked
      or there might be a man-in-the-middle-attack.
      Accept certificate for this session? [y,N] y
      /sbin/mount.davfs: warning: the server does not support locks
      /sbin/mount.davfs: Warning: can't write entry into mtab, but will mount the file system anyway
      

6.2 小米路由器上搭建Owncloud

网上找到过别人搭建的Nginx环境,但是不能直接使用(还是需要php和mysql等其他环境),也用opkg 工具尝试过安装相关的工具,基本没有适用于自己架构的软件源(或者很难找到)。

最终还是完全从源代码开始,交叉编译、安装了所需的所有软件包和依赖库,完成LNMP下Own cloud环境的搭建,还是自给自足可靠,当前期间也参考了许多其他人的过程。

6.2.1 准备交叉编译环境

6.2.2 下载相关软件包

  • State "INBOX" from [2016-04-27 三 11:15]
$cd /home/miracle/work/1_inbox/xiaomirouter/test
$wget -c http://xmlsoft.org/sources/libxml2-2.7.8.tar.gz
$wget -c http://zlib.net/zlib-1.2.8.tar.gz
$wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
$wget -c http://www.openssl.org/source/openssl-1.0.2g.tar.gz
$wget -c http://cn2.php.net/distributions/php-5.6.19.tar.xz
$wget -c https://curl.haxx.se/download/curl-7.48.0.tar.gz
$wget -c https://codeload.github.com/smart-make/libjpeg/zip/master
$wget -c http://iweb.dl.sourceforge.net/project/libpng/libpng16/1.6.21/libpng-1.6.21.tar.xz
$wget -c http://ftp.yzu.edu.tw/nongnu//freetype/freetype-2.6.3.tar.gz
$wget -c http://iweb.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
$wget -c http://heanet.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2
$wget -c http://tenet.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
$wget -c ftp://ftp.gnu.org/gnu/gettext/gettext-0.19.tar.gz
$wget -c http://heanet.dl.sourceforge.net/project/unixodbc/unixODBC/2.3.4/unixODBC-2.3.4.tar.gz
$wget -c http://nginx.org/download/nginx-1.8.1.tar.gz

owncloud-8.2.2.tgz

6.2.3 交叉编译安装MySql

注意,这里编译的是 mysql-5.1.72 其它更新的版本可能没有相应的configure文件。

6.2.3.1 下载相关软件包
$wget -c http://mirror.hust.edu.cn/gnu/ncurses/ncurses-5.6.tar.gz
$wget -c http://cdn.mysql.com/archives/mysql-5.1/mysql-5.1.72.tar.gz
6.2.3.2 编译PC板的MySql
$tar -xzvf mysql-5.1.72.tar.gz 
$mv mysql-5.1.72/ mysql-5.1.72_pc
$cd mysql-5.1.72_pc
$./configure --prefix=/usr/local/develop/mysql-5.1.72
$make

目的是为交叉编译过程顺利进行,这里先生成一个特殊的只能在pc上运行的工具。

6.2.3.3 交叉编译安装ncurses

这个工具需要先编译,这样后面的MySql和php对gettext的交叉编译才不会出现问题。

$tar -xzvf ncurses-5.6.tar.gz
$cd ncurses-5.6/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-static CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make 
$make install
  1. 交叉编译的MySql
    1. 首先修改configure文件,使之不会在交叉编译的时候运行测试软件

      $tar -xzvf mysql-5.1.72.tar.gz 
      $cd mysql-5.1.72
      $vim configure
      

      因为交叉编译的程序无法在pc上运行,如果运行生成的测试软件,必然会由于无法运行而报错,导致后面步骤无法继续。

      找到类似如下代码:

      if test "$cross_compiling" = yes; then
      { { $as_echo "$as_me:$LINENO: error: in \`$ac_pwd':" >&5
      $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
      { { $as_echo "$as_me:$LINENO: error: cannot run test program while cross compiling
      See \`config.log' for more details." >&5
      $as_echo "$as_me: error: cannot run test program while cross compiling
      See \`config.log' for more details." >&2;}
      { (exit 1); exit 1; }; }; }
      

      修改为:

      if test "$cross_compiling" = yes; then
      echo "skip corss_compiling test";
      
    2. 配置与编译

      $./configure --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --without-docs --without-man --without-debug --with-charset=utf8 --with-extra-charsets=gbk,gb2312,utf8,ascii --enable-local-infile --with-mysqld-user=root --host=arm-xiaomi-linux-uclibcgnueabi --with-named-curses-libs=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a CC=arm-xiaomi-linux-uclibcgnueabi-gcc
      $make
      

      这里运行 make 编译时,会报类似如下错误:

      make[2]:正在离开目录 `/home/miracle/work/1_inbox/xiaomirouter/test/mysql-5.1.72/sql'
      ./gen_lex_hash > lex_hash.h-t
      /bin/bash: ./gen_lex_hash: 无法执行二进制文件
      make[1]: *** [lex_hash.h] 错误 126
      make[1]:正在离开目录 `/home/miracle/work/1_inbox/xiaomirouter/test/mysql-5.1.72/sql'
      

      拷贝前面pc版本MySql中的gen_lex_hash继续:

      $cp ../mysql-5.1.72_pc/sql/gen_lex_hash sqlmiracle@miracle-Zhaoyang-cd sql
      $touch sql/gen_lex_hash
      $cd ..
      $make
      

      仍然可能报告如下错误:

      sql_parse.cc:5741:21: error: 操作符‘<’没有左操作数
      #if STACK_DIRECTION < 0
      

      修改如下:

      $vim sql/sql_parse.cc
      

      5739 #ifndef EMBEDDED_LIBRARY
      5740 
      5741 #if STACK_DIRECTION < 0                                                                                                                         
      5742 #define used_stack(A,B) (long) (A - B)
      5743 #else
      5744 #define used_stack(A,B) (long) (B - A)
      5745 #endif
      

      修改为

      5739 #ifndef EMBEDDED_LIBRARY
      5740 #define STACK_DIRECTION 1                                                                                                                       
      5741 #if STACK_DIRECTION < 0
      5742 #define used_stack(A,B) (long) (A - B)
      5743 #else
      5744 #define used_stack(A,B) (long) (B - A)
      5745 #endif
      

      然后继续编译、安装如下

      $make
      $make install
      

      至此,交叉编译版本的MySql编译安装完毕。

6.2.4 交叉编译安装PHP

6.2.4.1 交叉编译安装libxml

需要编译的版本是libxml2-2.7.8,更高的版本需要编译python。

$tar -xzvf libxml2-2.7.8.tar.gz 
$cd libxml2-2.7.8/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install
6.2.4.2 交叉编译安装zlib
6.2.4.3 交叉编译安装pcre
$export CC=
$tar -xzvf pcre-8.38.tar.gz 
$cd pcre-8.38/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install
6.2.4.4 交叉编译安装openssl
6.2.4.5 交叉编译安装libcurl
$tar -xzvf curl-7.48.0.tar.gz 
$cd curl-7.48.0/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install
6.2.4.6 交叉编译安装libjpeg
$unzip libjpeg-master.zip 
$cd libjpeg-master/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install
6.2.4.7 交叉编译安装libpng
$tar -xJvf libpng-1.6.21.tar.xz 
$cd libpng-1.6.21
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --with-zlib-prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include
$make
$make install
6.2.4.8 交叉编译安装libfreetype
$tar -xzvf freetype-2.6.3.tar.gz
$cd freetype-2.6.3
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --with-zlib-prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include LIBPNG_LIBS=-lpng
$make
$make install
6.2.4.9 交叉编译安装libmhash
$tar -xzvf mhash-0.9.9.9.tar.gz
$cd mhash-0.9.9.9/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --with-zlib-prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include

至此,会报告 rpl_malloc 相关错误,需要修改如下:

$vim include/mutils/mhash_config.h

交叉编译時autotools认为工具链的libc中不包含 mallocrealloc ,然后擅自做主张替换成了 rpl_mallocrpl_realloc 。把相关的几句话注释如下:

257 //#define malloc rpl_malloc

然后编译安装如下:

$make
$make install
6.2.4.10 交叉编译安装libmcrypt-2.5.8
$tar -xjvf libmcrypt-2.5.8.tar.bz2
$cd libmcrypt-2.5.8

还不能直接配置,需要如下修改:

$vim ./config.sub

添加对xiaomi路由器的支持:

...
287     arm-xiaomi-linux)
288       os=-linux
289       ;;
290     *-*-*)                                                                                                                                        
291         echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
292         exit 1
293         ;;
...

配置如下:

$./configure --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --host=arm-xiaomi-linux CC=arm-xiaomi-linux-uclibcgnueabi-gcc

编译之前需要如下修改:

$vim config.h

注释如下宏定义使得可以编译:

174 /* Define to rpl_malloc if the replacement function should be used. */                                                                            
175 //#define malloc rpl_malloc
176 
177 /* Define to rpl_realloc if the replacement function should be used. */
178 //#define realloc rpl_realloc

编译、安装如下:

$make
$make install
6.2.4.11 交叉编译安装mcrypt

这个貌似不用安装,但是保险起见还是把它安装上去。 配置如下:

$tar -xzvf mcrypt-2.6.8.tar.gz
$cd mcrypt-2.6.8
$./configure --with-gnu-ld --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --with-zlib-prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include

交叉编译之前,需要将 Makefile 中对非交叉编译包含的路径 /usr/lib 的依赖去掉如下修改:

$vim Makefile
92 #CFLAGS = -I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -I/usr/include -Wall                                            
93 CFLAGS = -I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -Wall

$vim src/Makefile
91 #CFLAGS = -I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -I/usr/include -Wall
92 CFLAGS = -I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -Wall

编译安装如下:

$make
$make install
6.2.4.12 交叉编译安装gettext

这个也貌似不需要安装,但是为保险起见也将其安装如下:

$tar -xzvf gettext-0.19.tar.gz
$cd gettext-0.19/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --with-libncurses-prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-named-curses-libs=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install

注意:此处可能导致后面 gettext 之时报错,如果报错,那么如下再进行一次 ncurses 编译:

$cd ncurses-5.6/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-shared CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make 
$make install
6.2.4.13 交叉编译安装libodbc

似乎无用,保险起见,编译安装如下:

$tar -xzvf unixODBC-2.3.4.tar.gz 
$cd unixODBC-2.3.4/
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-shared CC=arm-xiaomi-linux-uclibcgnueabi-gcc
$make
$make install
6.2.4.14 交叉编译安装php

这个部分是最关键,也最复杂。前面一切都是为了这个php做准备,有了php后面的nginx也才能够正常支持php的网站。

本文采用php-5.6.19,下载地址如下:http://php.net/get/php-5.6.19.tar.gz/from/a/mirror

如果需要最新地址,可到如下网址下载:http://php.net/downloads.php

  1. 代码与依赖
    $tar -xzvf php-5.6.19.tar.gz
    

    拷贝交叉编译工具链中的 libc.so, libm.so 库。这步虽然可能不必要(小米路由器上已有运行库),但是编译时确实需要。

    $mkdir -p /home/miracle/work/1_inbox/xiaomirouter/test/usr/lib
    $cp /home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/arm-xiaomi-linux-uclibcgnueabi/sysroot/usr/lib/{libc.so,libm.so} $_
    
  2. 配置前的修改

    使用configure脚本配置,会导致测试test的时候报错,错误原因类似mysql的配置,是因为交叉编译产生的测试程序不能直接运行,进而无法通过测试,修改configure脚本如下:

    $vim configure
    

    将如下脚本片段:

    if test "$cross_compiling" = yes; then :
    { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
    $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
    as_fn_error $? "cannot run test program while cross compiling
    See \`config.log' for more details" "$LINENO" 5 ; }
    else
    

    修改为:

    if test "$cross_compiling" = yes; then :
    	echo "skip corss_compiling test";
     # { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
    #$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
    #as_fn_error $? "cannot run test program while cross compiling
    #See \`config.log' for more details" "$LINENO" 5 ; }
    else
    
  3. 配置1–用于确认导出变量的配置

    经过如上修改之后, ./configure 脚本能够运行通过。但是,仅仅如此还是不够,这样配置后,即使后面的编译过程通过了,所得到的php也有许多的问题,这些问题只有在运行时才可能会被发现,那样我们有需要重新交叉编译、配置php了。过程比较麻烦,这里给出一些需要修改的地方,希望能够尽量避免日后重新配置安装。但是如果不得不重新配置安装的话,这里也为将来出现问题时如何解决给出了一个参考。

    我们编译php,首先做如下配置:

    $./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-png-dir --with-zlib --with-libxml-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-xml --disable-rpath  --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-mbregex --enable-mbstring --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-openssl-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-mcrypt=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-mhash --with-unixODBC=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap  --enable-opcache=no --without-pear --disable-fileinfo CC=arm-xiaomi-linux-uclibcgnueabi-gcc --with-gettext=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include/freetype2 CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include/freetype2
    
  4. 确定需要额外修改的配置信息

    这里需要修改的内容比较复杂,不可能完全将修改的内容在这里给出。但是相信进行到这里的朋友肯定也对自己所做的事情很清楚了,不需要详细的描述,通过这里给出的方法,应当能够自己根据实际情况将configure修改成适合自己的样子。

    1. 配置支持的函数开关

      上述配置完成之后,会生成一个 config.log, 其中包含了配置时的信息。对比在pc上配置、编译php后生成的 config.log 与该交叉配置、编译php的 config.log ,找到 ac_cv_func_xxx 变量的配置结果,会发现,有些pc版上为 yes ,而交叉编译版本时变成了 no 的变量,在交叉编译的configure中,根据自身路由器的情况,一般会将相应位置修改回 yes

      这里修改的变量内容很多,不一一列出,需要自行搜索。一般是在configure脚本中判断该变量之前的地方将其值修改。如果编译出错,一般是这样添加的变量对应的某些函数在当前交叉编译环境库中找不到,这时候,再在configure中将相应的函数定义相关变量的 yes 之处改回 no 即可。比如这里曾经修改过的: getloadavg, res_nsearch, strfmon.对应的函数就找不到,所以都又被 disable 了。

      例如如下就是修改过的代码片段:

      67029 ac_cv_func_fork=yes
      67030 ac_cv_func_waitpid=yes
      67031 ac_cv_func_sigaction=yes
      67032 
      67033 if test "$PHP_PCNTL" != "no"; then
      
      1. 例子

        下面的例子,展示了我们为什么要添加这些函数变量。

        1. 发现运行问题

          最初配置好php之后,适当修改 Makefile (修改方式参见后面), 编译、安装完成之后,运行某个php脚本时,发现php提示找不到 glob 的错误。

        2. 对比php运行环境

          于是在pc和小米路由器上写了一个简单的php脚本,

          <?php>
          phpinfo()
          <?>
          

          然后搭建好webserver并在浏览器上显示该脚本的运行结果,发现,pc版本中支持的函数多了一个 glob, 这应当是在pc上没有错误,而在小米路由器上有错误的原因。

        3. 修改源码包中相关内容

          该glob函数的定义,可以需要修改源代码,但是更根本的原因更可能是 configure 配置信息有问题,导致一些自动生成的源代码缺少了相关的函数定义。

          于是再对比pc版和交叉编译版的 config.log 发现确实对 glob 相关的定义有所不同,最终追踪到修改 configure 脚本如下:

          $vim configure
          ...
          87993 ac_cv_func_glob=yes
          87994 for ac_func in getcwd getwd asinh acosh atanh log1p hypot glob strfmon nice fpclass isinf isnan mempcpy strpncpy
          87995 do :
          ...
          

        同理,推测其它函数应当也有类似的问题,于是干脆直接对比 config.log, 将pc上激活,而在交叉编译版却被禁止的变量尽量都打开。

    2. 配置其它选项参数

      参照类似前面的方式,对比pc版本和交叉编译版本 config.log 文件,找到其它看起来比较重要的、却在pc上打开,交叉编译版本关闭的一些选项参数,进行类似修改。 比如:

      ac_cv_header_wchar_h=yes
      ac_cv_lib_crypt_crypt=yes
      ac_cv_header_time=yes
      ac_cv_header_time_h=yes
      ac_cv_crypt_md5=yes
      ac_cv_crypt_sha256=yes
      ac_cv_crypt_sha512=yes
      

      这些是我找到的一些看起来比较重要的变量,都依次在 configure 中将相应的变量直接硬编码成 yes 值了。

  5. 配置2–最终配置

    经过上述修改后,我们应该可以直接运行如下配置命令,生成Makefile了。

    $./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-png-dir --with-zlib --with-libxml-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-xml --disable-rpath  --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-mbregex --enable-mbstring --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-openssl-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-mcrypt=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-mhash --with-unixODBC=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap  --enable-opcache=no --without-pear --disable-fileinfo CC=arm-xiaomi-linux-uclibcgnueabi-gcc --with-gettext=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include/freetype2 CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include/freetype2
    
  6. Makefile修改
    $vim Makefile
    

    具体修改主要解决的是路径的问题,防止交叉编译的时候,链接到非交叉编译库的路径。修改的内容主要包括:

    1. 去掉 CFLAGS_CLEAN, INCLUDES, ext/iconv/iconv.lo 中对 /usr/include 的包含。
    2. ​去掉 EXTRA_LDFLAGS, EXTRA_LDFLAGS_PROGRAM, PHP_LDFLAGS 中对 /usr/lib 的搜索。
  7. 代码修改

    经过前面对Makefile修改之后,还需对源代码进行一些修改,以避免出现编译错误。大致如下:

    1. 修改1

      $vim include/../main/php_config.h
      

      修改片段:

      15 //#define uint unsigned int
      16 //#define ulong unsigned long  
      ...
      535 #define HAVE_CRYPT 1  
      ...
      2516 /* Define to `unsigned int ' if <sys/types.h> does not define. */
      2517 //#define uint unsigned int
      2518 
      2519 /* Define to `unsigned long ' if <sys/types.h> does not define. */
      2520 //#define ulong unsigned long
      

      该修改主要解决对uint和ulong重复定义的错误信息。

    2. 修改2

      $vim php-5.6.19/ext/zip/lib/zip_add.c
      

      将如下片段:

      111 #if SIZEOF_OFF_T == 8                                                                                                                              
      112 #define ZIP_OFF_MAX ZIP_INT64_MAX
      113 #elif SIZEOF_OFF_T == 4
      114 #define ZIP_OFF_MAX ZIP_INT32_MAX
      115 #elif SIZEOF_OFF_T == 2
      116 #define ZIP_OFF_MAX ZIP_INT16_MAX
      117 #else
      118 #error unsupported size of off_t
      119 #endif
      

      改成:

      111 //#if SIZEOF_OFF_T == 8
      112 //#define ZIP_OFF_MAX ZIP_INT64_MAX
      113 //#elif SIZEOF_OFF_T == 4
      114 #define ZIP_OFF_MAX ZIP_INT32_MAX
      115 //#elif SIZEOF_OFF_T == 2
      116 //#define ZIP_OFF_MAX ZIP_INT16_MAX
      117 //#else
      118 //#error unsupported size of off_t
      119 //#endif
      

      该修改主要解决平台字节位相关错误,这些错误可能在运行时才能够体现出来比较难以定位,在这里修改小米路由器就是32字长的CPU,所以去掉无用的判断。

    3. 修改3

      $vim ext/standard/basic_functions.c
      

      添加#if HAVE_CRYPT…#endif 宏包含如下代码块:

      ...
      1837 /* {{{ password.c */
      1838 #if HAVE_CRYPT
      1839 ZEND_BEGIN_ARG_INFO_EX(arginfo_password_hash, 0, 0, 2)
      1840     ZEND_ARG_INFO(0, password)
      1841     ZEND_ARG_INFO(0, algo)
      1842     ZEND_ARG_INFO(0, options)
      1843 ZEND_END_ARG_INFO()
      1844 ZEND_BEGIN_ARG_INFO_EX(arginfo_password_get_info, 0, 0, 1)
      1845     ZEND_ARG_INFO(0, hash)
      1846 ZEND_END_ARG_INFO()
      1847 ZEND_BEGIN_ARG_INFO_EX(arginfo_password_needs_rehash, 0, 0, 2)
      1848     ZEND_ARG_INFO(0, hash)
      1849     ZEND_ARG_INFO(0, algo)
      1850     ZEND_ARG_INFO(0, options)
      1851 ZEND_END_ARG_INFO()
      1852 ZEND_BEGIN_ARG_INFO_EX(arginfo_password_verify, 0, 0, 2)
      1853     ZEND_ARG_INFO(0, password)
      1854     ZEND_ARG_INFO(0, hash)
      1855 ZEND_END_ARG_INFO()
      1856 #endif
      1857 /* }}} */
      ...
      2866 #if HAVE_CRYPT
      2867     PHP_FE(password_hash,                                                   arginfo_password_hash)
      2868     PHP_FE(password_get_info,                                               arginfo_password_get_info)
      2869     PHP_FE(password_needs_rehash,                                           arginfo_password_needs_rehash)
      2870     PHP_FE(password_verify,                                                 arginfo_password_verify)
      2871 #endif 
      ...
      3628 #if HAVE_CRYPT
      3629     BASIC_MINIT_SUBMODULE(password)
      3630 #endif
      

      该修改主要解决如下类似错误:

      ext/standard/basic_functions.o: In function `zm_startup_basic':
      basic_functions.c:(.text+0x12d4): undefined reference to `zm_startup_password'
      ext/standard/basic_functions.o: In function `.LC795':
      basic_functions.c:(.rodata+0x9de8): undefined reference to `zif_password_hash'
      basic_functions.c:(.rodata+0x9dfc): undefined reference to `zif_password_get_info'
      basic_functions.c:(.rodata+0x9e10): undefined reference to `zif_password_needs_rehash'
      basic_functions.c:(.rodata+0x9e24): undefined reference to `zif_password_verify'
      collect2: error: ld returned 1 exit status
      
  8. 交叉编译1-生成主要的二进制文件

    经过如上修改之后,即可以编译代码,如下:

    $make
    

    如上的编译并不能完全顺利进行下来,期间遇到如下问题:

    Generating phar.php
    /bin/bash: /home/miracle/work/1_inbox/xiaomirouter/test/php-5.6.19/sapi/cli/php: 无法执行二进制文件
    make: *** [ext/phar/phar.php] 错误 126
    

    原因是编译过程中需要在pc上运行 sapi/cli/php 生成一些必要的文件,但是由于编译过程是交叉编译,所以运行这个命令失败。需要借助pc版的php生成相应文件,大致过程见后面。

  9. 交叉编译2-借助pc版本的php完成最终编译

    前面也提到了,前面出现的错误不需要修改什么,但是需要事先编译好一个pc版本的php。pc上php的编译在前面有所介绍。

    当交叉编译出现这个错误之后,将事先编译好的pc版的 php 程序替换交叉编译源码目录中的 php-5.6.19/sapi/cli/php ,然后继续 make 即可。 make 之后,不要忘记再替换回之前的备份。

    1. 使用pc版本的 php 继续完成编译、安装

      $cd sapi/cli
      $mv php php.arm
      $cp /usr/local/develop/php-5.6.19/bin/php .
      (这里/usr/local/develop/php-5.6.19是pc上的php的路径)
      $cd ../../
      $make
      $make install
      
    2. 用之前交叉编译版本的 php 替换回安装好的程序中的 php 程序

      $cd sapi/cli
      $cp php php.pc
      $cp php.arm php
      $cp php /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/bin
      

    至此,Php的交叉编译过程完成。

6.2.5 交叉编译安装nginx

6.2.5.1 代码解压
$tar -xzvf nginx-1.8.1.tar.gz
$cd nginx-1.8.1
6.2.5.2 修改配置文件

源代码中的 auto 目录修改会影响后面用 configure 生成的 objs/ngx_auto_config.h 的内容。如果没有修改相应的配置文件,则在后面编译之时修改该头文件中相应的定义也行。保险起见,最好从配置文件上进行修改。

  1. cpu位相关内容

    $vim auto/types/sizeof
    

    修改如下:

    15 ngx_size=4                                                                                                                                        
    ...<中间的注释掉>...
    18 case $ngx_size in
    

    因为目标机器(小米路由器)是32位,而这里测试程序是对编译主机检测的结果,如果该结果与目标机器不一致就会运行出错。例如我机器是64位,没有改这个,运行nginx读取配置文件的时候有如下错:

    nginx: [emerg] "worker_processes" directive invalid value in /userdisk/data/opt/usr/local/develop/conf/nginx.conf:2
    

    这类运行时才出现的错误,一般很难定位,所以在开始尽量能避免则避免。

    另外要注意 auto/endianness , 需要交叉编译这个文件定义的测试程序,然后放到小米路由器上运行一下,看其结果与pc上对比看是否一样,以决定NGX_HAVE_LITTLE_ENDIAN宏的定义。

  2. 一些系统调用的支持配置

    $vim auto/os/linux
    

    注释修改类似如下

    87 # O_PATH and AT_EMPTY_PATH were introduced in 2.6.39, glibc 2.14                                                                                  
    88 
    89 #ngx_feature="O_PATH"
    90 #ngx_feature_name="NGX_HAVE_O_PATH"
    91 #ngx_feature_run=no
    92 #ngx_feature_incs="#include <sys/types.h>
    93 #                  #include <sys/stat.h>
    94 #                  #include <fcntl.h>"
    95 #ngx_feature_path=
    96 #ngx_feature_libs=
    97 #ngx_feature_test="int fd; struct stat sb;
    98 #                  fd = openat(AT_FDCWD, \".\", O_PATH|O_DIRECTORY|O_NOFOLLOW);
    99 #                  if (fstatat(fd, \"\", &sb, AT_EMPTY_PATH) != 0) return 1"
    100 #. auto/feature
    ...
    169 # crypt_r()
    170 
    171 #ngx_feature="crypt_r()"
    172 #ngx_feature_name="NGX_HAVE_GNU_CRYPT_R"
    173 #ngx_feature_run=no
    174 #ngx_feature_incs="#include <crypt.h>"
    175 #ngx_feature_path=
    176 #ngx_feature_libs=-lcrypt
    177 #ngx_feature_test="struct crypt_data  cd;
    178 #                  crypt_r(\"key\", \"salt\", &cd);"
    179 #. auto/feature
    

    如果这里的内容没有修改的话,那么在后面编译之时的代码,应当如下修改

    $vim objs/ngx_auto_config.h
    

    修改片段:

    ...
    38 //#ifndef NGX_HAVE_O_PATH
    39 //#define NGX_HAVE_O_PATH  1
    40 //#endif                                                                                                                                          
    ...
    63 //#ifndef NGX_HAVE_GNU_CRYPT_R
    64 //#define NGX_HAVE_GNU_CRYPT_R  1
    65 //#endif 
    ...
    
6.2.5.3 配置
$./configure  --with-openssl=/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g --with-pcre=/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g --with-http_ssl_module --with-http_dav_module --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop
6.2.5.4 修改Makefile
$vim objs/Makefile
  1. 修改1-交叉编译工具与选项

    #CC =	cc
    CC=arm-xiaomi-linux-uclibcgnueabi-gcc
    #CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g 
    CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter 
    CFLAGS+=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include
    CPP =	$(CC) -E
    CPPFLAGS+=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include
    LINK =	$(CC)
    LDFLAGS+=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib
    
  2. 修改2-头文件的包含

    ALL_INCS = -I src/core \
    -I src/event \
    -I src/event/modules \
    -I src/os/unix \
    -I /home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g \
    -I /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include \
    -I objs \
    -I src/http \
    -I src/http/modules \
    -I src/mail
    
  3. 修改3-依赖模块头文件(openssl与pcre)

    CORE_DEPS = src/core/nginx.h \
    ...
    /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include/openssl/ssl.h \
    /home/miracle/work/1_inbox/xiaomirouter/test/pcre-8.38/pcre.h \
    objs/ngx_auto_config.h
    
  4. 修改4-依赖模块安装路径包含(openssl与相应的交叉编译安装路径)

    CORE_INCS = -I src/core \
    -I src/event \
    -I src/event/modules \
    -I src/os/unix \
    -I /home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g \
    -I /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include \
    -I objs
    
  5. 修改5-libpcre.a的链接

    objs/nginx:	objs/src/core/nginx.o \
    ...
    objs/ngx_modules.o \
    /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libpcre.a
    
  6. 修改6-链接 lz 的路径

    $(LINK) -o objs/nginx \
    ...
    objs/ngx_modules.o \
    -lpthread -lcrypt /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libpcre.a /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libssl.a /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libcrypto.a -ldl -L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib -lz
    
  7. 修改7-注释掉不需要的编译

    #/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/pcre.h:	#/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/Makefile
    
    #/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/Makefile:	objs/Makefile
    #cd /home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g \
    #&& if [ -f Makefile ]; then $(MAKE) distclean; fi \
    #&& CC="$(CC)" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
    #./configure --disable-shared 
    
    #/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/.libs/libpcre.a:	#/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/Makefile
    #cd /home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g \
    #&& $(MAKE) libpcre.la
    
    #/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/.openssl/include/openssl/ssl.h:	objs/Makefile
    #cd /home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g \
    #&& if [ -f Makefile ]; then $(MAKE) clean; fi \
    #&& ./config --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/openssl-1.0.2g/.openssl no-shared  \
    #&& $(MAKE) \
    #&& $(MAKE) install LIBDIR=lib
    
6.2.5.5 交叉编译、安装
$make
$make install

至此,nginx交叉编译、安装完成。

6.2.6 部署到小米路由器上的内容

6.2.6.1 安装包部署

每次升级路由器,都需要重新部署配置文件.

$vim /etc/profile
#custom
export PATH=$PATH:/userdisk/data/opt/usr/local/develop/bin
export PATH=$PATH:/userdisk/data/opt/usr/local/develop/sbin
export PATH=$PATH:/userdisk/data/opt/usr/local/develop/libexec
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/userdisk/data/opt/usr/local/develop/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/userdisk/data/opt/usr/local/develop/lib/mysql
6.2.6.2 配置文件以及数据库的部署

先给出一个脚本 install.sh, 用来集中描述对配置文件的部署:

#!/bin/sh
PREFIX=/userdisk/data/opt/usr/local/develop
[ -f /etc/my.cnf ] && echo "/etc/my.cnf exists, skip." || cp ./my.cnf /etc
[ -f $PREFIX/etc/php-fpm.conf ] && echo "$PREFIX/etc/php-fpm.conf exists, skip."|| cp ./php-fpm.conf $PREFIX/etc
[ -f $PREFIX/etc/php.ini ] && echo "$PREFIX/etc/php.ini exists, skip."|| cp ./php.ini $PREFIX/etc
[ -f $PREFIX/conf/nginx.conf ] && echo "$PREFIX/conf/nginx.conf exists, skip."|| cp ./nginx.conf $PREFIX/conf

mkdir -p $PREFIX/data
mkdir -p $PREFIX/tmp
chmod 777 -R $PREFIX/tmp
mysql_install_db --user=root --basedir=$PREFIX --datadir=$PREFIX/data

对于以上脚本,我们需要了解如下:

  1. PREFIX=/userdisk/data/opt/usr/local/develop

    这句话指定了我们将要将交叉编译的内容部署到小米路由器上的哪个路径,路径用环境变量 PREFIX 来指定。

  2. [ -f /etc/my.cnf ] && echo "/etc/my.cnf exists, skip." || cp ./my.cnf /etc

    这句话指定 MySql 的配置文件,这个文件拷贝自源码包下的 support-files/my-small.cnf, 将其修改后拷贝到小米路由器的 /etc 下,并重命名为 my.cnf 做为MySql的配置文件。

    对于该文件的修改主要如下片段:

    [mysqld]
    user = root
    basedir	= /userdisk/data/opt/usr/local/develop
    
  3. [ -f $PREFIX/etc/php-fpm.conf ] && echo "$PREFIX/etc/php-fpm.conf exists, skip."|| cp ./php-fpm.conf $PREFIX/etc

    这句话给出 php-fpm 的配置,该文件拷贝自安装目录 $PREFIX/etc/php-fpm.conf 下的 php-fpm.conf.default 文件, 主要修改如下:

    [global]
    ; Pid file
    ; Note: the default prefix is /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/var
    ; Default Value: none
    ;pid = run/php-fpm.pid
    
    ; Error log file
    ; If it's set to "syslog", log is sent to syslogd instead of being written
    ; in a local file.
    ; Note: the default prefix is /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/var
    ; Default Value: log/php-fpm.log
    ;error_log = log/php-fpm.log
    
    ...
    
    ;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
    ;                            specific port;
    ;   '[::]:port'            - to listen on a TCP socket to all addresses
    ;                            (IPv6 and IPv4-mapped) on a specific port;
    ;   '/path/to/unix/socket' - to listen on a unix socket.
    ; Note: This value is mandatory.
    listen = 127.0.0.1:9000
    
    ...
    
    ; Redirect worker stdout and stderr into main error log. If not set, stdout and
    ; stderr will be redirected to /dev/null according to FastCGI specs.
    ; Note: on highloaded environement, this can cause some delay in the page
    ; process time (several ms).
    ; Default Value: no
    ;catch_workers_output = yes
    
  4. [ -f $PREFIX/etc/php.ini ] && echo "$PREFIX/etc/php.ini exists, skip."|| cp ./php.ini $PREFIX/etc

    这个文件拷贝自源代码包中的 php.ini-development, 修改如下:

    ...
    
    ; Log errors to specified file. PHP's default behavior is to leave this value
    ; empty.
    ; http://php.net/error-log
    ; Example:
    ;error_log = php_errors.log
    ; Log errors to syslog (Event Log on Windows).
    error_log = /userdisk/data/opt/usr/local/develop/var/log/php_errors.log
    
    ...
    
    ; Maximum size of POST data that PHP will accept.
    ; Its value may be 0 to disable the limit. It is ignored if POST data reading
    ; is disabled through enable_post_data_reading.
    ; http://php.net/post-max-size
    post_max_size = 512M
    
    ...
    
    ; Directory in which the loadable extensions (modules) reside.
    ; http://php.net/extension-dir
    ; extension_dir = "./"
    ; On windows:
    extension_dir = "/userdisk/data/opt/usr/local/develop/lib/php/extensions/no-debug-non-zts-20131226"
    
    ...
    
    ; Directory where the temporary files should be placed.
    ; Defaults to the system default (see sys_get_temp_dir)
    ; sys_temp_dir = "/tmp"
    sys_temp_dir = "/userdisk/data/opt/usr/local/develop/tmp"
    
    ...
    
    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 512M
    
    ...
    
    ; Default socket name for local MySQL connects.  If empty, uses the built-in
    ; MySQL defaults.
    ; http://php.net/pdo_mysql.default-socket
    pdo_mysql.default_socket= /tmp/mysql.sock
    
    ...
    
    ;
    ;     session.save_path = "N;MODE;/path"
    ;
    ; where MODE is the octal representation of the mode. Note that this
    ; does not overwrite the process's umask.
    ; http://php.net/session.save-path
    ; session.save_path = "/tmp"
    session.save_path = "/userdisk/data/opt/usr/local/develop/tmp"
    
    ...
    
  5. [ -f $PREFIX/conf/nginx.conf ] && echo "$PREFIX/conf/nginx.conf exists, skip."|| cp ./nginx.conf $PREFIX/conf

    这个是nginx的配置文件,在 $PREFIX/conf/ 中有一个备份的文件 nginx.conf.default, (其他的nginx配置也是类似的备份方式),对该文件的修改,和PC上基本一样。

    这里给出其完整配置如下:

    worker_processes  2;
    
    error_log  /userdisk/data/opt/usr/local/develop/logs/error.log;
    #error_log  /userdisk/data/opt/usr/local/develop/logs/error.log  notice;
    #error_log  /userdisk/data/opt/usr/local/develop/logs/error.log  info;
    #error_log  /userdisk/data/opt/usr/local/develop/logs/error.log  alert;
    
    pid        /var/run/nginx.pid;
    
    
    events {
    worker_connections  1024;
    }
    
    
    http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /userdisk/data/opt/usr/local/develop/logs/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    ###test php
    server {
    listen       18080;
    server_name 192.168.31.1;
    
    location / {
    #root   /userdisk/data/web/test;
    root   /userdisk/data/web/test;
    index  index.php index.html;
    }
    
    # pass the PHP scripts to FastCGI server listening on 192.168.31.1:19000
    #
    location ~ \.php$ {
    fastcgi_pass   127.0.0.1:19000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /userdisk/data/web/test$fastcgi_script_name;
    include        fastcgi_params;
    }
    }
    
    ###myown cloud with ssl
    upstream php-handler {
    server 127.0.0.1:19000;
    #server unix:/var/run/php5-fpm.sock;
    }
    
    server {
    listen 18000;
    server_name 192.168.31.1;
    # enforce https
    return 301 https://$server_name$request_uri;
    }
    server {
    listen 443 ssl;
    server_name 192.168.31.1;
    ssl_certificate /userdisk/data/web/server.crt;
    ssl_certificate_key /userdisk/data/web/server.key;
    # Add headers to serve security related headers
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
    
    # Path to the root of your installation
    root /userdisk/data/web;
    # set max upload size
    location / {
    index index.html;
    }
    
    location /blog {
    alias /userdisk/data/web/blog;
    index index.html;
    }
    location /blog/quietheart {
    alias /userdisk/data/web/blog/quietheart;
    index index.html;
    }
    
    location /mycloud/owncloud {
    alias /userdisk/data/web/mycloud/owncloud;
    client_max_body_size 1G;
    fastcgi_buffers 64 4K;
    # Disable gzip to avoid the removal of the ETag header
    gzip off;
    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;
    index index.php;
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;
    rewrite ^/.well-known/carddav /remote.php/dav/ permanent;
    rewrite ^/.well-known/caldav /remote.php/dav/ permanent;
    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
    rewrite ^/remote/(.*) /remote.php last;
    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    try_files $uri $uri/ =404;
    }
    
    location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }
    
    location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
    deny all;
    }
    
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
    deny all;
    }
    
    #		location / {
    #		rewrite ^/remote/(.*) /remote.php last;
    #		rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    #			try_files $uri $uri/ =404;
    #		}
    
    location ~ \.php(?:$|/) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS on;
    fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass   php-handler;
    fastcgi_intercept_errors on;
    }
    
    location ~* \.(?:css|js)$ {
    add_header Cache-Control "public, max-age=7200";
    # Add headers to serve security related headers
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
    # Optional: Don't log access to assets
    access_log off;
    }
    
    # Optional: Don't log access to other assets
    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
    access_log off;
    }
    }
    }
    
  6. 创建程序运行时所需要的目录

    如下目录需要手动创建,运行时会向其中写入相关的文件:

    $mkdir -p $PREFIX/data
    $mkdir -p $PREFIX/tmp
    $chmod 777 -R $PREFIX/tmp
    
  7. 安装数据库相关内容

    主要是数据库所在路径,配置之后即可启动MySql服务,如下:

    $mysql_install_db --user=root --basedir=$PREFIX --datadir=$PREFIX/data
    
6.2.6.3 编写运行脚本

启动脚本主要包括:MySql启动脚本、Php环境启动脚本、以及nginx启动脚本。

后面会给出使用方式,这里先给出脚本内容分别如下:

  1. MySql启动脚本:mysql.server

    #!/bin/sh
    # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
    # This file is public domain and comes with NO WARRANTY of any kind
    
    # MySQL daemon start/stop script.
    
    # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
    # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
    # When this is done the mysql server will be started when the machine is
    # started and shut down when the systems goes down.
    
    # Comments to support chkconfig on RedHat Linux
    # chkconfig: 2345 64 36
    # description: A very fast and reliable SQL database engine.
    
    # Comments to support LSB init script conventions
    ### BEGIN INIT INFO
    # Provides: mysql
    # Required-Start: $local_fs $network $remote_fs
    # Should-Start: ypbind nscd ldap ntpd xntpd
    # Required-Stop: $local_fs $network $remote_fs
    # Default-Start:  2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: start and stop MySQL
    # Description: MySQL is a very fast and reliable SQL database engine.
    ### END INIT INFO
    
    # If you install MySQL on some other places than /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72, then you
    # have to do one of the following things for this script to work:
    #
    # - Run this script from within the MySQL installation directory
    # - Create a /etc/my.cnf file with the following information:
    #   [mysqld]
    #   basedir=<path-to-mysql-installation-directory>
    # - Add the above to any other configuration file (for example ~/.my.ini)
    #   and copy my_print_defaults to /usr/bin
    # - Add the path to the mysql-installation-directory to the basedir variable
    #   below.
    #
    # If you want to affect other MySQL variables, you should make your changes
    # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
    
    # If you change base dir, you must also change datadir. These may get
    # overwritten by settings in the MySQL configuration files.
    
    basedir=/userdisk/data/opt/usr/local/develop
    datadir=
    
    # Default value, in seconds, afterwhich the script should timeout waiting
    # for server start. 
    # Value here is overriden by value in my.cnf. 
    # 0 means don't wait at all
    # Negative numbers mean to wait indefinitely
    service_startup_timeout=900
    
    # The following variables are only set for letting mysql.server find things.
    
    # Set some defaults
    pid_file=
    server_pid_file=
    use_mysqld_safe=1
    user=root
    if test -z "$basedir"
    then
    basedir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72
    bindir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72/bin
    if test -z "$datadir"
    then
    datadir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72/var
    fi
    sbindir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72/sbin
    libexecdir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/mysql-5.1.72/libexec
    else
    bindir="$basedir/bin"
    if test -z "$datadir"
    then
    datadir="$basedir/data"
    fi
    sbindir="$basedir/sbin"
    libexecdir="$basedir/libexec"
    fi
    
    # datadir_set is used to determine if datadir was set (and so should be
    # *not* set inside of the --basedir= handler.)
    datadir_set=
    
    #
    # Use LSB init script functions for printing messages, if possible
    #
    lsb_functions="/lib/lsb/init-functions"
    if test -f $lsb_functions ; then
    . $lsb_functions
    else
    log_success_msg()
    {
    echo " SUCCESS! $@"
    }
    log_failure_msg()
    {
    echo " ERROR! $@"
    }
    fi
    
    PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
    export PATH
    
    mode=$1    # start or stop
    shift
    other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
    # Expected: "--skip-networking --skip-grant-tables"
    # They are not checked here, intentionally, as it is the resposibility
    # of the "spec" file author to give correct arguments only.
    
    case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
    esac
    
    parse_server_arguments() {
    for arg do
    case "$arg" in
    --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
    bindir="$basedir/bin"
    if test -z "$datadir_set"; then
    datadir="$basedir/data"
    fi
    sbindir="$basedir/sbin"
    libexecdir="$basedir/libexec"
    ;;
    --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
    datadir_set=1
    ;;
    --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    --pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    --use-mysqld_safe) use_mysqld_safe=1;;
    --use-manager)     use_mysqld_safe=0;;
    esac
    done
    }
    
    parse_manager_arguments() {
    for arg do
    case "$arg" in
    --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
    done
    }
    
    wait_for_pid () {
    verb="$1"
    manager_pid="$2"  # process ID of the program operating on the pid-file
    i=0
    avoid_race_condition="by checking again"
    while test $i -ne $service_startup_timeout ; do
    
    case "$verb" in
    'created')
    # wait for a PID-file to pop into existence.
    test -s $pid_file && i='' && break
    ;;
    'removed')
    # wait for this PID-file to disappear
    test ! -s $pid_file && i='' && break
    ;;
    *)
    echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
    exit 1
    ;;
    esac
    
    # if manager isn't running, then pid-file will never be updated
    if test -n "$manager_pid"; then
    if kill -0 "$manager_pid" 2>/dev/null; then
    :  # the manager still runs
    else
    # The manager may have exited between the last pid-file check and now.  
    if test -n "$avoid_race_condition"; then
    avoid_race_condition=""
    continue  # Check again.
    fi
    
    # there's nothing that will affect the file.
    log_failure_msg "Manager of pid-file quit without updating file."
    return 1  # not waiting any more.
    fi
    fi
    
    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1
    done
    
    if test -z "$i" ; then
    log_success_msg
    return 0
    else
    log_failure_msg
    return 1
    fi
    }
    
    # Get arguments from the my.cnf file,
    # the only group, which is read from now on is [mysqld]
    if test -x ./bin/my_print_defaults
    then
    print_defaults="./bin/my_print_defaults"
    elif test -x $bindir/my_print_defaults
    then
    print_defaults="$bindir/my_print_defaults"
    elif test -x $bindir/mysql_print_defaults
    then
    print_defaults="$bindir/mysql_print_defaults"
    else
    # Try to find basedir in /etc/my.cnf
    conf=/etc/my.cnf
    print_defaults=
    if test -r $conf
    then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
    d=`echo $d | sed -e 's/[     ]//g'`
    if test -x "$d/bin/my_print_defaults"
    then
    print_defaults="$d/bin/my_print_defaults"
    break
    fi
    if test -x "$d/bin/mysql_print_defaults"
    then
    print_defaults="$d/bin/mysql_print_defaults"
    break
    fi
    done
    fi
    
    # Hope it's in the PATH ... but I doubt it
    test -z "$print_defaults" && print_defaults="my_print_defaults"
    fi
    
    #
    # Read defaults file from 'basedir'.   If there is no defaults file there
    # check if it's in the old (depricated) place (datadir) and read it from there
    #
    
    extra_args=""
    if test -r "$basedir/my.cnf"
    then
    extra_args="-e $basedir/my.cnf"
    else
    if test -r "$datadir/my.cnf"
    then
    extra_args="-e $datadir/my.cnf"
    fi
    fi
    
    parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
    
    # Look for the pidfile 
    parse_manager_arguments `$print_defaults $extra_args manager`
    
    #
    # Set pid file if not given
    #
    if test -z "$pid_file"
    then
    pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
    else
    case "$pid_file" in
    /* ) ;;
    * )  pid_file="$datadir/$pid_file" ;;
    esac
    fi
    if test -z "$server_pid_file"
    then
    server_pid_file=$datadir/`/bin/hostname`.pid
    else
    case "$server_pid_file" in
    /* ) ;;
    * )  server_pid_file="$datadir/$server_pid_file" ;;
    esac
    fi
    
    case "$mode" in
    'start')
    # Start daemon
    
    # Safeguard (relative paths, core dumps..)
    cd $basedir
    
    manager=$bindir/mysqlmanager
    if test -x $libexecdir/mysqlmanager
    then
    manager=$libexecdir/mysqlmanager
    elif test -x $sbindir/mysqlmanager
    then
    manager=$sbindir/mysqlmanager
    fi
    
    echo $echo_n "Starting MySQL"
    if test -x $manager -a "$use_mysqld_safe" = "0"
    then
    if test -n "$other_args"
    then
    log_failure_msg "MySQL manager does not support options '$other_args'"
    exit 1
    fi
    # Give extra arguments to mysqld with the my.cnf file. This script may
    # be overwritten at next upgrade.
    "$manager" \
    --mysqld-safe-compatible \
    --user="$user" \
    --pid-file="$pid_file" >/dev/null 2>&1 &
    wait_for_pid created $!; return_value=$?
    
    # Make lock for RedHat / SuSE
    if test -w /var/lock/subsys
    then
    touch /var/lock/subsys/mysqlmanager
    fi
    exit $return_value
    elif test -x $bindir/mysqld_safe
    then
    # Give extra arguments to mysqld with the my.cnf file. This script
    # may be overwritten at next upgrade.
    pid_file=$server_pid_file
    $bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
    wait_for_pid created $!; return_value=$?
    
    # Make lock for RedHat / SuSE
    if test -w /var/lock/subsys
    then
    touch /var/lock/subsys/mysql
    fi
    exit $return_value
    else
    log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
    fi
    ;;
    
    'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.
    
    # The RedHat / SuSE lock directory to remove
    lock_dir=/var/lock/subsys/mysqlmanager
    
    # If the manager pid_file doesn't exist, try the server's
    if test ! -s "$pid_file"
    then
    pid_file=$server_pid_file
    lock_dir=/var/lock/subsys/mysql
    fi
    
    if test -s "$pid_file"
    then
    mysqlmanager_pid=`cat $pid_file`
    
    if (kill -0 $mysqlmanager_pid 2>/dev/null)
    then
    echo $echo_n "Shutting down MySQL"
    kill $mysqlmanager_pid
    # mysqlmanager should remove the pid_file when it exits, so wait for it.
    wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
    else
    log_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!"
    rm $pid_file
    fi
    
    # delete lock for RedHat / SuSE
    if test -f $lock_dir
    then
    rm -f $lock_dir
    fi
    exit $return_value
    else
    log_failure_msg "MySQL manager or server PID file could not be found!"
    fi
    ;;
    
    'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
    $0 start $other_args
    else
    log_failure_msg "Failed to stop running server, so refusing to try to start."
    exit 1
    fi
    ;;
    
    'reload'|'force-reload')
    if test -s "$server_pid_file" ; then
    read mysqld_pid <  $server_pid_file
    kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
    touch $server_pid_file
    else
    log_failure_msg "MySQL PID file could not be found!"
    exit 1
    fi
    ;;
    'status')
    # First, check to see if pid file exists
    if test -s "$server_pid_file" ; then 
    read mysqld_pid < $server_pid_file
    if kill -0 $mysqld_pid 2>/dev/null ; then 
    log_success_msg "MySQL running ($mysqld_pid)"
    exit 0
    else
    log_failure_msg "MySQL is not running, but PID file exists"
    exit 1
    fi
    else
    # Try to find appropriate mysqld process
    mysqld_pid=`pidof $libexecdir/mysqld`
    
    # test if multiple pids exist
    pid_count=`echo $mysqld_pid | wc -w`
    if test $pid_count -gt 1 ; then
    log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
    exit 5
    elif test -z $mysqld_pid ; then 
    if test "$use_mysqld_safe" = "0" ; then 
    lockfile=/var/lock/subsys/mysqlmanager
    else
    lockfile=/var/lock/subsys/mysql
    fi 
    if test -f $lockfile ; then 
    log_failure_msg "MySQL is not running, but lock exists"
    exit 2
    fi 
    log_failure_msg "MySQL is not running"
    exit 3
    else
    log_failure_msg "MySQL is running but PID file could not be found"
    exit 4
    fi
    fi
    ;;
    *)
    # usage
    echo "Usage: $0  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
    exit 1
    ;;
    esac
    
    exit 0
    
  2. php环境运行脚本: php-fpm.sh

    #!/bin/sh
    #
    PHP_PATH=/userdisk/data/opt/usr/local/develop
    DESC="php-fpm daemon"
    NAME=php-fpm
    # php-fpm
    DAEMON=$PHP_PATH/sbin/$NAME
    CONFIGFILE=$PHP_PATH/etc/php-fpm.conf
    PIDFILE=/var/run/$NAME.pid
    
    rh_start() {
    $DAEMON -c $PHP_PATH/etc -y $CONFIGFILE || echo -n " already running"
    }
    
    rh_stop() {
    kill -QUIT `cat $PIDFILE` || echo -n " not running"
    }
    
    case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    rh_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    rh_stop
    echo "."
    ;;
    restart)
    echo -n "Restarting $DESC: $NAME"
    rh_stop
    sleep 1
    rh_start
    echo "."
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
    esac
    exit 0
    
6.2.6.4 创建数据库根用户帐号与http帐号

数据库帐号用于登录和操作数据库,http帐号是访问某些Owncloud安装目录子目录数据所需要的帐号。

该过程只需执行一次,如下:

$./mysql.server start
$mysqladmin -u root password '<根用户密码>'
$vim /etc/passwd
...添加www-data用户类似如下...
www-data:x:1024:1024:www-data:/root:/bin/ash
$vim /etc/group
...添加www-data组类似如下...
www-data:x:1024:
6.2.6.5 readme
(one time)1. ./install.sh
3. ./mysql.server start
(one time)4. mysqladmin -u root password '<6+>'
(one time)5. edit
/etc/passwd
www-data:x:1024:1024:www-data:/root:/bin/ash
/etc/group
www-data:x:1024:
6. ./php-fpm.sh start
(one time every two year)7.openssl
export OPENSSL_CONF=/userdisk/data/opt/usr/local/develop/ssl/openssl.cnf
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr

cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 730 -in server.csr -signkey server.key -out server.crt
8. ./nginx.sh start


mkdir /userdisk/data/web/mycloud/oc_data
mkdir /userdisk/data/usr/local/develop/tmp
chmod -R 777 /userdisk/data/usr/local/develop/tmp

6.3 参考资料

云存储相关: seafile:开源,部分收费(比如目录选择性同步、搜索、文件读写控制),服务还不错,界面比较简陋。 https://www.seafile.com/download/

*own cloud:开源,免费,服务一般,有人反映卡,功能齐全,支持平台多。 https://owncloud.org/

SparkleShare(开源免费,不支持IOS,使用git做为引擎,中文支持差?) http://www.oschina.net/p/sparkleshare/similar_projects?lang=25&sort=time

云存储架构cloudstack vs openstack http://infosec.blog.51cto.com/226250/1565179

介绍Linux下常见的开源云平台&&存储服务 http://blog.csdn.net/gatieme/article/details/44977487

对比sparkleshare、owncloud、seafile https://www.reddit.com/r/linux/comments/1efdnd/sparkleshare_owncloud_or_seafile/

三大WEB服务器对比分析 http://hai0378.iteye.com/blog/1860220

openssl key的生成 http://www.poluoluo.com/server/201401/261178.html

nginx配置相关 http://www.nginx.cn/591.html

文件同步工具: http://alternativeto.net/software/goodsync/

http://www.pc6.com/pc/wenjiantongburj/

小米路由安装nginx: http://www.miui.com/thread-2592877-1-1.html

小米路由器交叉编译php: http://bbs.xiaomi.cn/t-11228942

小米开放平台 http://www.miwifi.com/miwifi_open.html

7 小米路由器上使用 git 版本控制系统

7.1 下载 git 源码

$wget --no-check-certificate -c https://codeload.github.com/git/git/tar.gz/v2.8.2

7.2 非交叉编译 git 过程

先了解一下非交叉编译的过程,再进行交叉编译。

  1. 生成配置脚本

    $tar -xzvf git-2.8.2.tar.gz
    $cd git-2.8.2
    $make configure
    $./configure --prefix=/usr/local/develop --without-tcltk
    

    执行完 configure 之后,会生成一个 config.mak.autogen 文件, Makefile 中会包含这个文件。

  2. 手动配置编译模块

    根据 INSTALL 介绍,按需配置需要需掉的编译模块,修改 config.mak.autogen

    $vim config.mak.autogen
    ...
    ####Custom
    NO_PERL=YesPlease
    #NO_TCLTK=YesPlease
    
    $make all
    

    git 有些功能还是需要外部依赖的,这里,将 perltcltk 相关内容去掉。会影响的功能,参见 INSTALL 介绍。

    实际执行完 make all 后,就可在当前目录中运行 ./git 进行测试了。

7.3 交叉编译 git

7.3.1 配置交叉编译环境路径

7.3.2 生成配置脚本

$tar -xzvf git-2.8.2.tar.gz
$cd git-2.8.2
$make configure
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-curl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib --without-tcltk CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include

执行完 configure 之后,会生成一个 config.mak.autogen 文件, Makefile 中会包含这个文件。

注意,运行 configure 之前,需要如下修改 configure 脚本,否则会出现无法运行测试文件相关的错误。修改如下:

$vim configure
...
6250 if test "$cross_compiling" = yes; then :                                                                                                         
6251     echo "skip corss_compiling test";
6252 #  { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
6253 #$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
6254 #as_fn_error $? "cannot run test program while cross compiling
6255 #See \`config.log' for more details" "$LINENO" 5; }
...
6300 if test "$cross_compiling" = yes; then :
6301     echo "skip corss_compiling test";                                                                                                            
6302 #  { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
6303 #$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
6304 #as_fn_error $? "cannot run test program while cross compiling
6305 #See \`config.log' for more details" "$LINENO" 5; }
6306 else
...

7.3.3 编译、安装

$make all
$make install

至此, git 的交叉编译完成,部署到服务器添加环境变量后上即可运行。

注意,编译之前需要修改 Makefile 使用交叉编译工具链

$vim Makefile
...
439 CC = arm-xiaomi-linux-uclibcgnueabi-cc                                                                                                           
440 AR = arm-xiaomi-linux-uclibcgnueabi-ar
...

7.3.4 部署与配置

7.3.4.1 部署路径

由于编译、安装时本地目录与部署的目录不同,运行的时候可能会出现各种问题。建议本地安装的目录和部署目录尽量一致。(考虑 chroot ?)

  1. 实际情况

    假设编译时安装的路径为: /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop

    部署的路径为: /userdisk/data/opt/usr/local/develop

7.3.4.2 配置
  1. 整理

    已经封装成脚本 git_setup.sh ,仅需如下执行:

    #cd /userdisk/data/myinstalled/xiaomi_cross_compiled_pkgs
    #./git_setup.sh
    #. /etc/profile
    

    脚本内容:

    #cat git_setup.sh
     #!/bin/sh
     mount -o remount -rw /
     git config --global init.templatedir /userdisk/data/opt/usr/local/develop/share/git-core/templates
     git config --global http.sslVerify false
     git config --global user.name "quietheart@xiaomi"
     git config --global user.email "vaqeteart@qq.com"
     ln -s /userdisk/data/opt/usr/local/develop/libexec/git-core/* /usr/bin
    
  2. 原理

    根据 man 手册, 运行: git config --system xxx 会读写 /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/etc 路径(小米路由器上不存在)

    运行: git config --global xxx 会读写 ~/.gitconfig (小米路由器只读)

    运行: git config xxx 会读写 .git/config, 对于第一次运行的来说,不存在这个路径。

    意图最初的配置文件在 /userdisk/data/opt/usr/local/develop/etc/gitconfig 读取,(因为无法操作 system 的以及 global 的配置)

    所以,有几个方法在第一次运行git时指定配置:

    1. 配置 GIT_CONFIG

      $export GIT_CONFIG=/userdisk/data/opt/usr/local/develop/etc/gitconfig
      $git config init.templatedir /userdisk/data/opt/usr/local/develop/share/git-core/templates
      

      但是这样会修改已有的 local 的 config, 行为,使之在已有git repo中运行时,不会修改其 .git/config

    2. 命令行中指定路径

      $git -C <config path> xxx
      

      但是这样默认会在路径中寻找 config 的文件,实践似乎不管用。

    3. 命令行指定选项

      $git -c init.templatedir=/userdisk/data/opt/usr/local/develop/share/git-core/templates clone xxx
      
    4. 开启标准路径文件写权限。

      执行下述命令即可:

      $mount -o remount -rw /
      

      参考:开启ssh后无法获得系统文件夹写入权限?

    注意了,第一次运行,一定要指定好 git config init.templatedir /userdisk/data/opt/usr/local/develop/share/git-core/templates

  3. 过程

    当已经有了一个仓库之时,直接用 git config 为相应的本地库设置好那些需要设置的选项,默认系统最好设置如下内容如:

    #mount -o remount -rw /
    #vim /etc/profile
     #custom
     export PATH=$PATH:/userdisk/data/opt/usr/local/develop/bin
     #export PATH=$PATH:/userdisk/data/opt/usr/local/develop/sbin
     #export PATH=$PATH:/userdisk/data/opt/usr/local/develop/libexec
     export PATH=$PATH:/userdisk/data/opt/usr/local/develop/libexec/git-core
     export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/userdisk/data/opt/usr/local/develop/lib
     #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/userdisk/data/opt/usr/local/develop/lib/mysql
    
    #. /etc/profile
    #ln -s /userdisk/data/opt/usr/local/develop/libexec/git-core/* /usr/bin/
    
    #git config --global init.templatedir /userdisk/data/opt/usr/local/develop/share/git-core/templates
    #git config --global http.sslVerify false
    #git config --global user.name "quietheart@xiaomi"
    #git config --global user.email "vaqeteart@qq.com"
    #git config --global receive.denyNonFastforwards true
    #git config --global receive.denycurrentbranch ignore
    

    这些信息保存在 ~/.gitconfig 中。

7.3.5 WAIT/FORWARD FAQ [%][/]

  • State "WAIT/FORWARD" from [2016-08-09 二 15:36]
    wait for all done.
7.3.5.1 DONE fatal: Unable to find remote helper for 'https'
  • State "DONE" from [2016-07-27 三 09:24]

添加路径: export PATH=$PATH:/userdisk/data/opt/usr/local/develop/libexec/git-core

7.3.5.2 DONE warning: templates not found /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/share/git-core/templates
  • State "DONE" from [2016-07-27 三 09:24]

命令中指定templates: git -c init.templatedir=/userdisk/data/opt/usr/local/develop/share/git-core/templates clone htt ps://vaqeteart@bitbucket.org/vaqeteart/quietheart.git or in config file:

[init]
     templatedir = /userdisk/data/opt/usr/local/develop/share/git-core/templates
7.3.5.3 DONE Protocol "https" not supported or disabled in libcurl
  • State "DONE" from [2016-07-27 三 09:24]

编译 libcurl 之时需要加入相关选项:

./configure --host=arm-xiaomi-linux-uclibcgnueabi --enable-static --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --enable-http --with-ssl=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/ CC=arm-xiaomi-linux-uclibcgnueabi-gcc

之后再编译 git

注意,编译 libcurl 如果出现无法链接 libssl.a, 需要加入 -fPIC 重新编译 openssl, 编译完成之后,运行 curl --version 即可看到支持不支持 https

7.3.5.4 xiaomi路由器上建立一个本地仓库后,最好用什么配置做为默认的本地仓库配置?

如下:

$git config init.templatedir /userdisk/data/opt/usr/local/develop/share/git-core/templates
$git config http.sslVerify false
$git config receive.denycurrentbranch ignore
7.3.5.5 WAIT/FORWARD SSL certificate problem: unable to get local issuer certificate
  • State "WAIT/FORWARD" from "NEXT" [2016-08-09 二 15:34]
    wair for more secury method.
  • State "NEXT" from [2016-07-27 三 09:24]
  • 方法1:命令行中禁用 https 验证(不安全)

    $git -c init.templatedir=/userdisk/data/opt/usr/local/develop/share/git-core/templates -c http.sslVerify=false clone https://vaqeteart@bitbucket.org/vaqeteart/quietheart.git
    
  • 方法2:环境变量禁用 https 验证(不安全)

    $export GIT_SSL_NO_VERIFY=true
    $git -c init.templatedir=/userdisk/data/opt/usr/local/develop/share/git-core/templates clone https://github.com/vaqeteart/vaqeteart.github.io
    
  • 方法3:ssh-keygen??
7.3.5.6 DONE 其他主机如何clone库?
  • State "DONE" from "NEXT" [2016-08-09 二 15:35]
  • State "NEXT" from "DONE" [2016-08-03 三 19:37]
  • State "DONE" from "DONE" [2016-08-03 三 19:37]
  • State "DONE" from "NEXT" [2016-07-28 四 17:04]
  • State "NEXT" from [2016-07-28 四 17:02]

如果开启了ssh功能,假设小米路由器上的git库路径为 /path/to/gitrepo, 账户为 user, 密码为 passwd, 那么其它主机只需要:

$git clone user@ip:/path/to/gitrepo .
....根据提示输入密码passwd....

由于小米路由器上的ssh端口做了映射,所以clone之前,先将主机上的 .ssh/config 中加入类似如下:

Host xiaomi
HostName <主机域名或者ip>
User user
Port <被映射的端口>
IdentityFile ~/.ssh/id_rsa

之后只需如下进行:

$git clond ssh://xiaomi:<path>
7.3.5.7 DONE ash: git-upload-pack: not found
  • State "DONE" from "NEXT" [2016-08-09 二 15:35]
  • State "NEXT" from [2016-08-03 三 19:37]

主要是运行 git-upload-pack 的路径不对。

  1. 命令行中指定
$git clone --upload-pack "/userdisk/data/opt/usr/local/develop/bin/git-upload-pack" ssh://xxx

或在服务器上,为 git-upload-pack 建立软链接,类似如下:

$ln -s /userdisk/data/opt/usr/local/develop/bin/git-upload-pack /usr/bin

参考:

http://stackoverflow.com/questions/11128464/git-upload-pack-command-not-found

http://www.linuxidc.com/Linux/2012-07/66270.htm

7.3.5.8 DONE error: cannot run pack-objects: No such file or directory
  • State "DONE" from "NEXT" [2016-08-09 二 15:35]
  • State "NEXT" from [2016-08-08 一 14:17]

类似前面,为全部git组件创建软链接的方式最简单(创建之前最好能检查一下已有的):

$ln -s /userdisk/data/opt/usr/local/develop/libexec/git-core/* /usr/bin
7.3.5.9 DONE error: failed to push some refs to 'ssh://xiaomi
  • State "DONE" from "NEXT" [2016-08-09 二 15:35]
  • State "NEXT" from [2016-08-08 一 14:17]

远程客户端运行 git push 之时出现,应当在服务端为当前git仓库打开允许 push 的选项,如下:

$git config receive.denycurrentbranch ignore

推送之后,到服务端上(xiaomi路由器上),可用 git log 查看到远端的修改,但是其工作目录中如果包含工作文件的话,推送的内容不会反映到工作目录中。

这个时候,应当将工作目录中的内容备份,然后:

$git reset --hard

8 Misc

8.1 整理

所有交叉编译好的内容,集中在 /userdisk/data/myinstalled/xiaomi_cross_compiled_pkgs

8.2 小米路由器添加ddns

ddns可以实现当ip地址不固定的时候,也能通过域名正确访问到相应的路由器。

大致原理是需要有一个ddns服务提供商,然后路由器定时、及时地将ip地址变化信息及时更新给ddns服务商,ddns服务商将域名与最新的ip地址相对应。

在路由器web管理界面上有相应的选项,建议用花生壳提供的动态路由。现在花生壳注册用户名和密码,获取到一个免费的域名,再将账户信息填写到路由器中ddns的相应位置即可。

8.3 小米路由器修改ssh启动的欢迎语

修改 /etc/banner 文件。

8.4 云存储相关资料

一个比较全的列表: http://superuser.com/questions/31512/how-to-synchronize-the-home-folder-between-multiple-computers

oschina中的列表: http://m.oschina.net/project/tag/104/storage

非常全的各类工具(云存储、云计算、云业务等只是其中的一部分) https://github.com/jobbole/awesome-sysadmin-cn

dvcs-autosync https://github.com/rmayr/dvcs-autosync/blob/master/README python实现,具体功能未知,不直观。

git-annex 貌似用一个比较另类的语言实现:

http://www.techug.com/git-annex-lfs-bigfiles

9 WAIT/FORWARD FAQ [12/25] [48%]

  • State "WAIT/FORWARD" from [2016-04-07 四 18:52]
    wait for all resolved.

9.1 DONE 小米路由如何开启ssh?

  • State "DONE" from "WAIT/FORWARD" [2016-04-16 六 18:21]
  • State "WAIT/FORWARD" from "DONE" [2016-04-07 四 18:58]
    等待整理。
  • State "DONE" from "INBOX" [2016-03-14 一 17:12]

Capture Time:[2016-03-02 三 14:56]


开启ssh可以本地局域网登录的方法,参考:小米路由器ssh开启

官网: http://www.miwifi.com/miwifi_open.html

外网访问参考: http://jingyan.baidu.com/article/9113f81b3a5d982b3314c74a.html?st=3&net_type=2&bd_page_type=1&os=0&showimg=1&rst=6

如果实现公网访问,需要开启防火墙端口。

9.1.1 下面内容仅参考暂不用

小米路由器mini如何设置外网访问ssh

关键是 修改 /etc/config/firewall

# port redirect of remapped ssh port (22001) on wan
config redirect 'myssh'       
option src              wan       
option src_dport        22001       
option dest             lan       
option dest_port        22       
option proto            tcp

重启防火墙 /etc/init.d/firewall restart

登录: ssh -p 10022 root@<ip>

远程传输: scp -P 10022 <file> root@<ip>:<path>

9.2 DONE 小米路由器防火墙如何配置?

  • State "DONE" from "WAIT/FORWARD" [2016-04-13 三 19:35]
  • State "WAIT/FORWARD" from "INBOX" [2016-03-28 一 11:27]
    wait for more organize.
  • State "INBOX" from [2016-03-15 二 09:38]

参见:小米路由器防火墙配置

9.3 DONE 小米路由如何添加域名访问?

  • State "DONE" from "INBOX" [2016-04-13 三 19:42]

Capture Time:[2016-03-02 三 14:56]


首先申请dns域名,然后将之与ip关联,但是由于ip地址不固定,所以需要使用ddns服务。参见:小米路由器添加ddns

9.4 DONE 小米路由器交叉编译环境配置

  • State "DONE" from "WAIT/FORWARD" [2016-08-08 一 17:23]
  • State "WAIT/FORWARD" from "DONE" [2016-08-08 一 11:42]
    wait for arrange.
  • State "DONE" from [2016-07-26 二 16:59]

参考:小米路由器交叉编译环境配置

9.5 DONE 如何使用软件管理工具管理小米路由上的软件?

  • State "DONE" from "INBOX" [2016-04-16 六 18:19]

Capture Time:[2016-03-04 五 18:28]


小米路由器是openwrt的系统,可借助opkg工具,参考:使用opkg在小米路由器软件管理

9.6 DONE 开启ssh后无法获得系统文件夹写入权限?

  • State "DONE" from "WAIT/FORWARD" [2016-08-08 一 17:25]
  • State "WAIT/FORWARD" from [2016-08-05 五 17:21]
    wait for more.

参考:访问文件系统权限

9.7 DONE 在小米路由器上搭建基于LNMP的Owncloud云平台

  • State "DONE" from "WAIT/FORWARD" [2016-08-08 一 17:37]
  • State "WAIT/FORWARD" from "DONE" [2016-04-16 六 16:45]
    wait for arrange.
  • State "DONE" from "INBOX" [2016-04-16 六 16:45]

Capture Time:[2016-03-04 五 19:55]


注,Owncloud功能比较全面,但是也相对不是很稳定,而且感觉消耗资源。对于资源紧张的小米路由器,目前已经不考虑用其做为主要云服务器。暂时用git来满足分布访问的需求。直接考虑的方式,获取有更好的能替代Owncloud的方式。

参考:在小米路由器上搭建基于LNMP的Owncloud云平台

9.8 CANCEL Myowncloud

  • State "CANCEL" from "WAIT/FORWARD" [2016-08-08 一 17:33]
    Owncloud不稳定,数据虽然不会丢失,但是有时也很麻烦,已经不采用,此处仅供参考。
  • State "WAIT/FORWARD" from [2016-08-08 一 17:32]
    wait for arrange.

注,Owncloud功能比较全面,但是也相对不是很稳定,而且感觉消耗资源。对于资源紧张的小米路由器,目前已经不考虑用其做为主要云服务器。暂时用git来满足分布访问的需求。直接考虑的方式,获取有更好的能替代Owncloud的方式。

9.8.1 site: <暂不可用>

9.8.2 step

9.8.2.1 安装apache(pc)

这里,安装apache之前需要安装组件:apr,apr-util,pcre,最后再安装httpd。

这里,发现nginx更适合,所以不用apache了。

  1. 下载
    $ wget -c http://apache.opencas.org//httpd/httpd-2.4.18.tar.bz2
    $ wget -c http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.bz2
    $ wget -c http://apache.opencas.org//apr/apr-util-1.5.4.tar.gz
    $ wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
    
  2. 安装
    $sudo ./configure --prefix=/usr/local/development/apr-1.5.2
    $sudo make
    $sudo make install
    
    $sudo ./configure --prefix=/usr/local/development/apr-util-1.5.4 --with-apr=/usr/local/development/apr-1.5.2
    $sudo make
    $sudo make install
    
    $sudo ./configure --prefix=/usr/local/development/pcre-8.38 --with-apr=/usr/local/development/apr-1.5.2 --with-apr-util=/usr/local/development/apr-util-1.5.4
    $sudo make
    $sudo make install
    
    $sudo ./configure --prefix=/usr/local/development/httpd-2.4.18 --enable-so --enable-mods-shared=all --with-apr=/usr/local/development/apr-1.5.2 --with-apr-util=/usr/local/development/apr-util-1.5.4 --with-pcre=/usr/local/development/pcre-8.38
    $sudo make
    $sudo make install
    $sudo ln -s /usr/local/development/httpd-2.4.18/bin/apachectl /etc/init.d
    
  3. 启动
    $sudo /etc/init.d/apachectl start
    
  4. 调试

    如果启动出现如下两个错误:

    miracle@miracle-Zhaoyang-E49:~/myinstalled/httpd-2.4.18$ sudo /etc/init.d/apachectl start
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to sup
    press this message
    (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    AH00015: Unable to open logs
    

    那么 第一个问题:修改 /usr/local/development/httpd-2.4.18/conf/httpd.conf, 添加 ServerName localhost:80. 第二个问题:应该是端口被占用,可能启动了其他apache,可以如下修改 /usr/local/development/httpd-2.4.18/conf/httpd.conf

    Listen 90
    ServerName localhost:90
    
  5. 访问

    基于如上配置,打开浏览器,地址栏键入 localhost:90 看到 "It works!" 即表示成功。

  6. 配置
9.8.2.2 安装nginx(xiaomirouter)

cd ~/xiaomirouter

  1. 下载
    $wget -c http://bigota.miwifi.com/xiaoqiang/sdk/tools/package/sdk_package.zip
    $wget -c http://nginx.org/download/nginx-1.8.1.tar.gz
    $wget -c http://www.openssl.org/source/openssl-1.0.2g.tar.gz
    $wget -c http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.bz2
    $wget -c http://apache.opencas.org//apr/apr-util-1.5.4.tar.gz
    $wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
    #$wget -c http://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
    
  2. 配置交叉编译工具环境
    $export PATH=~/xiaomirouter/test/sdk_package/toolchain/bin:$PATH
    $export CUSTOM_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop
    
  3. 交叉编译
    1. openssl
  4. 其他
    1. 安装

      只指定安装路径,没有其他选项

      $sudo ./configure --prefix=/usr/local/development/nginx-1.8.1
      $sudo make
      $sudo make install
      

      加入 openssl 的编译:

      $./configure --prefix=/usr/local/development/nginx-1.8.1 --with-openssl=/home/miracle/myinstalled/openssl-1.0.2g --with-http_ssl_module
      $sudo make
      

      这里做 sudo make 的时候会自动编译ssl,如果手动编译ssl的话,那么大致如下:

      $sudo ./config --prefix=/usr/local/development/openssl-1.0.2g
      $sudo make depend
      $sudo make
      $sudo make install
      

      参考:http://www.cnblogs.com/skynet/p/4146083.html 卸载只需要将 /usr/local/develop/nginx-1.8.1 删除即可。

  5. 配置
    • State "INBOX" from [2016-03-29 二 18:37]

    配置文件在 /usr/local/development/nginx-1.8.1/conf 下。

  6. 启动

    直接运行 ./nginx

    修改配置后 ./nginx -s reload

    更多内容 ./nginx -h

  7. 文档
  8. Misc

    openssl

    $sudo ./config --prefix=/usr/local/development/openssl-1.0.2g
    $sudo make depend
    $sudo make install
    
9.8.2.3 安装php5.4

这一步要放在安装完apache之后。

$ sudo add-apt-repository ppa:ondrej/php5
$ sudo apt-get update
$ sudo apt-get install php5-cli php5-fpm php-pear php5-mysql libcurl4-openssl-dev
#$ sudo pecl install pecl_http  # Update /etc/php5/fpm/php.ini and /etc/php5/cli/php.ini with http.so
$ php -v
9.8.2.4 源码安装php
  1. openssl

    http://www.openssl.org/source/openssl-1.0.2g.tar.gz ./config –prefix=/usr/local/develop/openssl-1.0.2g sudo make depend sudo make sudo make install

  2. depend library

    sudo apt-get install libxml2 sudo apt-get install libxml2-dev sudo apt-get install libxml2-utils sudo apt-get install libcurl3 sudo apt-get install libcurl sudo apt-get install libcurl4-gnutls-dev

  3. php

    http://php.net/downloads.php ./configure –prefix=/usr/local/develop/php-5.6.19 –with-config-file-path=/usr/local/develop/php-5.6.19/etc –enable-fpm –with-fpm-user=www-data –with-fpm-group=www-data –with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –disable-rpath –enable-magic-quotes –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-mbstring –with-mcrypt –enable-ftp –with-gd –enable-gd-native-ttf –with-openssl=/usr/local/develop/openssl-1.0.2g –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –enable-soap –without-pear –with-gettext –disable-fileinfo

    sudo make sudo make install

9.9 CANCEL owncloud升级

  • State "CANCEL" from "WAIT/FORWARD" [2016-08-08 一 17:34]
    Owncloud不稳定,数据虽然不会丢失,但是有时也很麻烦,已经不采用,此处仅供参考。
  • State "WAIT/FORWARD" from "NEXT" [2016-08-08 一 17:29]
    wait for try.
  • State "NEXT" from [2016-08-08 一 17:29]

下载,注意将 https:// 改成 http://

$wget -c http://download.owncloud.org/community/owncloud-9.0.2.tar.bz2
  1. 停用所有第三方app.
  2. rsync --inplace -rtv owncloud_latest/owncloud/ owncloud/

php -c userdisk/opt/usr/local/develop/etc occ maintenance:mimetype:update-db php -c userdisk/opt/usr/local/develop/etc occ maintenance:mimetype:update-js php -c userdisk/opt/usr/local/develop/etc occ upgrade

参考:

https://teddysun.com/211.html

https://www.librehat.com/the-owncloud-upgrade-prolonged-stagnation-maintance-solution/

9.10 CANCEL owncloud issue

  • State "CANCEL" from "NEXT" [2016-08-08 一 17:34]
    Owncloud不稳定,数据虽然不会丢失,但是有时也很麻烦,已经不采用,此处仅供参考。
  • State "NEXT" from "INBOX" [2016-08-08 一 17:29]

Capture Time:[2016-04-28 四 13:46]


同步总是发生错误,其中之一是文件大小过大.

无法上传大文件的错误: 上传大文件的错误:

Request Entity Too Large

https://forum.owncloud.org/viewtopic.php?f=17&t=7733

无法上传文件的错误: 上传文件出现类似如下错误:

An exception occurred while executing 'INSERT INTO `oc_filecache` (`mimepart`,`mimetype`,`mtime`,`size`,`etag`,`storage_mtime`,`permissions`,`parent`,`path_hash`,`path`,`name`,`storage`) SELECT ?,?,?,?,?,?,?,?,?,?,?,? FROM `oc_filecache` WHERE `storage` = ? AND `path_hash` = ? HAVING COUNT(*) = 0' with params ["8", "18", 1461910752, 1250, "d73b2a5f54292c00fbe81872c61e9381", 1461910752, 27, "19", "04cf4abd8d26a455196d8cf88cab5fe0", "files\/Documents\/\u6587\u4ef6\u7ba1\u7406.md", "\u6587\u4ef6\u7ba1\u7406.md", "3", "3", "04cf4abd8d26a455196d8cf88cab5fe0"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '17101' for key 'PRIMARY'

网上说是因为某些用户垃圾箱文件太多导致,清空垃圾箱即可,最好定时清空垃圾箱.?貌似还是不行? http://github.com/owncloud/core/issues/12591

i've tracked the files down in this message, and it belongs to a user who has about 6500 files in his trashbin.
Retention was at 30 days, I've lowered it to 7, to see if that helps, but it deletes only 1 file every 10 seconds or so, so that will take weeks...
I had to restart the mysql server twice now to prevent bigger problems.

https://github.com/owncloud/core/issues/17887

9.11 DONE 如何小米路由器上使用 git 版本控制系统?

  • State "DONE" from "WAIT/FORWARD" [2016-08-09 二 15:33]
  • State "WAIT/FORWARD" from "INBOX" [2016-07-26 二 17:05]
    wait for refile.
  • State "INBOX" from [2016-06-12 日 16:59]

参考:小米路由器上使用 git 版本控制系统

9.12 LATER 交叉编译mysql-5.7.11

  • State "LATER" from "MAYBE/FUTURE" [2017-02-20 一 11:39]
  • State "MAYBE/FUTURE" from "INBOX" [2016-08-08 一 17:34]
  • State "INBOX" from [2016-06-12 日 16:59]

新版本的 mysql 不是用 GNU Auto tools 生成 Makefile 而是使用 CMake, 所以编译方法有所不同。

9.12.1 NEXT <2017-04-27 四 11:46> try mysql-5.7.18

  • State "NEXT" from [2017-04-27 四 12:01]
9.12.1.1 下载

Mysql: https://dev.mysql.com/downloads/mysql/

$wget --no-check-certificate -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-i686.tar
9.12.1.2 依赖
$sudo apt-get install cmake

9.12.2 CANCEL try cmake

  • State "CANCEL" from [2017-04-27 四 12:01]
    can't compile..
$sudo apt-get install cmake
$cd mysql-5.7.11
$vim xx
$cmake -DCMAKE_INSTALL_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop -DMYSQL_UNIX_ADDR=/var/run/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=utf8,gbk -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/data -DSTACK_DIRECTION=1 -DCURSES_LIBRARY=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a -DCURSES_INCLUDE_PATH=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -DWITH_MYISAM_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=../boost.arm

参考:

http://www.th7.cn/db/mysql/201603/178674.shtml

http://dev.mysql.com/doc/internals/en/cmake.html

http://www.cmake.org/Wiki/CMake_Cross_Compiling

http://blog.chinaunix.net/uid-29570002-id-5112786.html

PC版本的编译:

mysql-5.7.11.pc$ cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=../boost
$cmake . -DWITH_BOOST=../boost/boost_1_59_0
$make

arm版本的编译:

9.12.2.1 DONE 自带文档
  • State "DONE" from [2016-08-17 三 12:04]

INSTALL-SOURCE

To list the configuration options, use one of the following
commands:
shell> cmake . -L   # overview
shell> cmake . -LH  # overview with help text
shell> cmake . -LAH # all params with help text
shell> ccmake .     # interactive display
9.12.2.2 <2016-08-15 一 16:29> how to manage cross compile?
$sudo apt-get install cmake
$cd usr/local/develop/include
$ln -s ncurses/ncurses.h .
$ln -s ncurses/curses.h .
$cd mysql-5.7.11

$vim Toolchain_xiaomi.cmake
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER   /home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/bin/arm-xiaomi-linux-uclibcgnueabi-gcc)
SET(CMAKE_CXX_COMPILER /home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/bin/arm-xiaomi-linux-uclibcgnueabi-g++)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH  /home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/lib)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#$cmake -DCMAKE_TOOLCHAIN_FILE=`pwd`/Toolchain_xiaomi.cmake -DCMAKE_INSTALL_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop -DMYSQL_UNIX_ADDR=/var/run/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=utf8,gbk -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/data -DSTACK_DIRECTION=1 -DCURSES_LIBRARY=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a -DCURSES_INCLUDE_PATH=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -DWITH_MYISAM_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=../boost.arm .
#$cmake -DCMAKE_INSTALL_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop -DWITH_DEBUG=0 -DDEFAULT_CHARSET=utf8 -DWITH_EXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 
$cmake -DCMAKE_TOOLCHAIN_FILE=`pwd`/Toolchain_xiaomi.cmake -DCMAKE_INSTALL_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop -DMYSQL_UNIX_ADDR=/var/run/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=utf8,gbk -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/data -DSTACK_DIRECTION=1 -DCURSES_LIBRARY=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a -DCURSES_INCLUDE_PATH=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_BOOST=`pwd`/../boost.arm .

运行两次cmake?

how to translate from auto tools?

$./configure --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop 
--without-docs ??none??
--without-man ??
--without-debug 
--with-charset=utf8 
--with-extra-charsets=gbk,gb2312,utf8,ascii 
--enable-local-infile 
--with-mysqld-user=root ???none???
--host=arm-xiaomi-linux-uclibcgnueabi ???none???
--with-named-curses-libs=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a ???

CC=arm-xiaomi-linux-uclibcgnueabi-gcc ???
9.12.2.3 <2016-08-17 三 12:03> how to manage boost error?

in cmake/boost.cmake

...
  ## Did we get a path name to the directory of the .tar.gz or .zip file?
  FIND_FILE(LOCAL_BOOST_ZIP
            NAMES "${BOOST_PACKAGE_NAME}.tar.gz" "${BOOST_PACKAGE_NAME}.zip"
            PATHS ${WITH_BOOST}
            NO_DEFAULT_PATH
           )
  MESSAGE(STATUS "@@@@@@NAMES: ${BOOST_PACKAGE_NAME}.tar.gz")
  MESSAGE(STATUS "@@@@@@PATHS(WITH_BOOST): ${WITH_BOOST}")
  MESSAGE(STATUS "@@@@@@LOCAL_BOOST_ZIP: ${LOCAL_BOOST_ZIP}")
  ## Did we get a path name to the directory of an unzipped version?
  FIND_FILE(LOCAL_BOOST_DIR
            NAMES "${BOOST_PACKAGE_NAME}"
            PATHS ${WITH_BOOST}
            NO_DEFAULT_PATH
           )
  MESSAGE(STATUS "@@@@@@NAMES: ${BOOST_PACKAGE_NAME}")
  MESSAGE(STATUS "@@@@@@PATHS(WITH_BOOST): ${WITH_BOOST}")
  MESSAGE(STATUS "@@@@@@LOCAL_BOOST_DIR: ${LOCAL_BOOST_DIR}")
  ## Did we get a path name to an unzippped version?
  FIND_PATH(LOCAL_BOOST_DIR
            NAMES "boost/version.hpp"
            PATHS ${WITH_BOOST}
            NO_DEFAULT_PATH
           )
  MESSAGE(STATUS "@@@@@@NAMES: boost/version.hpp")
  MESSAGE(STATUS "@@@@@@PATHS(WITH_BOOST): ${WITH_BOOST}")
  MESSAGE(STATUS "@@@@@@LOCAL_BOOST_DIR: ${LOCAL_BOOST_DIR}")
...

output for cmake

miracle@miracle-Zhaoyang-E49:~/work/1_inbox/xiaomirouter/test/mysql-5.7.11$ cmake -DCMAKE_TOOLCHAIN_FILE=/home/miracle/work/1_inbox/xiaomirouter/test/mysql-5.7.11/Toolchain_xiaomi.cmake -DCMAKE_INSTALL_PREFIX=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop -DMYSQL_UNIX_ADDR=/var/run/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=utf8,gbk -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/data -DSTACK_DIRECTION=1 -DCURSES_LIBRARY=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib/libncurses.a -DCURSES_INCLUDE_PATH=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_BOOST="/home/miracle/work/1_inbox/xiaomirouter/test/boost.arm" .
-- Running cmake version 2.8.7
-- Configuring with MAX_INDEXES = 64U
-- SIZEOF_VOIDP 4
-- MySQL 5.7.11
-- Packaging as: mysql-5.7.11-Linux-
-- @@@@@@NAMES: boost_1_59_0.tar.gz
-- @@@@@@PATHS(WITH_BOOST): /home/miracle/work/1_inbox/xiaomirouter/test/boost.arm
-- @@@@@@LOCAL_BOOST_ZIP: LOCAL_BOOST_ZIP-NOTFOUND
-- @@@@@@NAMES: boost_1_59_0
-- @@@@@@PATHS(WITH_BOOST): /home/miracle/work/1_inbox/xiaomirouter/test/boost.arm
-- @@@@@@LOCAL_BOOST_DIR: LOCAL_BOOST_DIR-NOTFOUND
-- @@@@@@NAMES: boost/version.hpp
-- @@@@@@PATHS(WITH_BOOST): /home/miracle/work/1_inbox/xiaomirouter/test/boost.arm
-- @@@@@@LOCAL_BOOST_DIR: LOCAL_BOOST_DIR-NOTFOUND
-- WITH_BOOST=/home/miracle/work/1_inbox/xiaomirouter/test/boost.arm
-- BOOST_INCLUDE_DIR
-- LOCAL_BOOST_DIR LOCAL_BOOST_DIR-NOTFOUND
-- LOCAL_BOOST_ZIP LOCAL_BOOST_ZIP-NOTFOUND
-- Could not find (the correct version of) boost.
-- MySQL currently requires boost_1_59_0

CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
cmake/boost.cmake:176 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:443 (INCLUDE)


-- Configuring incomplete, errors occurred!

9.13 NEXT 小米路由器搭建ftp服务

  • State "NEXT" from "LATER" [2017-02-20 一 11:39]
  • State "LATER" from "INBOX" [2016-08-08 一 17:35]

vsftp官方网址:

https://security.appspot.com/vsftpd.html

vsftpd.beasts.org

9.13.1 交叉编译环境

9.13.2 源代码下载:

$wget https://security.appspot.com/downloads/vsftpd-3.0.3.tar.gz

9.13.3 编译libcrypt

9.13.4 修改Makefile

/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib

  1. CC

    CC = arm-xiaomi-linux-uclibcgnueabi-gcc
    
  2. CFLAGS

    CFLAGS	=	-O2 -fPIE --param=ssp-buffer-size=4 \
         -Wall -W -Wshadow -Werror -Wformat-security \
         -D_FORTIFY_SOURCE=2 \
         #-pedantic -Wconversion
    
  3. LIBS

    LIBS	=	-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib -lcrypt
    

9.13.5 编译

9.13.6 开启防火墙

xkkjjjconfig rule 'ftp21'
        option name 'myftp'                           
        option src 'wan'                              
        option proto 'tcp'                            
        option target 'ACCEPT'                        
        option name ''\''ftp wan accept tcp port 21'\'''     
        option dest_port '21'
config rule 'ftp20'
        option src 'wan'  
        option proto 'tcp'
        option target 'ACCEPT'
        option name ''\''ftp wan accept tcp port 20'\'''
        option dest_port '20'

9.13.7 部署

#vi /etc/config/firewall
......
config rule 'ftp21'           
        option name 'myftp'    
        option src 'wan'        
        option proto 'tcp'                
        option target 'ACCEPT'                        
        option name ''\''ftp wan accept tcp port 21'\'''                                
        option dest_port '21'                                        

config rule 'ftp20'                                                     
        option src 'wan'                                                          
        option proto 'tcp'                                                                    
        option target 'ACCEPT'                                                                                                  
        option name ''\''ftp wan accept tcp port 20'\'''                                                                        
        option dest_port '20'
#/etc/init.d/firewall restart

#cp vsftpd /userdisk/data/opt/usr/local/develop/bin/
#cp vsftpd.conf /userdisk/data/opt/usr/local/develop/etc/
#cp vsftpd.user_list /userdisk/data/opt/usr/local/develop/etc/
#mkdir -p /userdisk/data/opt/usr/local/develop/share/empty

9.13.8 配置

9.13.8.1 配置文件 vsftpd.conf
###### Connection
# Standalone mode
listen=YES

listen_port=21
max_clients=200
max_per_ip=4
#listen_address=
connect_from_port_20=YES
ftp_data_port=20
pasv_enable=YES
pasv_max_port=0
pasv_min_port=0

###### Features
ftpd_banner=Welcome to QuietHeart FTP service.
write_enable=YES
xferlog_enable=YES
xferlog_std_format=YES
xferlog_file=/userdisk/data/opt/usr/local/develop/var/log/vsftpd.log
ls_recurse_enable=NO
download_enable=YES
ascii_upload_enable=NO
ascii_download_enable=NO
async_abor_enable=YES

#Important
allow_writeable_chroot=YES

###### Anonymous
anonymous_enable=YES
ftp_username=ftp
no_anon_password=YES
anon_root=/userdisk/data/ftp/public

anon_upload_enable=NO
anon_world_readable_only=YES
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
anon_max_rate=50000

chown_uploads=YES
chown_username=ftp

anon_umask=077

###### Local user
local_enable=YES
local_root=/userdisk/data/ftp
local_umask=022

###### Misc
# Security
hide_ids=YES
chroot_list_enable=NO
#chroot_list_file=/etc/vsftpd.chroot_list

#Only access own root dir, can't access outside.
chroot_local_user=YES

userlist_deny=NO
userlist_file=/userdisk/data/opt/usr/local/develop/etc/vsftpd.user_list

# Performance
one_process_model=NO
secure_chroot_dir=/userdisk/data/opt/usr/local/develop/share/empty
idle_session_timeout=120
data_connection_timeout=300
accept_timeout=60
connect_timeout=60
9.13.8.2 配置文件 vsftpd.user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES(default), never allow users in this file,and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd.ftpusers
# for users that are denied.
xiaomiftp
9.13.8.3 匿名ftp用户信息
#vi /etc/passwd
......
ftp:*:55:55:ftp:/userdisk/data/ftp/public:/bin/false
......
9.13.8.4 添加用户

假设添加 xiaomiftp 注意:目前添加用户的目的是为了让前面的 local_root 路径和用户主目录一致。

  1. /etc/passwd 中的 ftp 对应行复制,然后修改用户名和用户id为非重复的,假设为: xiaomiftp
  2. /etc/group 中添加对应的 xiaomiftp 组(开始复制并相应修改)
  3. /etc/shadow 添加 xiaomiftp 对应的项(开始复制并修改即可)
  4. 设置密码

    #passwd xiaomiftp
    xxxxx
    
  5. 之后会看到类似如下行:

    #cat /etc/passwd
    ......
    xiaomiftp:$1$IIOSCgTf$8sQxyfRWcz2kfOf0/9jmy0:2:2:xiaomiftp:/userdisk/data/ftp:/bin/false
    ......
    #cat /etc/group
    ......
    xiaomiftp:x:2:
    ......
    

9.13.9 运行

#vsftpd /userdisk/data/opt/usr/local/develop/etc/vsftpd.conf &

9.13.10 使用

直接通过 windows/linux/android 相应的文件浏览器,通过网址: ftp://<your ip> 即可打开并访问你的路由器了。

9.13.11 常见错误

9.13.11.1 http://www.aiisen.com/blog-27a6a852-ad4d-4e23-9a1c-3c5aca2c724d.html
#错误解决:530 This FTP server is anonymous only
将local_enable=YES前的#(注释)取消;

在anonymous_enable=YES前加上#。
# 错误解决:500 OOPS: vsftpd: security: 'one_process_model' is anonymous only 
one_process_model=yes 改成 one_process_model=no
9.13.11.2 http://server.it168.com/server/2008-03-20/200803200803872_1.shtml
日志文件文件出现错误信息“500 OOPS: vsftpd: not found: directory given in 'secure_chroot_dir':/usr/share/empty”。

解决方法:vsftpd.conf 添加一行:
secure_chroot_dir=/opt/usr/share/empty
建立一个目录
mkdir /opt/usr/share/empty
9.13.11.3 http://blog.csdn.net/bluishglc/article/details/42399439
miracle@xmnb4003210:~/work$ ftp <your ip>
Connected to <your ip>.
220 Welcome to QuietHeart FTP service.
Name (<your ip>:miracle): xiaomiftp
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection

9.14 MAYBE/FUTURE 交叉编译vim

  • State "MAYBE/FUTURE" from "INBOX" [2016-08-08 一 17:35]
  • State "INBOX" from [2016-08-03 三 19:05]

9.15 NEXT Seafile

  • State "NEXT" from "MAYBE/FUTURE" [2016-08-08 一 17:35]
  • State "MAYBE/FUTURE" from "CANCEL" [2016-07-29 五 12:05]
  • State "CANCEL" from [2016-07-28 四 18:32]

虽然开源,但是从代码结构上来看,编译比较麻烦,取消。

且对mysql的交叉编译环境暂时不待见。而且文件全都是加密过的,不能直接访问。

额外的功能如搜索等,还需收费。

9.15.1 DONE PC 上安装Seafile

  • State "DONE" from "NEXT" [2016-07-26 二 15:35]
  • State "NEXT" from "INBOX" [2016-07-26 二 15:35]

Capture Time:[2016-07-25 一 19:01]


下载:https://www.seafile.com/en/download/

$wget -c /home/miracle/下载/seafile-server_5.1.4_x86-64.tar.gz
$tar -xzvf seafile-server_5.1.4_x86-64.tar.gz
$cd seafile-server_5.1.4_x86-64
$sudo apt-get install python-mysqldb
$./setup-seafile-mysql.sh
...
Press ENTER to continue
-----------------------------------------------------------------
[Enter]

What is the name of the server? It will be displayed on the client.
3 - 15 letters or digits
[ server name ] my-seafile [Enter]

What is the ip or domain of the server?
For example: www.mycompany.com, 192.168.1.101
[ This server's ip or domain ] 172.16.246.34 [Enter]

Where do you want to put your seafile data?
Please use a volume with enough free space
[ default "/media/sda6/cloud/seafile/seafile-data" ] [Enter]

Which port do you want to use for the seafile fileserver?
[ default "8082" ] [Enter]

-------------------------------------------------------
Please choose a way to initialize seafile databases:
-------------------------------------------------------

[1] Create new ccnet/seafile/seahub databases
[2] Use existing ccnet/seafile/seahub databases

[ 1 or 2 ] 1[Enter]

What is the host of mysql server?
[ default "localhost" ] [Enter]

What is the port of mysql server?
[ default "3306" ] [Enter]

What is the password of the mysql root user?
[ root password ] xxxxxx[Enter]

Enter the name for mysql user of seafile. It would be created if not exists.
[ default "root" ] myseafile[Enter]

Enter the password for mysql user "myseafile":
[ password for myseafile ] xxxxxx[Enter]

Enter the database name for ccnet-server:
[ default "ccnet-db" ] [Enter]

Enter the database name for seafile-server:
[ default "seafile-db" ] [Enter]

Enter the database name for seahub:
[ default "seahub-db" ] [Enter]

---------------------------------
This is your configuration
---------------------------------

    server name:            my-seafile
    server ip/domain:       172.16.246.34

    seafile data dir:       /media/sda6/cloud/seafile/seafile-data
    fileserver port:        8082

    database:               create new
    ccnet database:         ccnet-db
    seafile database:       seafile-db
    seahub database:        seahub-db
    database user:          myseafile



---------------------------------
Press ENTER to continue, or Ctrl-C to abort
---------------------------------

[Enter]
....
-----------------------------------------------------------------
Your seafile server configuration has been finished successfully.
-----------------------------------------------------------------

run seafile server:     ./seafile.sh { start | stop | restart }
run seahub  server:     ./seahub.sh  { start <port> | stop | restart <port> }

-----------------------------------------------------------------
If you are behind a firewall, remember to allow input/output of these tcp ports:
-----------------------------------------------------------------

port of seafile fileserver:   8082
port of seahub:               8000

When problems occur, Refer to

        https://github.com/haiwen/seafile/wiki

for information.
$./seafile.sh start
$./seahub.sh start
......
What is the email for the admin account?
[ admin email ] quiet_heart000@126.com

What is the password for the admin account?
[ admin password ] 

Enter the password again:
[ admin password again ] 
......

访问之时,浏览器输入: 127.0.0.1:8000

文档:http://manual.seafile.com/

9.15.2 编译Seafile

9.15.2.1 编译libsearpc
$./autogen.sh
$./configure --prefix=`pwd`/../prefix
checking for JANSSON... no
configure: error: Package requirements (jansson >= 2.2.1) were not met:

No package 'jansson' found
$wget -c http://www.digip.org/jansson/releases/jansson-2.7.tar.gz
$./configure --prefix=`pwd`/../prefix
$make
$make install
$./configure --prefix=`pwd`/../prefix LDFLAGS=-L`pwd`/../prefix/lib JANSSON_CFLAGS=-I`pwd`/../prefix/include JANSSON_LIBS="-L`pwd`/../prefix/lib -ljansson"
gcc -DHAVE_CONFIG_H -I. -I..    -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I../lib -g -O2 -MT searpc-demo-server.o -MD -MP -MF .deps/searpc-demo-server.Tpo -c -o searpc-demo-server.o searpc-demo-server.c
In file included from ../lib/searpc.h:7:0,
                 from searpc-demo-server.c:10:
../lib/searpc-server.h:6:21: 致命错误: jansson.h:没有那个文件或目录
编译中断。

添加 JANSSON_CFLAGS

$vim demo/Makefile
70 COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \                                                                          
71     $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(JANSSON_CFLAGS)
$./configure --prefix=`pwd`/../prefix LDFLAGS=-L`pwd`/../prefix/lib JANSSON_CFLAGS=-I`pwd`/../prefix/include JANSSON_LIBS="-L`pwd`/../prefix/lib -ljansson"
$make
$make install
9.15.2.2 编译ccnet

下载依赖

编译依赖

编译 ccnet

$cd ccnet-5.1.4-server
$./autogen.sh
./configure --prefix=`pwd`/../prefix --enable-server
checking for uuid_generate in -luuid... no
configure: error: *** Unable to find uuid library

https://sourceforge.net/projects/libuuid/files/libuuid-1.0.3.tar.gz/download

$cd libuuid-1.0.3/
$./configure --prefix=`pwd`/../prefix
$make
$make install
$cd ccnet-5.1.4-server
$./configure --prefix=`pwd`/../prefix --enable-server LDFLAGS=-L`pwd`/../prefix/lib
found library pthread
checking for sqlite3_open in -lsqlite3... no
configure: error: *** Unable to find sqlite3 library
$wget -c http://www.sqlite.org/2016/sqlite-amalgamation-3140100.zip
$cd sqlite-autoconf-3140100
$./configure --prefix=`pwd`/../prefix
$make
$make install
$cd ccnet-5.1.4-server
$export PKG_CONFIG_PATH=`pwd`/../prefix/lib/pkgconfig
$./configure --prefix=`pwd`/../prefix --enable-server LDFLAGS=-L`pwd`/../prefix/lib
configure: error: Package requirements (libevent >= 2.0) were not met:
No package 'libevent' found
$wget -c https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
$./configure --prefix=`pwd`/../prefix LDFLAGS=-L`pwd`/../prefix/lib
$make
$make install
$./configure --prefix=`pwd`/../prefix --enable-server LDFLAGS=-L`pwd`/../prefix/lib
...
checking for ZDB... no
configure: error: Package requirements (zdb >= 2.10) were not met:
No package 'zdb' found
...
$wget -c http://www.tildeslash.com/libzdb/dist/libzdb-3.1.tar.gz
$./configure --prefix=`pwd`/../prefix --with-mysql LDFLAGS=-L`pwd`/../prefix/lib CFLAGS=-I`pwd`/../prefix/include
$make
$make install
$./configure --prefix=`pwd`/../prefix --enable-server LDFLAGS=-L`pwd`/../prefix/lib
$make
...
valac -C --pkg posix ccnetobj.vala
/bin/bash: valac: 未找到命令
make[2]: *** [ccnetobj.c] 错误 127
...
$sudo apt-get intall valac
$make 
...
../common/rpc-service.c:40:30: 致命错误: searpc-signature.h:没有那个文件或目录
编译中断。
make[3]: *** [rpc-service.o] 错误 1
make[3]:正在离开目录 `/home/miracle/下载/seafile/build/ccnet-5.1.4-server/net/daemon'
make[2]: *** [all-recursive] 错误 1
make[2]:正在离开目录 `/home/miracle/下载/seafile/build/ccnet-5.1.4-server/net'
make[1]: *** [all-recursive] 错误 1
make[1]:正在离开目录 `/home/miracle/下载/seafile/build/ccnet-5.1.4-server'
make: *** [all] 错误 2
...

9.15.3 交叉编译Seafile

9.16 MAYBE/FUTURE SparkleShare

  • State "MAYBE/FUTURE" from "WAIT/FORWARD" [2017-02-20 一 11:39]
  • State "WAIT/FORWARD" from "CANCEL" [2016-08-03 三 09:15]
    is it available as a git client?

    考察是否能将其仅做为git户端使用,这样就不需要服务端有运行的了。服务端只需要有git即可。

  • State "CANCEL" from "NEXT" [2016-07-28 四 18:30]
    取消,因为依赖 git-lfs,而 git-lfs 是用 go 写的,并且可能依赖外部服务。
  • State "NEXT" from [2016-07-27 三 09:29]

这是一个基于git的类dropbox网盘。

还需要基于图形环境。

9.16.1 CANCEL PC上的编译安装

  • State "CANCEL" from "MAYBE/FUTURE" [2016-08-03 三 11:56]
    没有必要在PC上源码安装,只需在使用的机器上有一个sparkleshare客户端即可,因为服务端就是git服务器。没有必要考虑交叉编译。
  • State "MAYBE/FUTURE" from [2016-08-03 三 09:38]
9.16.1.1 依赖
  1. 安装curl
  2. 安装git
  3. CANCEL 安装git-lfs
    • State "CANCEL" from "NEXT" [2016-07-28 四 18:32]
      取消,考虑git-annex.
    • State "NEXT" from [2016-07-27 三 10:45]

    官网:https://github.com/github/git-lfs/releases

    下载:

    $wget -c https://github.com/github/git-lfs/archive/v1.2.0.tar.gz
    
  4. libtool
    $sudo apt-get install libtool
    
  5. mono
9.16.1.2 下载:
$wget -c https://github.com/hbons/SparkleShare/archive/1.5.0.tar.gz
$./autogen.sh

9.16.2 Ubuntu上直接安装

9.16.2.1 安装

因为服务端就是git服务器,只需在使用的机器上有一个sparkleshare客户端即可,没有必要在PC上源码安装,也没有必要考虑交叉编译。

$sudo add-apt-repository ppa:warp10/sparkleshare
$sudo apt-get update
$sudo apt-get install sparkleshare libwebkit1.1-cil git-core

可能还需要安装python-nautilus,防止某些系统打开之后软件崩溃:

$sudo apt-get install python-nautilus
9.16.2.2 运行
$sparkleshare start
9.16.2.3 添加已托管的git项目(位于ssh主机上)

选择已有的一个git服务器下的git目录添加, 经常会出现无法同步文件的情况。

如果机器是ssh登录的话,通过终端的log可看到,sparkle会自动登录远端服务器进行 git clone, 但是ssh密码却没有交互输入的地方。

解决方法: 需要将key文件上传到服务器中,实现免密ssh登录。 sparkleshare目录中有key文件,.ssh中也有。 上传用类似这个命令: ssh-copy-id quietheart@112.74.19.38

9.16.2.4 服务端的配置

Sparkle shared其实就是一个 git 图形客户端。自动定期同步其管辖目录下的git文件。服务端的配置非常简单,只需要有一个git仓库即可。

关于git的配置这里不做描述,需要的可参考git的相关使用文档: https://git-scm.com/book/zh/v2

常见的用于sparkle的比较重要的git配置如下:

  1. 允许push

    服务端git仓库需要配置成允许push, 修改配置文件修改其 receive.denyCurrentBranch 的值为 ignore , 或者在具有工作目录的仓库中运行 git config receive.denyCurrentBranch ignore .

  2. 使用裸库替代有工作目录的git库

    如果服务端git仓库中包含一个工作目录,那么客户端的Sparkleshare 完成 push之后,服务端会看不到变化的文件。原因是服务端如果在被push的分支上,就无法在实时更新其工作目录结构,需要在服务端手动运行 git reset --hard 才行。

    最好的方法是,将服务端的git库设为裸库(或者镜像?)。

9.17 MAYBE/FUTURE dvcs-autosync

  • State "MAYBE/FUTURE" from [2016-07-28 四 19:53]

9.18 CANCEL git annex

  • State "CANCEL" from "NEXT" [2016-07-29 五 11:57]
    语言太奇葩:Haskell.
  • State "NEXT" from "CANCEL" [2016-07-29 五 11:36]
  • State "CANCEL" from "NEXT" [2016-07-28 四 19:39]
    使用麻烦,安装也不一定好弄
  • State "NEXT" from [2016-07-28 四 18:32]

开发语言比较奇葩

git-annex is Free Software, written in Haskell. You can contribute!

9.18.1 源代码

git clone https://github.com/joeyh/git-annex.git

9.19 MAYBE/FUTURE git lfs

  • State "MAYBE/FUTURE" from "NEXT" [2016-08-03 三 19:02]
  • State "NEXT" from [2016-07-29 五 12:05]

服务器?go语言? https://git-lfs.github.com/

9.20 NEXT 交叉编译安装Python

  • State "NEXT" from [2016-10-28 五 16:26]

9.20.1 下载

$wget -c http://mirrors.sohu.com/python/3.4.1/Python-3.4.1.tar.xz
$tar -xJvf Python-3.4.1.tar.xz

9.20.2 配置交叉编译环境路径

9.20.3 安装

9.20.3.1 先编译PC版本的Parser
$./configure
$make Parser/pgen
9.20.3.2 修改 configure

如果不修改,后面运行 ./configure 的时候会报错,并且,根据这 config.log 可以定位到下面需要修改的位置。

修改如下:

$vim configure
......
if test "$cross_compiling" = yes; then
        echo "skip cross compile test"
#    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for python interpreter for cross build" >&5
#$as_echo_n "checking for python interpreter for cross build... " >&6; }
#    if test -z "$PYTHON_FOR_BUILD"; then
#        for interp in python$PACKAGE_VERSION python3 python; do
#           which $interp >/dev/null 2>&1 || continue
#           if $interp -c 'import sys;sys.exit(not sys.version_info[:2] >= (3,3))'; then
#               break
#           fi
#            interp=
#       done
#        if test x$interp = x; then
#           as_fn_error $? "python$PACKAGE_VERSION interpreter not found" "$LINENO" 5
#       fi
#        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $interp" >&5
#$as_echo "$interp" >&6; }
#       PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib:$(srcdir)/Lib/$(PLATDIR) '$interp
#    fi
elif test "$cross_compiling" = maybe; then
......
$./configure --host=arm-xiaomi-linux-uclibcgnueabi --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop CC=arm-xiaomi-linux-uclibcgnueabi-gcc LDFLAGS=-L/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/lib CFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include CPPFLAGS=-I/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop/include --disable-ipv6

9.21 NEXT 创建一套小米路由器setup工具脚本

  • State "NEXT" from [2017-02-20 一 11:40]

比如: setup_mi.sh

./setup_mi.sh git [enabled|disabled]
./setup_mi.sh ftp [enabled|disabled]
...

9.23 NEXT 编译openssh

  • State "NEXT" from [2017-05-04 四 09:29]

9.23.1 实践

9.23.1.1 下载
$wget -c ftp://ftp.kddilabs.jp/pub/OpenBSD/OpenSSH/portable/openssh-7.5p1.tar.gz
9.23.1.2 配置交叉编译环境
9.23.1.3 交叉编译
  1. 依赖的编译

    需要编译zlib和openssl, 参考: 交叉编译安装zlib

    交叉编译openssl

  2. ssh的编译

    /home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop

    $tar -xzvf openssh-7.5p1.tar.gz
    $cd openssh-7.5p1
    $./configure --prefix=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --host=arm-xiaomi-linux-uclibcgnueabi --with-libs --with-zlib=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --with-ssl-dir=/home/miracle/work/1_inbox/xiaomirouter/test/usr/local/develop --disable-etc-default-login CC=arm-xiaomi-linux-uclibcgnueabi-gcc AR=arm-xiaomi-linux-uclibcgnueabi-ar
    $vim Makefile
    ...
    #STRIP_OPT=-s
    STRIP_OPT=-s --strip-program=/home/miracle/work/1_inbox/xiaomirouter/test/sdk_package/toolchain/bin/arm-xiaomi-linux-uclibcgnueabi-strip
    ...
    $make
    $make install-nokeys
    
  3. 配置文件
    $vim /usr/local/develop/etc/sshd_config
    ...
    HostKey /userdisk/data/opt/usr/local/develop/etc/ssh_host_rsa_key
    HostKey /userdisk/data/opt/usr/local/develop/etc/ssh_host_dsa_key
    ...
    Subsystem       sftp    /userdisk/data/opt/usr/local/develop/libexec/sftp-server
    ...
    
  4. 运行

    部署到xiaomi路由上,添加好环境变量之后。

    #vi /etc/passwd
    ...
    sshd:*:56:56:sshd:/root:/bin/false       
    ...
    #cd /userdisk/data/opt/usr/local/develop/etc/
    #ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
    #ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
    #mkdir /var/empty
    #/userdisk/data/opt/usr/local/develop/sbin/sshd -f /userdisk/data/opt/usr/local/develop/etc/sshd_config
    

9.24 NEXT 整理一些常见库的交叉编译

  • State "NEXT" from [2017-05-04 四 09:42]

参考:小米路由器上交叉编译一些常见库

9.25 NEXT leannote

  • State "NEXT" from [2017-05-18 四 16:28]

9.25.1 DONE PC上的Leannote编译安装

  • State "DONE" from [2017-05-19 五 17:55]
  • State "LATER" from [2017-04-28 五 10:43]
9.25.1.1 下载
9.25.1.2 安装
  1. 安装Mondb和leannote
    $tar -xzvf leanote-linux-amd64-v2.4.bin.tar.gz
    $pwd
    /home/miracle/myinstalled/leanote
    
    $sudo apt-get install mongodb
    $mkdir -p ~/mydata/mongodb_data
    $mongod --dbpath ~/mydata/mongodb_data
    ...
    

    注意:如果不使用 apt-get 安装 mongodb, 可下载前面给出的Mongodb的链接,解压后,以将 Mongodb 的执行路径添加到PATH中。

  2. 导入Leannote数据

    不同的Mongodb版本,数据导入略有不同。

    Mongodb v2

    $mongorestore -h localhost -d leanote --directoryperdb /home/miracle/myinstalled/leanote/mongodb_backup/leanote_install_data/
    

    Mongodb v3

    $mongorestore -h localhost -d leanote --dir /home/miracle/myinstalled/leanote/mongodb_backup/leanote_install_data/
    

    查看导入的数据

    $ mongo
    MongoDB shell version: 2.0.4
    connecting to: test
    > show dbs
    leanote 0.203125GB
    local   (empty)
    > use leanote
    switched to db leanote
    > show collections
    albums
    attachs
    blog_comments
    blog_likes
    blog_singles
    configs
    email_logs
    files
    find_pwds
    ......
    

    初始数据users表中已有2个用户:

    user1 username: admin, password: abc123 (管理员, 只有该用户才有权管理后台, 请及时修改密码)
    user2 username: demo@leanote.com, password: demo@leanote.com (仅供体验使用)
    
9.25.1.3 配置Leannote
$vim conf/app.conf
...
app.secret=任意值
...
9.25.1.4 运行Leannote
$cd bin
$chmod +x run.sh
$./run.sh

9.25.2 交叉编译适用于小米路由的go环境

9.25.3 交叉编译Mongodb