Total Pageviews

Sunday 16 December 2018

在Tomatoware ARM下,安装Go环境





一. 预备工作:
首先请建立一个足够大的 SWAP 分区/文件,建议 512M 以上。通过源码编译 Go 的时候路由器的 256M 内存很快就会被耗尽。如下是建立一个 1G 的 swapfile 并且挂载为交换区:
dd if=/dev/zero of=/mnt/swap/swapfile bs=1024 count=1048576
mkswap /mnt/swap/swapfile
swapon /mnt/swap/swapfile
然后你需要指定一个临时文件存放目录 TMPDIR,在编译 Go 时,Tomatoware 内置的 TMP 和 TEMP 环境变量不能被编译过程识别,它会把临时文件全部丢去 /tmp 目录,而 /tmp 目录是用内存虚拟的,空间瞬间就会被耗尽。
export TMPDIR=/mmc/tmp
二. 开始编译:(安装目标目录为 /mmc/usr/go,你可以修改为你自己的)
下载解压源码:
rm -fr /mmc/usr/go
curl -sSL https://storage.googleapis.com/golang/go1.4.3.src.tar.gz | tar -xz -C /mmc/usr
#或者:
rm -fr /mmc/usr/go
curl -O https://storage.googleapis.com/golang/go1.4.3.src.tar.gz
tar -xzf go1.4.3.src.tar.gz -C /mmc/usr
编译:
cd /mmc/usr/go/src
./make.bash

这里耗时约 20分钟(ASUS RT-AC68P 1000MHz);
测试:
export PATH=/mmc/usr/go/bin:$PATH
go version
#应该显示:
go version go1.4.3 linux/arm
打包:
为了在其他路由器上也能直接使用,我们可以压缩打包这个编译好的 Go:
tar --numeric-owner -czf /mnt/data/go1.4.3.linux-armv7.tar.gz -C /mmc/usr go
保存这个压缩包,今后在其他路由器上,只需:
rm -fr /mmc/usr/go
tar -xzf /mnt/data/go1.4.3.linux-armv7.tar.gz -C /mmc/usr
export PATH=/mmc/usr/go/bin:$PATH
go version
#应该显示:
go version go1.4.3 linux/arm
即可。
三. 升级为 Go 1.5.2:
Go 1.5.2 并不能直接编译,它需要 1.4.3 版本的支持,还好,我们上面已经编译好 1.4.3 版本的 Go 了。那么开始编译 1.5.2:
mv /mmc/usr/go /mmc/usr/go1.4
rm -fr /mmc/usr/go
curl -sSL https://storage.googleapis.com/golang/go1.5.2.src.tar.gz | tar -xz -C /mmc/usr
cd /mmc/usr/go/src
GOROOT_BOOTSTRAP=/mmc/usr/go1.4 ./make.bash
这个时间很长,你可以去做点别的,大约 30 分钟到 1 小时不等;编译好后你一样可以用上面的方法打包好以便今后使用。
四. 编译实战:
我们以 https://github.com/cyfdecyf/cow 为例来静态编译一下 COW 程序:
export GOPATH=/mnt/data/compile/go
cd /mnt/data/compile/go
go get github.com/cyfdecyf/cow

这里可能会提示出错:
package golang.org/x/crypto/blowfish: unrecognized import path "golang.org/x/crypto/blowfish"
package golang.org/x/crypto/cast5: unrecognized import path "golang.org/x/crypto/cast5"
package golang.org/x/crypto/salsa20/salsa: unrecognized import path "golang.org/x/crypto/salsa20/salsa"

因为 golang.org 被 X 了;使用设置 http_proxy 以及 https_proxy 环境变量的办法在我这不行,所以我们曲线救国:
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://go.googlesource.com/crypto

这样再编译就没问题了:
cd /mnt/data/compile/go/src/github.com/cyfdecyf/cow
go build

不过这样编译出来的不是静态文件,静态编译要这样:
CGO_ENABLED=0 go build
参考: How to compile Go on any ARM device
-----





Tomatoware MIPSEL 下静态编译 shadowsocks-libev


先配置下选项,我这里以 PolarSSL 为例:
./configure --prefix=/opt --with-crypto-library=polarssl --with-polarssl=/mmc/lib --with-polarssl-include=/mmc/include
为了解决编译时出现的 epoll 相关错误(应为 uClibc 版本低的原因),需要:
sed -i "s/epoll_create1/epoll_create/g" ./libev/ev_epoll.c
使用 ldtools 的话,可以在编译二进制文件时指定 -all-static:
sed -i "s/LDFLAGS = /LDFLAGS = -all-static/g" ./src/Makefile
然后就可以:
make
strip ./src/ss-*
make install
-----------

Tomatoware ARM 下静态编译 Pcap_DNSProxy

