Class: VagrantPlugins::QEMU::Action::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-qemu/action/export.rb

Overview

Produces the box's disk artifacts and metadata.json in export.temp_dir: flattens each box-disk overlay into a standalone qcow2 and describes them in a libvirt-format metadata.json. Only the VM's box disks are packaged; additional disks, DVDs and cloud-init seed ISOs are left out.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Export

Returns a new instance of Export.



14
15
16
17
# File 'lib/vagrant-qemu/action/export.rb', line 14

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_qemu::action::export")
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-qemu/action/export.rb', line 19

def call(env)
  @env = env

  if env[:machine].state.id != :stopped
    raise Vagrant::Errors::VMPowerOffToPackage
  end

  export
  @app.call(env)
end

#exportObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-qemu/action/export.rb', line 30

def export
  driver   = @env[:machine].provider.driver
  temp_dir = Pathname.new(@env["export.temp_dir"])
  disks    = driver.box_disk_paths

  if disks.empty?
    raise Errors::BoxInvalid, name: @env[:machine].name, err: "No box disk found to package"
  end

  multi     = disks.size > 1
  disk_meta = []
  disks.each_with_index do |src, i|
    name = multi ? "box_#{i + 1}.img" : "box.img"
    @env[:ui].info(I18n.t("vagrant_qemu.packaging_disk", name: name))
    driver.convert_box_disk(src, temp_dir.join(name))
    disk_meta << { "path" => name }
  end

  (temp_dir, disk_meta, multi)
end

#virtual_size_gb(img) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/vagrant-qemu/action/export.rb', line 63

def virtual_size_gb(img)
  stdout, stderr, status = Open3.capture3("qemu-img", "info", "--output=json", img.to_s)
  if !status.success?
    raise Errors::ExecuteError, command: "qemu-img info", stderr: stderr, stdout: stdout
  end
  bytes = JSON.parse(stdout)["virtual-size"]
  (bytes.to_f / (1024**3)).ceil
end

#write_metadata(temp_dir, disk_meta, multi) ⇒ Object

libvirt box metadata: provider must be "libvirt" (the provider's box_format) or the packaged box won't match on box add.



53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-qemu/action/export.rb', line 53

def (temp_dir, disk_meta, multi)
   = {
    "provider"     => "libvirt",
    "format"       => "qcow2",
    "virtual_size" => virtual_size_gb(temp_dir.join(disk_meta.first["path"])),
  }
  ["disks"] = disk_meta if multi
  File.write(temp_dir.join("metadata.json"), JSON.pretty_generate())
end