mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-01-07 14:33:08 -06:00
Create storage pools and volumes with correct permissions
This commit is contained in:
parent
6ce8420a3b
commit
b0eddf6770
@ -27,7 +27,7 @@ module VagrantPlugins
|
||||
if !env[:machine].config.vm.box
|
||||
b2.use CreateDomain
|
||||
b2.use CreateNetworks
|
||||
b2.use CreateNetworkInterfaces
|
||||
#b2.use CreateNetworkInterfaces
|
||||
b2.use SetBootOrder
|
||||
b2.use StartDomain
|
||||
else
|
||||
@ -44,7 +44,7 @@ module VagrantPlugins
|
||||
b2.use PrepareNFSSettings
|
||||
b2.use ShareFolders
|
||||
b2.use CreateNetworks
|
||||
b2.use CreateNetworkInterfaces
|
||||
#b2.use CreateNetworkInterfaces
|
||||
b2.use SetBootOrder
|
||||
|
||||
b2.use StartDomain
|
||||
|
@ -8,6 +8,7 @@ module VagrantPlugins
|
||||
# image as new domain volume.
|
||||
class CreateDomainVolume
|
||||
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
|
||||
include VagrantPlugins::ProviderLibvirt::Util::StorageUtil
|
||||
|
||||
def initialize(app, _env)
|
||||
@logger = Log4r::Logger.new('vagrant_libvirt::action::create_domain_volume')
|
||||
@ -48,8 +49,8 @@ module VagrantPlugins
|
||||
xml.target do
|
||||
xml.format(type: 'qcow2')
|
||||
xml.permissions do
|
||||
xml.owner 0
|
||||
xml.group 0
|
||||
xml.owner storage_uid(env)
|
||||
xml.group storage_gid(env)
|
||||
xml.mode '0600'
|
||||
xml.label 'virt_image_t'
|
||||
end
|
||||
@ -58,8 +59,8 @@ module VagrantPlugins
|
||||
xml.path(@backing_file)
|
||||
xml.format(type: 'qcow2')
|
||||
xml.permissions do
|
||||
xml.owner 0
|
||||
xml.group 0
|
||||
xml.owner storage_uid(env)
|
||||
xml.group storage_gid(env)
|
||||
xml.mode '0600'
|
||||
xml.label 'virt_image_t'
|
||||
end
|
||||
|
@ -27,6 +27,11 @@ module VagrantPlugins
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if env[:machine].provider_config.qemu_use_session
|
||||
@app.call(env)
|
||||
return
|
||||
end
|
||||
|
||||
# only one vm at a time should try to set up networks
|
||||
# otherwise they'll have inconsitent views of current state
|
||||
# and conduct redundant operations that cause errors
|
||||
|
@ -13,6 +13,11 @@ module VagrantPlugins
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if env[:machine].provider_config.qemu_use_session
|
||||
@app.call(env)
|
||||
return
|
||||
end
|
||||
|
||||
# If there were some networks created for this machine, in machines
|
||||
# data directory, created_networks file holds UUIDs of each network.
|
||||
created_networks_file = env[:machine].data_dir + 'created_networks'
|
||||
|
@ -4,6 +4,10 @@ module VagrantPlugins
|
||||
module ProviderLibvirt
|
||||
module Action
|
||||
class HandleBoxImage
|
||||
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
|
||||
include VagrantPlugins::ProviderLibvirt::Util::StorageUtil
|
||||
|
||||
|
||||
@@lock = Mutex.new
|
||||
|
||||
def initialize(app, _env)
|
||||
@ -31,11 +35,12 @@ module VagrantPlugins
|
||||
config = env[:machine].provider_config
|
||||
box_image_file = env[:machine].box.directory.join('box.img').to_s
|
||||
env[:box_volume_name] = env[:machine].box.name.to_s.dup.gsub('/', '-VAGRANTSLASH-')
|
||||
env[:box_volume_name] << "_vagrant_box_image_#{begin
|
||||
env[:machine].box.version.to_s
|
||||
rescue
|
||||
''
|
||||
end}.img"
|
||||
env[:box_volume_name] << "_vagrant_box_image_#{
|
||||
begin
|
||||
env[:machine].box.version.to_s
|
||||
rescue
|
||||
''
|
||||
end}.img"
|
||||
|
||||
# Override box_virtual_size
|
||||
if config.machine_virtual_size
|
||||
@ -44,7 +49,7 @@ module VagrantPlugins
|
||||
# is not supported and will be ignored
|
||||
env[:ui].warn I18n.t(
|
||||
'vagrant_libvirt.warnings.ignoring_virtual_size_too_small',
|
||||
requested: config.machine_virtual_size, minimum: box_virtual_size
|
||||
requested: config.machine_virtual_size, minimum: box_virtual_size
|
||||
)
|
||||
else
|
||||
env[:ui].info I18n.t('vagrant_libvirt.manual_resize_required')
|
||||
@ -75,17 +80,41 @@ module VagrantPlugins
|
||||
message = "Creating volume #{env[:box_volume_name]}"
|
||||
message << " in storage pool #{config.storage_pool_name}."
|
||||
@logger.info(message)
|
||||
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,
|
||||
pool_name: config.storage_pool_name
|
||||
)
|
||||
rescue Fog::Errors::Error => e
|
||||
raise Errors::FogCreateVolumeError,
|
||||
error_message: e.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,
|
||||
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
|
||||
@ -103,7 +132,11 @@ module VagrantPlugins
|
||||
# storage pool.
|
||||
if env[:interrupted] || !ret
|
||||
begin
|
||||
fog_volume.destroy
|
||||
if config.qemu_use_session
|
||||
libvirt_volume.delete
|
||||
else
|
||||
fog_volume.destroy
|
||||
end
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
@ -113,6 +146,19 @@ 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
|
||||
|
@ -5,6 +5,8 @@ module VagrantPlugins
|
||||
module Action
|
||||
class HandleStoragePool
|
||||
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
|
||||
include VagrantPlugins::ProviderLibvirt::Util::StorageUtil
|
||||
|
||||
|
||||
@@lock = Mutex.new
|
||||
|
||||
@ -37,6 +39,9 @@ module VagrantPlugins
|
||||
# Fog libvirt currently doesn't support creating pools. Use
|
||||
# ruby-libvirt client directly.
|
||||
begin
|
||||
@storage_pool_path = storage_pool_path(env)
|
||||
@storage_pool_uid = storage_uid(env)
|
||||
@storage_pool_gid = storage_gid(env)
|
||||
libvirt_pool = env[:machine].provider.driver.connection.client.define_storage_pool_xml(
|
||||
to_xml('default_storage_pool')
|
||||
)
|
||||
|
@ -40,6 +40,7 @@ module VagrantPlugins
|
||||
# Libvirt storage pool name, where box image and instance snapshots will
|
||||
# be stored.
|
||||
attr_accessor :storage_pool_name
|
||||
attr_accessor :storage_pool_path
|
||||
|
||||
# Turn on to prevent hostname conflicts
|
||||
attr_accessor :random_hostname
|
||||
@ -148,6 +149,9 @@ module VagrantPlugins
|
||||
# Additional qemuargs arguments
|
||||
attr_accessor :qemu_args
|
||||
|
||||
# Use qemu session instead of system
|
||||
attr_accessor :qemu_use_session
|
||||
|
||||
def initialize
|
||||
@uri = UNSET_VALUE
|
||||
@driver = UNSET_VALUE
|
||||
@ -251,6 +255,7 @@ module VagrantPlugins
|
||||
@mgmt_attach = UNSET_VALUE
|
||||
|
||||
@qemu_args = []
|
||||
@qemu_use_session = UNSET_VALUE
|
||||
end
|
||||
|
||||
def boot(device)
|
||||
@ -542,7 +547,9 @@ module VagrantPlugins
|
||||
# Setup connection uri.
|
||||
uri = @driver.dup
|
||||
virt_path = case uri
|
||||
when 'qemu', 'openvz', 'uml', 'phyp', 'parallels', 'kvm'
|
||||
when 'qemu', 'kvm'
|
||||
@qemu_use_session ? '/session' : '/system'
|
||||
when 'openvz', 'uml', 'phyp', 'parallels'
|
||||
'/system'
|
||||
when '@en', 'esx'
|
||||
'/'
|
||||
@ -592,6 +599,7 @@ module VagrantPlugins
|
||||
@password = nil if @password == UNSET_VALUE
|
||||
@id_ssh_key_file = 'id_rsa' if @id_ssh_key_file == UNSET_VALUE
|
||||
@storage_pool_name = 'default' if @storage_pool_name == UNSET_VALUE
|
||||
@storage_pool_path = nil if @storage_pool_path == UNSET_VALUE
|
||||
@random_hostname = false if @random_hostname == UNSET_VALUE
|
||||
@management_network_name = 'vagrant-libvirt' if @management_network_name == UNSET_VALUE
|
||||
@management_network_address = '192.168.121.0/24' if @management_network_address == UNSET_VALUE
|
||||
@ -706,6 +714,7 @@ module VagrantPlugins
|
||||
@mgmt_attach = true if @mgmt_attach == UNSET_VALUE
|
||||
|
||||
@qemu_args = [] if @qemu_args == UNSET_VALUE
|
||||
@qemu_use_session = false if @qemu_use_session == UNSET_VALUE
|
||||
end
|
||||
|
||||
def validate(machine)
|
||||
|
@ -29,6 +29,10 @@ 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
|
||||
|
@ -3,11 +3,11 @@
|
||||
<source>
|
||||
</source>
|
||||
<target>
|
||||
<path>/var/lib/libvirt/images</path>
|
||||
<path><%= @storage_pool_path %></path>
|
||||
<permissions>
|
||||
<mode>0755</mode>
|
||||
<owner>-1</owner>
|
||||
<group>-1</group>
|
||||
<owner><%= @storage_pool_uid %></owner>
|
||||
<group><%= @storage_pool_gid %></group>
|
||||
</permissions>
|
||||
</target>
|
||||
</pool>
|
||||
|
14
lib/vagrant-libvirt/templates/default_storage_volume.xml.erb
Normal file
14
lib/vagrant-libvirt/templates/default_storage_volume.xml.erb
Normal file
@ -0,0 +1,14 @@
|
||||
<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>
|
@ -5,6 +5,7 @@ module VagrantPlugins
|
||||
autoload :Collection, 'vagrant-libvirt/util/collection'
|
||||
autoload :Timer, 'vagrant-libvirt/util/timer'
|
||||
autoload :NetworkUtil, 'vagrant-libvirt/util/network_util'
|
||||
autoload :StorageUtil, 'vagrant-libvirt/util/storage_util'
|
||||
autoload :ErrorCodes, 'vagrant-libvirt/util/error_codes'
|
||||
end
|
||||
end
|
||||
|
27
lib/vagrant-libvirt/util/storage_util.rb
Normal file
27
lib/vagrant-libvirt/util/storage_util.rb
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
module VagrantPlugins
|
||||
module ProviderLibvirt
|
||||
module Util
|
||||
module StorageUtil
|
||||
def storage_uid(env)
|
||||
env[:machine].provider_config.qemu_use_session ? Process.uid : 0
|
||||
end
|
||||
|
||||
def storage_gid(env)
|
||||
env[:machine].provider_config.qemu_use_session ? Process.gid : 0
|
||||
end
|
||||
|
||||
def storage_pool_path(env)
|
||||
if env[:machine].provider_config.storage_pool_path
|
||||
env[:machine].provider_config.storage_pool_path
|
||||
elsif env[:machine].provider_config.qemu_use_session
|
||||
File.expand_path('~/.local/share/libvirt/images')
|
||||
else
|
||||
'/var/lib/libvirt/images'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -107,6 +107,8 @@ 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: |-
|
||||
|
Loading…
Reference in New Issue
Block a user