Set undefine flags to keep NVRAM (#1329)

Handle enabling and disabling of NVRAM during start domain.

This patch contains latent support for upstream PR fog-libvirt#102 to support destroy
with NVRAM enabled once that is merged.

Fixes #1027.
This commit is contained in:
byonoy-dne
2022-05-06 13:43:42 +02:00
committed by GitHub
parent f734a89681
commit c4fcfee510
10 changed files with 398 additions and 11 deletions

View File

@@ -43,11 +43,14 @@ module VagrantPlugins
domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
undefine_flags = 0
undefine_flags |= ProviderLibvirt::Util::DomainFlags::VIR_DOMAIN_UNDEFINE_KEEP_NVRAM if env[:machine].provider_config.nvram
if env[:machine].provider_config.disks.empty? &&
env[:machine].provider_config.cdroms.empty?
# if using default configuration of disks and cdroms
# cdroms are consider volumes, but cannot be destroyed
domain.destroy(destroy_volumes: true)
destroy_domain(domain, destroy_volumes: true, flags: undefine_flags)
else
domain_xml = libvirt_domain.xml_desc(1)
xml_descr = REXML::Document.new(domain_xml)
@@ -57,7 +60,7 @@ module VagrantPlugins
env[:ui].warn(I18n.t('vagrant_libvirt.destroy.obsolete_method'))
end
domain.destroy(destroy_volumes: false)
destroy_domain(domain, destroy_volumes: false, flags: undefine_flags)
volumes = domain.volumes
@@ -164,6 +167,14 @@ module VagrantPlugins
libvirt_disk.destroy if libvirt_disk
end
end
def destroy_domain(domain, destroy_volumes:, flags:)
if domain.method(:destroy).parameters.first.include?(:flags)
domain.destroy(destroy_volumes: destroy_volumes, flags: flags)
else
domain.destroy(destroy_volumes: destroy_volumes)
end
end
end
end
end

View File

@@ -375,10 +375,30 @@ module VagrantPlugins
end
end
undefine_flags = 0
nvram = REXML::XPath.first(xml_descr, '/domain/os/nvram')
if config.nvram
if nvram.nil?
descr_changed = true
nvram = REXML::Element.new('nvram', REXML::XPath.first(xml_descr, '/domain/os'))
nvram.text = config.nvram
else
if (nvram.text or '').strip != config.nvram
descr_changed = true
nvram.text = config.nvram
end
undefine_flags |= ProviderLibvirt::Util::DomainFlags::VIR_DOMAIN_UNDEFINE_KEEP_NVRAM
end
elsif !nvram.nil?
descr_changed = true
undefine_flags |= ProviderLibvirt::Util::DomainFlags::VIR_DOMAIN_UNDEFINE_NVRAM
nvram.parent.delete_element(nvram)
end
# Apply
if descr_changed
begin
libvirt_domain.undefine
libvirt_domain.undefine(undefine_flags)
new_descr = String.new
xml_descr.write new_descr
env[:machine].provider.driver.connection.servers.create(xml: new_descr)

View File

@@ -9,6 +9,7 @@ module VagrantPlugins
autoload :NetworkUtil, 'vagrant-libvirt/util/network_util'
autoload :StorageUtil, 'vagrant-libvirt/util/storage_util'
autoload :ErrorCodes, 'vagrant-libvirt/util/error_codes'
autoload :DomainFlags, 'vagrant-libvirt/util/domain_flags'
autoload :Ui, 'vagrant-libvirt/util/ui'
end
end

View File

@@ -0,0 +1,15 @@
# Ripped from https://libvirt.org/html/libvirt-libvirt-domain.html#types
module VagrantPlugins
module ProviderLibvirt
module Util
module DomainFlags
# virDomainUndefineFlagsValues
VIR_DOMAIN_UNDEFINE_MANAGED_SAVE = 1 # Also remove any managed save
VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA = 2 # If last use of domain, then also remove any snapshot metadata
VIR_DOMAIN_UNDEFINE_NVRAM = 4 # Also remove any nvram file
VIR_DOMAIN_UNDEFINE_KEEP_NVRAM = 8 # Keep nvram file
VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA = 16 # If last use of domain, then also remove any checkpoint metadata Future undefine control flags should come here.
end
end
end
end