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