Sha256: b407ee2f1109193304ceb8fa065e06cd7e96db2299d9b073a60761d93898d0c8

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module Vagrant
  module Actions
    module VM
      class MoveHardDrive < Base
        def execute!
          unless @runner.powered_off?
            error_and_exit(:vm_power_off_to_move_hd)
            return
          end

          destroy_drive_after { clone_and_attach }
        end

        def hard_drive
          @hard_drive ||= find_hard_drive
        end

        # TODO won't work if the first disk is not the boot disk or even if there are multiple disks
        def find_hard_drive
          @runner.vm.storage_controllers.each do |sc|
            sc.devices.each do |d|
              return d if d.image.is_a?(VirtualBox::HardDrive)
            end
          end
        end

        def clone_and_attach
          logger.info "Cloning current VM Disk to new location (#{new_image_path})..."
          hard_drive.image = hard_drive.image.clone(new_image_path, @runner.env.config.vm.disk_image_format, true)

          logger.info "Attaching new disk to VM ..."
          @runner.vm.save
        end

        def destroy_drive_after
          old_image = hard_drive.image

          yield

          logger.info "Destroying old VM Disk (#{old_image.filename})..."
          old_image.destroy(true)
        end

        # Returns the path to the new location for the hard drive
        def new_image_path
          File.join(@runner.env.config.vm.hd_location, hard_drive.image.filename)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
vagrantup-0.4.3.dev lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.4.1 lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.4.0 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.4.2 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.4.1 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.4.0 lib/vagrant/actions/vm/move_hard_drive.rb