Class: Macinbox::Actions::CreateVMDKFromImage

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/actions/create_vmdk_from_image.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ CreateVMDKFromImage

Returns a new instance of CreateVMDKFromImage.

Raises:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/macinbox/actions/create_vmdk_from_image.rb', line 14

def initialize(opts)
  @input_image       = opts[:image_path]  or raise ArgumentError.new(":image_path not specified")
  @output_path       = opts[:vmdk_path]   or raise ArgumentError.new(":vmdk_path not specified")
  @vmware_fusion_app = opts[:vmware_path] or raise ArgumentError.new(":vmware_path not specified")

  @collector         = opts[:collector]   or raise ArgumentError.new(":collector not specified")
  @debug             = opts[:debug]

  raise Macinbox::Error.new("input image not found")   unless File.exist? @input_image
  raise Macinbox::Error.new("VMware Fusion not found") unless File.exist? @vmware_fusion_app
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/macinbox/actions/create_vmdk_from_image.rb', line 26

def run
  @temp_dir = Task.backtick %W[ /usr/bin/mktemp -d -t create_vmdk_from_image ]
  @collector.add_temp_dir @temp_dir

  Logger.info "Attaching the image..." do

    @collector.on_cleanup do
      %x( /usr/sbin/diskutil eject #{@device.shellescape} > /dev/null 2>&1 ) if @device
    end

    @device = %x(
      /usr/bin/hdiutil attach #{@input_image.shellescape} -nomount |
      /usr/bin/grep _partition_scheme |
      /usr/bin/cut -f1 |
      /usr/bin/tr -d [:space:]
    )

    raise Macinbox::Error.new("failed to attach the image") unless File.exist? @device
  end

  Logger.info "Converting the image to VMDK format..." do
    rawdiskCreator = "#{@vmware_fusion_app}/Contents/Library/vmware-rawdiskCreator"
    vdiskmanager = "#{@vmware_fusion_app}/Contents/Library/vmware-vdiskmanager"
    Dir.chdir(@temp_dir) do
      Task.run %W[ #{rawdiskCreator} create #{@device} fullDevice rawdisk lsilogic ]
      Task.run %W[ #{vdiskmanager} -t 0 -r rawdisk.vmdk macinbox.vmdk ]
    end
    Task.run %W[ /usr/sbin/diskutil eject #{@device.shellescape} ]
    @device = nil
  end

  Logger.info "Moving the VMDK to the destination..." do
    FileUtils.chown ENV["SUDO_USER"], nil, "#{@temp_dir}/macinbox.vmdk"
    FileUtils.mv "#{@temp_dir}/macinbox.vmdk", @output_path
  end

end