1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Merge branch 'feature-1592'

This commit is contained in:
Jaime Melis 2012-12-20 15:29:05 +01:00
commit 18c0c3795b
10 changed files with 380 additions and 51 deletions

View File

@ -183,7 +183,8 @@ else
CHOWN_DIRS="$ROOT"
fi
SHARE_DIRS="$SHARE_LOCATION/examples"
SHARE_DIRS="$SHARE_LOCATION/examples \
$SHARE_LOCATION/tgt"
ETC_DIRS="$ETC_LOCATION/im_ec2 \
$ETC_LOCATION/vmm_ec2 \
@ -451,6 +452,7 @@ INSTALL_FILES=(
NETWORK_OVSWITCH_FILES:$VAR_LOCATION/remotes/vnm/ovswitch
NETWORK_VMWARE_FILES:$VAR_LOCATION/remotes/vnm/vmware
EXAMPLE_SHARE_FILES:$SHARE_LOCATION/examples
TGT_SHARE_FILES:$SHARE_LOCATION/tgt
INSTALL_NOVNC_SHARE_FILE:$SHARE_LOCATION
INSTALL_GEMS_SHARE_FILE:$SHARE_LOCATION
HOOK_FT_FILES:$VAR_LOCATION/remotes/hooks/ft
@ -1065,6 +1067,12 @@ EXAMPLE_SHARE_FILES="share/examples/vm.template \
share/examples/private.net \
share/examples/public.net"
#-------------------------------------------------------------------------------
# File required to interact with a tgtd server
#-------------------------------------------------------------------------------
TGT_SHARE_FILES="share/scripts/tgt/tgt-setup-lun-one"
#-------------------------------------------------------------------------------
# HOOK scripts, to be installed under $VAR_LOCATION/remotes/hooks
#-------------------------------------------------------------------------------

View File

