HOWTO: OpenWrt, cgminer & BFL Single

两个月前出掉几张5850后又弄了个BFL Single回来继续挖bitcoin,一直挂在DIY的NAS上使用。前阵子NAS上raid5损坏(已恢复),就有想法换个设备挂Single了。在bitcointalk上看到有人在使用DD-WRT跑cgminer,正好手上有闲置的路由器,咱也研究研究。

0.准备

a.Dualwan WR-500U(可以刷OpenWrt的路由器)

b.Debian虚拟机(linux编译环境)

c.BFL Single(cgminer支持的FPGA挖矿设备)

1.固件

http://downloads.openwrt.org/backfire/10.03.1/http://downloads.openwrt.org/snapshots/trunk/(trunk版本需要64位编译环境) 下载路由器对应版本的固件给路由刷上。 我的路由是brcm47xx系列,下个trunk的openwrt-brcm47xx-squashfs.trx直接在路由的升级页面刷新就OK了。刷新后记得恢复一下默认设置来清除nvram。trunk版本的固件没有包含web配置的组件咋办,那就装个呗,或者直接捅路由的pp :mrgreen: (其实还能用mtd命令清除nvram,不过有的设备操作不当容易变砖,,就不细说了)。

2.编译

http://downloads.openwrt.org/backfire/10.03.1/http://downloads.openwrt.org/snapshots/trunk/ 下载路由器固件对应版本的交叉编译工具链。 我下载的是OpenWrt-Toolchain-brcm47xx-for-mipsel-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2。 在linux编译环境中解压并设置各种变量

1
2
3
4
5
6
7
8
9
10
11
12
13
export TOOLCHAIN_DIR="/root/OpenWrt-Toolchain-brcm47xx-for-mipsel-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mipsel_gcc-4.6-linaro_uClibc-0.9.33.2"
export PATH="$PATH:$TOOLCHAIN_DIR/bin:$TOOLCHAIN_DIR/usr/bin"
export AR="mipsel-openwrt-linux-uclibc-ar"
export AS="mipsel-openwrt-linux-uclibc-as"
export LD="mipsel-openwrt-linux-uclibc-ld"
export NM="mipsel-openwrt-linux-uclibc-nm"
export CC="mipsel-openwrt-linux-gcc"
export CPP="mipsel-openwrt-linux-cpp"
export GCC="mipsel-openwrt-linux-gcc"
export CXX="mipsel-openwrt-linux-g++"
export RANLIB="mipsel-openwrt-linux-uclibc-ranlib"
export LDFLAGS="-L$TOOLCHAIN_DIR/lib -L$TOOLCHAIN_DIR/usr/lib"
export CPPFLAGS="-I$TOOLCHAIN_DIR/include -I$TOOLCHAIN_DIR/usr/include"

下载cgminer的源码开始编译

git clone https://github.com/ckolivas/cgminer.git
cd cgminer
#只启用了Bitforce的支持,参数请参考源里的README
#依赖包就不写了,缺啥就apt-get补啥吧
CFLAGS="-Os -s -Wall -march=mips32 -mtune=mips32 -D_GNU_SOURCE" ./autogen.sh "--disable-opencl --disable-adl --without-curses --without-libudev --disable-icarus --disable-modminer --disable-ztex --enable-bitforce --host=mips-linux"

在编译前源码有个地方要修改下,不然编译会报错终止

cgminer.c: In function 'hashmeter': cgminer.c:3773: warning: implicit declaration of function 'roundl' cgminer.c:3773: warning: incompatible implicit declaration of built-in function 'roundl'

Orz…OpenWrt的math库函数不全 用编辑器打开miner.c 找到

global_hashrate = roundl(rolling) * 1000000;

把roundl改为round之后就可以编译成功了,至于修改之后有啥后果,非码农表示不知道。 编译好的cgminer就是我要的文件。

3.配置

把路由的网络配置好,能够访问互联网就可以了,其它的按需配置吧。 安装程序和硬件需要的包
opkg update opkg install kmod-usb-core kmod-usb-serial kmod-usb-serial-ftdi kmod-usb2 usbutils screen syslog-ng
修改/etc/modules.d/65-usb-serial-ftdi文件内容为
ftdi_sio vendor=0x0403 product=0x6014
重启路由,现在插入设备lsusb和demsg可以看到我的BFL Single了

root@OpenWrt:~# lsusb

Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0000:0000
Bus 001 Device 003: ID 0bda:8187 Realtek Semiconductor Corp. RTL8187 Wireless Adapter
Bus 001 Device 004: ID 0403:6014 Future Technology Devices International, Ltd FT232H Single HS >USB-UART/FIFO IC

