From 41d3bbe20027e9fc4fd1f7fe1e0386d320d405a1 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Wed, 7 Mar 2012 21:13:33 +0100 Subject: [PATCH] feature #1112: Implement clone, delete, ln, mv, mvds for tm_iscsi --- src/datastore_mad/remotes/iscsi/cp | 5 +- src/datastore_mad/remotes/iscsi/mkfs | 5 +- src/datastore_mad/remotes/iscsi/rm | 11 +--- src/datastore_mad/remotes/libfs.sh | 34 ------------ src/mad/sh/scripts_common.sh | 59 ++++++++++++++++++++ src/tm_mad/iscsi/clone | 68 +++++++++++++++++++++++ src/tm_mad/iscsi/delete | 59 ++++++++++++++++++++ src/tm_mad/iscsi/ln | 80 ++++++++++++++++++++++++++++ src/tm_mad/iscsi/mv | 74 +++++++++++++++++++++++++ src/tm_mad/iscsi/mvds | 50 +++++++++++++++++ 10 files changed, 398 insertions(+), 47 deletions(-) create mode 100755 src/tm_mad/iscsi/clone create mode 100755 src/tm_mad/iscsi/delete create mode 100755 src/tm_mad/iscsi/ln create mode 100755 src/tm_mad/iscsi/mv create mode 100755 src/tm_mad/iscsi/mvds diff --git a/src/datastore_mad/remotes/iscsi/cp b/src/datastore_mad/remotes/iscsi/cp index 5e4a797899..e7de096d11 100755 --- a/src/datastore_mad/remotes/iscsi/cp +++ b/src/datastore_mad/remotes/iscsi/cp @@ -79,8 +79,9 @@ let TID=ID+BASE_TID REGISTER_CMD=$(cat <&2 - exit 1 - fi + $SUDO $(tgtadm_target_delete "$TID") + $SUDO $LVREMOVE -f $VG_NAME/$LV_NAME EOF ) diff --git a/src/datastore_mad/remotes/libfs.sh b/src/datastore_mad/remotes/libfs.sh index 55d5aada37..76a91c6227 100644 --- a/src/datastore_mad/remotes/libfs.sh +++ b/src/datastore_mad/remotes/libfs.sh @@ -136,37 +136,3 @@ function check_restricted { echo 0 } - - -# ------------------------------------------------------------------------------ -# iSCSI functions -# ------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------- -# Returns the command to create a new target -# @param $1 - ID of the image -# @param $2 - Target Host -# @param $3 - Device -# @return the command to create a new target -#------------------------------------------------------------------------------- - -function iscsi_target_new { - ID="$1" - IQN="$2" - - echo "$TGTADM --lld iscsi --op new --mode target --tid $ID "\ - "--targetname $IQN" -} - -function iscsi_logicalunit_new { - ID="$1" - DEV="$2" - - echo "$TGTADM --lld iscsi --op new --mode logicalunit --tid $ID "\ - "--lun 1 --backing-store $DEV" -} - -function iscsi_target_delete { - ID="$1" - echo "$TGTADM --lld iscsi --op delete --mode target --tid $ID" -} diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 6b4ef552d9..1296f3c65a 100755 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -23,6 +23,7 @@ DATE=date DD=dd DU=du GREP=grep +ISCSIADM=iscsiadm LVCREATE=lvcreate LVREMOVE=lvremove LVS=lvs @@ -244,3 +245,61 @@ EOF` exit $SSH_EXEC_RC fi } + + +# ------------------------------------------------------------------------------ +# iSCSI functions +# ------------------------------------------------------------------------------ + +#------------------------------------------------------------------------------- +# Returns the command to create a new target +# @param $1 - ID of the image +# @param $2 - Target Host +# @param $3 - Device +# @return the command to create a new target +#------------------------------------------------------------------------------- + +function tgtadm_target_new { + ID="$1" + IQN="$2" + + echo "$TGTADM --lld iscsi --op new --mode target --tid $ID "\ + "--targetname $IQN;" +} + +function tgtadm_target_bind_all { + ID="$1" + echo "$TGTADM --lld iscsi --op bind --mode target --tid $ID -I ALL" +} + +function tgtadm_logicalunit_new { + ID="$1" + DEV="$2" + + echo "$TGTADM --lld iscsi --op new --mode logicalunit --tid $ID "\ + "--lun 1 --backing-store $DEV" +} + +function tgtadm_target_delete { + ID="$1" + echo "$TGTADM --lld iscsi --op delete --mode target --tid $ID" +} + +### + +function iscsiadm_discovery { + TARGET_HOST="$1" + echo "$ISCSIADM -m discovery -t st -p $TARGET_HOST" +} + +function iscsiadm_login { + IQN="$1" + TARGET_HOST="$2" + echo "$ISCSIADM -m node --targetname $IQN -p $TARGET_HOST --login" +} + +function iscsiadm_logout { + IQN="$1" + echo "$ISCSIADM -m node --targetname $IQN --logout" +} + diff --git a/src/tm_mad/iscsi/clone b/src/tm_mad/iscsi/clone new file mode 100755 index 0000000000..39edcbe028 --- /dev/null +++ b/src/tm_mad/iscsi/clone @@ -0,0 +1,68 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +# clone fe:SOURCE host:remote_system_ds/disk.i size +# - fe is the front-end hostname +# - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk +# - host is the target host to deploy the VM +# - remote_system_ds is the path for the system datastore in the host + +SRC=$1 # iqn.2012-02.org.opennebula:o200.vg-one.lv-one-0 +DST=$2 # o202:/var/lib/one//datastores/0/0/disk.0 + +if [ -z "${ONE_LOCATION}" ]; then + TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh +else + TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh +fi + +. $TMCOMMON + +#------------------------------------------------------------------------------- +# Set dst path and dir +#------------------------------------------------------------------------------- + +TARGET=`arg_path $SRC` # o200.vg-one.lv-one-0 +DST_PATH=`arg_path $DST` # /var/lib/one/datastores/0/0/disk.0 +DST_HOST=`arg_host $DST` # o202 +DST_DIR=`dirname $DST_PATH` # /var/lib/one/datastores/0/0 + +BASE_IQN=`echo $SRC|$CUT -d: -f1` +TARGET=`echo $SRC|$CUT -d: -f2` +LV_NAME=`echo $TARGET|$AWK -F. '{print $(NF)}'` +VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'` +DEV="/dev/$VG_NAME/$LV_NAME" + +exit +ssh_make_path $DST_HOST $DST_DIR + +#------------------------------------------------------------------------------- +# Copy files to the remote host +#------------------------------------------------------------------------------- +case $SRC in +http://*) + log "Downloading $SRC" + RMT_CMD="$WGET -O $DST_PATH $SRC" + ssh_exec_and_log "$DST_HOST" "$RMT_CMD" "Error downloading $SRC" + ;; + +*) + log "Cloning $SRC in $DST_PATH" + exec_and_log "$SCP $SRC $DST" "Error copying $SRC to $DST" + ;; +esac diff --git a/src/tm_mad/iscsi/delete b/src/tm_mad/iscsi/delete new file mode 100755 index 0000000000..c30639d900 --- /dev/null +++ b/src/tm_mad/iscsi/delete @@ -0,0 +1,59 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +# DELETE +# - host is the target host to deploy the VM +# - remote_system_ds is the path for the system datastore in the host + +DST=$1 # o202:/var/lib/one//datastores/0/0/disk.0 + +if [ -z "${ONE_LOCATION}" ]; then + TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh +else + TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh +fi + +. $TMCOMMON + +#------------------------------------------------------------------------------- +# Return if deleting a disk, we will delete them when removing the +# remote_system_ds directory for the VM (remotely) +#------------------------------------------------------------------------------- +DST_PATH=`arg_path $DST` +DST_HOST=`arg_host $DST` + +if [ `is_disk $DST_PATH` -eq 1 ]; then + # Disk + LOGOUT_CMD=$(cat < +# +# - hostX is the target host to deploy the VM +# - system_ds is the path for the system datastore in the host + +SRC=$1 +DST=$2 + +if [ -z "${ONE_LOCATION}" ]; then + TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh +else + TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh +fi + +. $TMCOMMON + +# {:path=> +# "/var/lib/one/remotes/tm/iscsi/mv o202:/var/lib/one//datastores/0/3/disk.0 rama:/var/lib/one/datastores/0/3/disk.0", +# :result=>"SUCCESS", +# :info=>"-"} + +# {:path=> +# "/var/lib/one/remotes/tm/shared/mv o202:/var/lib/one//datastores/0/3 rama:/var/lib/one/datastores/0/3", +# :result=>"SUCCESS", +# :info=>"-"} + +#------------------------------------------------------------------------------- +# Return if moving a disk, we will move them when moving the whole system_ds +# directory for the VM +#------------------------------------------------------------------------------- +SRC_PATH=`arg_path $SRC` +SRC_HOST=`arg_host $SRC` + +if [ `is_disk $SRC_PATH` -eq 0 ]; then + log "Removing directory" + ssh_exec_and_log "$SRC_HOST" "rm -rf $SRC_PATH" + + exit 0 +fi + +if [ "$SRC" == "$DST" ]; then + log "Not moving $SRC to $DST, they are the same path" + exit 0 +fi + +log "Logging out $IQN" + +LOGOUT_CMD=$(cat <