使用 Tomato 固件,先下载 tomatoware:
CMake: ./bootstrap –prefix=/mmc && make && make install
libsodium: ./configure –prefix=/mmc && make && make install
然后静态编译 Pcap_DNSProxy ,需要修改下源码的 /Pcap_DNSProxy/Source/Pcap_DNSProxy/CMakeLists.txt 文件:
Executable file name
#static
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive")
#static
add_executable(Pcap_DNSProxy ${MAIN_SRC_FILES})
IF(PLATFORM_OPENWRT)
INSTALL(TARGETS Pcap_DNSProxy RUNTIME DESTINATION sbin)
ENDIF(PLATFORM_OPENWRT)
注意:单独使用 -static 编译出来的文件执行会出错,要结合后面那堆参数:
terminate called after throwing an instance of ‘std::system_error’ what(): Enable multithreading to use std::thread: Operation not permitted

已提交,作者已添加:
* 使用 ./Linux_Build.sh 执行编译程序 * 添加参数 –enable-static 即 ./Linux_Build.sh –enable-static 可启用静态编译.
----------------------

在 Tomatoware-arm 下静态编译 Redsocks2


直接编译会出错:
/opt/lib//libcrypto.a(c_zlib.o): In function zlib_stateful_expand_block’: c_zlib.c:(.text+0x54): undefined reference toinflate’ /opt/lib//libcrypto.a(c_zlib.o): In function zlib_stateful_compress_block’: c_zlib.c:(.text+0xd8): undefined reference todeflate’ /opt/lib//libcrypto.a(c_zlib.o): In function bio_zlib_free’: c_zlib.c:(.text+0x128): undefined reference toinflateEnd’ c_zlib.c:(.text+0x144): undefined reference to deflateEnd’ /opt/lib//libcrypto.a(c_zlib.o): In functionzlib_stateful_finish’: c_zlib.c:(.text+0x194): undefined reference to inflateEnd’ c_zlib.c:(.text+0x19c): undefined reference todeflateEnd’ /opt/lib//libcrypto.a(c_zlib.o): In function zlib_stateful_init’: c_zlib.c:(.text+0x224): undefined reference toinflateInit_’ c_zlib.c:(.text+0x26c): undefined reference to deflateInit_’ /opt/lib//libcrypto.a(c_zlib.o): In functionbio_zlib_ctrl’: c_zlib.c:(.text+0x4a0): undefined reference to deflate’ c_zlib.c:(.text+0x56c): undefined reference tozError’ /opt/lib//libcrypto.a(c_zlib.o): In function bio_zlib_write’: c_zlib.c:(.text+0x708): undefined reference todeflate’ c_zlib.c:(.text+0x794): undefined reference to zError’ c_zlib.c:(.text+0x824): undefined reference todeflateInit_’ /opt/lib//libcrypto.a(c_zlib.o): In function bio_zlib_read’: c_zlib.c:(.text+0x900): undefined reference toinflate’ c_zlib.c:(.text+0x954): undefined reference to zError’ c_zlib.c:(.text+0x9e0): undefined reference toinflateInit_’ collect2: error: ld returned 1 exit status Makefile:94: recipe for target ‘redsocks2’ failed make: *** [redsocks2] Error 1
解决方法,修改 Makefile: override LIBS += -lssl -lcrypto -ldl 改成: override LIBS += -lssl -lcrypto -ldl -lz 然后: CCFLAGS=-static make 或者(tomatoware) LDFLAGS="-Wl,-static -static -static-libgcc" make
编译成功。
--------

定时备份 Tomato 上的 Optware 系统脚本

网上的都要用到 find -mtime,这个参数默认的 busybox 是不支持的,当然你可以下 findunti ,我这个脚本无需额外的程序支持:
#!/bin/sh
# optware backup
# Sun Feb 19 00:34:42 CST 2012
# by Punk

keep=3                                                #保留历史备份数
backup_source="opt"                            #需要备份的目录,不可有/开头
backup_dest="/cifs2/hdtv/!opt_bak/"      #备份文件存储目录
date=`date +%Y-%m-%d`
hostname=$(hostname -s)
prename="$hostname-optware"
filename="$prename-$date.tgz"

delete_old() {
echo Deleting old backup of Optware...
totalline=`find "$backup_dest" -name "$prename*.tgz" | wc -l`
if [ $totalline -ge $keep ];then
    dell=$(($totalline-$keep))
    find "$backup_dest" -name "$prename-*.tgz" | sort | head -n $dell | xargs rm -rf
fi
echo Deleting old backup of Optware Finished!
}

backup() {
echo "Backing Up Your Optware System..."
tar czf $backup_dest/$filename -C / $backup_source
echo "Optware Backup Finished!"
}
backup
delete_old
-------------

No comments:

Post a Comment