1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

Feature #1240: Add LVM drivers adapted to the new datastore subsystem

(cherry picked from commit 8379ac09aa56971f69e8f2986d6a61723ec23c43)

Conflicts:

	src/datastore_mad/remotes/xpath.rb
This commit is contained in:
Jaime Melis 2012-04-18 02:01:31 +02:00
parent 65ecb979d4
commit ec7c9106d6
10 changed files with 609 additions and 3 deletions

104
src/datastore_mad/remotes/lvm/cp Executable file
View File

@ -0,0 +1,104 @@
#!/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
else
LIB_LOCATION=$ONE_LOCATION/lib
fi
. $LIB_LOCATION/sh/scripts_common.sh
DRIVER_PATH=$(dirname $0)
source ${DRIVER_PATH}/../libfs.sh
source ${DRIVER_PATH}/lvm.conf
# -------- Get cp 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/DATASTORE/TEMPLATE/HOST \
/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VG_NAME \
/DS_DRIVER_ACTION_DATA/IMAGE/PATH)
BASE_PATH="${XPATH_ELEMENTS[0]}"
RESTRICTED_DIRS="${XPATH_ELEMENTS[1]}"
SAFE_DIRS="${XPATH_ELEMENTS[2]}"
UMASK="${XPATH_ELEMENTS[3]}"
DST_HOST="${XPATH_ELEMENTS[4]:-$HOST}"
VG_NAME="${XPATH_ELEMENTS[5]:-$VG_NAME}"
SRC="${XPATH_ELEMENTS[6]}"
set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK"
SIZE=`fs_du $SRC`
LV_NAME="lv-one-${ID}"
LVM_SOURCE="$DST_HOST:$VG_NAME.$LV_NAME"
DEV="/dev/$VG_NAME/$LV_NAME"
REGISTER_CMD=$(cat <<EOF
set -e
$SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
EOF
)
case $SRC in
http://*)
log "Downloading $SRC to the image repository"
DUMP="$WGET $SRC -O-"
;;
*)
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 image $SRC to the image repository"
DUMP="cat $SRC"
;;
esac
ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" "Error registering $DST_HOST:$DEV"
exec_and_log "eval $DUMP | $SSH $DST_HOST $SUDO $DD of=$DEV bs=64k" \
"Error dumping $SRC to $DST_HOST:$DEV"
echo "$LVM_SOURCE $SIZE"

View File

@ -0,0 +1,24 @@
# -------------------------------------------------------------------------- #
# 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. #
#--------------------------------------------------------------------------- #
# Default volume group
VG_NAME=vg-one
# Default LVM server host
HOST=localhost
# Default LV snapshot SIZE
DEFAULT_SIZE=512

View File

@ -0,0 +1,92 @@
#!/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
source ${DRIVER_PATH}/lvm.conf
# -------- 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/DATASTORE/TEMPLATE/HOST \
/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VG_NAME \
/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]}"
DST_HOST="${XPATH_ELEMENTS[4]:-$HOST}"
VG_NAME="${XPATH_ELEMENTS[5]:-$VG_NAME}"
FSTYPE="${XPATH_ELEMENTS[6]}"
SIZE="${XPATH_ELEMENTS[7]:-0}"
set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK"
LV_NAME="lv-one-${ID}"
LVM_SOURCE="$DST_HOST:$VG_NAME.$LV_NAME"
DEV="/dev/$VG_NAME/$LV_NAME"
REGISTER_CMD=$(cat <<EOF
set -e
$SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
$SUDO $(mkfs_command "$DEV" "$FSTYPE")
EOF
)
# ------------ Image to save_as disk, no need to create a FS ------------
if [ "$FSTYPE" = "save_as" ]; then
echo "$LVM_SOURCE $SIZE"
exit 0
fi
# ------------ Create the image to the repository ------------
ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" \
"Error registering $DST_HOST:$DEV"
echo "$LVM_SOURCE $SIZE"

View File

@ -0,0 +1,73 @@
#!/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
source ${DRIVER_PATH}/lvm.conf
# -------- 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/TEMPLATE/HOST \
/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BASE_TID)
SRC="${XPATH_ELEMENTS[0]}"
DST_HOST="${XPATH_ELEMENTS[1]:-$HOST}"
BASE_TID="${XPATH_ELEMENTS[2]:-$BASE_TID}"
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"
let TID=ID+BASE_TID
RM_COMMAND=$(cat <<EOF
$SUDO $(tgtadm_target_delete "$TID")
$SUDO $LVREMOVE -f $VG_NAME/$LV_NAME
EOF
)
log "Removing $DST_HOST:$DEV from the image repository"
ssh_exec_and_log "$DST_HOST" "$RM_COMMAND" \
"Error removing $DST_HOST:$DEV"
exit 0

View File

@ -22,18 +22,24 @@
require 'base64'
require 'rexml/document'
require 'getoptlong'
require 'pp'
opts = opts = GetoptLong.new(
[ '--stdin', '-s', GetoptLong::NO_ARGUMENT ],
[ '--base64', '-b', GetoptLong::REQUIRED_ARGUMENT ]
)
tmp64 = ""
source = :stdin
tmp64 = ""
begin
opts.each do |opt, arg|
case opt
when '--stdin'
source = :stdin
when '--base64'
tmp64 = arg
source = :b64
tmp64 = arg
end
end
rescue Exception => e
@ -42,7 +48,13 @@ end
values = ""
tmp = Base64::decode64(tmp64)
case source
when :stdin
tmp = STDIN.read
when :b64
tmp = Base64::decode64(tmp64)
end
xml = REXML::Document.new(tmp).root
ARGV.each do |xpath|

