Module: VagrantPlugins::QEMU::Cap::Disk

Defined in:
lib/vagrant-qemu/cap/disk.rb

Constant Summary collapse

DEFAULT_DISK_EXT_LIST =
["qcow2", "iso"].map(&:freeze).freeze
DEFAULT_DISK_EXT =
"qcow2".freeze
@@logger =
Log4r::Logger.new("vagrant_qemu::cap::disk")

Class Method Summary collapse

Class Method Details

.cleanup_disks(machine, defined_disks, disk_meta) ⇒ nil

Parameters:

  • machine (Vagrant::Machine)
  • defined_disks (VagrantPlugins::Kernel_V2::VagrantConfigDisk)
  • disk_meta (Hash)
    • A hash of all the previously defined disks from the last configure_disk action

Returns:

  • (nil)


73
74
75
# File 'lib/vagrant-qemu/cap/disk.rb', line 73

def self.cleanup_disks(machine, defined_disks, disk_meta)
  return if disk_meta.values.flatten.empty?
end

.configure_disks(machine, defined_disks) ⇒ Hash

Returns configured_disks - A hash of all the current configured disks.

Parameters:

  • machine (Vagrant::Machine)
  • defined_disks (VagrantPlugins::Kernel_V2::VagrantConfigDisk)

Returns:

  • (Hash)

    configured_disks - A hash of all the current configured disks



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
63
64
65
66
# File 'lib/vagrant-qemu/cap/disk.rb', line 34

def self.configure_disks(machine, defined_disks)
  # Vagrant re-invokes this on every action_start (including a
  # same-process reload), always passing the complete current disk
  # list -- reset before the empty check so a disk removed from the
  # Vagrantfile doesn't leave a stale entry attached either.
  machine.provider.driver.reset_attached_drives!
  return {} if defined_disks.empty?

  configured_disks = {disk: [], floppy: [], dvd: []}
  defined_disks.each do |disk|
    @@logger.info("Disk: #{disk.to_yaml}")
    case disk.type
    when :disk
      disk_data = setup_disk(machine, disk)
      if !disk_data.empty?
        configured_disks[:disk] << disk_data
        machine.provider.driver.attach_disk(disk_data)
      end
    when :floppy
      machine.ui.info(I18n.t("vagrant_qemu.errors.floppy_unsupported"))
    when :dvd
      disk_data = setup_dvd(machine, disk)
      if !disk_data.empty?
        configured_disks[:dvd] << disk_data
        machine.provider.driver.attach_dvd(disk_data)
      end
    else
      @@logger.info("unsupported disk type: #{disk.type}")
    end
  end

  configured_disks
end

.default_disk_exts(machine) ⇒ Array

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (Array)


20
21
22
# File 'lib/vagrant-qemu/cap/disk.rb', line 20

def self.default_disk_exts(machine)
  DEFAULT_DISK_EXT_LIST
end

.set_default_disk_ext(machine) ⇒ String

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • (String)


14
15
16
# File 'lib/vagrant-qemu/cap/disk.rb', line 14

def self.set_default_disk_ext(machine)
  DEFAULT_DISK_EXT
end

.setup_disk(machine, disk) ⇒ Hash

Sets up all disk configs of type :disk

Parameters:

  • machine (Vagrant::Machine)
    • the current machine
  • disk (Config::Disk)
    • the current disk to configure

Returns:

  • (Hash)
    • disk_metadata


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-qemu/cap/disk.rb', line 84

def self.setup_disk(machine, disk)
  disk_dir = machine.provider.driver.disk_dir
  disk_path = disk_dir.join("#{disk.name}.#{disk.disk_ext}")

  disk_provider_config = disk.provider_config[:qemu] if disk.provider_config

  # configure_disks re-runs on every action_start (including a
  # same-process reload); qemu-img create truncates an existing file
  # unconditionally, so skip it once the disk already exists instead
  # of silently wiping it. Resizing an existing disk isn't handled.
  if !disk_path.file?
    args = ["create", "-f", "qcow2", disk_path.to_s, "#{disk.size}"]
    machine.provider.driver.execute("qemu-img", *args)
  end

  {UUID: disk.id, Name: disk.name, Path: disk_path.to_s, primary: !!disk.primary}
end

.setup_dvd(machine, disk) ⇒ Hash

Sets up all disk configs of type :dvd

Parameters:

  • machine (Vagrant::Machine)
    • the current machine
  • disk (Config::Disk)
    • the current disk to configure

Returns:

  • (Hash)
    • disk_metadata


107
108
109
# File 'lib/vagrant-qemu/cap/disk.rb', line 107

def self.setup_dvd(machine, disk)
  {UUID: disk.id, Name: disk.name, Path: disk.file, primary: !!disk.primary}
end

.validate_disk_ext(machine, disk_ext) ⇒ Bool

Parameters:

  • machine (Vagrant::Machine)
  • disk_ext (String)

Returns:

  • (Bool)


27
28
29
# File 'lib/vagrant-qemu/cap/disk.rb', line 27

def self.validate_disk_ext(machine, disk_ext)
  DEFAULT_DISK_EXT_LIST.include?(disk_ext)
end