LoginSignup
24

More than 5 years have passed since last update.

VagrantでVM起動時に、追加ディスクを作成してアタッチ - VirtualBox Provider

Last updated at Posted at 2014-06-10

VagrantでVMを作ってプロビジョニングをテストする際に、起動ディスク以外おnディスクがアタッチされている状況を再現したい事もあります。
対象がPublic/PrivateなIaaSならEC2(EBS)だったり、CloudStack, OpenStackでも結構見られる状況かなと。

確認したのはVagrant 1.6.2

ざっとこんな感じで記述します。

Vagrantfile
# -- snip --
    config.vm.define machine do |config|

      config.vm.provider :virtualbox do |vb|
        at_disk = 'tmp/name_of_file.vdi'
        unless File.exists?(at_disk)
          vb.customize ['createhd', '--filename', at_disk, '--size', 5 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', at_disk]
        end
      end

    end
# -- snip --

この記述で、次のような挙動になります。

  • vagrant up時に仮想ディスクファイルを(作成して)アタッチ
  • reset/reload時は内容残したままリブート
  • vagrant destroy時に一緒に削除

デフォルトではdestroy時、一緒に消えることに注意です。

ついでに、customizeについてはVBoxManageに渡すオプションを記述すればよいので、他のカスタマイズはVBoxManage本体のヘルプが参考になるはず。

例えばstorageattach

VBoxManage storageattach    <uuid|vmname>
                            --storagectl <name>
                            [--port <number>]
                            [--device <number>]
                            [--type dvddrive|hdd|fdd]
                            [--medium none|emptydrive|additions|
                                      <uuid|filename>|host:<drive>|iscsi]
                            [--mtype normal|writethrough|immutable|shareable|
                                     readonly|multiattach]
                            [--comment <text>]
                            [--setuuid <uuid>]
                            [--setparentuuid <uuid>]
                            [--passthrough on|off]
                            [--tempeject on|off]
                            [--nonrotational on|off]
                            [--discard on|off]
                            [--bandwidthgroup <name>]
                            [--forceunmount]
                            [--server <name>|<ip>]
                            [--target <target>]
                            [--tport <port>]
                            [--lun <lun>]
                            [--encodedlun <lun>]
                            [--username <username>]
                            [--password <password>]
                            [--initiator <initiator>]
                            [--intnet]

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
24