LoginSignup
75
77

More than 5 years have passed since last update.

vmコマンドラインツールのvagrant使用方法メモ

Last updated at Posted at 2013-03-18

コマンドラインから仮想マシンのVirtualBoxを操作する
gemパッケージのvagrantがすんばらしかったので使い方メモ。

あらかじめVirtualBoxをインストールする必要がある。

基本的な使い方

$ vagrant box add {title} {url}
$ vagrant init {title}
$ vagrant up

これだけで仮想環境が立ち上がる

boxの入手

osのイメージをboxと呼ぶ。
boxの入手先は以下
http://www.vagrantbox.es

centos6.3を立ち上げる

例としてcentOS6.3を立ち上げる。

$ mkdir centos
$ cd centos
$ vagrant box add centos https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box
$ vagrant init centos
$ vagrant up

vagrant initを行ったディレクトリにはVagrantfileが作成されて
起動を行うと.vagrantが作成される。

ssh接続

$ vagrant ssh

普通にssh接続する事も出来る

$ vagrant ssh-config --host vagrant-vm >> ~/.ssh/config
$ ssh vagrant-vm

基本的なコマンド

# Vagrantfileの作成
$ vagrant init
# Vagrantfileを元にしたvmの立ち上げ
$ vagrant up
# ssh接続
$ vagrant ssh
# vmのシャットダウン
$ vagrant halt
# vmの起動状態表示
$ vagrant status
# vmの削除
$ vagrant destroy

Vagrantfileの編集

Vagrantfileはrubyで記述できる

# 通常設定
Vagrant::Config.run do |config|
  config.vm.box = "centos"
end


# シングルvmカスタマイズ
Vagrant::Config.run do |config|
  config.vm.box = "centos"
  # ローカルに無ければ取ってくる
  config.vm.box_url = "https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box"
  # ip指定
  config.vm.network :hostonly, "192.168.33.10"
  config.vm.customize do |vm|
    vm.memory_size = 2048  # メモリ2G
    vm.name = "centOS6.3"  # 名前指定
  end
end


# 一度に複数のvm立ち上げ
Vagrant::Config.run do |config|
  config.vm.define :node01 do |cfg|
    cfg.vm.box = "centos"
    cfg.vm.network :hostonly, "192.168.30.10"
    cfg.vm.host_name = "node01"
  end

  config.vm.define :node02 do |cfg|
    cfg.vm.box = "centos"
    cfg.vm.network :hostonly, "192.168.30.11"
    cfg.vm.host_name = "node02"
  end

  config.vm.define :node03 do |cfg|
    cfg.vm.box = "centos"
    cfg.vm.network :hostonly, "192.168.30.12"
    cfg.vm.host_name = "node03"
  end
end

vagrantで起動できるpackageの作成

$ visudo
%wheel ALL=(ALL) NOPASSWD:ALL

$ echo 'UseDNS no' >> /etc/ssh/sshd_config

# vagrantユーザーの追加
$ adduser vagrant
$ passwd vagrant
$ gpasswd -a vagrant wheel
$ mkdir /home/vagrant/.ssh
$ chmod 700 /home/vagrant/.ssh
$ cd /home/vagrant/.ssh
$ curl 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' > authorized_keys
$ chown -R vagrant:vagrant /home/vagrant/.ssh

# ネットワーク関連
$ rm /var/lib/dhcp3/*

# MACアドレスの自動保存無効化
$ rm /etc/udev/rules.d/70-persistent-net.rules
$ mkdir /etc/udev/rules.d/70-persistent-net.rules

# ホスト側で実行
$ vagrant package --base VMディレクトリ --output BOX名.box

参考 : Vagrant のベースBOX作成手順 (Scientific Linux 6.1)

どうやら.vagrantがあるディレクトリでも実行できるらしい。
vagrantでupしたイメージを自分なりにいい感じにした後にpackage化して配布するのが簡単かも知れない。

状態の巻き戻し

vagrantの状態を巻き戻す方法

$ gem install sahara
$ vagrant up
# sandboxモードの開始
$ vagrant sandbox on
# 状態の巻き戻し
$ vagrant sandbox rollback
# sandboxモードの終了
$ vagrant sandbox off
# sandboxの内容を恒久的に反映
$ vagrant sandbox commit
# sandboxモードの確認
$ vagrant sandbox status

まとめて

id:naoya入門Chef-Solo-
を購入してvagrantの事を知りました。ありがとうございます。
chef-soloよりも興味出てしまったのでとりあえずvagrantの事知ってからchef-soloも使っていこうと思います。
vagrantは標準でchefと組み合わせる使い方が出来るので今度はchef合わせ技を試していく予定。
vagrantとchef組み合わせるの楽しそう!!!!

開発環境を手軽に配布できて手軽に起動できるの良いです。

入門Chef-Solo-も面白いのでぜひ

参考 :
Vagrant intro
Vagrant - naoyaのはてなダイアリー
Vagrant のベースBOX作成手順 (Scientific Linux 6.1)
入門Chef-Solo-
VagrantをPluginで拡張する
vagrant sandboxが便利な件

75
77
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
75
77