From aa2799ed3457b2234d2bf07ef4976459cc30c670 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 24 May 2013 18:42:50 +0200 Subject: [PATCH] feature #1977: Add attach nic operation --- src/vmm_mad/exec/one_vmm_sh | 1 + src/vmm_mad/remotes/vmware/attach_nic | 26 ++++++++++++++++--- src/vmm_mad/remotes/vmware/vi_driver.rb | 34 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/vmm_mad/exec/one_vmm_sh b/src/vmm_mad/exec/one_vmm_sh index d1f3bd3ca3..527bd607a8 100755 --- a/src/vmm_mad/exec/one_vmm_sh +++ b/src/vmm_mad/exec/one_vmm_sh @@ -25,5 +25,6 @@ fi LOCAL_ACTIONS="deploy,shutdown,reboot,cancel,save,restore,migrate,poll,pre" LOCAL_ACTIONS="${LOCAL_ACTIONS},post,clean" LOCAL_ACTIONS="${LOCAL_ACTIONS},snapshotcreate,snapshotrevert,snapshotdelete" +LOCAL_ACTIONS="${LOCAL_ACTIONS},attach_nic,detach_nic" exec $MAD_LOCATION/one_vmm_exec -l $LOCAL_ACTIONS $* diff --git a/src/vmm_mad/remotes/vmware/attach_nic b/src/vmm_mad/remotes/vmware/attach_nic index 8284e47213..922fc9f04b 100755 --- a/src/vmm_mad/remotes/vmware/attach_nic +++ b/src/vmm_mad/remotes/vmware/attach_nic @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env ruby # -------------------------------------------------------------------------- # # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs # @@ -15,7 +15,25 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # -source $(dirname $0)/../../scripts_common.sh -log_error "Attach network interface is not yet supported for VMware." -exit 1 +ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) +end + +$: << RUBY_LIB_LOCATION +$: << File.dirname(__FILE__) + +require 'vi_driver' + +deploy_id = ARGV[0] +mac = ARGV[1] +bridge = ARGV[2] +host = ARGV[5] + +vi_drv = VIDriver.new(host) + +puts vi_drv.poll_vm(deploy_id) diff --git a/src/vmm_mad/remotes/vmware/vi_driver.rb b/src/vmm_mad/remotes/vmware/vi_driver.rb index e93b6fdc88..460fa53b9d 100644 --- a/src/vmm_mad/remotes/vmware/vi_driver.rb +++ b/src/vmm_mad/remotes/vmware/vi_driver.rb @@ -303,4 +303,38 @@ class VIDriver return perfManager.retrieve_stats(objects, metrics, stat_opts) end + + # -------------------------------------------------------------------------# + # Attach a NIC to deploy_id, linked to bridge, with mac and of type = model# + # -------------------------------------------------------------------------# + def attach_nic(deploy_id, bridge, model, mac) + vm = get_vm(deploy_id)[:vm] + card_num = 1 # start in one, we want the next avaiable id + vm.config.hardware.device.each{ |dv| + if dv.class.ancestors[1] == VIM::VirtualEthernetCard + card_num = card_num + 1 + end + } + backing = VIM.VirtualEthernetCardNetworkBackingInfo(:deviceName => bridge) + device_spec = {:key => 0, + :deviceInfo => { + :label => "net" + card_num.to_s, + :summary => bridge + }, + :backing => backing, + :addressType => 'manual'), + :macAddress => mac } + + device = case model.downcase + when 'e1000' + VIM.VirtualE1000(device_spec) + when 'pcnet32' + VIM.VirtualPCNet32(device_spec) + when 'VirtualVmxnet' + VIM.VirtualVmxnet(device_spec) + end + spec = {:deviceChange => [:operation => :add, :device => device]} + + vm.ReconfigVM_Task(:spec => spec).wait_for_completion + end end