From 33b724ec4ba456611748c19af53100a7bebe0be8 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 7 Mar 2013 01:24:32 +0100 Subject: [PATCH] feature #1691: Implements attach NIC actions for KVM --- install.sh | 2 + src/vmm_mad/exec/one_vmm_exec.rb | 90 ++++++++++++++++++++++++++++++ src/vmm_mad/remotes/kvm/attach_nic | 34 +++++++++++ src/vmm_mad/remotes/kvm/detach_nic | 28 ++++++++++ 4 files changed, 154 insertions(+) create mode 100755 src/vmm_mad/remotes/kvm/attach_nic create mode 100755 src/vmm_mad/remotes/kvm/detach_nic diff --git a/install.sh b/install.sh index c89bc87bc6..f7e33de441 100755 --- a/install.sh +++ b/install.sh @@ -759,6 +759,8 @@ VMM_EXEC_KVM_SCRIPTS="src/vmm_mad/remotes/kvm/cancel \ src/vmm_mad/remotes/kvm/poll_ganglia \ src/vmm_mad/remotes/kvm/attach_disk \ src/vmm_mad/remotes/kvm/detach_disk \ + src/vmm_mad/remotes/kvm/attach_nic \ + src/vmm_mad/remotes/kvm/detach_nic \ src/vmm_mad/remotes/kvm/snapshot_create \ src/vmm_mad/remotes/kvm/snapshot_revert \ src/vmm_mad/remotes/kvm/snapshot_delete \ diff --git a/src/vmm_mad/exec/one_vmm_exec.rb b/src/vmm_mad/exec/one_vmm_exec.rb index 7d08bddb99..daba5f6aad 100755 --- a/src/vmm_mad/exec/one_vmm_exec.rb +++ b/src/vmm_mad/exec/one_vmm_exec.rb @@ -734,6 +734,96 @@ class ExecDriver < VirtualMachineDriver action.run(steps) end + # + # ATTACHNIC action to attach a new nic interface + # + def attach_nic(id, drv_message) + xml_data = decode(drv_message) + + begin + source = xml_data.elements["VM/TEMPLATE/NIC[ATTACH='YES']/BRIDGE"] + mac = xml_data.elements["VM/TEMPLATE/NIC[ATTACH='YES']/MAC"] + + source = source.text.strip + mac = mac.text.strip + rescue + send_message(action, RESULT[:failure], id, + "Error in #{ACTION[:attach_nic]}, BRIDGE and MAC needed in NIC") + return + end + + model = xml_data.elements["VM/TEMPLATE/NIC[ATTACH='YES']/MODEL"] + + model = model.text if !model.nil? + model = model.strip if !model.nil? + model = "-" if model.nil? + + action = VmmAction.new(self, id, :attach_nic, drv_message) + + steps=[ + # Execute pre-attach networking setup + { + :driver => :vnm, + :action => :pre + }, + # Attach the new NIC + { + :driver => :vmm, + :action => :attach_nic, + :parameters => [:deploy_id, mac, source, model] + }, + # Execute post-boot networking setup + { + :driver => :vnm, + :action => :post, + :parameters => [:deploy_info], + :fail_actions => [ + { + :driver => :vmm, + :action => :detach_nic, + :parameters => [:deploy_id, mac] + } + ] + } + ] + + action.run(steps) + end + + # + # DETACHNIC action to detach a nic interface + # + def detach_nic(id, drv_message) + xml_data = decode(drv_message) + + begin + mac = xml_data.elements["VM/TEMPLATE/NIC[ATTACH='YES']/MAC"] + mac = mac.text.strip + rescue + send_message(action, RESULT[:failure], id, + "Error in #{ACTION[:detach_nic]}, MAC needed in NIC") + return + end + + action = VmmAction.new(self, id, :detach_nic, drv_message) + + steps=[ + # Detach the NIC + { + :driver => :vmm, + :action => :detach_nic, + :parameters => [:deploy_id, mac] + }, + # Clean networking setup + { + :driver => :vnm, + :action => :clean + } + ] + + action.run(steps) + end + private def ensure_xpath(xml_data, id, action, xpath) diff --git a/src/vmm_mad/remotes/kvm/attach_nic b/src/vmm_mad/remotes/kvm/attach_nic new file mode 100755 index 0000000000..7c4e539d9d --- /dev/null +++ b/src/vmm_mad/remotes/kvm/attach_nic @@ -0,0 +1,34 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +source $(dirname $0)/kvmrc +source $(dirname $0)/../../scripts_common.sh + +DOMAIN=$1 +MAC=$2 +SOURCE=$3 +MODEL=$4 + +ATTACH_ARGS="--domain $DOMAIN --type bridge --source $SOURCE --mac $MAC" + +if [ "$MODEL" != "-" ]; then + ATTACH_ARGS="$ATTACH_ARGS --model $MODEL" +fi + +exec_and_log "virsh --connect $LIBVIRT_URI attach-interface $ATTACH_ARGS" \ + "Could not attach NIC ($MAC) to $DOMAIN" diff --git a/src/vmm_mad/remotes/kvm/detach_nic b/src/vmm_mad/remotes/kvm/detach_nic new file mode 100755 index 0000000000..5030d40373 --- /dev/null +++ b/src/vmm_mad/remotes/kvm/detach_nic @@ -0,0 +1,28 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +source $(dirname $0)/kvmrc +source $(dirname $0)/../../scripts_common.sh + +DOMAIN=$1 +MAC=$2 + +DETACH_ARGS="--domain $DOMAIN --type bridge --mac $MAC" + +exec_and_log "virsh --connect $LIBVIRT_URI detach-interface $DETACH_ARGS" \ + "Could not attach NIC ($MAC) to $DOMAIN"