takafumi blog

日々の勉強メモ

【Vagrant】 独自のboxを作成する

環境  Vagrant 1.8.1 VirtualBox 5.0.12

既存のVMからboxイメージを作成する

ホスト側から以下のようにする。

PS> vagrant package --base VMName --output Custom.box
==> VMName: Attempting graceful shutdown of VM...
    VMName: Guest communication could not be established! This is usually because
    VMName: SSH is not running, the authenticationinformation was changed,
    VMName: or some other networking issue. Vagrant will force halt, if
    VMName: capable.
==> VMName: Forcing shutdown of VM...
==> VMName: Clearing any previously set forwarded p
orts...
==> VMName: Exporting VM...
==> VMName: Compressing package to: Custom.box

--baseVirtualBox上の名前
--output は新しいboxの名前

【参考】

Vagrant:package作成時「VM not created. Moving on…」をエラーが出たら | ログってなんぼ

--outputが未指定だと、package.boxになる


新しいboxファイルを、vagrantのboxに登録する

PS> vagrant box add NewBox Custom.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'NewBox' (v0) for provider:
    box: Unpacking necessary files from: file://Custom.box
    box: Progress: 100% (Rate: 461M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'NewBox' (v0) for 'virtualbox'!

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

となる。

【Vagrant】 VirtualBoxのGuestAdditionsを自動で更新する

環境  Vagrant 1.8.1 VirtualBox 5.0.12

割と有名な事らしいけど、VirtualBoxのゲストでカーネルに更新があったときは、GuestAdditionsを入れなおす必要があるらしい。

VirtualBox共有フォルダ覚書 | dark_greenの日記 | スラド

当然知らず、vagrantVM作って即yum upgradeしたところ、vagrantでのファイル共有ができなくなった。

==> default: Mounting shared folders...                                        
    default: /vagrant =>  C:/Users/<ユーザー名>/VirtualBox VMs/CentOS7.x
Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

とはいえ、ゲスト側で毎度GuestAdition入れなおすのは面倒なので、何か方法がないかとググッてみた。

まさにこれです。

vagrantプラグインを以下のコマンドで導入。

PS> vagrant plugin install vagrant-vbguest

後は普通にvagrant upでOK。

起動時にゲストが自動で更新される。



【追記】

と思ったら罠が。

vagrant-vbguestは常に最新版のGuestAdditionsをインストールしようとする。

つまり、カーネル側も最新になっていないと、GuestAdditionsだけが更新され、結局バージョン違いで問題になる事がある。

vagrant-vbguestプラグインがGuestAdditionsを無効にしてしまう - Qiita

dotless-de/vagrant-vbguest · GitHub

と言う事で、Vagrantfileに通常は更新無効の設定を書いておく

Vagrant::Config.run do |config|
  config.vbguest.auto_update = false
end


【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