integ/base/tis-extensions/files/target
Dean Troyer 3cd12006bb StarlingX open source release updates
Signed-off-by: Dean Troyer <dtroyer@gmail.com>
2018-05-31 07:36:35 -07:00

291 lines
6.7 KiB
Bash

#!/bin/bash
#
# Filename: /etc/init.d/target
#
# Bring up/down iscsi LIO target
#
#########################################################################
#
#
# Copyright (c) 2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
DESC="target"
STATUS_FILE="/var/run/lio-target.running"
CINDER_CONF_DIR="/opt/cgcs/cinder"
LIO_CONF_DIR="${CINDER_CONF_DIR}/iscsi-target"
LIO_CONF_FILE="${LIO_CONF_DIR}/saveconfig.json"
TGT_CONF_FILE="${CINDER_CONF_DIR}/data/tgt-initiators-15.12-upgrade.conf"
BASE_TARGET_DIR="/etc/target"
# Tools
TARGETCLI="/usr/bin/targetcli"
RTSTOOL="/usr/bin/cinder-rtstool"
SM_QUERY="/usr/bin/sm-query"
# This will log to /var/log/platform.log
NAME=$(basename $0)
function log () {
logger -p local1.info "${NAME}: $1"
}
# Determine whether we are running on the active controller.
# Return value: 0 - controller is active, 1 - not active.
is_active_controller () {
# service drbd-cgcs - should be active before iscsi one
local SERVICE="drbd-cgcs"
local ACTIVE=$(${SM_QUERY} service ${SERVICE} | \
grep enabled-active)
if [ -z "${ACTIVE}" ] ; then
return 1
else
return 0
fi
}
# This is a function that migrates 15.12 TGT Target configuration
# to the LIO Target configuration file. The function is called only
# once on the first "swact" to the LIO controller after
# Software Upgrade.
migrate_tgt () {
log "Migrating 15.12 TGT iSCSI Target to LIO"
# TGT configuration directory
TGT_CONF_DIR="/opt/cgcs/cinder/data/volumes"
# Start the LIO target and enable it for configuration
/usr/bin/targetctl restore
if [ $? -ne 0 ] ; then
log "ERROR: trying to start the LIO target"
return 1
fi
if [ ! -d ${TGT_CONF_DIR} ] ; then
# User does not have attached volumes
log "No volumes to migrate. Migration is done"
return 0
fi
if [ ! "$(ls -A ${TGT_CONF_DIR})" ] ; then
# Cinder volumes configuration is empty
log "No volumes to migrate. Migration is done"
return 0
fi
PORTAL_IP=""
if [ -e /etc/hosts ] ; then
PORTAL_IP=$(grep controller-cinder /etc/hosts | awk '{print $1}')
fi
# If no Portal IP is specified, use default IP address
if [ -z "${PORTAL_IP}" ] ; then
PORTAL_IP="0.0.0.0"
fi
for volume in `find ${TGT_CONF_DIR} -name "volume-*"`
do
TARGET_NAME=$(grep -n target ${volume} | awk '{ print $2}' | \
sed 's/>//')
DEVICE=$(grep -n backing-store ${volume} | awk '{print $3}')
USERID=$(grep -n incominguser ${volume} | awk '{print $3}')
PASSWORD=$(grep -n incominguser ${volume} | awk '{print $4}')
INITIATOR=$(grep -n ${TARGET_NAME} ${TGT_CONF_FILE} | \
awk '{ print $5 }')
if [ ! "${TARGET_NAME}" -a "${DEVICE}" -a "${USERID}" -a \
"${PASSWORD}" -a "${INITIATOR}" ] ; then
log "ERROR: volume ${TARGET_NAME} configuration is not complete"
continue
fi
# Add the volume to the LIO configuration
${RTSTOOL} create ${DEVICE} ${TARGET_NAME} ${USERID} ${PASSWORD} \
False -a${PORTAL_IP} -p3260 2>/dev/null
if [ $? -ne 0 ] ; then
log "ERROR: Target creation failed for volume ${TARGET_NAME}"
continue
fi
log "Created target ${TARGET_NAME}"
${RTSTOOL} add-initiator ${TARGET_NAME} ${USERID} ${PASSWORD} \
${INITIATOR} 2>/dev/null
if [ $? -ne 0 ] ; then
log "ERROR: Add Initiator ${INITIATOR} failed for ${TARGET_NAME}"
continue
fi
log "Added Initiator ${INITIATOR} for ${TARGET_NAME}"
# Create a lun mapping from 1 to 0. Why? Well 15.12 used tgt which
# uses lun 1 as the first volume, but lio uses lun 0. This mapping
# allows iscsi references created in the old tgt realm to continue
# to work.
${TARGETCLI} "/iscsi/${TARGET_NAME}/tpg1/acls/${INITIATOR} create 1 0"
if [ $? -ne 0 ] ; then
log "ERROR: lun 1 mapping failed: Initiator ${INITIATOR} Target ${TARGET_NAME}"
else
log "Added lun 1 mapping: Initiator ${INITIATOR} Target ${TARGET_NAME}"
fi
done
# Save and verify the new LIO configuration
${RTSTOOL} save 2> /dev/null
if [ $? -ne 0 ] ; then
log "ERROR: Cannot save LIO Target configuration"
return 1
fi
${RTSTOOL} verify 2> /dev/null
if [ $? -ne 0 ] ; then
log "ERROR: LIO Target verification failed"
return 1
fi
log "TGT to LIO migration is done"
return 0
}
start () {
echo -n "Starting ${DESC}..."
if ! is_active_controller ; then
echo "failed. Controller is not active."
exit 1
fi
if [ ! -L ${BASE_TARGET_DIR} ] ; then
rm -rf ${BASE_TARGET_DIR}
ln -s ${LIO_CONF_DIR} ${BASE_TARGET_DIR}
fi
if [ ! -d ${LIO_CONF_DIR} ] ; then
# Create LIO configuration directory
mkdir -p ${LIO_CONF_DIR} && log "Created ${LIO_CONF_DIR}"
# Create default LIO configuration file
${TARGETCLI} saveconfig ${LIO_CONF_FILE}
fi
if [ -e ${TGT_CONF_FILE} -a -s ${TGT_CONF_FILE} ] ; then
migrate_tgt
if [ $? -ne 0 ] ; then
echo "tgt migration failed."
exit 1
fi
mv -f ${TGT_CONF_FILE} ${TGT_CONF_FILE}.bak
else
/usr/bin/targetctl restore
if [ $? -ne 0 ] ; then
echo "failed."
exit 1
fi
fi
touch ${STATUS_FILE}
echo "done."
}
stop () {
echo -n "Stopping ${DESC}..."
if [ ! -f ${STATUS_FILE} ] ; then
echo "service has not been started"
exit 0
fi
/usr/bin/targetctl clear
RET=$?
if [ -f ${STATUS_FILE} ] ; then
rm -f ${STATUS_FILE}
fi
if [ ${RET} -ne 0 ] ; then
echo "failed."
exit 1
fi
echo -n
}
status()
{
if [ ! -f ${STATUS_FILE} ] ; then
echo "${DESC} has not been started"
exit 3
else
echo "${DESC} had been started"
fi
}
restart () {
stop
start
}
reload()
{
if [ ! -f ${STATUS_FILE} ] ; then
echo "${DESC} has not been started"
exit 1
fi
/usr/bin/targetctl restore
if [ $? -ne 0 ] ; then
echo "failed."
exit 1
fi
}
case "$1" in
start)
start
;;
status)
status
;;
stop|forcedstop)
stop
;;
reload|force-reload)
reload
;;
restart|try-restart)
restart
;;
*)
echo $"Usage: $0 {start|status|stop|forcedstop|restart|try-restart|reload|force-reload}"
exit 1
;;
esac
exit 0