From ffaed2dd2d102af978b19fec391e5599c3ba2cc9 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Mon, 4 Nov 2019 02:48:17 -0600 Subject: [PATCH] F #3189: Fix LXD net patch (#3897) --- share/examples/network_hooks/99-lxd_clean.rb | 22 ++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/share/examples/network_hooks/99-lxd_clean.rb b/share/examples/network_hooks/99-lxd_clean.rb index bb343bdcc12..45a03a4af85 100755 --- a/share/examples/network_hooks/99-lxd_clean.rb +++ b/share/examples/network_hooks/99-lxd_clean.rb @@ -13,16 +13,28 @@ exit 0 unless vm.hypervisor == 'lxd' OpenNebula.log 'running lxd network post cleanup' +# Detect nics from XML +nics = [] vm.nics.each do |nic| next unless nic[:nic] - veth = nic[:nic][:target] + nics << nic +end + +# Detect if hotplug or shutdown +detach = nil +nics.each do |nic| + next unless nic[:nic][:attach] == 'YES' + + detach = nic[:nic][:target] +end +def clean_host_nic(veth) cmd = "ip link show #{veth}" _o, _e, s = Open3.capture3(cmd) - next unless s == 0 + return unless s == 0 cmd = "sudo ip link delete #{veth}" OpenNebula.log "Found lingering nic #{veth}\n Running #{cmd}" @@ -31,3 +43,9 @@ OpenNebula.log "#{o}\n#{e}" end + +if detach # only clean detached nic + clean_host_nic(detach) +else # clean all nics + nics.each {|nic| clean_host_nic(nic[:nic][:target]) } +end