ハイパーニートプログラマーへの道

頑張ったり頑張らなかったり

そして・・・vagrant up

Vagrantを導入、Boxの取得と初期化 - ハイパーニートプログラマーへの道

からの続き(のはず)ですが、ちょっと時間が開いてしまっているので細かい部分までは保証できませんが(汗

内容は

Vagrant入門 (全13回) - プログラミングならドットインストール

に沿って進めています。

そして・・・vagrant up

しばし待つ・・・。

なんか途中で、

GuestAdditions versions on your host (4.3.12) and guest (4.3.2) do not match.

とでていたが、いろいろ更新してくれるみたいで、

Complete!
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 4.3.12 - guest version is 4.3.2
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.12 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.2 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules[  OK  ]
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
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
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 4.3.12. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
[default] Mounting shared folders...
[default] -- /vagrant

まだまだ続く・・・。いや終った。
なんか最後のあたりfailedとでてるが、気にせず。
最小構成なので、Window Systemなんてないからなあ。
起動していたVirtualBoxにもできてるお。

f:id:noriyo_tcp:20140708012410p:plain

myCentOSVMディレクトリ上でlsすると

$ ls
Vagrantfile

Vagrantfileできてるー。

終了させる

$ vagrant halt
[default] Attempting graceful shutdown of VM...

VirtualBoxのほうも電源オフになっとる。

f:id:noriyo_tcp:20140708012536p:plain

vagrantの後にサブコマンド

サブコマンド 内容
status 状態を見る
suspend 休止
resume 再開
halt 停止
up 起動
reload 再起動
destroy 破棄

仮想マシンに接続

vagrant ssh

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.

こんなんでました。

[vagrant@localhost ~]$ pwd
/home/vagrant

Webサーバーインストール

sudo yum -y install httpd

// サーバースタート
sudo service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

なんか怒られるけど気にせず。

// 設定オン
$ sudo chkconfig httpd on
// ファイアーウォール切る
sudo service iptables stop  
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
// 設定
$ sudo chkconfig iptables off

Webページを表示させる

$ cd var/www/html

ここにhtmlファイルを作成

$ sudo vi index.html

<h1>Hello world!</h1>

という内容で作っておく。

$ exit
logout
Connection to 127.0.0.1 closed.

一旦抜ける。

Vagrantfileの編集

$ vi Vagrantfile

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network :private_network, ip: "192.168.33.10"

上記の部分の最後の行をコメントアウト。 編集内容の反映のために再起動

$ vagrant reload

再起動したら、ブラウザのURLバーに192.168.33.10と打ち込む。

共有フォルダを使う

Mac側での変更をvagrantにも反映させる。
VM側の/vagrantディレクトリにVagrantfileがある。これによって変更の同期ができる。

VMにログイン後

[vagrant@localhost ~]$ cd /vagrant
[vagrant@localhost vagrant]$ ls
Vagrantfile
$ touch test
[vagrant@localhost vagrant]$ ls
Vagrantfile  test

これでホスト側にもtestファイルができてる。

f:id:noriyo_tcp:20140708012626p:plain

$ sudo rm -rf /var/www/html
$ sudo ln -fs /vagrant /var/www/html
[vagrant@localhost vagrant]$ exit
logout
Connection to 127.0.0.1 closed.
noriyo-no-MacBook-Pro:myCentOSVM noriyo_tcp$ ls
Vagrantfile test
noriyo-no-MacBook-Pro:myCentOSVM noriyo_tcp$ vi index.html

一旦htmlファイルを削除して、/vagrantから/var/www/htmlにシンボリックリンクを張る。
これでホスト側の共有フォルダである、myCentOSVMでの変更内容がVM側に反映される。
ホスト側でindex.htmlを作成。
ブラウザで表示すると、反映されてる。

f:id:noriyo_tcp:20140708012731p:plain

[vagrant@localhost ~]$ cd /vagrant
[vagrant@localhost vagrant]$ ls
Vagrantfile  index.html  test

VMにログイン、/vagrantフォルダで確認。index.htmlファイルができてる。

Provisioning

もう1個仮想マシンを作成
ホームディレクトリに

$ mkdir myCentOSVM2
noriyo-no-MacBook-Pro:~ noriyo_tcp$ cd myCentOSVM2
$ vagrant init centos64
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.
noriyo-no-MacBook-Pro:myCentOSVM2 noriyo_tcp$ ls
Vagrantfile
noriyo-no-MacBook-Pro:myCentOSVM2 noriyo_tcp$ vi Vagrantfile 

作成されたVagrantfileを編集

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "centos64"

  config.vm.provision :shell, :inline => "echo hello world"

config.vm.provision :shell, :inline => "echo hello world" を追加

そしてvagrant up
hello worldとでる。

起動時にシェルスクリプト実行

config.vm.provision :shell, :path => "provision.sh"

と記述しておいて、今度はprovision.shの作成

$ sudo yum -y install httpd
$ sudo service httpd start
$ sudo chkconfig httpd on

↑Webサーバーインストール、スタート、設定反映を記述

vagrantが立ち上がっている状態なら、vagrant provisionでProvisioning fileを実行。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.

再びログイン。


vagrant-vbguest導入

vagrant-vbguestという、自動的にGuest Additionsを更新してくれるプラグインを入れる。
いろいろ注意点はあるみたいだが、大丈夫だろう。

$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.10.0)'!

[Vagrant] VirtualBoxのバージョンとGuest additionsのバージョンが合わない場合の対処法 - Code Life

Vagrant1.2.2から1.3.2にあげたらvagrant-vbguestが動作しない - Code Life

プラグインがバージョンアップしたら動かなくなった、みたいなことが書いてあるが
ファイルに記述して自動化するもいいけど、statusとdoでもなんとかなる?

Guest Additionsのバージョン確認

$ vagrant up
$ vagrant vbguest --status
$ vagrant vbguest --status
GuestAdditions 4.3.12 running --- OK.

Guest Additionsのアップデート

$ vagrant up
$ vagrant vbguest --do install