into the void

ソフトウェアに関する雑多な調査日記

syslogの使い方をあらためて整理してみた

syslogを使ったログ管理の方法をあらためて整理してみる。
syslogの実装は幾つかあるが、Ubuntuで標準搭載されているrsyslogを使うことにする。

OS Ubuntu Desktop 14.04.4 LTS
rsyslog rsyslog:amd64/trusty-updates 7.4.4-1ubuntu2.6 uptodate
logrotate logrotate:amd64/trusty 3.8.7-1ubuntu1 uptodate

ログの出力先を設定する

rsyslogの設定は下記のファイルで行う。

/etc/rsyslog.conf 共通設定
/etc/rsyslog.d/*.conf カスタマイズ設定

通常はrsyslog.confの方は編集せず、/etc/rsyslog.d/フォルダにファイルを追加して、そこにカスタマイズしたい設定を記入していく。

今回は自分で作ったアプリが出力するログについて、/var/log/myapp.logに出力する設定を追加する。

追加する設定ファイル /etc/rsyslog.d/myapp.conf
ログの出力先 /var/log/myapp.log
ログのファリシリティ local1
ログのレベル info以上

/etc/rsyslog.d/myapp.conf

$ cat /etc/rsyslog.d/myapp.conf 
local1.info	/var/log/myapp.log

rsyslogサービスを再起動する。

$ sudo services rsyslog restart

試しにloggerコマンドを使ってログを出力してみる。

$ logger -p local1.info "hello"

ちゃんと/var/log/myapp.confに出力されていることを確認する。

$ tail -f /var/log/myapp.log 
Sep 10 15:49:55 ik1-303-11997 samehada: hello

もう少し出力してみる。

$ logger -p local1.emerg 'it is emerg log'
$ logger -p local1.alert 'it is alert log'
$ logger -p local1.debug 'it is debug log'

myapp.logにはinfo以上のプライオリティのログが出力されている。debugプラオリティのログは出力されない。

$ tail -f /var/log/myapp.log 
Sep 10 15:49:55 ik1-303-11997 samehada: hello
Sep 10 15:53:22 ik1-303-11997 samehada: it is emerg log
Sep 10 15:53:32 ik1-303-11997 samehada: it is alert log

/var/log/syslogの方にはdebugログの出力されている。

$ tail -f /var/log/syslog
Sep 10 15:49:55 ik1-303-11997 samehada: hello
Sep 10 15:53:01 ik1-303-11997 samehada: emerg log
Sep 10 15:53:22 ik1-303-11997 samehada: it is emerg log
Sep 10 15:53:32 ik1-303-11997 samehada: it is alert log
Sep 10 15:54:28 ik1-303-11997 samehada: it is debug log

ログのフォーマットを設定する

いまログに出力されているのは

ログの出力時刻 %timegenerated%
ホスト名 %hostname%
タグ %syslogtag%
ログメッセージ %msg%

ファィリティとプライオリティも出力してみる。

ログのファリシティ %syslogfacility-text%
ログのプライオリティ %syslogpriority-text%

あとでデータベースに入れやすいように、デリミタをスペースからカンマに変えておく。

/var/log/myapp.conf

$ cat /etc/rsyslog.d/myapp.conf 
$template mytemplate, "%timegenerated%,%syslogfacility-text%,%syslogpriority-text%,%syslogtag%,%msg%\n"
local1.info	/var/log/myapp.log;mytemplate

出力してみる。

$ logger -p local1.info -t MYAPP 'start MYAPP'
$ cat /var/log/myapp.conf
Sep 10 17:24:44,local1,info,MYAPP:, start MYAPP

ログの出力時刻が秒精度でしか出ていないので、もっと高精度な値になるようにオプションを追加する。

ログの出力時刻 %timegenerated%:::date-rfc3339

最終的な設定ファイル。

$ cat /etc/rsyslog.d/myapp.conf 
$template mytemplate, "%timegenerated:::date-rfc3339%,%syslogfacility-text%,%syslogpriority-text%,%syslogtag%,%msg%\n"
local1.info	/var/log/myapp.log;mytemplate

ログ。マイクロ秒まで出力されている。

2016-09-11T20:23:36.244600+09:00,local1,info,TEST001:, this is a test
2016-09-11T20:23:46.247719+09:00,local1,info,TEST001:, this is a test

ログのローテーションの仕方を設定する

シスログで出力されるファイルは、logrotateによって定期的にローテート、圧縮される。
この設定は/etc/logrotate.d/rsyslogに書かれている。

$ cat /etc/logrotate.d/rsyslog 
/var/log/syslog
{
	rotate 7
	daily
	missingok
	notifempty
	delaycompress
	compress
	postrotate
		reload rsyslog >/dev/null 2>&1 || true
	endscript
}

/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
	rotate 4
	weekly
	missingok
	notifempty
	compress
	delaycompress
	sharedscripts
	postrotate
		reload rsyslog >/dev/null 2>&1 || true
	endscript
}

syslogの設定

rotate 6 バックアップするログの数。6ファイル。
daily 毎日ローテートする。
missingok 出力先のログファイルが存在しなくてもエラー処理しない。
notifempty ログファイルのサイズが0の場合はローテートしない。
delaycompress ローテート直後に圧縮せず、次のファイルのローテート時に圧縮する。
compress 古いファイルを圧縮する
postrotate ローテート後に実行する処理。rsyslogをreloadする。

ログファイルの状況。

$ ls -al /var/log/syslog*
-rw-r----- 1 syslog adm 149633 Sep 10 18:05 /var/log/syslog
-rw-r----- 1 syslog adm  59460 Sep 10 06:25 /var/log/syslog.1
-rw-r----- 1 syslog adm   3957 Sep  9 06:25 /var/log/syslog.2.gz
-rw-r----- 1 syslog adm   2293 Sep  8 06:25 /var/log/syslog.3.gz
-rw-r----- 1 syslog adm   1166 Sep  7 06:25 /var/log/syslog.4.gz
-rw-r----- 1 syslog adm   1233 Sep  6 06:25 /var/log/syslog.5.gz
-rw-r----- 1 syslog adm   1290 Sep  5 06:25 /var/log/syslog.6.gz
-rw-r----- 1 syslog adm   2367 Sep  4 06:25 /var/log/syslog.7.gz

ローテート処理のトリガーはlogrotate自身ではなく、crondからひいてもらう。
そのため、crond側に設定が必要。通常はlogrotateをインストールすると/etc/cron.daily/logroateが作成される。

myappのログもsyslogと同じように毎日ローテートされるようにしてみる。
バックアップは一ヶ月(30日)分。圧縮あり。

$ cat /etc/logrotate.d/rsyslog 
/var/log/myapp.log
{
	rotate 30
	daily
	missingok
	notifempty
	delaycompress
	compress
	postrotate
		reload rsyslog > /dev/null 2>&1 || true
	endscript
}
...

ログをデータベースに格納してクエリで検索する

圧縮したログを解凍して、mysqlデータベースに入れてみる。

まずはmysqlをインストール。

$ sudo apt-get install mysql-server

ログ出力時刻をTIMESTAMP型でマイクロ秒まで格納したかったんだけど、mysqlでは5.6.4以降でDATE、DATETIME、TIMESTAMPの精度がマイクロ秒まで引き上げられた模様。
残念ながらubuntu 14.04.4のaptでインストールされるのは5.5。

5.6を入れ直す。

sudo apt-get install mysql-server-5.6

データベースとテーブルを作成。

mysql> create database myapp_log;
Query OK, 1 row affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myapp_log          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> create table myapp_info (timegenerated DATETIME(6), facility CHAR(10), priority CHAR(10), tag CHAR(10), msg VARCHAR(100));
Query OK, 0 rows affected (1.29 sec)

mysql> show tables;
+---------------------+
| Tables_in_myapp_log |
+---------------------+
| myapp_info          |
+---------------------+
2 rows in set (0.01 sec)

mysql> desc myapp_info;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| timegenerated | datetime(6)  | YES  |     | NULL    |       |
| facility      | char(10)     | YES  |     | NULL    |       |
| priority      | char(10)     | YES  |     | NULL    |       |
| tag           | char(10)     | YES  |     | NULL    |       |
| msg           | varchar(100) | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

DockerをクロスコンパイルしてRaspberry Pi 3で動かしてみる

Dockerのソースコードをダウンロードして、ARMv7向けにクロスコンパイルしてみる。
クロスコパイル環境としてはさくらVPS上のUbuntuを使った。

まずはgithubで公開されているソースコードをダウンロード。

$ git clone https://github.com/docker/docker

make crossとするとクロスコンパイル用のDockerイメージがダウンロードされて、コンテナ上でクロスコンパイルが実行されるという仕組み。
ロスコンパイル用のツールチェーインなど用意する必要がなく非常に便利。

代わりにdocker環境は必要になるがすでにインストール済み。
今回のクロスビルドは一般ユーザでやりたいので、一般ユーザからもdockerコマンドが実行できるように、対象のユーザをdockerグループに追加していく。

$ sudo gpasswd -a <username> docker
$ make cross

x86環境のdockerバイナリをみると、依存ファイルがそこそこたくさんある。
クロスビルドした場合、これらの依存ライブラリはどういう形で提供されるのか? 考えながらmake完了を待つ。

$ ldd /usr/bin/docker
	linux-vdso.so.1 =>  (0x00007ffdd75b3000)
	libapparmor.so.1 => /usr/lib/x86_64-linux-gnu/libapparmor.so.1 (0x00007ff363654000)
	libsystemd-journal.so.0 => /lib/x86_64-linux-gnu/libsystemd-journal.so.0 (0x00007ff363439000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff36321b000)
	libdevmapper.so.1.02.1 => /lib/x86_64-linux-gnu/libdevmapper.so.1.02.1 (0x00007ff362fe2000)
	libltdl.so.7 => /usr/lib/x86_64-linux-gnu/libltdl.so.7 (0x00007ff362dd8000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff362a13000)
	libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007ff3627f0000)
	libcgmanager.so.0 => /lib/x86_64-linux-gnu/libcgmanager.so.0 (0x00007ff3625d5000)
	libnih.so.1 => /lib/x86_64-linux-gnu/libnih.so.1 (0x00007ff3623bd000)
	libnih-dbus.so.1 => /lib/x86_64-linux-gnu/libnih-dbus.so.1 (0x00007ff3621b3000)
	libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007ff361f6e000)
	liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff361d4c000)
	libgcrypt.so.11 => /lib/x86_64-linux-gnu/libgcrypt.so.11 (0x00007ff361acc000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff3618c4000)
	/lib64/ld-linux-x86-64.so.2 (0x00007ff363860000)
	libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x00007ff3616b3000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff3614af000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007ff361271000)
	libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff36106c000)

残念なことにメモリ不足でビルドが途中でエラー…。1GBでは足りなかったらしい。

---> Making bundle: dynbinary (in bundles/1.12.0-dev/dynbinary)
Building: bundles/1.12.0-dev/dynbinary-client/docker-1.12.0-dev
Created binary: bundles/1.12.0-dev/dynbinary-client/docker-1.12.0-dev
Building: bundles/1.12.0-dev/dynbinary-daemon/dockerd-1.12.0-dev
# github.com/docker/docker/cmd/dockerd
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: fork/exec /usr/bin/gcc: cannot allocate memory

make: *** [cross] Error 1

real	20m18.299s
user	0m4.724s
sys	0m2.415s

仕方ないのでMac用のDockerを使ってローカルでビルドしてみる。
https://docs.docker.com/docker-for-mac/

次はapt-getでエラー。IPv6でアクセスすると繋がらないサイトがあるらしい…。

E: Failed to fetch http://httpredir.debian.org/debian/pool/main/p/python-mock/python-mock_1.0.1-3_all.deb  Cannot initiate the connection to mirrors.tuna.tsinghua.edu.cn:80 (2402:f000:1:416:166:111:206:63). - connect (101: Network is unreachable) [IP: 2402:f000:1:416:166:111:206:63 80]

ルータのIPv6設定を無効化してリトライ。

...
Building: bundles/1.12.0-dev/cross/linux/386/docker-1.12.0-dev
Created binary: bundles/1.12.0-dev/cross/linux/386/docker-1.12.0-dev
Building: bundles/1.12.0-dev/cross/linux/arm/docker-1.12.0-dev
Created binary: bundles/1.12.0-dev/cross/linux/arm/docker-1.12.0-dev
...

real	29m40.215s
user	0m3.759s
sys	0m2.175s

今度は成功。様々な環境用にクロスビルドされるため、30分近くかかった…。

$ find ./bundles
./bundles
./bundles/1.12.0-dev
./bundles/1.12.0-dev/binary-client
./bundles/1.12.0-dev/binary-client/docker
./bundles/1.12.0-dev/binary-client/docker-1.12.0-dev
./bundles/1.12.0-dev/binary-client/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/binary-client/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/binary-daemon
./bundles/1.12.0-dev/binary-daemon/docker-containerd
./bundles/1.12.0-dev/binary-daemon/docker-containerd-ctr
./bundles/1.12.0-dev/binary-daemon/docker-containerd-ctr.md5
./bundles/1.12.0-dev/binary-daemon/docker-containerd-ctr.sha256
./bundles/1.12.0-dev/binary-daemon/docker-containerd-shim
./bundles/1.12.0-dev/binary-daemon/docker-containerd-shim.md5
./bundles/1.12.0-dev/binary-daemon/docker-containerd-shim.sha256
./bundles/1.12.0-dev/binary-daemon/docker-containerd.md5
./bundles/1.12.0-dev/binary-daemon/docker-containerd.sha256
./bundles/1.12.0-dev/binary-daemon/docker-proxy
./bundles/1.12.0-dev/binary-daemon/docker-proxy-1.12.0-dev
./bundles/1.12.0-dev/binary-daemon/docker-proxy-1.12.0-dev.md5
./bundles/1.12.0-dev/binary-daemon/docker-proxy-1.12.0-dev.sha256
./bundles/1.12.0-dev/binary-daemon/docker-runc
./bundles/1.12.0-dev/binary-daemon/docker-runc.md5
./bundles/1.12.0-dev/binary-daemon/docker-runc.sha256
./bundles/1.12.0-dev/binary-daemon/dockerd
./bundles/1.12.0-dev/binary-daemon/dockerd-1.12.0-dev
./bundles/1.12.0-dev/binary-daemon/dockerd-1.12.0-dev.md5
./bundles/1.12.0-dev/binary-daemon/dockerd-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross
./bundles/1.12.0-dev/cross/darwin
./bundles/1.12.0-dev/cross/darwin/amd64
./bundles/1.12.0-dev/cross/darwin/amd64/docker
./bundles/1.12.0-dev/cross/darwin/amd64/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/darwin/amd64/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/darwin/amd64/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/freebsd
./bundles/1.12.0-dev/cross/freebsd/386
./bundles/1.12.0-dev/cross/freebsd/386/docker
./bundles/1.12.0-dev/cross/freebsd/386/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/freebsd/386/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/freebsd/386/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/freebsd/amd64
./bundles/1.12.0-dev/cross/freebsd/amd64/docker
./bundles/1.12.0-dev/cross/freebsd/amd64/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/freebsd/amd64/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/freebsd/amd64/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/freebsd/arm
./bundles/1.12.0-dev/cross/freebsd/arm/docker
./bundles/1.12.0-dev/cross/freebsd/arm/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/freebsd/arm/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/freebsd/arm/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/linux
./bundles/1.12.0-dev/cross/linux/386
./bundles/1.12.0-dev/cross/linux/386/docker
./bundles/1.12.0-dev/cross/linux/386/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/linux/386/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/linux/386/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/linux/amd64
./bundles/1.12.0-dev/cross/linux/amd64/docker
./bundles/1.12.0-dev/cross/linux/amd64/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/linux/amd64/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-ctr
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-ctr.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-ctr.sha256
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-shim
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-shim.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd-shim.sha256
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-containerd.sha256
./bundles/1.12.0-dev/cross/linux/amd64/docker-proxy
./bundles/1.12.0-dev/cross/linux/amd64/docker-proxy-1.12.0-dev
./bundles/1.12.0-dev/cross/linux/amd64/docker-proxy-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-proxy-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/linux/amd64/docker-runc
./bundles/1.12.0-dev/cross/linux/amd64/docker-runc.md5
./bundles/1.12.0-dev/cross/linux/amd64/docker-runc.sha256
./bundles/1.12.0-dev/cross/linux/amd64/dockerd
./bundles/1.12.0-dev/cross/linux/amd64/dockerd-1.12.0-dev
./bundles/1.12.0-dev/cross/linux/amd64/dockerd-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/linux/amd64/dockerd-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/linux/arm
./bundles/1.12.0-dev/cross/linux/arm/docker
./bundles/1.12.0-dev/cross/linux/arm/docker-1.12.0-dev
./bundles/1.12.0-dev/cross/linux/arm/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/cross/linux/arm/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/cross/windows
./bundles/1.12.0-dev/cross/windows/386
./bundles/1.12.0-dev/cross/windows/386/docker-1.12.0-dev.exe
./bundles/1.12.0-dev/cross/windows/386/docker-1.12.0-dev.exe.md5
./bundles/1.12.0-dev/cross/windows/386/docker-1.12.0-dev.exe.sha256
./bundles/1.12.0-dev/cross/windows/386/docker.exe
./bundles/1.12.0-dev/cross/windows/amd64
./bundles/1.12.0-dev/cross/windows/amd64/docker-1.12.0-dev.exe
./bundles/1.12.0-dev/cross/windows/amd64/docker-1.12.0-dev.exe.md5
./bundles/1.12.0-dev/cross/windows/amd64/docker-1.12.0-dev.exe.sha256
./bundles/1.12.0-dev/cross/windows/amd64/docker-proxy-1.12.0-dev.exe
./bundles/1.12.0-dev/cross/windows/amd64/docker-proxy-1.12.0-dev.exe.md5
./bundles/1.12.0-dev/cross/windows/amd64/docker-proxy-1.12.0-dev.exe.sha256
./bundles/1.12.0-dev/cross/windows/amd64/docker-proxy.exe
./bundles/1.12.0-dev/cross/windows/amd64/docker.exe
./bundles/1.12.0-dev/cross/windows/amd64/dockerd-1.12.0-dev.exe
./bundles/1.12.0-dev/cross/windows/amd64/dockerd-1.12.0-dev.exe.md5
./bundles/1.12.0-dev/cross/windows/amd64/dockerd-1.12.0-dev.exe.sha256
./bundles/1.12.0-dev/cross/windows/amd64/dockerd.exe
./bundles/1.12.0-dev/dynbinary
./bundles/1.12.0-dev/dynbinary-client
./bundles/1.12.0-dev/dynbinary-client/docker
./bundles/1.12.0-dev/dynbinary-client/docker-1.12.0-dev
./bundles/1.12.0-dev/dynbinary-client/docker-1.12.0-dev.md5
./bundles/1.12.0-dev/dynbinary-client/docker-1.12.0-dev.sha256
./bundles/1.12.0-dev/dynbinary-daemon
./bundles/1.12.0-dev/dynbinary-daemon/docker-proxy
./bundles/1.12.0-dev/dynbinary-daemon/docker-proxy-1.12.0-dev
./bundles/1.12.0-dev/dynbinary-daemon/docker-proxy-1.12.0-dev.md5
./bundles/1.12.0-dev/dynbinary-daemon/docker-proxy-1.12.0-dev.sha256
./bundles/1.12.0-dev/dynbinary-daemon/dockerd
./bundles/1.12.0-dev/dynbinary-daemon/dockerd-1.12.0-dev
./bundles/1.12.0-dev/dynbinary-daemon/dockerd-1.12.0-dev.md5
./bundles/1.12.0-dev/dynbinary-daemon/dockerd-1.12.0-dev.sha256
./bundles/latest

目的のファイルはdocker-1.12.0-dev。大きな一つの実行可能バイナリになっている様子。

$ ls -al ./bundles/latest/cross/linux/arm/
total 22440
drwxr-xr-x  6 shizuku  staff       204  7  8 23:42 .
drwxr-xr-x  5 shizuku  staff       170  7  8 23:41 ..
lrwxrwxrwx  1 shizuku  staff        17  7  8 23:42 docker -> docker-1.12.0-dev
-rwxr-xr-x  1 shizuku  staff  11475574  7  8 23:42 docker-1.12.0-dev
-rw-r--r--  1 shizuku  staff        52  7  8 23:42 docker-1.12.0-dev.md5
-rw-r--r--  1 shizuku  staff        84  7  8 23:42 docker-1.12.0-dev.sha256

docker-1.12.0-devをraspberry pi 3上に持って行ってdaemonオプション付きで実行してみたが、下記のエラーが発生。

pi@raspberrypi:~ $ ./docker-1.12.0-dev daemon
`docker daemon` is not supported on Linux. Please run `dockerd` directly

dockerdなんてバイナリファイルはビルドされていない…。

debパッケージにもできるらしいのでやってみる。

Mac上のDockerではなぜか失敗。

仮想マシン上のUbuntu 14.04で再トライしたが失敗。

仕方がないので、ビルド済みのdebパッケージをダウンロードしてきてdpkgでインストール。
http://blog.hypriot.com/downloads/

動いた!

$ ps axuw | grep docker
root      1293 23.3  3.8 894328 36180 ?        Ssl  23:29   1:51 /usr/bin/docker daemon -H fd:// --storage-driver=overlay -D

イメージはarmhf用のものを指定する必要がある。ふつうにdocker run hello-worldとやるとx86用のhello-worldイメージが落ちてきてexec format errorとなる。

$ sudo docker search armhf
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
armv7/armhf-ubuntu                         'official' Ubuntu Docker images for the AR...   31                   
container4armhf/armhf-alpine               Automatically built base images of Alpine ...   22                   [OK]
ioft/armhf-ubuntu                          [ABR] Ubuntu Docker images for the ARMv7(a...   14                   [OK]
armhf/ubuntu                               Ubuntu is a Debian-based Linux operating s...   5                    
armhf/debian                               Debian is a Linux distribution that's comp...   3                    
moul/armhf-busybox                                                                         2                    [OK]
werwolfby/armhf-alpine-transmission        Minimal alpine docker image for transmissi...   2                    [OK]
cburki/armhf-dev                           Armhf (Raspberry Pi) cross development wit...   2                    [OK]
ioft/armhf-debian                          Debian Docker images for the ARMv7(armhf) ...   1                    [OK]
werwolfby/armhf-alpine-gogs                ARM hf compatible Docker Image with a mini...   1                    [OK]
werwolfby/armhf-alpine-nginx               ARM hf compatible Docker Image with a mini...   1                    [OK]
msvb/armhf-iotempire                       Collection of software packages to accompa...   0                    [OK]
lalyos/armhf-syncthing                     http://docs.syncthing.net in a container f...   0                    [OK]
sheenhx/armhf-concentrator                 CC3200 serial concentrator for Beagle Bone...   0                    [OK]
ctarwater/armhf-alpine-rpi-glibc-builder   Docker container to build an Alpine glibc ...   0                    [OK]
ijoijo/armhf-alpine                        Alpine Linux image for armhf devices (like...   0                    [OK]
cricketeerone/armhf-logspout               A fork of gliderlabs/logspout using armhfb...   0                    [OK]
armv7/armhf-ubuntu_automated-build         This repo has moved to https://registry.hu...   0                    [OK]
kennethlimcp/armhf-ghost                   The awesome Ghost blog, on armhf                0                    [OK]
wontfix/gecko-armhf-dev                    Linux/armhf cross build environment for Gecko   0                    [OK]
mathewpeterson/armhf-php7                  This image extends the official armhf php ...   0                    [OK]
cailloumajor/debian-armhf-qemu             Debian armhf image with qemu-user-static        0                    [OK]
container4armhf/armhf-busybox              Automated build of Busybox for armhf devic...   0                    [OK]
fish/alpine-armhf-dumb-init                alpine-armhf-docker image with "dumb-init"...   0                    [OK]
mjschultz/ubuntu-armhf                                                                     0                    [OK]

登録されているイメージはあんまりない。

とりあえずUbuntuの公式イメージを動かしてみる。

pi@raspberrypi:~ $ sudo docker run -it armv7/armhf-ubuntu bash
root@1f7f91e7e990:/# uname -a
Linux 1f7f91e7e990 4.4.7-v7+ #1 SMP Thu Apr 21 02:16:07 JST 2016 armv7l armv7l armv7l GNU/Linux

Ubuntu動いた!

次はdebでいれたdockerバイナリを自分でビルドしたdockerバイナリと入れ替えてみる。

pi@raspberrypi:~ $ cd /usr/bin
pi@raspberrypi:/usr/bin $ which docker
/usr/bin/docker
pi@raspberrypi:/usr/bin $ sudo mv docker docker.org
pi@raspberrypi:/usr/bin $ sudo cp ~/docker-1.12.0-dev docker
pi@raspberrypi:/usr/bin $ docker -v
Docker version 1.12.0-dev, build dd1a27c

pi@raspberrypi:/usr/bin $ sudo service docker restart
Job for docker.service failed. See 'systemctl status docker.service' and 'journalctl -xn' for details.

起動失敗。

自分でビルドしたバイナリを使うのはあきらめた…。

さくらVPSでQEMUを動かす

QEMU、さくらVPSでも動かしてみる。
QEMU用のウィンドウが開けるように、デスクトップ環境をインストールする。
さらにvnc serverも入れてリモートデスクトップ的につなぐ。

下記のサイトを参考にした。
さくら VPS に Linux Mint 17 (Xfce) を入れて VNC でデスクトップを利用する - tilfin's note

$ sudo apt-get install qemu
$ sudo apt-get install xfce4
$ sudo apt-get install vnc4server

vncサーバの設定。

$ vncserver :2
$ vncserver -kill :2

$ cat .vnc/xstartup 
#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
exec xfce4-session &

さらにvncでつなぐときのパスワード設定をvncpasswdコマンドで設定しておく。

ここまで準備ができたらMacのFinderから「サーバに接続」で、サーバのIP: 5902に接続。

なぜかXtermを起動して文字を入力すると固まる…。

xfceの標準ターミナル(?)であるxfce-terminalを追加でインストール。こちらはまともに入力できた。

qemuを起動。
動くには動くがとても重い。CPUスペック的にはローカルPCよりいいはずだけど、ローカルPC上の仮想マシン上のLinuxで動かしたQEMUより全然重い。

f:id:samehada_shiro:20160716223657j:plain

今回の目的とは関係がないけれど、VNC経由でYouTubeも見られる。音出てないけど。
VPS側の送信速度は20Mbps以上出ていて、動画視聴可能なレベル。
f:id:samehada_shiro:20160716225338j:plain

qemu上でDebian on ARM (armhf) を動かす

qemu上でARMv7(hardfloat)環境をエミューレートして、その上でDebian Linuxを動かしてみる。
qemuを動かす環境はVMWare上のUbuntu14.04。

まずはqemuをインストール。

$ sudo apt-get install qemu

次に ARM用のqemuイメージを下記のサイトからダウンロードしてくる。
カーネルイメージとinitrdイメージと、rootfsイメージ。
https://people.debian.org/~aurel32/qemu/armhf/

$ wget https://people.debian.org/~aurel32/qemu/armhf/debian_wheezy_armhf_standard.qcow2 .
$ wget https://people.debian.org/~aurel32/qemu/armhf/initrd.img-3.2.0-4-vexpress .
$ wget https://people.debian.org/~aurel32/qemu/armhf/vmlinuz-3.2.0-4-vexpress .

実行。

qemu-system-arm -M vexpress-a9 -kernel vmlinuz-3.2.0-4-vexpress -initrd initrd.img-3.2.0-4-vexpress -drive if=sd,file=debian_wheezy_armhf_standard.qcow2 -append "root=/dev/mmcblk0p2"

サクッと起動した。なぜかネットワークも使えて、apt-getもいける。

qemuwikiに書いてあった。
Documentation/Networking - QEMU

ホストと相互に通信したい場合は、bridgeの設定が必要。また今度やる。

ELF for ARM(リロケーションの仕組み)

ARMv7向けにコンパイルしたオブジェクトファイルを解析してリローケションの仕組みを調べてみた。

関数アドレスのリロケーション

外部関数の呼び出しを行っている箇所は、コンパイル時には関数のアドレスが不明。
そのため、リンク時までは具体的なアドレスは入れずに、再配置対象のシンボルとして管理しておく。
再配置処理は命令セットによってやり方が異なるため、ARMならARM用の、x86ならx86用、x64ならx64用の再配置処理が実装されている。

下記のシンプルなコードで再配置の様子を調べてみた。

対象コード

main.c

int main(void){
	hello();
	bye();
	return 0;
}

sub1.c

int gVal1 = 1;
int gVal2 = 2;
int gVal3 = 3;

void hello(){
	gVal2 = gVal1 + 1;
}

void bye(){
	gVal3 = gVal3 + 1;
}

コンパイルと逆アセンブル

ARM用のクロスコンパイラを使ってmain.cをコンパイル

$ arm-linux-gnueabihf-gcc -c main.c

ARM用のオブジェクトファイルができていることを確認。

$ file ./main.o
./main.o: ELF 32-bit LSB  relocatable, ARM, EABI5 version 1 (SYSV), not stripped

objdumpで逆アセンブル

$ arm-linux-gnueabihf-objdump -d main.o

main.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <main>:
   0:	e92d4800 	push	{fp, lr}
   4:	e28db004 	add	fp, sp, #4
   8:	ebfffffe 	bl	0 <hello>
   c:	ebfffffe 	bl	0 <bye>
  10:	e3a03000 	mov	r3, #0
  14:	e1a00003 	mov	r0, r3
  18:	e8bd8800 	pop	{fp, pc}

外部関数hello()とbye()を呼び出しているところに注目。

   8:	ebfffffe 	bl	0 <hello>
   c:	ebfffffe 	bl	0 <bye>

ブランチ命令(bl)のオペランドが0 といった形で、通常はここが分岐先のアドレスになるはず。
代わりに外部関数のシンボル名(hello, bye)が表示されている。
blのオペコードは0xeb。つづく3byteがジャンプ先のアドレスを示すオペランド。ここがhelloもbyeも0xfffffeになっている。

.rel.textセクションの解析

アセンブルした結果は、そこが再配置対象のコードで、そのシンボル名がhelloであるということが分かるように表示されている。
しかし、コード自体は、ebfffffeとなっているたけで、fffffeというオペコードから再配置対象らしいということは推測できるものの、helloというシンボルとの対応付けができるだけの情報は含まれていない。

ELF形式のオブジェクトファイルには命令コードが格納される.textの他に下記のセクションが格納されている。

$ arm-linux-gnueabihf-readelf -S ./main.o
There are 11 section headers, starting at offset 0x138:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000034 00001c 00  AX  0   0  4
  [ 2] .rel.text         REL             00000000 0003cc 000010 08      9   1  4
  [ 3] .data             PROGBITS        00000000 000050 000000 00  WA  0   0  1
  [ 4] .bss              NOBITS          00000000 000050 000000 00  WA  0   0  1
  [ 5] .comment          PROGBITS        00000000 000050 00005c 01  MS  0   0  1
  [ 6] .note.GNU-stack   PROGBITS        00000000 0000ac 000000 00      0   0  1
  [ 7] .ARM.attributes   ARM_ATTRIBUTES  00000000 0000ac 000031 00      0   0  1
  [ 8] .shstrtab         STRTAB          00000000 0000dd 000059 00      0   0  1
  [ 9] .symtab           SYMTAB          00000000 0002f0 0000c0 10     10   9  4
  [10] .strtab           STRTAB          00000000 0003b0 00001a 00      0   0  1

再配置に関する情報は.text.relセクションにまとめられている。
readelfで.text.relセクションを参照するとこんな感じで表示される。

Relocation section '.rel.text' at offset 0x3cc contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000008  00000a1c R_ARM_CALL        00000000   hello
0000000c  00000b1c R_ARM_CALL        00000000   bye

オブジェクトファイルのバイナリは下記。0x3CCから。

000003C0  68 65 6C 6C 6F 00 62 79 65 00 00 00 08 00 00 00 1C 0A 00 00 hello.bye...........
000003D4  0C 00 00 00 1C 0B 00 00                                     ........

.text.relのフォーマットは下記。定義はbinutilsソースコードに含まれるものを参照した。binutilsのソースは
http://ftp.gnu.org/gnu/binutils/からダウンロードできる。
include/elf/external.h

/* Relocation Entries */
typedef struct {
  unsigned char r_offset[4];    /* Location at which to apply the action */
  unsigned char r_info[4];      /* index and type of relocation */
} Elf32_External_Rel;

