LoginSignup
63
71

More than 3 years have passed since last update.

【備忘録】Vagrant 基本の巻

Last updated at Posted at 2014-08-12

boxの取得関連

MacOSターミナル
 # boxの一覧を表示する。
 $ vagrant box list
 # boxを追加する
 $ vagrant box add {title:任意のbox名} {url:上記boxのURL} 
 # boxを削除する
 $ vagrant box remove {title}
 # boxコマンドヘルプを表示する
 $ vagrant box -h
 # boxキャッシュを表示する
 $ ls ~/.vagrant.d/boxes

仮想環境の起動関連

MacOSターミナル
 # 1.仮想マシンディレクトリを作成し、移動する。
 $ mkdir {vmdir:仮想マシンディレクトリ} 
 $ cd {vmdir}
 # 2.初期化する。
 $ vagrant init {title}
 # Vagrantファイルを確認する。(任意)
 $ ls
 $ less Vagrantfile #Rubyでの記述あり。
 # 3.起動する。
 $ vagrant up

仮想環境の再起動/サスペンド/レジューム/削除

MacOSターミナル
 # 再起動する。
 $ vagrant reload
 # 状態を確認する。
 $ vagrant status
 # サスペンド/レジューム状態にする。
 $ vagrant {status:suspend(サスペンド)/resume(レジューム)}
 # 仮想マシンを削除する。
 $ vagrant destroy

自作パッケージのエクスポート

MacOSターミナル
$ vagrant package
#package.boxが出力される。
$ vagrant package --output django-python36-centos7
# 名前の指定も可能

$ vagrant box add BOX_NAME ./package.box
# BOX_NAMEは任意のボックス名、この名前でボックスが追加される。

仮想環境へのSSH接続

MacOSターミナル
 # 1.SSH接続する。
 $ vagrant ssh
 # 2.sshコマンドでのアクセスを可能とする。
 $ vagrant ssh-config --host {hostname}
 #仮想サーバへのssh設定を出力してくれるようだ。
 $ vagrant ssh-config --host {hostname} >> ~/.ssh/config
 $ ssh {hostname}

プライベートIP付与

  • こちらの設定で、固定IP(192.168.33.10)でアクセスできるようになる。
  • Vagrantfileを編集した後、必ず再起動する。
MacOSターミナル
 $ vi VagrantFile
Vagrantfile
config.vm.network :private_network, ip: "192.168.33.10" #コメント削除

ローカルIP付与

1.Vagrantfileの末尾に下記を追加する。

Vagrantfile
Vagrant::Config.run do |config|
  config.vm.network :bridged
end

2.vagrantを起動する。ブリッジネットワークを問われるので、en1を選択する。

MacOSターミナル
$ vagrant up
==> default: Clearing any previously set network interfaces...
 : 
==> default: Available bridged network interfaces:
1) en1: Wi-Fi (AirPort)
2) en0: Ethernet
3) en2: Thunderbolt 1
4) p2p0
5) bridge0
6) vmnet1
7) vmnet8
    default: What interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
 :
==> default: to force provisioning. Provisioners marked to run always will still run.
$ 

3.SSH接続し、IPを調べる。(この場合はen1)
4.ローカルIPでアクセスする。


共有フォルダを設定する

vagrant(SSH)
 $ sudo ln -fs /vagrant {path:共有先パス ex./var/www/html}

 # httpdルートを/vagrantに振り分ける例
 $ sudo rm -rf /var/www/html
 $ sudo ln -fs /vagrant /var/www/html

共有フォルダの実行権限を変更する。

  • Apacheルートを共有フォルダに設定した時、実行権限はVagrantfileで設定する。
Vagrantfile
   config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", :nfs => false,
    :owner => "vagrant",
    :group => "apache",
    :mount_options => ["dmode=775,fmode=664"]
  • centos/7では「type:"virtualbox"」をつけないと、フォルダの自動同期が有効にならなかったので、注意。
Vagrantfile
   config.vm.synced_folder ".", "/vagrant", type:"virtualbox"

共有フォルダをマウントできない

console(MacOS)
Installing Virtualbox Guest Additions 4.3.18 - guest version is 4.3.14
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.18 Guest Additions for Linux............
VirtualBox Guest Additions installer
You appear to have a version of the VBoxGuestAdditions software
on your system which was installed from a different source or using a
different type of installer.  If you installed it from a package from your
Linux distribution or if it is a default part of the system then we strongly
recommend that you cancel this installation and remove it properly before
installing this version.  If this is simply an older or a damaged
installation you may safely proceed.

Do you wish to continue anyway? [yes or no]

Cancelling installation.
An error occurred during installation of VirtualBox Guest Additions 4.3.18. 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: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/shoko_kb/vagrant/eXist2
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:

