LoginSignup
16
17

More than 5 years have passed since last update.

vagrant packageしないで、vagrantとVirtualBoxをアップデートしてしまってVMを起動できなくなったのを救う方法

Last updated at Posted at 2014-03-16

vagrant packageを使うと、VMを今の状態でとっておけるのですが、それをしないでvagrantをバージョンアップしたら、今まで起動していたVMが起動しなくなってしまいました。

幸い、 $(HOME)/.vagrant.d/boxes/に今までのvagrantで作成したファイルは残っていたのと、VirtualBoxで直接起動すると正常に起動できることから、VirtualBoxからexportしてbox化すれば、vagrantで再び起動することが可能でしたので、備忘録として残します。

手順

1. VirtualBoxで今まで起動していたVMが起動するかどうか確認する

VirtualBox GUIを起動するか、VBoxManageコマンドを使って起動するか確認します。

2. VMのエクスポート

VirtualBox GUIか、VBoxManageコマンドを使ってovaファイルでexportします。

VBoxManage export <VM name> -o export.ova

3. metadata.jsonのコピー

アップデートしたvagrantでサンプルのVMを作ってみて、$(HOME)/.vagrant.d/boxes/(sample VM name)にできたVMのファイルをコピーします。

metadata.json
{"provider": "virtualbox"}

4. Vagrantfileのコピーまたは作成

今まで動作していたVMが残っていれば、$(HOME)/.vagrant.d/boxes/(VM name)にVagrantfileが残っているのでそれをそのまま使います。残っていなければ3.で作成したサンプルのVMから拝借してconfig.vm.base_macを書き換えます。

Vagrant::Config.run do |config|
  config.vm.base_mac = "0800111DA111"   # Mac addressを書き換え
end

include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

MAC-addressは、VirtualBox GUIでVMの設定をみるか、VBoxManageコマンドで調べます。

VBoxManage showvminfo <VM name> --machinereadable

5. 拡張子をboxにして固める

  1. - 4.のファイルを拡張子をboxにして固めます。
tar zcf export.box export.ova metadata.json Vagrantfile

6. Boxを追加

vagrant box add <New vagrant vm name> export.box

7. 起動するためのVagrantfile作成

Vagrantfileのformatが若干変更されていたので、vagrant initでVagrantfileを作成し、元のファイルから設定をマージするのが楽だったので、それで作成しました。

vagrant init
edit Vagrantfile
vagrant up

環境

旧vagrant: 1.3.xあたり?
新vagrant: 1.5.1

参考

16
17
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
16
17