前半4byteが再配置対象のオフセット。後半4byteが再配置のタイプと、シンボルのシンボルテーブル上の位置情報。
0x3CCから始まるエントリの場合、前半が0x08000000なので、対象オフセットが0x08。逆アセンブルされた命令コード部みると。

00000000 <main>:
   0:	e92d4800 	push	{fp, lr}
   4:	e28db004 	add	fp, sp, #4
   8:	ebfffffe 	bl	0 <hello>
   c:	ebfffffe 	bl	0 <bye>
  10:	e3a03000 	mov	r3, #0
  14:	e1a00003 	mov	r0, r3
  18:	e8bd8800 	pop	{fp, pc}

たしかにオフセット0x08はhelloの呼び出し部。
後半が0x1C0A0000。1Cが再配置のタイプ。readelfの出力を信じればR_ARM_CALL。binutilsのソース上でも一致。
include/elf/arm.h

  RELOC_NUMBER (R_ARM_PLT32,             27)   /* deprecated - 32 bit PLT address.  */
  RELOC_NUMBER (R_ARM_CALL,              28)
  RELOC_NUMBER (R_ARM_JUMP24,            29)

0x0A0000がシンボルテーブル上の位置。
readelfでシンボルテーブルを表示すると

Symbol table '.symtab' contains 12 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS main.c
     2: 00000000     0 SECTION LOCAL  DEFAULT    1 
     3: 00000000     0 SECTION LOCAL  DEFAULT    3 
     4: 00000000     0 SECTION LOCAL  DEFAULT    4 
     5: 00000000     0 NOTYPE  LOCAL  DEFAULT    1 $a
     6: 00000000     0 SECTION LOCAL  DEFAULT    6 
     7: 00000000     0 SECTION LOCAL  DEFAULT    5 
     8: 00000000     0 SECTION LOCAL  DEFAULT    7 
     9: 00000000    28 FUNC    GLOBAL DEFAULT    1 main
    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND hello
    11: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND bye

