LoginSignup
24
27

More than 5 years have passed since last update.

既存VirtualBoxマシンをVagrant化するときにハマったエラー回避方法など

Last updated at Posted at 2016-03-13

macbook上で既存VirtualBoxマシンをVagrant化にチャレンジしてみましたが、vagrant up 時に ruby が大量のエラーを吐いたり、ゲストマシンにssh接続できなかったりと、つまずくところが多かったので解決方法をまとめました。

作業環境

■ホストマシン

  • Macbook Pro 15インチ
  • Mac OS X 10.10 Yosemite
  • VirtualBox 5.0.14 r105127
  • Vagrant 1.7.4 + Landrush + vagrant-vbguest
  • Ruby 2.0.0p481 (2014-05-08 revision 45883)

■ゲストマシン

  • CentOS 7.1 64bit
  • GuestAdditions 5.0.14

既存のVirtualBoxマシンをVagrant化するための準備をする

まず、下の記事を参考にして、既存のVirtualBoxマシンにログインして vagrant box 化するための準備をしました。

参考記事)既存のCentOSのイメージファイルを使ってvagrant用boxファイルを作ったメモ

■やったこと

参考記事を見て、実際にやったことは下の4つです。

  • VMの設定(rootパスワードをvargrantにする 、 vargrantユーザーを作成する )
  • Permission設定
  • VirtualBox Guest Additionsのインストール
  • SSH設定

■やらなかったこと

すでにmac上にVagrantコマンドの実行環境を整備済みだったのでRubyインストールなどは飛ばしました。
とりあえずVagrant化だけできればいいので Chef,Puppet の項目も実施しませんでした。

  • Ruby & RubyGemを入れる
  • Chef,Puppetのインストール
  • パッケージ作成

パッケージ作成のところは、記事を参考にせず別途作業しました。そのまとめは当記事中で解説しています。

Vagrantパッケージを作成する

次に、vagrant package コマンドでパッケージを作成します。

コマンドの引数にvboxファイルのパスを指定する必要があります。 パスが分からなければVirtualBox設定画面の「一般」タブからスナップショット保存先が確認できますが、その一階層上にvboxファイルが配置されています。

スクリーンショット 2016-03-13 17.03.54.png

vagrant package コマンドを実行します

% vagrant package --base "/Volumes/JetDrive/VirtualBox VMs/CentOS64bit/CentOS6 64bit.vbox"

ただ、既存のvboxファイルが60GBとデカかったためか、コマンド実行が完了するまでに1時間近くかかりました。
何かの作業の合間に実施することをオススメします。

コマンド実行が完了すると /Users/[ユーザー名]/package.box に vagrant boxファイルが出力されます。

Vagrantのホームディレクトリを準備します。

% mkdir /Users/[ユーザー名]/Documents/new-site.com/vagrant
% cd /Users/[ユーザー名]/Documents/new-site.com/vagrant

Vagrant boxファイルを追加します。

%  vagrant box add new-site.com /Users/[ユーザー名]/package.box
%  vagrant init new-site.com

デフォルトのポートフォワードではなくIPアドレスをふって使いたいのでVagrantfileファイルを編集して、config.vm.network "private_network" の行を追加します。

% vim Vagrantfile

以下行を追加する。
config.vm.network "private_network", ip: "192.168.33.10", auto_config:false

Vagrantマシンを起動します。

%  vagrant up

Vagrantマシンを起動中にことごとくエラーに見舞われる

Vagrant boxを公式サイトからダウンロードしてきたものを利用したり、人からもらったboxファイルを使う分にはつまづいたことはありませんが、同一ホストマシン上でboxを生成したり複製したりすると、思うようには行きません。

まず、一つ目のエラーとして、vagrant upコマンドを実行後に rubyスクリプトが文字エンコーディングエラー「Encoding::UndefinedConversionError」を吐いてしまいました。

長いので別記事にまとめています。

vagrant up時に文字エンコーディングエラー「Encoding::UndefinedConversionError」が表示されてしまう

なんとか解消して改めてVagrantマシンを起動しました。

%  vagrant up

そうしたらまた別の問題が起こりました。

複製した vagrant box のNICが認識されないことが原因で、vagrant up コマンドの最後に「Device eth1 does not seem 〜〜」みたいな不穏なメッセージが表示されてしまいました。

この状態では vagrant ssh コマンドを実行してもログインできませんでした。

これも解消できましたが、長いので別記事にまとめました。

複製したvagrant boxでNICエラーになりssh接続できない

