From 1f12736facfece5b409c29251fb72b27ef69b4d3 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 15 Jun 2012 17:25:26 +0200 Subject: [PATCH] feature #1307: Add detach and attach disk actions for VMware --- src/vmm_mad/remotes/vmware/attach_disk | 46 +++++++++++++++ src/vmm_mad/remotes/vmware/detach_disk | 33 +++++++++++ .../remotes/vmware/scripts_common_sh.sh | 59 +++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 src/vmm_mad/remotes/vmware/attach_disk create mode 100644 src/vmm_mad/remotes/vmware/detach_disk create mode 100644 src/vmm_mad/remotes/vmware/scripts_common_sh.sh diff --git a/src/vmm_mad/remotes/vmware/attach_disk b/src/vmm_mad/remotes/vmware/attach_disk new file mode 100644 index 0000000000..14223c277a --- /dev/null +++ b/src/vmm_mad/remotes/vmware/attach_disk @@ -0,0 +1,46 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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 $PWD/scripts_common_sh.sh + +DEPLOYID="$1" +SOURCE="$2" +TARGET="$3" +CONTROLLER_NUMBER=0 # Only one controller at the moment (up to 16 devices) + + +DISK_NAME=`basename $SOURCE` +TEMP_PATH=`dirname $SOURCE` +VM_ID=`basename $TEMP_PATH` +TEMP_PATH=`dirname $TEMP_PATH` +DATASTORE=`basename $TEMP_PATH` + +DISK_PATH="/vmfs/volumes/$DATASTORE/$VM_ID/$DISK_NAME/disk.vmdk" + +# Get the VMware ID corresponding to the deploy_id +VMWAREID=`vim-cmd vmsvc/getallvms|grep $DEPLOYID|cut -d' ' -f 1` + +TARGET_NUMBER=1 + +ATTACH_PARAMS="$VMWAREID $DISK_PATH $CONTROLLER_NUMBER $TARGET_NUMBER $DATASTORE" + +exec_and_log "$SUDO vim-cmd vmsvc/device.diskaddexisting $ATTACH_PARAMS" \ + "Could not attach $SOURCE ($TARGET) to $DOMAIN" + + + diff --git a/src/vmm_mad/remotes/vmware/detach_disk b/src/vmm_mad/remotes/vmware/detach_disk new file mode 100644 index 0000000000..87596b8fe2 --- /dev/null +++ b/src/vmm_mad/remotes/vmware/detach_disk @@ -0,0 +1,33 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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 $PWD/scripts_common_sh.sh + +DEPLOYID="$1" +IMAGE_PATH="$2" +UNIT_NUMBER="$3" + +CONTROLLER_NUMBER=0 # Only one controller at the moment (up to 16 devices) + +# Get the VMware ID corresponding to the deploy_id +VMWAREID=`vim-cmd vmsvc/getallvms|grep $DEPLOYID|cut -d' ' -f 1` + +DETACH_PARAMS="$VMWAREID $CONTROLLER_NUMBER $UNIT_NUMBER $IMAGE_PATH" + +exec_and_log "$SUDO vim-cmd vmsvc/device.diskremove $DETACH_PARAMS" \ + "Could not detach $TARGET from $DOMAIN" \ No newline at end of file diff --git a/src/vmm_mad/remotes/vmware/scripts_common_sh.sh b/src/vmm_mad/remotes/vmware/scripts_common_sh.sh new file mode 100644 index 0000000000..815bf3f479 --- /dev/null +++ b/src/vmm_mad/remotes/vmware/scripts_common_sh.sh @@ -0,0 +1,59 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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. # +#--------------------------------------------------------------------------- # + +error_message() +{ + ( + echo "ERROR MESSAGE --8<------" + echo "$1" + echo "ERROR MESSAGE ------>8--" + ) 1>&2 +} + +log_function() +{ + echo "$1: $SCRIPT_NAME: $2" 1>&2 +} + +log_error() +{ + log_function "ERROR" "$1" +} + +exec_and_log() +{ + message=$2 + + EXEC_LOG_ERR=`$1 2>&1 1>/dev/null` + EXEC_LOG_RC=$? + + if [ $EXEC_LOG_RC -ne 0 ]; then + log_error "Command \"$1\" failed: $EXEC_LOG_ERR" + + if [ -n "$2" ]; then + error_message "$2" + else + error_message "Error executing $1: $EXEC_LOG_ERR" + fi + exit $EXEC_LOG_RC + fi +} + +WHICH_SUDO=`which sudo` + +if [ ! -z "$WHICH_SUDO" -a -f "$WHICH_SUDO" ]; then + SUDO="sudo " +fi \ No newline at end of file