【メモ】VagrantとWindows Subsystem for Linux( WSL ) について

目的

以下の本を購入したので、環境構築を行うことにした。
www.oreilly.co.jp
ただし、環境については、本書には記載されていないMicrosoft WindowsWindows Subsystem for Linux( WSL ) 上で構築する。
結果的にはData Science Toolboxにログインできたが、SSHあたりがあやふやなので自分用のメモとして書き残しておく。

環境

VirtualBoxWindows側へインストール

以下の記事が詳しい。
qiita.com

VagrantをWSL側のディレクトリへインストール

  • まず、Windows上に最新のパッケージ(Debianパッケージ(64bit))をダウンロード(デスクトップへ保存)

www.vagrantup.com

  • ダウンロードしたVagrantパッケージファイルがある場所にカレントディレクトリを変更
$cd /mnt/c/Users/ユーザ名/Desktop/
  • パッケージファイルをインストールする
$sudo dpkg -i vagrant_2.1.4_x86_64.deb

設定ファイル(.bashrc)の修正

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox"

上記のコードを以下のようにvimにより設定ファイル(.bashrc)の末尾へ加えてUbuntuを再起動する。

$vi /home/ユーザ名/.bashrc

この作業については以下の記事を参考にした。
www.vagrantup.com
blog.ishimotty.com
www.gosoly.com

Vagrantの実行

$cd
$mkdir MyDSToolbox
$cd MyDSToolbox

Data Science Toolboxのダウンロード、起動

  • Data Science Toolboxの初期化及びVagrantfileの作成
$vagrant init data-science-toolbox/data-science-at-the-command-line
  • Data Science Toolboxのダウンロード
$vagrant up

ダウンロードが始まり以下の表示が出てくる。

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'data-science-toolbox/data-science-at-the-command-line' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'data-science-toolbox/data-science-at-the-command-line'
    default: URL: https://vagrantcloud.com/data-science-toolbox/data-science-at-the-command-line
==> default: Adding box 'data-science-toolbox/data-science-at-the-command-line' (v1.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/data-science-toolbox/boxes/data-science-at-the-command-line/versions/1.0.0/providers/virtualbox.box
    default: Download redirected to host: s3.amazonaws.com
==> default: Successfully added box 'data-science-toolbox/data-science-at-the-command-line' (v1.0.0) for 'virtualbox'!

しかしながら、以下のエラーが表示された。

There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The host path of the shared folder is not supported from WSL. Host
path of the shared folder must be located on a file system with
DrvFs type. Host path: .

この問題に対しては以下の記事を参考にした。
Windows Subsystem for Linuxのファイルシステムにおける注意点
www.vagrantup.com

とくに、2つめの記事の”Synced Folders”の 
  To use synced folders from within the WSL that do not support VolFs file systems, move the Vagrant project directory to a DrvFs file system location (/mnt/c/ prefixed path for example).
という文を参考にして、次のように対応してみた。

  • VagrantfileをWindows側の(Cドライブ直下に作成した)MyDSToolboxフォルダにコピーする
$cp ~/MyDSToolbox/Vagrantfile  /mnt/c/MyDSToolbox/
  • カレントディレクトリをCドライブ直下に作成したMyDSToolboxフォルダにしてvagrant upを実行する
$cd /mnt/c/MyDSToolbox/
$vagrant up

すると、今度は以下のようなエラーが表示された。

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'data-science-toolbox/data-science-at-the-command-line'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'data-science-toolbox/data-science-at-the-command-line' is up to date...
==> default: Setting the name of the VM: MyDSToolbox_default_1536676998589_64439
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
The private key to connect to this box via SSH has invalid permissions
set on it. The permissions of the private key should be set to 0600, otherwise SSH will
ignore the key. Vagrant tried to do this automatically for you but failed. Please set the
permissions on the following file to 0600 and then try running this command again:

/mnt/c/MyDSToolbox/.vagrant/machines/default/virtualbox/private_key

Note that this error occurs after Vagrant automatically tries to
do this for you. The likely cause of this error is a lack of filesystem
permissions or even filesystem functionality. For example, if your
Vagrant data is on a USB stick, a common case is that chmod is
not supported. The key will need to be moved to a filesystem that
supports chmod.

下記の記事を参考にしてSSHコマンドにより以下のコードを試してみた。
qiita.com

$ssh -i /mnt/c/MyDSToolbox/.vagrant/machines/default/virtualbox/private_key -p 2222 vagrant@127.0.0.1

この方法だと、エラーは表示されないが、次のような警告が表示される。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '/mnt/c/MyDSToolbox/.vagrant/machines/default/virtualbox/private_key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/mnt/c/MyDSToolbox/.vagrant/machines/default/virtualbox/private_key": bad permissions

これについてはどうしたらいいのか現時点ではわからないため、ペンディングしておき、Data Science Toolboxにログインできたらターミナルに以下のメッセージが表示される。

Welcome to the Data Science Toolbox for Data Science at the Command Line

Based on Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Data Science at the Command Line: http://datascienceatthecommandline.com
 * Data Science Toolbox:             http://datasciencetoolbox.org
 * Ubuntu documentation:             http://help.ubuntu.com
Last login: Tue Sep 30 20:38:16 2014 from 10.0.2.2

ログアウトするには以下のコマンドを入力する。

vagrant@data-science-toolbox:~$ exit

まとめ

アンインストールについて

以下の記事が詳しい。
sutepulu.com
www.vagrantup.com

Vagrantの基本的なコマンドについて

以下の記事が詳しい。
qiita.com