HTML、CSSの更新が反映されるようにする

Vagrant の shared_folder を利用する場合、HTML、CSSを更新しているのにブラウザに反映されないことがあります。

以下のように設定しておきましょう。

apacheの場合

# vim /etc/httpd/conf/httpd.conf
EnableSendfile off

nginxの場合

# /etc/nginx/nginx.conf
sendfile off;

編集が終わったら、いったんVagrantマシンをシャットダウンします。

% vagrant halt

ドキュメントルートの場所を移動する

ここまでくれば残るエラーはもうありません。
あとはドキュメントルートを shared_folder 配下へ移動させるだけです。

まず、ファイルを移動します。

# mkdir /vagrant/dev
# mv /home/web/public_html/* /vagrant/dev

※パスは環境に合わせて適宜打ち替えてください。

apacheのバーチャルホストを修正します。

# vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
    ServerAdmin siteadmin@web-site.jp
    DocumentRoot /vagrant/dev/
    ServerName web-site.jp
    ErrorLog logs/web-site.jp-error_log
    CustomLog logs/web-site.jp-access_log common
</VirtualHost>

Vagrantマシンを起動する

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: [landrush] Host DNS resolver config looks good.
==> default: [landrush] starting dns server
[landrush] Starting daemon...
Daemon in unknown state! Will clear previous state and continue.
[landrush] Daemon status: unknown
[landrush] Waiting for daemon to start...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions versions on your host (5.0.16) and guest (5.0.14) do not match.
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: ftp.yz.yamagata-u.ac.jp
 * extras: ftp.yz.yamagata-u.ac.jp
 * remi: mirror.innosol.asia
 * remi-safe: mirror.innosol.asia
 * updates: ftp.yz.yamagata-u.ac.jp
Package kernel-devel-2.6.32-573.12.1.el6.x86_64 already installed and latest version
Package gcc-4.4.7-16.el6.x86_64 already installed and latest version
Package 1:make-3.81-20.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-141.el6_7.1.x86_64 already installed and latest version
Package bzip2-1.0.5-7.el6_0.x86_64 already installed and latest version
Nothing to do
Copy iso file /Volumes/JetDrive/VirtualBox VMs/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.0.14 - guest version is 5.0.14
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.14 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.14 of VirtualBox Guest Additions...
Stopping VirtualBox Additions [FAILED]
(Cannot unload module vboxguest)
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
[  OK  ] VirtualBox Guest Addition service [  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the OpenGL support module[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong. The module is not built but the others are.)
Doing non-kernel setup of the Guest Additions[  OK  ]
You should restart your guest to make sure the new modules are actually used

Installing the Window System drivers
Installing X.Org Server 1.15 modules[  OK  ]
Setting up the Window System to use the Guest Additions[  OK  ]
You may need to restart the the Window System (or just restart the guest system)
to enable the Guest Additions.

Installing graphics libraries and desktop services components[  OK  ]
An error occurred during installation of VirtualBox Guest Additions 5.0.14. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
==> default: Checking for guest additions in VM...
==> default: [vagrant-hostsupdater] Checking for host entries
==> default: [vagrant-hostsupdater] Writing the following entries to (/etc/hosts)
==> default: [vagrant-hostsupdater]   192.168.33.10  new-site.com  # VAGRANT: bc42c791acd1ce330f896947a15fa460 (default) / 66d961b7-f327-4615-a10a-4fcefe4919f9
==> default: [vagrant-hostsupdater] This operation requires administrative access. You may skip it by manually adding equivalent entries to the hosts file.
Password:
==> default: Setting hostname...
==> default: [landrush] adding machine entry: new-site.com => 10.0.2.15
==> default: Mounting shared folders...
    default: /vagrant => /Users/[ユーザー名]/Documents/new-site.com/vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

最後までエラーが表示されずに完了しました!
途中でパスワードを求められますが、ホストマシンのパスワードを入力すればOKです。

ここまで来ればssh接続できます。

% vagrant ssh
Last login: Sat Mar 12 22:51:45 2016

まとめ

エラーに悩まされましたが一度解決できてしまえば、2回目、3回目のVagrantマシン化は もうrubyスクリプトのエラーは出ませんし、ssh接続エラーの対処も慣れたものです。

あまりこの辺の情報が見つからなかったので解決するまで時間がかかりました。
こんな苦労をせずに何かツールを使えばもっと楽にできるよとか、情報持っている方がいましたら教えてください。

24
27
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
24
27