LoginSignup
74

More than 5 years have passed since last update.

Vagrant の環境を複製する

Last updated at Posted at 2014-06-21

Vagrant の環境を複製する

そのまんまなんですけど。Vagrant の box として用意されているOSは、タイムゾーンが UTC になっているので JST に変更したりユーザを追加したり・・・と VM を作成する毎に設定するのが面倒な場合は複製してしまうのが簡単。そんなメモ。
ちなみにいい感じに作った vm をパッケージ化してもいいしそこらへんは自由度が高いと思いますが、完全に自動化&最新化したいと考えているなら Chef Solo を使う手もあります。

手順は、以下のとおり

  1. 複製ベースとなる Vagrantfile があるフォルダに移動する。
  2. パッケージ化コマンド vagrant package または vagrant package [vm name] --output [output.box] を実行する。
  3. 作成したパッケージを追加 vagrant box add する。
  4. box_url で上記パッケージを指定する。

具体的な手順

まずベースとなる OS を Vagrant の box からチョイスしそこに自分なりにカスタマイズした物をパッケージ化(box化)→boxを使用するという流れ。

ベースとなる vm の作成と環境設定

Chocolatey + VirtualBox + Vagrant の簡単セットアップ (Windows)を参考にしながら ベースとなる vm を作成します。作成が終了したら タイムゾーンを JST に変更したりユーザを追加したり yum -y update したり環境設定を行います。

パッケージ化コマンドの実行

vagrant package コマンドでパッケージ出力します。
ベースとなる vm の名前を centos65base として話を進めます。

command
c:\> vagrant package centos65base --output centos65base.box
==> redm: Clearing any previously set forwarded ports...
==> redm: Exporting VM...

ちなみに複数のvmを立ち上げてる場合は、This command requires a specific VM name to target in a multi-VM environment. というエラーになるのでbaseを指定する。

command
c:\> vagrant package basecentos66 centos66base --output centos66base.box
==> redm: Clearing any previously set forwarded ports...
==> redm: Exporting VM...

box の登録

command
c:\> vagrant box add centos65base c:\<boxの場所>\centos65base.box
c:\> vagrant box list
cantos65base (virtualbox, 0)

box を使用する

ここからは通常とおり。

command
c:\> vagrant init centos65base

または config.vm.box = "centos65base"と指定するだけ。

ホスト名に "_" を使用すると・・・

vm のホスト名を「centos6_5_base」と指定したら vagrant up で怒られました。 :tired_face: 注意されますねw

error
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The hostname set for the VM should only contain letters, numbers,
hyphens or dots. It cannot start with a hyphen or dot.

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
74