Skip to content

Commit

Permalink
tcm_restore: restore FC targets
Browse files Browse the repository at this point in the history
Signed-off-by: Hannes Reinecke <hare@suse.com>
  • Loading branch information
hreinecke committed Apr 25, 2017
1 parent ddf8e83 commit 39cfa4c
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions tcm_restore.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash

core=/sys/kernel/config/target/core
fc=/sys/kernel/config/target/fc

JSON=$1

apply_attributes () {
Expand Down Expand Up @@ -131,6 +133,101 @@ setup_iblock() {
echo "${pname}"
}

gen_fc_port_name() {
local port_num

port_num=${1#naa.}
for i in $(seq 1 2 13) ; do
j=$(expr $i + 1)
p=$(echo $port_num | cut --bytes=$i-$j)
printf "%02x:" $(( 16#$p ))
done
p=$(echo $port_num | cut --bytes=15-16)
printf "%02x\n" $(( 16#$p ))
}

setup_fc_luns() {
local jsonobj=$1
local tname=$2
local l
local v
local j
local idx lname storage_object iblockname backstore alua

l=0
while [ true ] ; do
j="${jsonobj}.luns[$l]"
v=$(cat $JSON | jq -c "${j}.index")
idx=$(eval echo $v)
[ "$idx" = "null" ] && break

lname=${tname}/lun/lun_${idx}
if [ ! -d ${lname} ] ; then
mkdir ${lname} || exit 1
fi
v=$(cat $JSON | jq -c "${j}.storage_object")
storage_object=$(eval echo $v)
case $storage_object in
/backstores/block/*)
iblockname=${storage_object##*/}
for d in ${core}/iblock_* ; do
[ -d $d ] || continue
if [ -d "$d/$iblockname" ] ; then
backstore="$d/$iblockname"
fi
done
;;
*)
backstore=
;;
esac
if [ -n "$backstore" ] ; then
v=$(cat $JSON | jq -c "${j}.alias")
alias=$(eval echo $v)
if [ "$alias" = "null" ] ; then
alias="mapped_lun"
fi
ln -s $backstore $lname/$alias
fi
v=$(cat $JSON | jq -c "${j}.alua_tg_pt_gp_name")
alua=$(eval echo $v)
if [ "$alua" != "null" ] && [ "$alua" != "default_tg_pt_gp" ] ; then
echo "$alua" > $tname/alua_tg_pt_gp
fi
(( l++ ))
done
}

setup_fc() {
local jsonobj=$1
local wwn
local t
local tpgt_id

if [ ! -d ${fc} ] ; then
mkdir ${fc} || exit 1
fi
wwn=$(gen_fc_port_name $2)
pname=${fc}/${wwn}
if [ ! -d ${pname} ] ; then
mkdir ${pname} || exit 1
fi

t=0
while [ true ] ; do
jo="${jsonobj}.tpgs[$t]"
j=$(cat $JSON | jq -c "${jo}")
[ $(eval echo $j) = "null" ] && return
tpgt_id=$(expr $t + 1)
tname="$pname/tpgt_${tpgt_id}"
if [ ! -d ${tname} ] ; then
mkdir ${tname} || exit 1
fi
setup_fc_luns $jo $tname
(( t++ ))
done
}

i=0
while [ true ] ; do
jsonobj=".storage_objects[$i]"
Expand All @@ -154,3 +251,23 @@ while [ true ] ; do
fi
(( i++ ))
done

i=0
while [ true ] ; do
jsonobj=".targets[$i]"
f=$(cat $JSON | jq -c "${jsonobj}.fabric")
fabric=$(eval echo $f)
case "$fabric" in
tcm_fc)
w=$(cat $JSON | jq -c "${jsonobj}.wwn")
wwn=$(eval echo $w)
if [ "$wwn" != "null" ] ; then
echo "Setting up FC WWN $wwn"
setup_fc ${jsonobj} $wwn
fi
;;
null)
break
esac
(( i++ ))
done

0 comments on commit 39cfa4c

Please sign in to comment.