mount: unknown filesystem type 'vboxsf'

Provision(起動都度事前処理)を設定する。

  • シェルコマンドを実行する。
Vagrantfile
 #Vagrantfileに追加のシェルを記載する。(vagrant upで再起動する。)
 config.vm.provision :shell, :inline => "echo hogehoge"

  • シェルスクリプトファイルを実行する。
Vagrantfile
 #Vagrantfileの隣にprovision.shを配置する。(vagrant provisionで再起動する。)
 config.vm.provision :shell, :path => "provision.sh" 
  • Vagrantfileを編集した後、必ず再起動する。
MacOSターミナル
 $ vagrant provision
 $ vagrant relaod

プラグイン系

MacOSターミナル
$ vagrant plugin update #プラグイン更新(インストール前に一式更新したほうがよい。)
$ vagrant plugin install {plugin} #インストール
$ vagrant plugin uninstall {plugin} #アンインストール
$ vagrant plugin -h #ヘルプ表示
$ vagrant plugin list #プラグイン一覧表示

sahara:状態管理

  • Vagrantの状態管理、コミット(状態反映)・ロールバック(状態破棄)が可能となる。
MacOSターミナル
# インストール
$ vagrant plugin install sahara
# sandboxモード実行
$ vagrant sandbox on
# ロールバック
$ vagrant sandbox rollback
# コミット
$ vagrant sandbox commit
# sandboxモード終了(コミットしていない変更は削除)
$ vagrant sandbox off
# sandboxのステータス確認
$ vagrant sandbox status

vagrant-omnibus:Chefの自動インストール

MacOSターミナル
# インストール
$ vagrant plugin install vagrant-omnibus

vagrant-vbguest:Virtualbox-guest-addtionの自動インストール

MacOSターミナル
# インストール
$ vagrant plugin install vagrant-vbguest
# 手動 Vitualbox guest インストール
$ vagrant vbguest

Vagrantfileを読んでみる。

  • 読書中。
Vagrantfile

# -*- mode: ruby -*- 
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
# Vagrantfile のバージョン
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  # 使用するボックスを指定する。
  config.vm.box = "package.box"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # ボックスの自動アップデート設定
  # OFF時は`vagrant box outdated`を実行しないかぎり、アップデートはかからない。(非推奨)
  config.vm.box_check_update = true

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # ポートフワードマッピングの設定
  # 例)Host:8080にアクセスした時にGuest:80にアクセスする。
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # プライベートネットワーク接続の設定
  # (デフォルト:192.168.33.10でアクセス可。)
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # パブリックネットワークに接続する場合
  config.vm.network "public_network"

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  # SSH Agent Forwardingの設定(デフォルト:false)→不明
  config.ssh.forward_agent = true

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # 同期フォルダの追加
  # デフォルトでvagrantフォルダと/vagrantが同期しているが、
  # さらに、同期フォルダが必要な場合、設定する。
  # 例)../dataを/vagrant_dataでマウントして使用する。
  config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Don't boot with headless mode
  #   vb.gui = true
  #
  #   # Use VBoxManage to customize the VM. For example to change memory:
  #   vb.customize ["modifyvm", :id, "--memory", "1024"]
  # end
  #
  # View the documentation for the provider you're using for more
  # information on available options.

  # Enable provisioning with CFEngine. CFEngine Community packages are
  # automatically installed. For example, configure the host as a
  # policy server and optionally a policy file to run:
  #
  # config.vm.provision "cfengine" do |cf|
  #   cf.am_policy_hub = true
  #   # cf.run_file = "motd.cf"
  # end
  #
  # You can also configure and bootstrap a client to an existing
  # policy server:
  #
  # config.vm.provision "cfengine" do |cf|
  #   cf.policy_server_address = "10.0.2.15"
  # end

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file default.pp in the manifests_path directory.
  #
  # config.vm.provision "puppet" do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "site.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  #
  # config.vm.provision "chef_solo" do |chef|
  #   chef.cookbooks_path = "../my-recipes/cookbooks"
  #   chef.roles_path = "../my-recipes/roles"
  #   chef.data_bags_path = "../my-recipes/data_bags"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { mysql_password: "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision "chef_client" do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # If you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"
end

アンインストール

MacOSターミナル
# プログラム本体を削除する。
rm -rf /Applications/Vagrant
rm -rf /usr/bin/vagrant
# ユーザーデータを削除する。
rm -rf ~/.vagrant.d
  • バージョン(1.6.3→1.6.5)が変更されたときに、プラグインが更新できないまま、はまってしまったのでクリーンインストールしたほうが早そうである。
  • ユーザーデータでボックスリストがクリアされるので、必要なボックスはローカルに退避したほうがよさそう?
63
71
2

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
63
71