Class: Macinbox::Actions::CreateVDIFromImage

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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ CreateVDIFromImage

Returns a new instance of CreateVDIFromImage.

Raises:



16
17
18
19
20
21
22
23
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 16

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

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

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

Instance Method Details

#attach_imageObject



46
47
48
49
50
51
52
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 46

def attach_image
  Logger.info "Attaching the image..." do
    @disk = VirtualDisk.new(@image)
    @collector.on_cleanup { @disk.detach! }
    @disk.attach
  end
end

#convert_imageObject



80
81
82
83
84
85
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 80

def convert_image
  Logger.info "Converting the image to VDI format..." do
    task_opts = $verbose ? {} : { :out => File::NULL }
    Task.run %W[ VBoxManage convertfromraw #{@disk.device} #{@temp_dir}/macinbox.vdi --format VDI ] + [task_opts]
  end
end

#copy_input_imageObject



39
40
41
42
43
44
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 39

def copy_input_image
  Logger.info "Copying the image..." do
    @image = "#{@temp_dir}/macinbox.sparseimage"
    Macinbox::copyfiles(from: @input_image, to: @image)
  end
end

#create_temp_dirObject



34
35
36
37
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 34

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

#runObject



25
26
27
28
29
30
31
32
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 25

def run
  create_temp_dir
  copy_input_image
  attach_image
  setup_efi_partition
  convert_image
  save_image
end

#save_imageObject



87
88
89
90
91
92
93
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 87

def save_image
  Logger.info "Moving the VDI to the destination..." do
    @disk.eject
    FileUtils.chown ENV["SUDO_USER"], nil, "#{@temp_dir}/macinbox.vdi"
    FileUtils.mv "#{@temp_dir}/macinbox.vdi", @output_path
  end
end

#setup_efi_partitionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/macinbox/actions/create_vdi_from_image.rb', line 54

def setup_efi_partition
  Logger.info "Setting up EFI partition..." do
    efi_mountpoint = "#{@temp_dir}/efi_mountpoint"
    FileUtils.mkdir efi_mountpoint
    @disk.mount_efi(at: efi_mountpoint)
    Task.run %W[ /bin/mkdir -p #{efi_mountpoint}/EFI/drivers ]
    Task.run %W[ /bin/cp /usr/standalone/i386/apfs.efi #{efi_mountpoint}/EFI/drivers/ ]
    File.write "#{efi_mountpoint}/startup.nsh", "      @echo -off\n      echo \"Loading APFS driver...\"\n      load \"fs0:\\EFI\\drivers\\apfs.efi\"\n      echo \"Refreshing media mappings...\"\n      map -r\n      echo \"Searching for bootloader...\"\n      for %d in fs1 fs2 fs3 fs4 fs5 fs6\n        if exist \"%d:\\System\\Library\\CoreServices\\boot.efi\" then\n          echo \"Found %d:\\System\\Library\\CoreServices\\boot.efi, launching...\"\n          \"%d:\\System\\Library\\CoreServices\\boot.efi\"\n        endif\n      endfor\n      echo \"Failed.\"\n    EOF\n    @disk.unmount_efi\n  end\nend\n"