From e63a0893ac7088f98949bc8f3a5584f1202d0062 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 7 Sep 2012 18:50:07 +0200 Subject: [PATCH] feature #1441: Add stub for VMFS datastore --- src/datastore_mad/remotes/libfs.sh | 18 +++ src/datastore_mad/remotes/vmfs/bridgelist | 3 + src/datastore_mad/remotes/vmfs/cp | 150 ++++++++++++++++++++++ src/datastore_mad/remotes/vmfs/mkfs | 94 ++++++++++++++ src/datastore_mad/remotes/vmfs/rm | 66 ++++++++++ src/datastore_mad/remotes/vmfs/vmfsrc | 18 +++ 6 files changed, 349 insertions(+) create mode 100644 src/datastore_mad/remotes/vmfs/bridgelist create mode 100755 src/datastore_mad/remotes/vmfs/cp create mode 100755 src/datastore_mad/remotes/vmfs/mkfs create mode 100755 src/datastore_mad/remotes/vmfs/rm create mode 100644 src/datastore_mad/remotes/vmfs/vmfsrc diff --git a/src/datastore_mad/remotes/libfs.sh b/src/datastore_mad/remotes/libfs.sh index 0f625ee90d..dfca4cb5b0 100644 --- a/src/datastore_mad/remotes/libfs.sh +++ b/src/datastore_mad/remotes/libfs.sh @@ -137,3 +137,21 @@ function check_restricted { echo 0 } + +#------------------------------------------------------------------------------- +# Gets the ESX host to be used as bridge to register a VMware disk +# Implements a round robin for the bridges +# @param $1 - Path to the list of ESX hosts to be used as bridges +# @return host to be used as bridge +#------------------------------------------------------------------------------- +function get_destination_host { + CONF_FILE_PATH="$1/bridgelist" + + BRIDGE_HOST=`head -1 $CONF_FILE_PATH` + + sed -i -e "1d" $CONF_FILE_PATH + + cat $BRIDGE_HOST >> $CONF_FILE_PATH + + echo $BRIDGE_HOST +} diff --git a/src/datastore_mad/remotes/vmfs/bridgelist b/src/datastore_mad/remotes/vmfs/bridgelist new file mode 100644 index 0000000000..f5e366f20f --- /dev/null +++ b/src/datastore_mad/remotes/vmfs/bridgelist @@ -0,0 +1,3 @@ +bridge-esx-1.mydomain +bridge-esx-2.mydomain +bridge-esx-3.mydomain diff --git a/src/datastore_mad/remotes/vmfs/cp b/src/datastore_mad/remotes/vmfs/cp new file mode 100755 index 0000000000..5d8bc21167 --- /dev/null +++ b/src/datastore_mad/remotes/vmfs/cp @@ -0,0 +1,150 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to copy a VM image (SRC) to the image repository as DST +# Several SRC types are supported +############################################################################### + +# -------- Set up the environment to source common tools & conf ------------ + +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one + VMWARERC=/etc/one/vmwarerc +else + LIB_LOCATION=$ONE_LOCATION/lib + VMWARERC=$ONE_LOCATION/etc/vmwarerc +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh +source $(dirname $0)/vmfsrc + +# -------- Get cp and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +UTILS_PATH="${DRIVER_PATH}/.." + +XPATH="$UTILS_PATH/xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RESTRICTED_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/SAFE_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ + /DS_DRIVER_ACTION_DATA/IMAGE/PATH \ + /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/MD5 \ + /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/SHA1) + +BASE_PATH="${XPATH_ELEMENTS[0]}" +RESTRICTED_DIRS="${XPATH_ELEMENTS[1]}" +SAFE_DIRS="${XPATH_ELEMENTS[2]}" +UMASK="${XPATH_ELEMENTS[3]}" +SRC="${XPATH_ELEMENTS[4]}" +MD5="${XPATH_ELEMENTS[5]}" +SHA1="${XPATH_ELEMENTS[6]}" + +# Set up the datastore +set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" + +DST=`generate_image_path` +DST_HOST=`get_destination_host` + +if [ "$SSH" != "yes" ]; then + USERNAME=`echo $(cat $VMWARERC |grep ":username:"|cut -d":" -f 3)` + PASSWORD=`echo $(cat $VMWARERC |grep ":password:"|cut -d":" -f 3)` + VI_PARAMS="--server $DST_HOST --username $USERNAME --password $PASSWORD" +fi + +# Create DST in DST_HOST +if [ "$SSH" == "yes" ]; then + ssh_make_path $DST_HOST $DST +else + exec_and_log 'vifs $VI_PARAMS --mkdir "$DST"' "Cannot create $DST in $DST_HOST" +fi + +# Prepare for a possible download in the front-end +if [ ! -d $TMP_DIR ]; then + mkdir -p $TMP_DIR +fi + +HASHES="" + +if [ -n "$MD5" ]; then + HASHES="$HASHES --md5 $MD5" +fi + +if [ -n "$SHA1" ]; then + HASHES="$HASHES --sha1 $SHA1" +fi + +COPY_COMMAND="$UTILS_PATH/downloader.sh $HASHES $SRC $TMP_DIR/$IMAGE_HASH" + +# ------------ Copy the image to the repository ------------- + +case $SRC in +http://*|https://* ) + log "Downloading $SRC to the image repository" + + exec_and_log "$COPY_COMMAND" "Error downloading $SRC" + ;; + +*) + if [ `check_restricted $SRC` -eq 1 ]; then + log_error "Not allowed to copy images from $RESTRICTED_DIRS" + error_message "Not allowed to copy image file $SRC" + exit -1 + fi + + log "Copying local disk folder $SRC to the image repository" + + if [ ! -d $SRC ]; then + exec_and_log "$COPY_COMMAND" "Error copying $SRC to $TMP_DIR/$IMAGE_HASH" + fi + + SRC="$TMP_DIR/$IMAGE_HASH" + ;; +esac + +# Rename the disk filename to disk.vmdk (warning: it does so in SRC) +if [ ! -f $SRC/disk.vmdk ]; then + BASE_DISK_FILE=`ls $SRC | grep -v '\-\(flat\|delta\|s[0-9]*\)\.vmdk$'` + + exec_and_log "mv -f $DST/$BASE_DISK_FILE $DST/disk.vmdk" \ + "Error renaming disk file $BASE_DISK_FILE to disk.vmdk" +fi + +# Make the final hop, front-end -> VMFS Datastore +if [ "$SSH" == "yes" ]; then + exec_and_log "$SCP -r $SRC $DST" "Error copying $SRC to $DST through SCP" +else + cd $SRC + for file in $(find . -type f); do + FNAME=$(basename $file) + exec_and_log 'vifs $VI_PARAMS -p $file $DST/$FNAME' "Cannot upload $file to $DST/$FNAME on $DST_HOST" + done +fi + +echo "$DST" diff --git a/src/datastore_mad/remotes/vmfs/mkfs b/src/datastore_mad/remotes/vmfs/mkfs new file mode 100755 index 0000000000..13cd67afdf --- /dev/null +++ b/src/datastore_mad/remotes/vmfs/mkfs @@ -0,0 +1,94 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to create a VM image (SRC) of size (SIZE) and formatted +# as (FS) +############################################################################### + +# -------- Set up the environment to source common tools & conf ------------ + +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one +else + LIB_LOCATION=$ONE_LOCATION/lib +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh + +# -------- Get mkfs and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RESTRICTED_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/SAFE_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ + /DS_DRIVER_ACTION_DATA/IMAGE/FSTYPE \ + /DS_DRIVER_ACTION_DATA/IMAGE/SIZE) + +BASE_PATH="${XPATH_ELEMENTS[0]}" +RESTRICTED_DIRS="${XPATH_ELEMENTS[1]}" +SAFE_DIRS="${XPATH_ELEMENTS[2]}" +UMASK="${XPATH_ELEMENTS[3]}" +FSTYPE="${XPATH_ELEMENTS[4]}" +SIZE="${XPATH_ELEMENTS[5]}" + +mkdir -p "$BASE_PATH" +set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" + +DST=`generate_image_path` + +# ------------ Image to save_as disk, no need to create a FS ------------ + +if [ "$FSTYPE" = "save_as" ]; then + echo "$DST" + exit 0 +fi + +# ------------ Create the image to the repository ------------ + +DISK=$DST/disk.vmdk +DISK_TMP=$DISK.tmp + +IMAGE_FORMAT=vmdk + +MKFS_CMD=`mkfs_command $DISK_TMP $FSTYPE` + +exec_and_log "mkdir -p $DST" \ + "Could not create disk directory $DST" +exec_and_log "$DD if=/dev/zero of=$DISK_TMP bs=1 count=1 seek=${SIZE}M" \ + "Could not create temporary image $DISK_TMP" +exec_and_log "$MKFS_CMD" \ + "Unable to create filesystem $FSTYPE in $DISK_TMP" +exec_and_log "$QEMU_IMG convert -O $IMAGE_FORMAT $DISK_TMP $DISK" \ + "Unable to convert to $IMAGE_FORMAT in $DISK_TMP" +exec_and_log "rm -f $DISK_TMP" \ + "Unable to remove temporary disk $DISK_TMP" + +echo "$DST" diff --git a/src/datastore_mad/remotes/vmfs/rm b/src/datastore_mad/remotes/vmfs/rm new file mode 100755 index 0000000000..bb3ac1601f --- /dev/null +++ b/src/datastore_mad/remotes/vmfs/rm @@ -0,0 +1,66 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to remove a VM image (SRC) from the image repository +############################################################################### + +# ------------ Set up the environment to source common tools ------------ + +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one +else + LIB_LOCATION=$ONE_LOCATION/lib +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh + +# -------- Get rm and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/IMAGE/SOURCE \ + /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH) + +SRC="${XPATH_ELEMENTS[0]}" +BASE_PATH="${XPATH_ELEMENTS[1]}" + +BASENAME_SRC=`basename "${SRC##$BASE_PATH}"` + +# ------------ Remove the image from the repository ------------ + +if [ -d "$SRC" -a `dirname "$SRC"` = "$BASE_PATH" -a -n "$BASENAME_SRC" ] +then + log "Removing $SRC from the image repository" + + exec_and_log "rm -r $SRC" \ + "Error deleting $SRC" +else + log_error "Bad formed or unavailable Image source: ${SRC}" + exit 1 +fi diff --git a/src/datastore_mad/remotes/vmfs/vmfsrc b/src/datastore_mad/remotes/vmfs/vmfsrc new file mode 100644 index 0000000000..bb6463166c --- /dev/null +++ b/src/datastore_mad/remotes/vmfs/vmfsrc @@ -0,0 +1,18 @@ +# -------------------------------------------------------------------------- # +# 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. # +#--------------------------------------------------------------------------- # + +TMP_DIR=/var/lib/one/tmp +SSH=no