Use fog-libvirt facilities to create volumes for QEMU session.

This is supported since fog-libvirt 0.6.0 via https://github.com/fog/fog-libvirt/pull/60
This commit is contained in:
Vít Ondruch
2019-07-17 17:43:35 +02:00
parent c3af181c83
commit 608037c15c
5 changed files with 15 additions and 74 deletions

View File

@@ -4,7 +4,6 @@ module VagrantPlugins
module ProviderLibvirt
module Action
class HandleBoxImage
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
include VagrantPlugins::ProviderLibvirt::Util::StorageUtil
@@ -81,41 +80,20 @@ module VagrantPlugins
message << " in storage pool #{config.storage_pool_name}."
@logger.info(message)
if config.qemu_use_session
begin
@name = env[:box_volume_name]
@allocation = "#{box_image_size / 1024 / 1024}M"
@capacity = "#{box_virtual_size}G"
@format_type = box_format ? box_format : 'raw'
@storage_volume_uid = storage_uid env
@storage_volume_gid = storage_gid env
libvirt_client = env[:machine].provider.driver.connection.client
libvirt_pool = libvirt_client.lookup_storage_pool_by_name(
config.storage_pool_name
)
libvirt_volume = libvirt_pool.create_volume_xml(
to_xml('default_storage_volume')
)
rescue => e
raise Errors::CreatingVolumeError,
error_message: e.message
end
else
begin
fog_volume = env[:machine].provider.driver.connection.volumes.create(
name: env[:box_volume_name],
allocation: "#{box_image_size / 1024 / 1024}M",
capacity: "#{box_virtual_size}G",
format_type: box_format,
owner: storage_uid env,
group: storage_gid env,
pool_name: config.storage_pool_name
)
rescue Fog::Errors::Error => e
raise Errors::FogCreateVolumeError,
error_message: e.message
end
end
# Upload box image to storage pool
ret = upload_image(box_image_file, config.storage_pool_name,
@@ -132,11 +110,7 @@ module VagrantPlugins
# storage pool.
if env[:interrupted] || !ret
begin
if config.qemu_use_session
libvirt_volume.delete
else
fog_volume.destroy
end
rescue
nil
end
@@ -146,19 +120,6 @@ module VagrantPlugins
@app.call(env)
end
def split_size_unit(text)
if text.kind_of? Integer
# if text is an integer, match will fail
size = text
unit = 'G'
else
matcher = text.match(/(\d+)(.+)/)
size = matcher[1]
unit = matcher[2]
end
[size, unit]
end
protected
# Fog Libvirt currently doesn't support uploading images to storage

View File

@@ -29,10 +29,6 @@ module VagrantPlugins
error_key(:creating_storage_pool_error)
end
class CreatingVolumeError < VagrantLibvirtError
error_key(:creating_volume_error)
end
class ImageUploadError < VagrantLibvirtError
error_key(:image_upload_error)
end

View File

@@ -1,14 +0,0 @@
<volume>
<name><%= @name %></name>
<allocation unit="<%= split_size_unit(@allocation)[1] %>"><%= split_size_unit(@allocation)[0] %></allocation>
<capacity unit="<%= split_size_unit(@capacity)[1] %>"><%= split_size_unit(@capacity)[0] %></capacity>
<target>
<format type="<%= @format_type %>"/>
<permissions>
<owner><%= @storage_volume_uid %></owner>
<group><%= @storage_volume_gid %></group>
<mode>0744</mode>
<label>virt_image_t</label>
</permissions>
</target>
</volume>

View File

@@ -109,8 +109,6 @@ en:
`vagrant up` command again.
creating_storage_pool_error: |-
There was error while creating Libvirt storage pool: %{error_message}
creating_volume_error: |-
There was error while creating Libvirt volume: %{error_message}
image_upload_error: |-
Error while uploading image to storage pool: %{error_message}
no_domain_error: |-

View File

@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-expectations", "~> 3.5.0"
s.add_development_dependency "rspec-mocks", "~> 3.5.0"
s.add_runtime_dependency 'fog-libvirt', '>= 0.3.0'
s.add_runtime_dependency 'fog-libvirt', '>= 0.6.0'
s.add_runtime_dependency 'fog-core', '~> 2.1'
# Make sure to allow use of the same version as Vagrant by being less specific