@ -0,0 +1,279 @@
#!/bin/bash
# LUN assignment script
#
# Copyright (C) 2007 Erez Zilber <erezz@voltaire.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
usage()
{
name=$(basename $0);
echo "usage:";
echo -e "\t$name -n tgt_name -d dev -b bs_name -t transport [initiator_IP1 initiator_IP2 ...]";
echo "defaults:";
echo -e "\tbacking store: rdwr";
echo -e "\ttransport: iscsi";
echo -e "\tinitiator: ALL";
echo "examples:";
echo -e "\t$name -n tgt-1 -d /dev/sdb1 192.168.1.2";
echo -e "\t$name -n tgt-2 -d /tmp/null -b null -t iser";
echo -e "\t$name -n tgt-3 -d ~/disk3.bin -b rdwr 192.168.1.2 192.168.1.3";
}
verify_params()
{
if ! [ "$dev" -o "$bs_type" == "null" ]; then
echo "Error: a device is mandatory";
exit 1;
else
return;
fi
# Make sure that the device exists
if ! [ -b $dev -o -f $dev -o -c $dev ]; then
echo "Error: $dev is not a device";
exit 1;
fi
if ! [ "$tgt_name" ]; then
echo "Error: target name is mandatory";
exit 1;
fi
if ! [ "$lld_name" ]; then
echo "Error: lld name empty";
exit 1;
fi
}
find_vacant_tgt_id()
{
tmp_file=`mktemp`
tgtadm --lld $lld_name --op show --mode target | strings > $tmp_file
id_list=$(cat $tmp_file | grep Target | cut -d" " -f2 | sed s/://)
next_vacant_id=1
for id in $id_list; do
if (($id > $next_vacant_id)); then
break;
else
next_vacant_id=$((next_vacant_id+1))
fi
done
rm -f $tmp_file
echo $next_vacant_id
}
find_vacant_lun()
{
tid=$1
tgt_found=0
next_vacant_lun=0
tmp_file=`mktemp`
tgtadm --lld $lld_name --op show --mode target | strings | tgt-admin -s | grep 'LUN\|Target' > $tmp_file
retcode=$?
while read line; do
# Check if we finished going over this target
if ((tgt_found == 1 && $(echo $line | grep -c "^Target") == 1)); then
break
fi
# Check if we found the requested target
if (($(echo $line | grep -c "Target $tid:") == 1)); then
tgt_found=1
continue
fi
if ((tgt_found == 1 && $(echo $line | grep -c "LUN:") == 1)); then
curr_lun=$(echo $line | cut -d" " -f2)
if (($curr_lun > $next_vacant_lun)); then
break
else
next_vacant_lun=$((next_vacant_lun+1))
fi
fi
done < $tmp_file
rm -f $tmp_file
if ((tgt_found == 0)); then
echo "Error: could not find a LUN for target $tid, guessing 1 as next vacant LUN"
return 1
fi
if ((next_vacant_lun == 0)); then
echo "Error: could not find a LUN for target $tid, guessing 1 as next vacant LUN"
return 1
fi
return $next_vacant_lun
}
err_exit()
{
if ((new_tgt == 0)); then
exit 1
fi
echo "Deleting new target, tid=$tid"
tgtadm --lld $lld_name --op delete --mode target --tid $tid
res=$?
if [ $res -ne 0 ]; then
echo "Error: failed to delete target, tid=$tid"
fi
exit 1
}
check_if_tgt_exists()
{
tgt_list=$(tgtadm --lld $lld_name --op show --mode target | strings | grep Target | cut -d" " -f3)
for curr_tgt in $tgt_list; do
if [ $tgt_name = $curr_tgt ]; then
return 1
fi
done
return 0
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
lld_name="iscsi"
while getopts "d:n:b:t:h:" opt
do
case ${opt} in
d)
dev=$OPTARG;;
n)
tgt_name=$OPTARG;;
b)
bs_type=$OPTARG;;
t)
lld_name=$OPTARG;;
h*)
usage
exit 1
esac
done
shift $(($OPTIND - 1))
initiators=$*
verify_params
# Check if tgtd is running (we should have 2 daemons)
tgtd_count=`pidof tgtd | wc -w`
if [ $tgtd_count -lt 1 ]; then
echo "tgtd is not running"
echo "Exiting..."
exit 1
fi
echo "Using transport: $lld_name"
if ! [[ $tgt_name =~ ^iqn ]]; then
tgt_name="iqn.2001-04.com.$(hostname -s)-$tgt_name"
fi
# Make sure that a target with the same name doesn't exist
check_if_tgt_exists
if [ $? -eq 1 ]; then
echo "Error: target named $tgt_name already exists"
read -p "Add a new lun to the existing target? (yes/NO): " add_lun
if [ $add_lun != "yes" ]; then
exit 1
fi
tid=$(tgtadm --lld $lld_name --op show --mode target | strings | grep $tgt_name | cut -d" " -f2)
tid=${tid%:}
new_tgt=0
else
i=0
while [ true ]
do
let i=i+1
tid=`find_vacant_tgt_id`
#tid=$?
# Create the new target
echo -n "Creating new target (name=$tgt_name, tid=$tid)..."
tgtoutput=`tgtadm --lld $lld_name --op new --mode target --tid $tid -T $tgt_name 2>&1`
res=$?
if [ "$tgtoutput" == "tgtadm: this target already exists" ]; then
echo "Target already exists (this should not happen)... Attempt $i"
elif [ $res -eq 0 ]; then # output is not about duplication and return value is zero, this is the normal condition
break
fi
if [ $i -eq 100 ]; then
echo "Too many attempts, giving up"
break
fi
done
if [ $res -ne 0 ]; then
echo "Error: failed to create target (name=$tgt_name, tid=$tid)"
exit 1
fi
new_tgt=1
fi
echo "OK"
find_vacant_lun $tid
lun=$?
echo "LUN ok, $lun"
# Add a logical unit to the target
echo "Adding a logical unit ($dev) to target, tid=$tid"
if [ $bs_type ]; then
echo "Setting backing store type: $bs_type"
bs_opt="-E $bs_type"
fi
tgtadm --lld $lld_name --op new --mode logicalunit --tid $tid --lun $lun -b $dev $bs_opt
res=$?
if [ $res -ne 0 ]; then
echo "Error: failed to add a logical unit ($dev) to target, tid=$tid"
err_exit
fi
# Define which initiators can use this target
if test "$initiators" ; then
# Allow access only for specific initiators
echo "Accepting connections only from $initiators"
for initiator in $initiators; do
tgtadm --lld $lld_name --op bind --mode target --tid $tid -I $initiator
res=$?
if [ $res -ne 0 ]; then
echo "Error: could not assign initiator $initiator to the target"
fi
done
else
# Allow access for everyone
echo "Accepting connections from all initiators"
tgtadm --lld $lld_name --op bind --mode target --tid $tid -I ALL
res=$?
if [ $res -ne 0 ]; then
echo "Error: failed to set access for all initiators"
err_exit
fi
fi

