LoginSignup
14
12

More than 5 years have passed since last update.

RaspberryPi1(2とzeroも)で動かすgolang製アプリをクロスコンパイル(onMac)

Last updated at Posted at 2016-02-24

私NASストレージとしてRaspberryPi1を運用してまして
golangで作られたtorrentClientをインストールしてみたくなりました。
そこで初めてクロスコンパイルしてみました。

超簡単で以下のコマンドのみ

GOOS=linux GOARCH=arm GOARM=6 go build

特に./make.bashとか--cross-compile-allを使ってクロスコンパイル環境を作ることもなく
そのままクロスコンパイルできました。
homebrewでインストールすると最初からクロスコンパイル環境できているんですかね。
(goのバージョンは1.6です。)

ポイントはGOARM=6なところ

http://elinux.org/RPi_HardwareによるとRaspberryPiのCPUは以下となっております。

ボード名 CPU cat /proc/cpuinfo
RaspberryPi(1,zero) 700 MHz ARM11 ARM1176JZF-S core ARMv6-compatible processor rev 7 (v6l)
RaspberryPi2 900MHz quad-core ARM Cortex-A7 ARMv7 Processor rev 5 (v7l)

なのでGOARM=6とすれば1,zero用に
GOARM=7とすれば2用にバイナリが作れるってことですね。

間違ったアーキテクチャでコンパイルすると以下の実行エラーが出ます。

$ cloud-torrent
Illegal instruction

raspberpi1上でアプリをコンパイルしようとしたら凄く時間がかかりまして、Macでクロスコンパイルしたら一瞬でした。

作業履歴

cloud-torrentというgolang製アプリをRaspberryPi1に導入する

バイナリ作成

Macにて以下の作業を実施

$ go get -v github.com/jpillora/cloud-torrent
$ cd $GOPATH/src/github.com/jpillora/cloud-torrent
$ GOOS=linux GOARCH=arm GOARM=6 godep go build 
$ file cloud-torrent
cloud-torrent: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped

scp等で作成したバイナリを/home/pi/bin/に配置

daemonとして設定する

supervisorの導入

RaspberryPi上で作業

$ sudo apt-get update
$ sudo apt-get install supervisor

設定

sudo vim /etc/supervisor/conf.d/cloud_torrent_sv.conf

以下のように記述

/etc/supervisor/conf.d/cloud_torrent_sv.conf
[program:cloud_torrent]
command=/home/pi/bin/cloud-torrent  ; 起動コマンド
numprocs=1   ; 起動するプロセス数
redirect_stderr=true  ; エラー出力を標準出力にリダイレクト
stdout_logfile=/var/log/cloud_torrent.log  ; 標準出力ログのファイル名
user=pi

以下のコマンドで有効化

$ sudo supervisorctl reread
$ sudo supervisorctl add cloud_torrent
$ sudo supervisorctl status
cloud_torrent                    RUNNING    pid 9663, uptime 2:03:24

初期状態では:3000ポートにてアクセス

参考文献

https://github.com/golang/go/wiki/GoArm
http://qiita.com/Jxck_/items/02185f51162e92759ebe
https://thinkit.co.jp/story/2013/08/06/4162
http://raspi.seesaa.net/article/415778601.html

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12