Sha256: 077d28e98a172512315479d2d958c9bfb2d42ba394b2b438bf63cd1e97e4a3b6

Contents?: true

Size: 1.4 KB

Versions: 11

Compression:

Stored size: 1.4 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, Vagrant.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(Vagrant.config.vm.hd_location, hard_drive.image.filename)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
vagrantup-0.3.4 lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.3.3 lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.3.2 lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.3.1 lib/vagrant/actions/vm/move_hard_drive.rb
vagrantup-0.3.0 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.3.4 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.3.3 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.3.2 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.3.1 lib/vagrant/actions/vm/move_hard_drive.rb
vagrant-0.3.0 lib/vagrant/actions/vm/move_hard_drive.rb
bmabey-vagrant-0.2.0 lib/vagrant/actions/vm/move_hard_drive.rb