目次
vagrant
Install
- virtualbox
https://www.virtualbox.org/wiki/Downloads
VirtualBox-4.2.16-86992-Win.exe
※Ver4.3はvirtualboxが動作しないバグあり
- vagrant
http://www.vagrantup.com/
Vagrant Archive
http://downloads.vagrantup.com/
Vagrant_1.2.4.msi
- BOXファイル
http://www.vagrantbox.es/
セットアップ済みのOSイメージ
VMWareとVirtualBOXの2種類あるが、VirtualBOX使用。
基本動作
$ vagrant box add {title} {url} $ vagrant init {title} $ vagrant up
ヘルプ表示
C:\Users\*****>vagrant -h Usage: vagrant [-v] [-h] command [<args>] -v, --version Print the version and exit. -h, --help Print this help. Available subcommands: box destroy halt init package plugin provision reload resume sandbox ssh ssh-config status suspend up For help on any individual command run `vagrant COMMAND -h`
バージョン表示
C:\Users\*****>vagrant -v Vagrant version 1.2.4
BOXファイルをダウンロード
C:\Users\*****>vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box
標準ではC:\Users\*\.vagrant.d\boxes配下にダウンロードされる
ダウンロード後に作成される「C:\Users\*\.vagrant.d\boxes\centos\virtualbox\Vagrantfile」に
アプリのインストールなど共通処理を記述しておくと、セットアップ時に自動実行される
OS初期化
C:\Users\*****>vagrant init centos
コマンド実行時のディレクトリ(標準ではC:\Users\*\)に設定ファイル「Vagrantfile」が作成される
VM作成・起動
C:\Users\*****>vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'centos'... [0K[default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Fixed port collision for 22 => 2222. Now on port 2200. [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2200 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- /vagrant
状態確認
C:\Users\*****>vagrant status Current machine states: default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
ssh接続
C:\Users\*****>vagrant ssh `ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client. Or use the PuTTY SSH client with the following authentication information shown below: Host: 127.0.0.1 Port: 2200 Username: vagrant Private key: C:/Users/*****/.vagrant.d/insecure_private_key
SSHクライアント(puttyなど)で接続
デフォルトのユーザ/パスワードは「vagrant/vagrant」
login as: vagrant vagrant@127.0.0.1's password:(vagrant) Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$ sudo su - [root@localhost ~]#
vm起動
vagrant up
vm停止
vagrant halt
vm削除
vagrant destroy
プロジェクト毎に設定ファイルを分けて、任意のvm名で起動する。
プロジェクト名:samplePJ
vm名:sampleVM
ipアドレス:192.168.10.199
C:\Users\*****>mkdir samplePJ
C:\Users\*****>cd samplePJ
C:\Users\*****\samplePJ>vagrant init centos 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.
C:\Users\*\samplePJ\Vagrantfileを編集
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "centos" config.vm.define :sampleVM do |sampleVM| sampleVM.vm.network :private_network, ip: "192.168.10.199" end end
C:\Users\*****\samplePJ>vagrant init centos 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.
C:\Users\*****\samplePJ>vagrant up sampleVM Bringing machine 'sampleVM' up with 'virtualbox' provider... [sampleVM] Importing base box 'centos'... [0K[sampleVM] Matching MAC address for NAT networking... [sampleVM] Setting the name of the VM... [sampleVM] Clearing any previously set forwarded ports... [sampleVM] Fixed port collision for 22 => 2222. Now on port 2200. [sampleVM] Creating shared folders metadata... [sampleVM] Clearing any previously set network interfaces... [sampleVM] Preparing network interfaces based on configuration... [sampleVM] Forwarding ports... [sampleVM] -- 22 => 2200 (adapter 1) [sampleVM] Booting VM... [sampleVM] Waiting for VM to boot. This can take a few minutes. [sampleVM] VM booted and ready for use! [sampleVM] Configuring and enabling network interfaces... [sampleVM] Mounting shared folders... [sampleVM] -- /vagrant
C:\Users\*****\samplePJ\.vagrant\machines C:\Users\*****\samplePJ\.vagrant\machines\sampleVM\virtualbox\id
0c17f8bf-e56f-4a1b-8f7c-6c94bb587415
C:\Users\*****\VirtualBox VMs\~
samplePJ_1390202702ディレクトリが作成されて、配下にvm保存
C:\Users\*****\samplePJ>vagrant status Current machine states: sampleVM running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
C:\Users\*****\samplePJ>vagrant ssh sampleVM `ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client. Or use the PuTTY SSH client with the following authentication information shown below: Host: 127.0.0.1 Port: 2200 Username: vagrant Private key: C:/Users/*****/.vagrant.d/insecure_private_key
putty
login as: vagrant vagrant@192.168.10.199's password:(vagrant) Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$ sudo su - [root@localhost ~]#
追加ディスク
「db01-disk001.vdi」という5GBのファイルを作成して、アタッチする。
Vagrant.configure(2) do |config| config.vm.define :db01 do |node| node.vm.box = "chef/centos-6.6" node.vm.hostname = "db01.dev" node.vm.network "private_network", ip: "192.168.56.211" node.vm.provider "virtualbox" do |vm| ext_disk = 'db01-disk001.vdi' unless File.exists?(ext_disk) vm.customize ['createhd', '--filename', ext_disk, '--size', 5 * 1024] end vm.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', ext_disk] end end end
プラグイン「sahara」
インストール
C:\Users\*****\samplePJ>vagrant plugin install sahara
sandboxモード開始
C:\Users\*****\samplePJ>vagrant sandbox on sampleVM 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
sandboxモードの確認
C:\Users\*****>vagrant sandbox status [sampleVM] Sandbox mode is on
状態の巻き戻し
vmでphpをインストールする
[root@localhost ~]# yum list installed | grep php [root@localhost ~]# yum install -y php ~省略~ Complete! [root@localhost ~]# yum list installed | grep php php.x86_64 5.3.3-27.el6_5 @updates php-cli.x86_64 5.3.3-27.el6_5 @updates php-common.x86_64 5.3.3-27.el6_5 @updates
vagrantからロールバックコマンドを実行する
C:\Users\*****>vagrant sandbox rollback sample 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
vmでphpがインストールされていない(される前に戻っている)事を確認
[root@localhost ~]# yum list installed | grep php
sandboxの内容を反映
C:\Users\*****>vagrant sandbox commit sample 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
[root@localhost ~]# yum list installed | grep mysql
[root@localhost ~]# yum install -y mysql
~省略~
Complete!
sandboxモードの終了
C:\Users\*****>vagrant sandbox off sample 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
※commitされていない処理はcommitされる
C:\Users\*****>vagrant sandbox status [sample] Sandbox mode is off
[root@localhost ~]# yum list installed | grep mysql mysql.x86_64 5.1.71-1.el6 @base mysql-libs.x86_64 5.1.71-1.el6 @base
vagrant-global-status
C:\Users\*>vagrant plugin install vagrant-global-status
Installing the 'vagrant-global-status' plugin. This can take a few minutes…
Installed the plugin 'vagrant-global-status (0.1.4)'!
C:\Users\*>vagrant global-status -a
文字化け。。。
C:\Users\*>vagrant status
Current machine states:
sample inaccessible (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
C:\Users\*>“C:\Program Files\Oracle\VirtualBox\VBoxManage.exe” list vms
“<inaccessible>” {4f131712-d950-4f4c-bf08-d9b80d89abe9}
C:\Users\*>“C:\Program Files\Oracle\VirtualBox\VBoxManage.exe” unregistervm {4f131712-d950-4f4c-bf08-d9b80d89abe9}
C:\Users\*>vagrant status
Current machine states:
sample not created (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
Tips
Failed to mount folders
==> cen: Mounting shared folders... cen: /vagrant => C:/centos/web/ 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 The error output from the last command was: /sbin/mount.vboxsf: mounting failed with the error: No such device
# /etc/init.d/vboxadd setup Removing existing VirtualBox non-DKMS kernel modules [ OK ] Building the VirtualBox Guest Additions kernel modules The headers for the current running kernel were not found. If the following module compilation fails then this could be the reason. The missing package can be probably installed with yum install kernel-devel-2.6.32-504.23.4.el6.x86_64 Building the main Guest Additions module [FAILED] (Look at /var/log/vboxadd-install.log to find out what went wrong) Doing non-kernel setup of the Guest Additions [ OK ]
# yum -y install gcc perl kernel-devel-2.6.32-504.23.4.el6.x86_64 # /etc/init.d/vboxadd setup