89
src/tm_mad/lvm/clone Executable file
View File

@ -0,0 +1,89 @@
#!/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
DST=$2
VM_ID=$3
DS_ID=$4
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
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/lvm/lvm.conf
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
DST_DIR=`dirname $DST_PATH`
#-------------------------------------------------------------------------------
# Get SIZE through XPATH
#-------------------------------------------------------------------------------
DISK_ID=$(echo $DST_PATH|awk -F. '{print $NF}')
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
XPATH="$XPATH /VM/TEMPLATE/DISK[DISK_ID='$DISK_ID']/SIZE"
SIZE=$(onevm show -x $VM_ID | $XPATH )
[ -z "$SIZE" ] && SIZE=$DEFAULT_SIZE
#-------------------------------------------------------------------------------
# Get other LVM related fields
#-------------------------------------------------------------------------------
LV_NAME=`echo $SRC_PATH|cut -d. -f2`
VG_NAME=`echo $SRC_PATH|cut -d. -f1`
TARGET_DEV="/dev/$VG_NAME/$LV_NAME"
LV_SNAPSHOT="$LV_NAME-$VM_ID-$DISK_ID"
LV_SNAPSHOT_DEV="/dev/$VG_NAME/$LV_SNAPSHOT"
#-------------------------------------------------------------------------------
# Create the snapshot and link it
#-------------------------------------------------------------------------------
CLONE_CMD=$(cat <<EOF
set -e
mkdir -p $DST_DIR
$SUDO $LVCREATE -s -L$SIZE -n $LV_SNAPSHOT $TARGET_DEV
ln -s "$LV_SNAPSHOT_DEV" "$DST_PATH"
EOF
)
ssh_exec_and_log "$DST_HOST" "$CLONE_CMD" \
"Error cloning $TARGET_DEV to $LV_SNAPSHOT_DEV"
exit 0

64
src/tm_mad/lvm/delete Executable file
View File

@ -0,0 +1,64 @@
#!/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:remote_system_ds/disk.i|host:remote_system_ds/>
# - host is the target host to deploy the VM
# - remote_system_ds is the path for the system datastore in the host
DST=$1
VM_ID=$2
DS_ID=$3
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`
# Delete the device if it's a clone (LVM snapshot)
DELETE_CMD=$(cat <<EOF
DEV=\$(readlink $DST_PATH)
VM=$(basename \$DEV|cut -d- -f4)
DISK_ID=$(basename \$DEV|cut -d- -f5)
if [ -n "\$VM" -a -n "\$DISK_ID" ]; then
$SUDO $LVREMOVE -f \$DEV
fi
EOF
)
if [ `is_disk $DST_PATH` -eq 1 ]; then
# Disk
ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
"Error deleting $DST_PAtH"
else
# Directory
log "Deleting $DST_PATH"
ssh_exec_and_log "$DST_HOST" "rm -rf $DST_PATH" "Error deleting $DST_PATH"
fi
exit 0

61
src/tm_mad/lvm/ln Executable file
View File

@ -0,0 +1,61 @@
#!/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
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
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
DST_DIR=`dirname $DST_PATH`
LV_NAME=`echo $SRC_PATH|cut -d. -f2`
VG_NAME=`echo $SRC_PATH|cut -d. -f1`
TARGET_DEV="/dev/$VG_NAME/$LV_NAME"
LINK_CMD=$(cat <<EOF
set -e
mkdir -p $DST_DIR
ln -s "$TARGET_DEV" "$DST_PATH"
EOF
)
ssh_exec_and_log "$DST_HOST" "$LINK_CMD" \
"Error linking $TARGET_DEV"
exit 0

19
src/tm_mad/lvm/mv Executable file
View File

@ -0,0 +1,19 @@
#!/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. #
#--------------------------------------------------------------------------- #
exit 0

68
src/tm_mad/lvm/mvds Executable file
View File

@ -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. #
#--------------------------------------------------------------------------- #
# mvds host:remote_system_ds/disk.i fe:SOURCE
# - 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
DST=$2
VM_ID=$3
DS_ID=$3
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
#-------------------------------------------------------------------------------
SRC_PATH=`arg_path $SRC`
SRC_HOST=`arg_host $SRC`
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
VG_NAME=$(echo $DST_PATH|cut -d. -f1)
LV_NAME=$(echo $DST_PATH|cut -d. -f2)
TARGET_DEV=/dev/$VG_NAME/$LV_NAME
DUMP_CMD=$(cat <<EOF
DEV=\$(readlink $SRC_PATH)
SOURCE_DEV=\$(echo \$DEV|sed 's/-[0-9]*-[0-9]*$//')
SIZE=\$($SUDO $LVS \$SOURCE_DEV --noheadings|awk '{print \$4}'|tr -d m)
$SUDO $LVCREATE -L\${SIZE}M ${VG_NAME} -n ${LV_NAME}
$SUDO $DD if=$SRC_PATH of=$TARGET_DEV bs=64k
$SUDO $LVREMOVE -f \$DEV
EOF
)
#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------
log "Dumping $SRC to $DST"
ssh_exec_and_log "$SRC_HOST" "$DUMP_CMD" "Error dumping $SRC to $DST"
exit 0