Class: Tenderloin::Actions::VM::MoveHardDrive

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

Instance Attribute Summary

Attributes inherited from Base

#run_args, #runner

Instance Method Summary collapse

Methods inherited from Base

#cleanup, #initialize, #prepare, #rescue

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

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

Instance Method Details

#clone_and_attachObject



29
30
31
32
33
34
35
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 29

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, Tenderloin.config.vm.disk_image_format, true)

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

#destroy_drive_afterObject



37
38
39
40
41
42
43
44
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 37

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
13
14
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 5

def execute!
  unless @runner.powered_off?
    error_and_exit(<<-error)
The virtual machine must be powered off to move its disk.
error
    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



21
22
23
24
25
26
27
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 21

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



16
17
18
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 16

def hard_drive
  @hard_drive ||= find_hard_drive
end

#new_image_pathObject

Returns the path to the new location for the hard drive



47
48
49
# File 'lib/tenderloin/actions/vm/move_hard_drive.rb', line 47

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