Class: Vagrant::Actions::VM::MoveHardDrive

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/actions/vm/move_hard_drive.rb

Instance Attribute Summary

Attributes inherited from Base

#runner

Instance Method Summary collapse

Methods inherited from Base

#cleanup, #follows, #initialize, #precedes, #prepare, #rescue

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

This class inherits a constructor from Vagrant::Actions::Base

Instance Method Details

#clone_and_attachObject



27
28
29
30
31
32
33
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 27

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

#destroy_drive_afterObject



35
36
37
38
39
40
41
42
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 35

def destroy_drive_after
  old_image = hard_drive.image

  yield

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

#execute!Object



5
6
7
8
9
10
11
12
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 5

def execute!
  unless @runner.powered_off?
    error_and_exit(:vm_power_off_to_move_hd)
    return
  end

  destroy_drive_after { clone_and_attach }
end

#find_hard_driveObject

TODO won’t work if the first disk is not the boot disk or even if there are multiple disks



19
20
21
22
23
24
25
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 19

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

#hard_driveObject



14
15
16
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 14

def hard_drive
  @hard_drive ||= find_hard_drive
end

#new_image_pathObject

Returns the path to the new location for the hard drive



45
46
47
# File 'lib/vagrant/actions/vm/move_hard_drive.rb', line 45

def new_image_path
  File.join(Vagrant.config.vm.hd_location, hard_drive.image.filename)
end