takafumi blog

日々の勉強メモ

【Vagrant】 Vagrant + VirtualBox で仮想環境を構築

環境  Vagrant 1.8.1 VirtualBox 5.0.12

Vagrantを使った、基本的な仮想環境作成。

ホスト:Windows 10
ゲスト:CentOS 7.1


ホスト側の事前作業

の最新版を公式からDLし、インストールしておく。


ベースとなるOSのイメージ『box』を追加

ベースとして様々なboxイメージが配布されているが、一番無難で安全そうなChef社のbento organizationからboxをダウンロードする。

bento | Atlas by HashiCorp

【参考】 blog.chopschips.net

PS> vagrant box add bento/centos-7.1
==> box: Loading metadata for box 'bento/centos-7.1'
    box: URL: https://atlas.hashicorp.com/bento/centos-7.1
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2
==> box: Adding box 'bento/centos-7.1' (v2.2.2) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/bento/boxes/centos-7.1/versions/2.2.2/providers/virtualbox.box
    box: Progress: 100% (Rate: 1465k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'bento/centos-7.1' (v2.2.2) for 'virtualbox'!

途中で

1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2

と聞かれるので、VirualBoxを選択する。
これでイメージがダウンロードされる。

現在のbox一覧を確認したいときは

PS> vagrant box list
bento/centos-7.1 (virtualbox, 2.2.2)


Windows上のディレクトリ

Vagrantのboxファイルなどは
C:\Users\<ユーザー名>\.vagrant.d
にある


boxのリネーム

boxのリネームは、実ファイルをリネームするだけ。
vagrantコマンドで表示されるものも変更される。
Windowsだとboxのあるディレクトリは

C:\Users\<ユーザー名>\.vagrant.d\boxes\

この下にあるboxファイルをリネームすればよい。


Vagrantfileを作成

ホスト側のVMを作りたいディレクトリに移動。
ここではC:/Users/<ユーザー名>/VirtualBox VMs/CentOS7.xとする

以下のコマンドを実行。
vagrantを実行するための設定ファイル、Vagrantfileが作成される。

PS> vagrant init <box名>
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.


Vagrant起動

Vagrantfileを作ったディレクトリで以下のコマンドを実行すると、VMが自動で作成される。

PS> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-7.1' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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: Running 'pre-boot' VM customizations...
==> 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
    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
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/<ユーザー名>/VirtualBox VMs/CentOS7.x


VMに接続する

sshで接続できる。

デフォルトでは

IP   127.0.0.1
post 2222

ID、PASSはデフォルト

ID   vagrant
PASS vagrant

になっている。


vagrantを終了する

ホスト側で以下のコマンドを実行する。

PS> vagrant halt
==> default: Attempting graceful shutdown of VM...


【Vagrant】【VirtualBox】 それでも起動しないとき

環境  Vagrant 1.8.1 VirtualBox 5.0.12

【VirtualBox】 が起動しない時の対処 - takafumi blog
の続き

VirtualBoxが動いたので、これでvagrant upできると思ったら

==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'paused' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

VirtualBoxコマンドラインから起動できない?
と思ったが、他のPCでは起動したので、またセキュリティソフト系の問題だと思う。
とりあえず、vagrant upしたときにVirtualBoxGUIも起動するようにする。

Vagrantfileを以下のように変更。

diff -u Vagrantfile.ORI Vagrantfile
--- Vagrantfile.ORI     2016-01-01 08:51:50.786582400 +0000
+++ Vagrantfile 2016-01-02 06:37:37.158967400 +0000
@@ -43,13 +43,13 @@
   # backing providers for Vagrant. These expose provider-specific options.
   # Example for VirtualBox:
   #
-  # config.vm.provider "virtualbox" do |vb|
-  #   # Display the VirtualBox GUI when booting the machine
-  #   vb.gui = true
-  #
+  config.vm.provider "virtualbox" do |vb|
+    # Display the VirtualBox GUI when booting the machine
+    vb.gui = true
+
   #   # Customize the amount of memory on the VM:
   #   vb.memory = "1024"
-  # end
+  end
   #
   # View the documentation for the provider you are using for more
   # information on available options.

これで、vagrant up時にVirtualBoxGUIも起動するようになる。

そして再びvagrant up
よし、起動した。sshでも問題なし。

【VirtualBox】 が起動しない時の対処

環境   VirtualBox 5.0.12

VirtualBoxが起動しない。

こんなエラー。

Timed out after 60001 ms waiting for child request #1
(CloseEvents).
(rc=258)

where: supR3HardNtChildWaitFor what: 5 Unknown Status 258 (0x102)
(258) - Unknown Status 258 (0x102)

で調べると、Avira Antivirusが影響しているらしい。

VirtualBox ゲストOSが起動しないことへの対処 | Tomehachi's Blog

いろいろ試してみたところ、うちの環境ではデバイスの無効化

[デバイスマネージャー]
↓
[表示]
↓
[非表示のデバイスの表示]
↓
[プラグ アンド プレイではないドライバー]
↓
[avipbb]

プロパティを開く

[ドライバー]タブ
↓
[種類]「無効」に

これで解決。

ちなみにavipbb.sysルートキット検出に関するシステムファイルらしい。

【Node.js】【npm】 npm コマンド超基本

環境   CentOS 7.0 JavaScript Node.js 0.12.2 npm 2.7.4


インストール

  • npm自体のインストール

nodeをインストールすると、自動でインストールされる。

  • npm install <モジュール名>

ローカルインストール

現在のディレクトリ直下のnpmプロジェクト内に./node_modulesディレクトリを作成し、モジュールをインストールする。

  • npm install -g <モジュール名>

グローバルインストール

マシン全体で共有されるモジュール。
標準設定のままnpmをインストールしていれば、/usr/local/node_modules/以下にモジュールがインストールされる。

  • npm install (-g) <モジュール名> --save

通常のinstall + 現在のプロジェクトのpackage.jsonにモジュールの依存関係を書き出す。
すでにinstallした状態で実行しても、package.jsonに依存関係が追加される。

マシン上グローバルにあるモジュールを、package.jsonに書き出す必要がある場合も使用。

  • npm install

package.jsonのあるプロジェクト内で実行すると、package.jsonに書かれた依存性のあるモジュールを全てinstallする。

何かのプロジェクトをGitHubからcloneした場合や、express-genelatorなどで雛形を作成した場合に必要なモジュールをインストールすつのに使用できる。


アンインストール

  • `npm remove (-g) <モジュール名> (--save)

モジュールをアンインストールする。
--saveを使うとpackage.jsonから依存性を削除する事ができる。


その他
  • npm ls (-g)

現在のプロジェクト(グローバルなプロジェクト)にあるモジュールを表示する。

  • npm help <cmd> (-l)

コマンドのヘルプを表示。
-lをつけると全ての内容を表示する。

  • npm view <モジュール名>

モジュールのpackage.jsonを表示する。


Node.jsの練習がてら作ってみた

github.com

【VMwarePlayer】【VirtualBox】 共有ディレクトリとシンボリックリンク

npmとかパッケージ管理ソフトで、インストールの際に失敗する事があるので調べていたら気が付いたのでメモ。

基本的にホストがLinuxのときは、VMwarePlayer、VirtualBox共にシンボリックリンクが作れない。

一応npmには--no-bin-linksというオプションで、シンボリックリンクを作らない方法があるけど、基本的には開発時に共有ディレクトリは使わない方が良さげ。


【参考】
【npm】【Vagrant】Vagrantの共有フォルダ上でnpm... - 人生dat落ち

`

【Haskell】 Project Euler Prblem4 を少し考えて解いてみた

環境   ghc 7.10.2 CentOS7.0

Problem 4 - PukiWiki

Euler problems/1 to 10 - HaskellWiki
だと結構力技なので、少し考えてみた。

  1 main :: IO ()
  2 main = print $ head [(x, y, z) | x <- palindromes dgt, y <- factors dgt, let z = x `div` y, x `mod` y == 0 && between z ]
  3     where dgt = 3
  4           between n = length (show n) == dgt
  5
  6 factorMax :: Int -> Int
  7 factorMax dgt = read . take dgt $ repeat '9' :: Int
  8
  9 factorMin :: Int -> Int
 10 factorMin dgt = read $ '1':(take (dgt - 1) $ repeat '0') :: Int
 11
 12 factors :: Int -> [Int]
 13 factors dgt = [fMax, fMax-1 .. fMin]
 14     where fMax = factorMax dgt
 15           fMin = factorMin dgt
 16
 17 palindromes :: Int -> [Int]
 18 palindromes dgt = filter isPalindrome [sqrMax, sqrMax-1..sqrMin]
 19     where sqrMax = factorMax dgt * factorMax dgt
 20           sqrMin = factorMin dgt * factorMin dgt
 21
 22
 23 isPalindrome :: Show a => a -> Bool
 24 isPalindrome x = s == reverse s
 25     where s = show x

https://gist.github.com/takafumi-s/dae0cb533bc9e0c2c10e#file-problem4-hs

前者の通常の回答をproblem4、上記のコードをproblem4'としてコンパイルする。

$ time ./problem4
906609

real    0m0.097s
user    0m0.094s
sys     0m0.003s

$ time ./problem4\'
(906609,993,913)

real    0m0.027s
user    0m0.026s
sys     0m0.000s

多少いい感じ。