root@OpenWrt:~# dmesg

[ 20.844000] usb 1-1.2: new high-speed USB device number 4 using ehci-platform
[ 20.952000] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected
[ 20.960000] usb 1-1.2: Detected FT232H
[ 20.964000] usb 1-1.2: Number of endpoints 2
[ 20.968000] usb 1-1.2: Endpoint 1 MaxPacketSize 512
[ 20.976000] usb 1-1.2: Endpoint 2 MaxPacketSize 512
[ 20.980000] usb 1-1.2: Setting MaxPacketSize 512
[ 20.984000] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0

现在可以把编译好的cgminer传到路由上测试了
scp root@xxx:/ooo/cgminer/cgminer /bin/cgminer chmod +x /bin/cgminer cgminer -o pit.deepbit.net:8332 -u xxx -p ooo -S /dev/ttyUSB0
程序运行起来就基本OK了,接下来把启动脚本加上就打完收工 在/etc/init.d/下创建如下三个文件serialmodules,CGMiner,cgminer.sh serialmodules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh /etc/rc.common
#init script mod from https://github.com/pshep/cgminer/downloads
START=45

prefix=
export PATH=${prefix}/bin:${prefix}/sbin:${prefix}/usr/sbin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=serialmodules
SCRIPT=${0##*/}

EXTRA_COMMANDS="status"

status() {
lsmod grep -e ftdi_sio -e usbserial
}
start() {
KERNEL=`uname -r`
if cd /lib/modules/$KERNEL ; then
logger "${SCRIPT}" "inserting serialmodules"

/sbin/insmod usbserial.ko
/sbin/insmod ftdi_sio.ko vendor=0x0403 product=0x6014

cd - 2>&1 >/dev/null
fi
}
stop() {
KERNEL=`uname -r`
if cd /lib/modules/$KERNEL ; then
logger "${SCRIPT}" "removing serialmodules"

/sbin/rmmod usbserial.ko
/sbin/rmmod ftdi_sio.ko

cd - 2>&1 >/dev/null
fi
}
restart() {
stop
sleep 1
start
}

CGMiner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh /etc/rc.common
#init script mod from https://github.com/pshep/cgminer/downloads
START=99

prefix=
export PATH=${prefix}/bin:${prefix}/sbin:${prefix}/usr/sbin:/sbin:/bin:/usr/sbin:/usr/bin

SCRIPTDIR=${prefix}/etc/init.d
SCRIPT=${0##*/}

[ -e ${CONF} ] logger "${SCRIPT}" "WARNING: ${CONF} does not exist"

start() {
if pidof cgminer >/dev/null ; then
echo "cgminer already running"
else
logger "${SCRIPT}" "Starting cgminer:"
screen -dmS miner ${SCRIPTDIR}/cgminer.sh
fi
}

stop() {
if ! pidof cgminer >/dev/null ; then
echo "cgminer is already stopped"
else
logger "${SCRIPT}" "Stopping cgminer:"
killall -s SIGINT cgminer 2>/dev/null
sleep 3
killall screen 2>/dev/null
fi
}

restart() {
stop
echo "waiting 10 seconds..."
sleep 10
start
}

cgminer.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
#init script mod from https://github.com/pshep/cgminer/downloads
unset LD_LIBRARY_PATH
unset LD_PRELOAD

prefix=
CONFDIR=${prefix}/etc
CONF=${CONFDIR}/cgminer.conf
LOGDIR=${prefix}/var/log
TTYNAME=ttyUSB*
TTYDIR=/dev/

while true ; do
DEVS=`find ${TTYDIR} -type c -name "${TTYNAME}"`
NOW="`date +%Y%m%d-%H%M%S`"
LOGFILE=${LOGDIR}/${NOW}.$$.log
${prefix}/bin/cgminer -c ${CONF} --api-listen -S ${DEVS} -T -Q 2 -q 2>${LOGFILE}
sleep 30
done

赋予三个文件可执行权限
chmod +x serialmodules CGMiner cgminer.sh
设置为开机启动
/etc/init.d/serialmodules enbale /etc/init.d/CGMiner enbale
别忘了在/etc下面创建cgminer的配置文件,给个范例 cgminer.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"pools" : [
{
"url" : "pit.deepbit.net:8332",
"user" : "xxx",
"pass" : "ooo"
},
{
"url" : "mine3.btcguild.com:8332",
"user" : "xxx",
"pass" : "ooo"
}
],

"failover-only" : true,
"api-allow" : "W:192.168.0/24"
}

完鸟。

4.补充

可以安装http+php来使用源码里的miner.php查看cgminer的状态。