masayuki5160's diary

名古屋でエンジニアしてます。

Vagrantでboxの複製

やっぱりすきな環境をいつでも立ち上げれるようにコピーをつくっときたいな、、
ということでVagrantでやってみました。


手順1:Vagrantfileのあるところで、vagrant packageでboxを作成する。

tanaka-no-MacBook-Air:TestCentOS masayuki5160$ ls -l
total 16

  • rw-r--r-- 1 masayuki5160 staff 4381 5 6 12:46 Vagrantfile

tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$ vagrant package
[default] Clearing any previously set forwarded ports...
[default] Creating temporary directory for export...
[default] Exporting VM...
[default] Compressing package to: /Users/masayuki5160/vagrant/TestCentOS/package.box
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$
tanaka-no-MacBook-Air:TestCentOS masayuki5160$ ls -l
total 688624

  • rw-r--r-- 1 masayuki5160 staff 4381 5 6 12:46 Vagrantfile
  • rw-r--r-- 1 masayuki5160 staff 352564740 5 9 20:19 package.box

tanaka-no-MacBook-Air:TestCentOS masayuki5160$




手順2:新しく作成するサーバ用のディレクトリをつくり、複製したboxからインスタンスを作成。

tanaka-no-MacBook-Air:vagrant masayuki5160$ mkdir TestCentOS2
tanaka-no-MacBook-Air:vagrant masayuki5160$ cd TestCentOS2
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ vagrant box add centos2 ../TestCentOS/package.box
Downloading or copying the box...
Extracting box...te: 349M/s, Estimated time remaining: --:--:--)
Successfully added box 'centos2' with provider 'virtualbox'!
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ ls -l
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ vagrant init centos2
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.
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ ls -l
total 16

  • rw-r--r-- 1 masayuki5160 staff 4345 5 9 20:33 Vagrantfile

tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos2'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (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] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
tanaka-no-MacBook-Air:TestCentOS2 masayuki5160$ vagrant ssh
Last login: Wed May 8 23:59:54 2013 from 10.0.2.2
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$



Vagrantすげーなーてのと、
やっぱりMacBookAirのアルティメットモデルを思い切ってかってよかったー、
と改めて思いました。

【参考サイト】

せっかく調べたのでVagrantのコマンドを貼っておきます
http://blog.mizoshiri.com/archives/1387

Vagrantで環境を複製する方法
http://www.u-pyon.com/2013/01/vagrant.php