View File

@ -64,6 +64,9 @@ BASE_IQN="${XPATH_ELEMENTS[6]:-$BASE_IQN}"
SRC="${XPATH_ELEMENTS[7]}"
SIZE="${XPATH_ELEMENTS[8]}"
# Check if 'TGTSETUPLUN' is installed
tgt_setup_lun_install "$DST_HOST" "$BASE_PATH"
set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK"
LV_NAME="lv-one-${ID}"
@ -76,13 +79,9 @@ DEV_SRC="/dev/$VG_NAME/$LV_SRC"
CLONE_CMD=$(cat <<EOF
set -e
$SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
$SUDO $DD if=$DEV_SRC of=$DEV bs=64k
$SUDO $DD if=$DEV_SRC of=$DEV bs=2M
TID=\$($SUDO $(tgtadm_next_tid))
$SUDO $(tgtadm_target_new "\$TID" "$IQN")
$SUDO $(tgtadm_target_bind_all "\$TID")
$SUDO $(tgtadm_logicalunit_new "\$TID" "$DEV")
$SUDO $(tgt_setup_lun "$IQN" "$DEV")
$SUDO $(tgt_admin_dump_config "$TARGET_CONF")
EOF
)

View File

@ -76,6 +76,9 @@ SHA1="${XPATH_ELEMENTS[10]}"
NO_DECOMPRESS="${XPATH_ELEMENTS[11]}"
LIMIT_TRANSFER_BW="${XPATH_ELEMENTS[12]}"
# Check if 'TGTSETUPLUN' is installed
tgt_setup_lun_install "$DST_HOST" "$BASE_PATH"
set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK"
LV_NAME="lv-one-${ID}"
@ -86,13 +89,7 @@ REGISTER_CMD=$(cat <<EOF
set -e
$SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
TID=\$($SUDO $(tgtadm_next_tid))
[ -z "\$TID" ] && TID=1
$SUDO $(tgtadm_target_new "\$TID" "$IQN")
$SUDO $(tgtadm_target_bind_all "\$TID")
$SUDO $(tgtadm_logicalunit_new "\$TID" "$DEV")
$SUDO $(tgt_setup_lun "$IQN" "$DEV")
$SUDO $(tgt_admin_dump_config "$TARGET_CONF")
EOF
)
@ -123,7 +120,7 @@ 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" \
exec_and_log "eval $DUMP | $SSH $DST_HOST $SUDO $DD of=$DEV bs=2M" \
"Error dumping $SRC to $DST_HOST:$DEV"
echo "$IQN"

View File