たしかに0x0A = 10のエントリにhelloの情報が登録されている。
ここまでで大体のからくりが分かった。
あと不思議なのが、再配置対象のオフセット0x08はbl命令のオフセット。0xebfffffeのどこをhelloのアドレスで置き換えればよいかの情報がここまでで出てきていない。
ヒントは再配置タイプのR_ARM_CALL。binutilsのソースの中を探してみたら、再配置タイプ別の処理定義をしている部分を発見。
bad/elf32-arm.c

 HOWTO (R_ARM_CALL,            /* type */
         2,                     /* rightshift */
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
         24,                    /* bitsize */
         TRUE,                  /* pc_relative */
         0,                     /* bitpos */
         complain_overflow_signed,/* complain_on_overflow */
         bfd_elf_generic_reloc, /* special_function */
         "R_ARM_CALL",          /* name */
         FALSE,                 /* partial_inplace */
         0x00ffffff,            /* src_mask */
         0x00ffffff,            /* dst_mask */
         TRUE),                 /* pcrel_offset */

src_mask、dst_maskで0xebfffffeのどこが書き換え対象を指定している模様。オペコードblのマシン語は0xebなので、そこを除く24bitをhelloのアドレスで再配置時に書き換えるものと思われる。

再配置後(リンク語)のコード

