Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

vagrantその0025 Windows10でvagrantを使う

参考にさせて頂いたページ

Windows10にVirtualBoxとVagrantでCentOSの仮想環境を作ってみた / ユービックログ

VagrantでBox追加時にエラーが出た時の話 - Naba Blog

今回の環境(使用したインストーラー)

virtualboxのインストール

Windows10にVirtualBoxとVagrantでCentOSの仮想環境を作ってみた / ユービックログ

を参考に環境構築をしていく。

VirtualBox-5.0.24-108355-Win.exe をダウンロードしてインストールした。

特に設定変更などはせずにインストールした。

C:\Program Files\Oracle\VirtualBox

にインストールした。

vagrantのインストール

vagrant_1.8.4.msi をダウンロードしてインストールした。

C:\HashiCorp\Vagrant\

にインストールした。

vagrant box add しようとしたらエラー

cd ~
vagrant box add 任意の名前 URL

したら以下のようなエラーが出た。

An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Microsoft Visual C++ 2010 再頒布可能パッケージ をインストールしてエラーを解消する

VagrantでBox追加時にエラーが出た時の話 - Naba Blog

に倣って

をインストールした。

そのときのインストーラーは以下の二つ。

  • vcredist_x86.exe
  • vcredist_x64.exe

vagrant box add する

もういちど

Windows10にVirtualBoxとVagrantでCentOSの仮想環境を作ってみた / ユービックログ

にならって続きを行う。

CentOS 6.7 x64 (Minimal, Puppet 4.2.3, Guest Additions 4.3.30)

を選択することにしたので、

https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box

のboxを使う。

例えば以下のように

vagrant box add centos67box https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box

みたいな感じで実行する。

これは時間がかかる。

回線速度にもよるんだろうけど、うちは遅いので20分くらいかかった。

以下のように表示されてbox追加成功した。

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos67box' (v0) for provider:
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
    box:
==> box: Successfully added box 'centos67box' (v0) for 'virtualbox'!

以下のコマンドで追加済みのboxを確認する。

vagrant box list

vagrant init する

任意の場所に vagrant_sample ディレクトリを作成して進めてみる。

mkdir sample_vagrant cd sample_vagrant

centos67boxという名前でboxを追加したので

以下のコマンドを実行した。

vagrant init centos67box

以下のように表示された。

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.

このディレクトリにVagrantfileができた。

vagrant up する

Windowsの場合は、ここからはコマンドプロンプトじゃなくて、msysgitでインストールされるgitbashを使うと良いと思う。

以下のコマンドを実行した。

vagrant up

以下のように表示されて成功した。

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos67box'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_sample_default_1467987696042_15307
==> 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...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/motomichi/Desktop/all/vagrant_all/vagrant_sample

ここでこけることがよくあるし、実際そういうブログとかも多いと思うのでラッキーだった。

何年か前にwindows7とかMacOSXでやったときはvirtualboxvagrantのバージョンの組み合わせ次第で駄目だったり、WindowsBIOS設定を変更して、仮想化機能を有効にしないといけなかったり、色々躓いた。

仮想化機能の有効化については以下のような感じかな。

Windows7:Virtual PC(バーチャルPC)を使うには - 教えて!HELPDESK

仮想化機能を有効にするためのBIOS設定|テックウインド株式会社

仮想サーバにログインしてみる

以下のコマンドを実行した。

vagrant ssh

ログインできた。

rootユーザーにスイッチしてみる。

su

パスワードの入力を促されるので

vagrant

rootユーザーになった。

ログアウトしてみる。

exit
exit

仮想サーバをシャットダウンしておく。

vagrant halt

シャットダウンされた。

とりあえず今回はここまで。