@ -66,6 +66,9 @@ BASE_IQN="${XPATH_ELEMENTS[6]:-$BASE_IQN}"
FSTYPE="${XPATH_ELEMENTS[7]}"
SIZE="${XPATH_ELEMENTS[8]:-0}"
# Check if 'TGTSETUPLUN' is installed
tgt_setup_lun_install "$DST_HOST" "$BASE_PATH"
set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK"
LV_NAME="lv-one-${ID}"
@ -76,12 +79,7 @@ REGISTER_CMD=$(cat <<EOF
set -e
$SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
TID=\$($SUDO $(tgtadm_next_tid))
[ -z "\$TID" ] && TID=1
$SUDO $(tgtadm_target_new "\$TID" "$IQN")
$SUDO $(tgtadm_target_bind_all "\$TID")
$SUDO $(tgtadm_logicalunit_new "\$TID" "$DEV")
$SUDO $(tgt_setup_lun "$IQN" "$DEV")
$SUDO $(tgt_admin_dump_config "$TARGET_CONF")
if [ "$FSTYPE" != "save_as" ]; then

View File

@ -44,6 +44,7 @@ SYNC=sync
TAR=tar
TGTADM=tgtadm
TGTADMIN=tgt-admin
TGTSETUPLUN=tgt-setup-lun-one
VMKFSTOOLS=vmkfstools
WGET=wget
@ -349,12 +350,12 @@ function tgtadm_target_delete {
function tgtadm_get_tid_for_iqn {
IQN="$1"
echo "$TGTADM --lld iscsi --op show --mode target | \
echo "$TGTADM --lld iscsi --op show --mode target | strings | \
grep \"$IQN\" | awk '{split(\$2,tmp,\":\"); print(tmp[1]);}'"
}
function tgtadm_next_tid {
echo "$TGTADM --lld iscsi --op show --mode target | \
echo "$TGTADM --lld iscsi --op show --mode target | strings | \
$GREP \"Target\" | tail -n 1 | \
$AWK '{split(\$2,tmp,\":\"); print tmp[1]+1;}'"
}
@ -402,6 +403,30 @@ function iqn_get_vg_name {
echo $TARGET|$AWK -F. '{print $(NF-1)}'
}
function tgt_setup_lun_install {
DST_HOST="$1"
BASE_PATH="$2"
CHECK_FILE="$BASE_PATH/.tgt-setup-lun"
if [ ! -f "$CHECK_FILE" ]; then
$SSH "$DST_HOST" "$SUDO $TGTSETUPLUN" 2>&1 | \
$GREP -q "command not found"
if [ "$?" = "0" ]; then
error_message "$TGTSETUPLUN is not installed in $DST_HOST."
exit 127
else
touch "$CHECK_FILE"
fi
fi
}
function tgt_setup_lun {
IQN="$1"
DEV="$2"
echo "$TGTSETUPLUN -d $DEV -n $IQN 1>&2"
}
function iqn_get_host {
IQN="$1"
TARGET=`echo "$IQN"|$CUT -d: -f2`
@ -426,10 +451,10 @@ function vmfs_set_up {
if [ "$USE_SSH" != "yes" ]; then
USERNAME=`echo $(cat $VMWARERC |grep ":username:"|cut -d":" -f 3|tr -d '"')`
PASSWORD=`echo $(cat $VMWARERC |grep ":password:"|cut -d":" -f 3|tr -d '"')`
if [ -z $PASSWORD ]; then
VI_PARAMS="--server $DST_HOST --username $USERNAME --password \"\""
else
VI_PARAMS="--server $DST_HOST --username $USERNAME --password $PASSWORD"
if [ -z $PASSWORD ]; then
VI_PARAMS="--server $DST_HOST --username $USERNAME --password \"\""
else
VI_PARAMS="--server $DST_HOST --username $USERNAME --password $PASSWORD"
fi
fi
}

View File

@ -81,14 +81,10 @@ CLONE_CMD=$(cat <<EOF
$SUDO $LVCREATE -L\${SIZE} ${VG_NAME} -n ${TARGET_LV_NAME}
# clone lv with dd
$SUDO $DD if=$SOURCE_DEV of=$TARGET_DEV bs=64k
$SUDO $DD if=$SOURCE_DEV of=$TARGET_DEV bs=2M
# new iscsi target
TID=\$($SUDO $(tgtadm_next_tid))
$SUDO $(tgtadm_target_new "\$TID" "$NEW_IQN")
$SUDO $(tgtadm_target_bind_all "\$TID")
$SUDO $(tgtadm_logicalunit_new "\$TID" "$TARGET_DEV")
$SUDO $(tgt_setup_lun "$NEW_IQN" "$TARGET_DEV")
$SUDO $(tgt_admin_dump_config "$TARGET_CONF")
EOF
)
@ -96,18 +92,27 @@ EOF
ssh_exec_and_log "$TARGET_HOST" "$CLONE_CMD" \
"Error cloning $DST_HOST:$TARGET_DEV"
DISCOVERY_CMD=$(cat <<EOF
LINK_CMD=$(cat <<EOF
set -e
# discover and login
mkdir -p $DST_DIR
$SUDO $(iscsiadm_discovery "$TARGET_HOST")
$SUDO $(iscsiadm_login "$NEW_IQN" "$TARGET_HOST")
sleep 2
# poll
while [ ! -L /dev/disk/by-path/*$NEW_IQN-lun-1 ]; do
sleep 1
done
# link
DISK_BY_PATH=\$(ls /dev/disk/by-path/*$NEW_IQN-lun-1)
ln -s "\$DISK_BY_PATH" "$DST_PATH"
DISK_DEVNAME=\$(readlink -f \$DISK_BY_PATH | awk -F'/' '{ print \$NF }')
EOF
)
ssh_exec_and_log "$DST_HOST" "$DISCOVERY_CMD" \
"Error discovering $NEW_IQN in $DST_HOST"
ssh_exec_and_log "$DST_HOST" "$LINK_CMD" \
"Error linking $NEW_IQN in $DST_HOST"
exit 0

View File

@ -58,18 +58,26 @@ VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
TARGET_DEV="/dev/$VG_NAME/$LV_NAME"
TARGET_HOST=${TARGET%.$VG_NAME.$LV_NAME}
DISCOVER_CMD=$(cat <<EOF
LINK_CMD=$(cat <<EOF
set -e
# discover and login
mkdir -p $DST_DIR
$SUDO $(iscsiadm_discovery "$TARGET_HOST")
$SUDO $(iscsiadm_login "$IQN" "$TARGET_HOST")
sleep 2
# poll
while [ ! -L /dev/disk/by-path/*$IQN-lun-1 ]; do
sleep 1
done
# link
DISK_BY_PATH=\$(ls /dev/disk/by-path/*$IQN-lun-1)
ln -s "\$DISK_BY_PATH" "$DST_PATH"
EOF
)
ssh_exec_and_log "$DST_HOST" "$DISCOVER_CMD" \
"Error registering $DST_HOST:$DEV"
ssh_exec_and_log "$DST_HOST" "$LINK_CMD" \
"Error linking $DST_HOST:$TARGET_DEV"
exit 0

View File

@ -114,19 +114,29 @@ if is_iscsi "$DST_HOST"; then
TARGET_HOST=$(iqn_get_host "$IQN")
LOGIN_CMD=$(cat <<EOF
set -e
mkdir -p $DST_DIR
$SUDO $(iscsiadm_discovery "$TARGET_HOST")
$SUDO $(iscsiadm_login "$IQN" "$TARGET_HOST")
LINK_CMD=$(cat <<EOF
set -e
# discover and login
mkdir -p $DST_DIR
$SUDO $(iscsiadm_discovery "$TARGET_HOST")
$SUDO $(iscsiadm_login "$IQN" "$TARGET_HOST")
# poll
while [ ! -L /dev/disk/by-path/*$IQN-lun-1 ]; do
sleep 1
DISK_BY_PATH=\$(ls /dev/disk/by-path/*$IQN-lun-1)
ln -sf "\$DISK_BY_PATH" "$DST_PATH"
done
# link
DISK_BY_PATH=\$(ls /dev/disk/by-path/*$IQN-lun-1)
ln -sf "\$DISK_BY_PATH" "$DST_PATH"
EOF
)
ssh_exec_and_log "$DST_HOST" "$LOGIN_CMD" \
"Error logging in $IQN"
ssh_exec_and_log "$DST_HOST" "$LINK_CMD" \
"Error linking to $DISK_BY_PATH $DST_PATH"
fi
exit 0

View File

@ -101,7 +101,7 @@ CLONE_CMD=$(cat <<EOF
set -e
# clone lv with dd
$SUDO $DD if=$SOURCE_DEV of=$TARGET_DEV bs=64k
$SUDO $DD if=$SOURCE_DEV of=$TARGET_DEV bs=2M
# remove if source_dev is not persistent
if [ -z "$PERSISTENT" ]; then