main.oとsub1.oをリンクしてmainという実行可能バイナリを作成。これを逆アセンブルした。

000083e4 <main>:
    83e4:	e92d4800 	push	{fp, lr}
    83e8:	e28db004 	add	fp, sp, #4
    83ec:	eb000003 	bl	8400 <hello>
    83f0:	eb00000e 	bl	8430 <bye>
    83f4:	e3a03000 	mov	r3, #0
    83f8:	e1a00003 	mov	r0, r3
    83fc:	e8bd8800 	pop	{fp, pc}

00008400 <hello>:
    8400:	e52db004 	push	{fp}		; (str fp, [sp, #-4]!)
    8404:	e28db000 	add	fp, sp, #0
    8408:	e59f3018 	ldr	r3, [pc, #24]	; 8428 <hello+0x28>
    840c:	e5933000 	ldr	r3, [r3]
    8410:	e2832001 	add	r2, r3, #1
    8414:	e59f3010 	ldr	r3, [pc, #16]	; 842c <hello+0x2c>
    8418:	e5832000 	str	r2, [r3]
    841c:	e24bd000 	sub	sp, fp, #0
    8420:	e49db004 	pop	{fp}		; (ldr fp, [sp], #4)
    8424:	e12fff1e 	bx	lr
    8428:	000105f0 	.word	0x000105f0
    842c:	000105f4 	.word	0x000105f4

main関数でhelloを呼び出していた部分が、0xebfffffeから0xeb000003に書き換わっている。
bl

Ubuntu 14.04でASP.NET Core 1.0を動かしてみる

Ubuntu 12.10でうまくいかなかったので、14.04環境に変えて仕切り直し。
前回と
intothevoid.hatenablog.com
全く同じ手順で、さくっとインストール完了。dnxが動作するようになった。

$ uname -a
Linux ik1-303-11997 3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="14.04.4 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.4 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
$ dnx --help
Microsoft .NET Execution environment CoreClr-x64-1.0.0-rc1-16609

Usage: dnx [options]

Options:
  --project|-p               Path to the project.json file or the application folder. Defaults to the current folder if not provided.
  --appbase                  Application base directory path
  --lib                 Paths used for library look-up
  --debug                          Waits for the debugger to attach before beginning execution.
  --bootstrapper-debug             Waits for the debugger to attach before bootstrapping runtime.
  -?|-h|--help                     Show help information
  --version                        Show version information
  --watch                          Watch file changes
  --packages          Directory containing packages
  --configuration   The configuration to run under
  --port                     The port to the compilation server

YOEMANのインストール

YOEMANのインストールは下記のページが詳しかった。
Node.jsのアプリとしての実装のようで、Node.jsが必要。
Yeoman: Getting it to Work on Ubuntu - Truthy Falsey

Node.jsをいれて、そのあとでNode.jsのパッケージ管理ツールであるNPMも入れる。
node.jsのサイトからnode-v4.4.4-linux-x64.tar.xzをダウンロードしてきて展開して使った。
https://nodejs.org/en/

$ mkdir ~/nodejs
$ cd ~/nodejs
$ xz node-v4.4.4-linux-x64.tar.xz
$ tar -xvf node-v4.4.4-linux-x64.tar

nodeコマンドとnpmコマンドがパス指定しなくても実行できるように.bashrcにexportを追加しておく。

export PATH=$PATH:/home/xxxxx/nodejs/node-v4.4.4-linux-x64/bin

npmをつかって必要なパッケージをインストールする。
最初にnpm自身をアプデートしておく。(しておかないとyoのインストール時にWARNが出た)

$ npm install -g nam
$ npm yo grunt-cli bower
$ npm install -g generator-webapp
$ npm install -g generator-aspnet

まずはConsoleアプリケーションを作ってみて、.NET Coreが動作することを確認する。

shizuku@ik1-303-11997:~$ mkdir dntest
shizuku@ik1-303-11997:~$ cd dntest/
shizuku@ik1-303-11997:~/dntest$ yo aspnet

     _-----_
    |       |    .--------------------------.
    |--(o)--|    |      Welcome to the      |
   `---------´   |   marvellous ASP.NET 5   |
    ( _´U`_ )    |        generator!        |
    /___A___\    '--------------------------'
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

? What type of application do you want to create? Console Application
? What's the name of your ASP.NET application? ConsoleApp1
   create ConsoleApp1/.gitignore
   create ConsoleApp1/Program.cs
   create ConsoleApp1/project.json


Your project is now created, you can use the following commands to get going
    cd "ConsoleApp1"
    dnu restore
    dnu build (optional, build will also happen when it's run)
    dnx run

生成されるテンプレートは下記のような中身。

ConsoleApp1$ find .
.
./project.lock.json
./Program.cs
./.gitignore
./project.json

project.jsonに設定情報が。

$ cat project.json 
{
  "version": "1.0.0-*",
  "description": "ConsoleApp1 Console Application",
  "authors": [ "" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },

  "tooling": {
    "defaultNamespace": "ConsoleApp1"
  },

  "dependencies": {
  },

  "commands": {
    "ConsoleApp1": "ConsoleApp1"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}


dnu restoreを実行するとこのプロジェクトの実行に必要なライブラリがダウンロードされる。
何が必要かはおそらくproject.jsonのdependenciesから決定される。

dun buildでビルド。で、失敗。
なかなかうまくいかないな。

.NET Coreのアプリケーションは、異なる複数の環境で実行されることを前提に作ることができるらしい。
.NET Frameworkとか、.NET Coreとか。
project.jsonのframeworkのところで、対応する環境を定義するらしい。dnx451というのが.NET Framework。dnxcore50というのが.NET Core。
さっきdun buildでエラーが出ていたのは、dnx451のモジュールのようだったので、ひとまずproject.jsonからdnx451を削除してみた。(今回はLinuxの.NET Core環境でアプリを動作させることが目的なので、.NET Frameworkのサポートは不要)
https://docs.asp.net/en/latest/conceptual-overview/dotnetcore.html

すると、ビルドは通り、dnx runでの実行も成功。やっと動いた。まだコンソールアプリだけど。
たしかにMonoなしで、.NET Core 1.0 (CoreCLR + CoreFX)でC#アプリが動くようになっている。

$ dnx run
Hello World

ビルドした後のディレクトリの中はこんな感じ。

ConsoleApp1$ find .
.
./project.lock.json
./bin
./bin/Debug
./bin/Debug/dnxcore50
./bin/Debug/dnxcore50/ConsoleApp1.xml
./bin/Debug/dnxcore50/ConsoleApp1.dll
./project.json.org
./Program.cs
./.gitignore
./project.json

ちゃんとMONO/.Net用のバイナリができている。なかなか面白い。

file ./bin/Debug/dnxcore50/ConsoleApp1.dll 
./bin/Debug/dnxcore50/ConsoleApp1.dll: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows

ASP.NET Webアプリの作成

YOEMANでWebアプリのテンプレートをつくる。

$ yo aspnet

     _-----_
    |       |    .--------------------------.
    |--(o)--|    |      Welcome to the      |
   `---------´   |   marvellous ASP.NET 5   |
    ( _´U`_ )    |        generator!        |
    /___A___\    '--------------------------'
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

? What type of application do you want to create? Web Application
? What's the name of your ASP.NET application? WebApp1
   create WebApp1/gulpfile.js
   create WebApp1/Dockerfile
   create WebApp1/.bowerrc
   create WebApp1/.gitignore
   create WebApp1/appsettings.json
   create WebApp1/bower.json
   create WebApp1/package.json
   create WebApp1/project.json
   create WebApp1/README.md
   create WebApp1/Startup.cs
   create WebApp1/Controllers/AccountController.cs
   create WebApp1/Controllers/HomeController.cs
   create WebApp1/Controllers/ManageController.cs
   create WebApp1/Migrations/00000000000000_CreateIdentitySchema.Designer.cs
   create WebApp1/Migrations/00000000000000_CreateIdentitySchema.cs
   create WebApp1/Migrations/ApplicationDbContextModelSnapshot.cs
   create WebApp1/Models/ApplicationDbContext.cs
   create WebApp1/Models/ApplicationUser.cs
   create WebApp1/Services/IEmailSender.cs
   create WebApp1/Services/ISmsSender.cs
   create WebApp1/Services/MessageServices.cs
   create WebApp1/ViewModels/Account/ExternalLoginConfirmationViewModel.cs
   create WebApp1/ViewModels/Account/ForgotPasswordViewModel.cs
   create WebApp1/ViewModels/Account/LoginViewModel.cs
   create WebApp1/ViewModels/Account/RegisterViewModel.cs
   create WebApp1/ViewModels/Account/ResetPasswordViewModel.cs
   create WebApp1/ViewModels/Account/SendCodeViewModel.cs
   create WebApp1/ViewModels/Account/VerifyCodeViewModel.cs
   create WebApp1/ViewModels/Manage/AddPhoneNumberViewModel.cs
   create WebApp1/ViewModels/Manage/ChangePasswordViewModel.cs
   create WebApp1/ViewModels/Manage/ConfigureTwoFactorViewModel.cs
   create WebApp1/ViewModels/Manage/FactorViewModel.cs
   create WebApp1/ViewModels/Manage/IndexViewModel.cs
   create WebApp1/ViewModels/Manage/ManageLoginsViewModel.cs
   create WebApp1/ViewModels/Manage/RemoveLoginViewModel.cs
   create WebApp1/ViewModels/Manage/SetPasswordViewModel.cs
   create WebApp1/ViewModels/Manage/VerifyPhoneNumberViewModel.cs
   create WebApp1/Views/_ViewImports.cshtml
   create WebApp1/Views/_ViewStart.cshtml
   create WebApp1/Views/Account/ConfirmEmail.cshtml
   create WebApp1/Views/Account/ExternalLoginConfirmation.cshtml
   create WebApp1/Views/Account/ExternalLoginFailure.cshtml
   create WebApp1/Views/Account/ForgotPassword.cshtml
   create WebApp1/Views/Account/ForgotPasswordConfirmation.cshtml
   create WebApp1/Views/Account/Lockout.cshtml
   create WebApp1/Views/Account/Login.cshtml
   create WebApp1/Views/Account/Register.cshtml
   create WebApp1/Views/Account/ResetPassword.cshtml
   create WebApp1/Views/Account/ResetPasswordConfirmation.cshtml
   create WebApp1/Views/Account/SendCode.cshtml
   create WebApp1/Views/Account/VerifyCode.cshtml
   create WebApp1/Views/Home/About.cshtml
   create WebApp1/Views/Home/Contact.cshtml
   create WebApp1/Views/Home/Index.cshtml
   create WebApp1/Views/Manage/AddPhoneNumber.cshtml
   create WebApp1/Views/Manage/ChangePassword.cshtml
   create WebApp1/Views/Manage/Index.cshtml
   create WebApp1/Views/Manage/ManageLogins.cshtml
   create WebApp1/Views/Manage/SetPassword.cshtml
   create WebApp1/Views/Manage/VerifyPhoneNumber.cshtml
   create WebApp1/Views/Shared/_Layout.cshtml
   create WebApp1/Views/Shared/_LoginPartial.cshtml
   create WebApp1/Views/Shared/_ValidationScriptsPartial.cshtml
   create WebApp1/Views/Shared/Error.cshtml
   create WebApp1/wwwroot/css/site.css
   create WebApp1/wwwroot/css/site.min.css
   create WebApp1/wwwroot/favicon.ico
   create WebApp1/wwwroot/images/ASP-NET-Banners-01.png
   create WebApp1/wwwroot/images/ASP-NET-Banners-02.png
   create WebApp1/wwwroot/images/Banner-01-Azure.png
   create WebApp1/wwwroot/images/Banner-02-VS.png
   create WebApp1/wwwroot/js/site.js
   create WebApp1/wwwroot/js/site.min.js
   create WebApp1/wwwroot/web.config


Your project is now created, you can use the following commands to get going
    cd "WebApp1"
    dnu restore
    dnu build (optional, build will also happen when it's run)
    dnx web

dun restoreするとASP.NETの実行に必要なパッケージがnugetからたくさんダウンロードされる。

Installing Microsoft.Extensions.Configuration.UserSecrets.1.0.0-rc1-final
Installing Microsoft.Extensions.Logging.Console.1.0.0-rc1-final
Installing Microsoft.Extensions.PlatformAbstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Primitives.1.0.0-rc1-final
Installing Microsoft.Extensions.Logging.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Logging.Debug.1.0.0-rc1-final
Installing Microsoft.Extensions.Logging.1.0.0-rc1-final
Installing Microsoft.Extensions.DependencyInjection.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.FileProviderExtensions.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.FileExtensions.1.0.0-rc1-final
Installing Microsoft.AspNet.FileProviders.Physical.1.0.0-rc1-final
Installing Microsoft.AspNet.FileProviders.Abstractions.1.0.0-rc1-final
Installing Microsoft.AspNet.IISPlatformHandler.1.0.0-rc1-final
Installing Microsoft.AspNet.Http.Extensions.1.0.0-rc1-final
Installing Microsoft.Extensions.WebEncoders.Core.1.0.0-rc1-final
Installing Microsoft.Net.Http.Headers.1.0.0-rc1-final
Installing Microsoft.AspNet.Http.Abstractions.1.0.0-rc1-final
Installing Microsoft.AspNet.Http.Features.1.0.0-rc1-final
Installing Microsoft.AspNet.Http.1.0.0-rc1-final
Installing Microsoft.AspNet.WebUtilities.1.0.0-rc1-final
Installing Microsoft.AspNet.StaticFiles.1.0.0-rc1-final
Installing Microsoft.AspNet.Hosting.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.WebEncoders.1.0.0-rc1-final
Installing Microsoft.Extensions.OptionsModel.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.Binder.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.1.0.0-rc1-final
Installing Microsoft.AspNet.Server.Kestrel.1.0.0-rc1-final
Installing System.Numerics.Vectors.4.1.1-beta-23516
Installing Microsoft.AspNet.Hosting.1.0.0-rc1-final
Installing Microsoft.Extensions.DependencyInjection.1.0.0-rc1-final
Installing Microsoft.Dnx.Compilation.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.CommandLine.1.0.0-rc1-final
Installing Microsoft.AspNet.Hosting.Server.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Configuration.EnvironmentVariables.1.0.0-rc1-final
Installing System.Diagnostics.DiagnosticSource.4.0.0-beta-23516
Installing System.Diagnostics.Tracing.4.0.0
Installing System.Threading.4.0.0
Installing Microsoft.AspNet.Tooling.Razor.1.0.0-rc1-final
Installing Microsoft.AspNet.Razor.Runtime.4.0.0-rc1-final
Installing Microsoft.AspNet.Html.Abstractions.1.0.0-rc1-final
Installing Microsoft.AspNet.Razor.4.0.0-rc1-final
Installing Newtonsoft.Json.6.0.6
Installing Microsoft.Extensions.Configuration.Json.1.0.0-rc1-final
Installing Microsoft.AspNet.Authentication.Cookies.1.0.0-rc1-final
Installing Microsoft.AspNet.Authentication.1.0.0-rc1-final
Installing Microsoft.AspNet.DataProtection.1.0.0-rc1-final
Installing Microsoft.AspNet.Cryptography.Internal.1.0.0-rc1-final
Installing Microsoft.AspNet.DataProtection.Abstractions.1.0.0-rc1-final
Installing Microsoft.AspNet.Diagnostics.Entity.7.0.0-rc1-final
Installing Microsoft.AspNet.Diagnostics.1.0.0-rc1-final
Installing Microsoft.AspNet.Diagnostics.Abstractions.1.0.0-rc1-final
Installing EntityFramework.Relational.7.0.0-rc1-final
Installing EntityFramework.Core.7.0.0-rc1-final
Installing Microsoft.Extensions.Caching.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Caching.Memory.1.0.0-rc1-final
Installing System.Collections.Immutable.1.1.36
Installing Ix-Async.1.2.5
Installing Remotion.Linq.2.0.1
Installing EntityFramework.MicrosoftSqlServer.7.0.0-rc1-final
Installing Microsoft.AspNet.Identity.EntityFramework.3.0.0-rc1-final
Installing Microsoft.AspNet.Identity.3.0.0-rc1-final
Installing Microsoft.AspNet.Cryptography.KeyDerivation.1.0.0-rc1-final
Installing EntityFramework.Commands.7.0.0-rc1-final
Installing EntityFramework.Relational.Design.7.0.0-rc1-final
Installing EntityFramework.Sqlite.7.0.0-rc1-final
Installing Microsoft.Data.Sqlite.1.0.0-rc1-final
Installing Microsoft.Dnx.Runtime.1.0.0-rc1-final
Installing Microsoft.Dnx.Loader.1.0.0-rc1-final
Installing Microsoft.CodeAnalysis.CSharp.1.1.0-rc1-20151109-01
Installing Microsoft.CodeAnalysis.Common.1.1.0-rc1-20151109-01
Installing System.Collections.Immutable.1.1.37
Installing System.Diagnostics.Debug.4.0.0
Installing System.Collections.4.0.0
Installing System.Linq.4.0.0
Installing System.Runtime.Extensions.4.0.0
Installing Microsoft.CodeAnalysis.Analyzers.1.0.0
Installing System.Reflection.Metadata.1.1.0
Installing System.Runtime.InteropServices.4.0.0
Installing System.Text.Encoding.Extensions.4.0.0
Installing Microsoft.AspNet.Mvc.6.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Localization.6.0.0-rc1-final
Installing Microsoft.AspNet.Localization.1.0.0-rc1-final
Installing Microsoft.Extensions.Localization.Abstractions.1.0.0-rc1-final
Installing Microsoft.Extensions.Globalization.CultureInfoCache.1.0.0-rc1-final
Installing Microsoft.Extensions.Localization.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Cors.6.0.0-rc1-final
Installing Microsoft.AspNet.Cors.6.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Core.6.0.0-rc1-final
Installing Microsoft.AspNet.Authorization.1.0.0-rc1-final
Installing Microsoft.Extensions.MemoryPool.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Abstractions.6.0.0-rc1-final
Installing Microsoft.AspNet.Routing.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.DataAnnotations.6.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.ApiExplorer.6.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Formatters.Json.6.0.0-rc1-final
Installing Microsoft.AspNet.JsonPatch.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.ViewFeatures.6.0.0-rc1-final
Installing Microsoft.AspNet.Antiforgery.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Razor.6.0.0-rc1-final
Installing Microsoft.AspNet.PageExecutionInstrumentation.Interfaces.1.0.0-rc1-final
Installing Microsoft.AspNet.Razor.Runtime.Precompilation.4.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.Razor.Host.6.0.0-rc1-final
Installing Microsoft.Dnx.Compilation.CSharp.Abstractions.1.0.0-rc1-final
Installing Microsoft.Dnx.Compilation.CSharp.Common.1.0.0-rc1-final
Installing Microsoft.AspNet.Mvc.TagHelpers.6.0.0-rc1-final
Installing Microsoft.Extensions.FileSystemGlobbing.1.0.0-rc1-final
Installing Microsoft.Extensions.CodeGenerators.Mvc.1.0.0-rc1-final
Installing Microsoft.Extensions.CodeGeneration.Templating.1.0.0-rc1-final
Installing Microsoft.Extensions.CodeGeneration.EntityFramework.1.0.0-rc1-final
Installing Microsoft.Extensions.CodeGeneration.Core.1.0.0-rc1-final
Installing Microsoft.Extensions.CodeGeneration.1.0.0-rc1-final
Installing System.Runtime.Serialization.Primitives.4.0.11-beta-23409
Installing System.Resources.ResourceManager.4.0.1-beta-23516
Installing System.Collections.4.0.10
Installing System.AppContext.4.0.0
Installing System.IO.FileSystem.Watcher.4.0.0-beta-23516
Installing System.IO.FileSystem.Primitives.4.0.1-beta-23516
Installing System.IO.FileSystem.4.0.1-beta-23516
Installing System.IO.4.0.11-beta-23516
Installing System.Text.RegularExpressions.4.0.11-beta-23516
Installing System.Runtime.Extensions.4.0.11-beta-23516
Installing System.Collections.Concurrent.4.0.11-beta-23516
Installing System.Diagnostics.Tracing.4.0.20
Installing System.Reflection.Extensions.4.0.1-beta-23516
Installing System.Threading.4.0.10
Installing System.ComponentModel.4.0.1-beta-23516
Installing System.Diagnostics.Debug.4.0.11-beta-23516
Installing System.Diagnostics.Tools.4.0.1-beta-23516
Installing System.Threading.Thread.4.0.0-beta-23516
Installing System.Security.Cryptography.Algorithms.4.0.0-beta-23516
Installing System.Security.Cryptography.Primitives.4.0.0-beta-23516
Installing System.Globalization.4.0.11-beta-23516
Installing System.Reflection.4.1.0-beta-23225
Installing System.Runtime.InteropServices.4.0.21-beta-23516
Installing System.Linq.Expressions.4.0.11-beta-23516
Installing System.Dynamic.Runtime.4.0.11-beta-23516
Installing System.Reflection.Emit.4.0.0
Installing System.Reflection.Emit.ILGeneration.4.0.0
Installing System.Linq.Expressions.4.0.10
Installing System.Threading.Tasks.4.0.11-beta-23516
Installing System.Text.Encoding.4.0.11-beta-23516
Installing System.Security.Claims.4.0.1-beta-23516
Installing System.Security.Principal.4.0.1-beta-23516
Installing System.Linq.Queryable.4.0.1-beta-23516
Installing System.ComponentModel.TypeConverter.4.0.1-beta-23516
Installing System.ComponentModel.Primitives.4.0.0
Installing System.Globalization.Extensions.4.0.1-beta-23516
Installing System.Net.Primitives.4.0.11-beta-23516
Installing System.Reflection.TypeExtensions.4.0.1-beta-23409
Installing System.Net.WebSockets.4.0.0-beta-23516
Installing Microsoft.Win32.Primitives.4.0.0
Installing System.Security.Cryptography.X509Certificates.4.0.0-beta-23516
Installing System.Security.Cryptography.Encoding.4.0.0-beta-23516
Installing System.Text.Encoding.Extensions.4.0.11-beta-23516
Installing System.IO.4.0.10
Installing System.Runtime.Handles.4.0.1-beta-23516
Installing System.Data.Common.4.0.1-beta-23516
Installing System.Collections.NonGeneric.4.0.0
Installing System.ComponentModel.Annotations.4.0.11-beta-23516
Installing System.ObjectModel.4.0.11-beta-23516
Installing System.Diagnostics.Contracts.4.0.1-beta-23516
Installing System.Threading.ThreadPool.4.0.10-beta-23516
Installing System.Threading.Timer.4.0.1-beta-23516
Installing System.Diagnostics.TraceSource.4.0.0-beta-23516
Installing System.Diagnostics.Tracing.4.0.21-beta-23516
Installing System.Diagnostics.StackTrace.4.0.1-beta-23516
Installing System.Security.Principal.Windows.4.0.0-beta-23516
Installing System.Security.Principal.4.0.0
Installing System.Security.Claims.4.0.0
Installing System.Net.Http.4.0.1-beta-23516
Installing System.Net.Primitives.4.0.0
Installing Microsoft.Win32.Registry.4.0.0-beta-23516
Installing System.Xml.XDocument.4.0.11-beta-23516
Installing System.Diagnostics.Tools.4.0.0
Installing System.Xml.ReaderWriter.4.0.10
Installing System.IO.FileSystem.4.0.0
Installing System.Threading.Overlapped.4.0.0
Installing System.Text.RegularExpressions.4.0.10
Installing System.Text.Encoding.CodePages.4.0.1-beta-23516
Installing System.Data.SqlClient.4.0.0-beta-23516
Installing System.Xml.ReaderWriter.4.0.0
Installing System.Data.Common.4.0.0
Installing System.Text.RegularExpressions.4.0.0
Installing System.ComponentModel.4.0.0
Installing System.Reflection.4.1.0-beta-23516
Installing System.AppContext.4.0.1-beta-23516
Installing System.Runtime.Loader.4.0.0-beta-23516
Installing System.Resources.ReaderWriter.4.0.0-beta-23516
Installing System.Xml.ReaderWriter.4.0.11-beta-23516
Installing System.Threading.Tasks.Parallel.4.0.1-beta-23516
Installing System.Collections.Concurrent.4.0.10
Installing System.Diagnostics.Process.4.0.0-beta-23409
Installing runtime.unix.System.Diagnostics.Debug.4.0.11-beta-23516
Installing runtime.linux.System.Runtime.Extensions.4.0.11-beta-23516
Installing runtime.linux.System.Security.Cryptography.Algorithms.4.0.0-beta-23516
Installing runtime.linux.System.IO.FileSystem.4.0.1-beta-23516
Installing runtime.linux.System.IO.FileSystem.Watcher.4.0.0-beta-23516
Installing runtime.any.System.Linq.Expressions.4.0.11-beta-23516
Installing System.Reflection.Emit.Lightweight.4.0.0
Installing runtime.unix.System.Globalization.Extensions.4.0.1-beta-23516
Installing runtime.unix.System.Net.Primitives.4.0.11-beta-23516
Installing runtime.unix.System.Security.Cryptography.Encoding.4.0.0-beta-23516
Installing System.Collections.Concurrent.4.0.0
Installing runtime.unix.System.Security.Cryptography.X509Certificates.4.0.0-beta-23516
Installing System.Security.Cryptography.OpenSsl.4.0.0-beta-23516
Installing System.Runtime.Numerics.4.0.0
Installing System.Globalization.Calendars.4.0.0
Installing runtime.linux.System.Net.Http.4.0.1-beta-23516
Installing runtime.unix.System.Diagnostics.TraceSource.4.0.0-beta-23516
Installing runtime.linux.System.Diagnostics.Process.4.1.0-beta-23409
Installing System.Threading.ThreadPool.4.0.10-beta-23409
Installing runtime.unix.System.Text.Encoding.CodePages.4.0.1-beta-23516
Installing runtime.unix.System.Data.SqlClient.4.0.0-beta-23516
Installing System.Net.Security.4.0.0-beta-23516
Installing System.Net.NameResolution.4.0.0-beta-23516
Installing System.Threading.Timer.4.0.0
Installing System.Net.Sockets.4.1.0-beta-23516
Installing System.Runtime.InteropServices.RuntimeInformation.4.0.0-beta-23516
Installing System.Net.Primitives.4.0.10
Installing System.Private.Networking.4.0.0
Installing System.ComponentModel.EventBasedAsync.4.0.10

dun buildとするとConsoleアプリ同様、エラーがたくさん出るので、project.jsonを編集して、DNXを.NET Core 1.0に限定する。

dnx webすると、libuvにuv_loop_size()というシンボルが見つからないというエラーで怒られる。
apt-get でいれたlibuv.soのreadelfで見てみるとたしかにそんな名前のシンボルはない。
libuvのサイトからソースをダウンロードしてビルドしてみた。


動いた。

 dnx web
info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
      User profile is available. Using '/home/shizuku/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

これだとローカルからしかアクセスできないので、project.jsonでWebサーバの引数を変更して、どのアドレスからでもアクセスできるようにする。
下記が最終的なproject.json

$ cat project.json 
{
  "version": "1.0.0-*",
  "userSecretsId": "aspnet5-WebApp1-3e3d5f5f-85cd-4edd-b04d-e55588eb76e2",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "tooling": {
    "defaultNamespace": "WebApp1"
  },

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Sqlite": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Dnx.Runtime":"1.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://0.0.0.0:5004",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [
      "npm install",
      "bower install",
      "gulp clean",
      "gulp min"
    ]
  }
}

ブラウザからアクセスするといい感じのWebサイトが表示される。
f:id:samehada_shiro:20160518225202p:plain

Ubuntu 12.10にASP.NET Core 1.0をインストールしようとして失敗した話

Linux (Ubuntu)環境でASP.NETアプリを動かしてみる。

基本的には下記のサイトの説明にしたがって環境構築していく。
Installing ASP.NET 5 On Linux — ASP.NET documentation


Linux向けのASP.NETASP.NET 5 RC 。Release Candidate
あと、最近名称が変わってASP.NET Core 1.0というのが今後は使われていくらしい。

NETの実行環境には3種類あって、目的、環境に合わせて選択することができる。

.NET Frameworkは昔からある本家の.NET。フルパッケージ。ただし、Windows環境でのみ動く。
.NET Coreは、.NET Frameworkのサブセット。ポータブルなランタイムCoreCLRとライブラリCoreFXからなる。Windowsだけでなく、LinuxMac OS環境でも動く。MS主体のオープンソースプロジェクトとして開発されており、ソースの参照が可能なことはもちろん、開発への参加も可能。
Monoは.NET FrameworkWindows環境以外でも動くようにポーティングしたもの。オープンソースプロジェクト。MSによる実装ではない。

では、インストールしてみる。
まず、DNVM(.NET Version Manager)なるものをインストールする。これは.NETの実行環境(DNX = .NET Execution Environment)を管理するためのツール。DNXは.NET CoreかMono。それぞれに異なるバージョンが存在する。それら複数のDNXを管理して、どのDNXを使うかを設定する。
必要となるツールをapt-getで入れた後に、DNVMインストール用のシェルスクリプトをダウンロードしてきて実行する。

sudo apt-get install unzip curl
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

実行するとDNVMの実体であるdnvm.shがとあるサイトから自動でダンロードされて、~/.dnx/dnvm/dnvm.shに保存される。
さらに~/bashrcに下記のコマンドが追加されて、ログイン時にdnvm.shが読み込まれるようになる。

[ -s "/home/xxxxx/.dnx/dnvm/dnvm.sh" ] && . "/home/xxxxx/.dnx/dnvm/dnvm.sh" # Load dmvm

DNVMのインストール後はdnvmコマンドが使えるようになっている。

$ dnvm
    ___  _  ___   ____  ___
   / _ \/ |/ / | / /  |/  /
  / // /    /| |/ / /|_/ / 
 /____/_/|_/ |___/_/  /_/  

.NET Version Manager - Version 1.0.0-rc2-15546
By Microsoft Open Technologies, Inc.

DNVM can be used to download versions of the .NET Execution Environment and manage which version you are using.
You can control the URL of the stable and unstable channel by setting the DNX_FEED and DNX_UNSTABLE_FEED variables.

Current feed settings:
Default Stable: https://www.nuget.org/api/v2
Default Unstable: https://www.myget.org/F/aspnetvnext/api/v2
Current Stable Override: 
Current Unstable Override: 

Use dnvm [help|-h|-help|--help]  to display help text.

DNVMがインストールできたので、DNVMを使って、.NET Coreをインストールしてみる。
インストールために必要なパッケージがあるのでまずは入れる。

sudo apt-get install libunwind8 gettext libssl-dev libcurl4-openssl-dev zlib1g libicu-dev uuid-dev

.NET Coreのインストール。

$ dnvm upgrade -r coreclr
Determining latest version
Latest version is 1.0.0-rc1-update2 
Downloading dnx-coreclr-linux-x64.1.0.0-rc1-update2 from https://www.nuget.org/api/v2
Download: https://www.nuget.org/api/v2/package/dnx-coreclr-linux-x64/1.0.0-rc1-update2
######################################################################## 100.0%
Installing to /home/xxxxx/.dnx/runtimes/dnx-coreclr-linux-x64.1.0.0-rc1-update2
Adding /home/xxxxx/.dnx/runtimes/dnx-coreclr-linux-x64.1.0.0-rc1-update2/bin to process PATH
Setting alias 'default' to 'dnx-coreclr-linux-x64.1.0.0-rc1-update2'

~/.dnx/runtimesの下に.NET Coreのファイルがダウンロードされる。
あと、PATH環境変数にそこへのパスが追加されるのと、dnvmのdefault設定に.NET Core rc1 update2が設定される。

$ cat ~/.dnx/alias/default.alias 
dnx-coreclr-linux-x64.1.0.0-rc1-update2

(.NET ASPのページではDNVMを使わない.NET Coreのインストール方法も書かれているが、そこでダウンロードできるのはdnx-coreclr-linux-x64.1.0.0-rc1-update1.tar.gz。DNVMを使った場合はupdate2がダウンロードされてきたので、DNVMを使った方がいいかも。)

ここまでで.NET Coreのインストールは完了のはず。

だが、dnxコマンドやdnuコマンドを実行しても、何の出力もされない。。。
どうもうまく動いていない様子。

ほぼ同じ手順でMax OS環境にもセッティングしてみたが、こちらはうまくいった。

つかっているUbuntuのバージョンが古いことがまずいのだろうか?

Linux quetzal 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

Mac OS Xの出力

$ dnx --help
Microsoft .NET Execution environment CoreClr-x64-1.0.0-rc1-16609

Usage: dnx [options]

Options:
  --project|-p               Path to the project.json file or the application folder. Defaults to the current folder if not provided.
  --appbase                  Application base directory path
  --lib                 Paths used for library look-up
  --debug                          Waits for the debugger to attach before beginning execution.
  --bootstrapper-debug             Waits for the debugger to attach before bootstrapping runtime.
  -?|-h|--help                     Show help information
  --version                        Show version information
  --watch                          Watch file changes
  --packages          Directory containing packages
  --configuration   The configuration to run under
  --port                     The port to the compilation server

Ubuntu 12.10の出力

$ dnx --help

何のエラーも出ない。。。