-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCSTLun
More file actions
378 lines (378 loc) · 13.2 KB
/
SCSTLun
File metadata and controls
378 lines (378 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
#
#
# SCSTLun OCF RA. Exports and manages SCST iSCSI Logical Units.
#
# (c) 2012 Riccardo Bicelli
# and Linux-HA contributors
#
# Based on ISCSILogicalUnit from Florian Haas, Dejan Muhamedagic,
#
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like. Any license provided herein, whether implied or
# otherwise, applies only to this software file. Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
# Set force_close
OCF_RESKEY_force_close=true
: ${OCF_RESKEY_force_close=${OCF_RESKEY_force_close_default}}
#######################################################################
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="SCSTLun" version="0.2">
<version>1.0</version>
<longdesc lang="en">
Manages SCST iSCSI Logical Unit. An iSCSI Logical unit is a subdivision of
an SCSI Target, exported via a daemon that speaks the iSCSI protocol.
</longdesc>
<shortdesc lang="en">Manages iSCSI Logical Units (LUs)</shortdesc>
<parameters>
<parameter name="target_iqn" required="1" unique="0">
<longdesc lang="en">
The iSCSI Qualified Name (IQN) that this Logical Unit belongs to.
</longdesc>
<shortdesc lang="en">iSCSI target IQN</shortdesc>
<content type="string" />
</parameter>
<parameter name="device_name" required="1" unique="0">
<longdesc lang="en">
Device Name assigned in SCST. When using vdisk handlers it could be an
arbitrary name. When using other handlers (such as dev_tape or dev_changer) it
must be a pointer to device in form H:C:I:L.
</longdesc>
<shortdesc lang="en">Device Name Assigned in SCST</shortdesc>
<content type="string" />
</parameter>
<parameter name="lun" required="1" unique="0">
<longdesc lang="en">
The Logical Unit number (LUN) exposed to initiators.
</longdesc>
<shortdesc lang="en">Logical Unit number (LUN)</shortdesc>
<content type="integer" />
</parameter>
<parameter name="handler" required="0" unique="0">
<longdesc lang="en">
The handler used (vdisk_blockio, vdisk_fileio, dev_tape ...).</longdesc>
<shortdesc lang="en">Handler used</shortdesc>
<content type="string" />
</parameter>
<parameter name="scsi_id" required="0" unique="0">
<longdesc lang="en">
The t10 device ID of LUN. If not specified default SCST value will be used.
Please note that some initiators, like ESXi, are using only some of the first
characters to identify LUN, like 4-6 chars.
</longdesc>
<shortdesc lang="en">t10 device id</shortdesc>
<content type="integer" />
</parameter>
<parameter name="scsi_sn" required="0" unique="0">
<longdesc lang="en">
SCSI Serial Number
</longdesc>
<shortdesc lang="en">SCSI sn</shortdesc>
<content type="integer" />
</parameter>
<parameter name="path" required="0" unique="0">
<longdesc lang="en">
The path to the block device exposed. A regular file is allowed too.
</longdesc>
<shortdesc lang="en">Block device (or file) path</shortdesc>
<content type="string" />
</parameter>
<parameter name="force_close" required="0" unique="0">
<longdesc lang="en">
When stopping LUN, if is set to 1, force_close when the resource is
stopping tries to forcibily terminate ISCSI sessions with initiators
connected to the LUN.
Default to 1.
</longdesc>
<shortdesc lang="en">Forcibily Close Session during LUN stop</shortdesc>
<content type="boolean" default="true" />
</parameter>
<parameter name="additional_parameters" required="0" unique="0">
<longdesc lang="en">
Additional LU parameters. A space-separated list of "name=value" pairs
which will be passed through to the iSCSI daemon's management
interface. The supported parameters are implementation
dependent. Neither the name nor the value may contain whitespace.
</longdesc>
<shortdesc lang="en">List of iSCSI LU parameters</shortdesc>
<content type="string" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="15" />
<action name="stop" timeout="180" />
<action name="status" timeout="10" interval="10" depth="0" />
<action name="monitor" timeout="10" interval="10" depth="0" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="10" />
</actions>
</resource-agent>
END
}
# Initialization ######################################################
SYSFS_ROOTPATH="/sys/kernel/scst_tgt"
SYSFS_PATH="/sys/kernel/scst_tgt/targets/iscsi"
l_module=""
SCST_BASE="/sys/kernel/scst_tgt"
ISCSI_BASE="${SCST_BASE}/targets/iscsi"
#######################################################################
l_load_module () {
n=`lsmod | grep -c "^$1 "`
if [ $n -gt 0 ];
then
return 0
else
ocf_log info "Loading Kernel Module ${1}"
modprobe $1> /dev/null 2>&1 || return 1
return 0
fi
}
l_check_module () {
case "${OCF_RESKEY_handler}" in
dev_cdrom) l_module=scst_cdrom;;
dev_changer) l_module=scst_changer;;
dev_disk*) l_module=scst_disk;;
dev_modisk*) l_module=scst_modisk;;
dev_processor) l_module=scst_processor;;
dev_raid) l_module=scst_raid;;
dev_tape*) l_module=scst_tape;;
dev_user) l_module=scst_user;;
vdisk*|vcdrom) l_module=scst_vdisk;;
*) l_module=none;;
esac
}
l_start_handler () {
#Check Handler, then load module
l_check_module
l_load_module $l_module
}
l_stop_handler () {
local HANDLER_NOT_REQUIRED=true;
#Check if handler is used for other devices, then unload module.
for i in $( ls "${SYSFS_ROOTPATH}/handlers/${OCF_RESKEY_handler}" ) ; do
if [ -d ${SYSFS_ROOTPATH}/handlers/${OCF_RESKEY_handler}/${i} ]; then
HANDLER_NOT_REQUIRED=false
break
fi
done
if $HANDLER_NOT_REQUIRED ; then
ocf_log info "Handler ${OCF_RESKEY_handler} not required, unloading kernel module"
l_check_module
# rmmod $l_module
if [ $? -ne 0 ]; then
ocf_log warn "Unable to unload module ${l_module} for handler ${OCF_RESKEY_handler}. It is possible another handler is using same module."
fi
fi
return 0
}
SCSTLun_usage() {
cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}
SCSTLun_start() {
SCSTLun_monitor
if [ $? = $OCF_SUCCESS ]; then
return $OCF_SUCCESS
fi
local params
if [ ! ${OCF_RESKEY_path} == "" ]; then
params="filename=${OCF_RESKEY_path}"
fi
if [ ! ${OCF_RESKEY_additional_parameters} == "" ]; then
params="${params} ${OCF_RESKEY_additional_parameters}"
fi
ocf_log info "Disabling target ${OCF_RESKEY_target_iqn}"
#echo 0 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/enabled"
ocf_log info "Starting lun ${OCF_RESKEY_lun} on target ${OCF_RESKEY_target_iqn}"
# Load Handler Modules
l_start_handler #|| exit $OCF_ERR_GENERIC
# Open Device
ocf_log info "Opening device ${OCF_RESKEY_device_name}, target ${OCF_RESKEY_target_iqn}"
echo "del_device ${OCF_RESKEY_device_name}" > "${SCST_BASE}/handlers/${OCF_RESKEY_handler}/mgmt"
echo "add_device ${OCF_RESKEY_device_name} ${params// /;}" > "${SCST_BASE}/handlers/${OCF_RESKEY_handler}/mgmt"
if [ $? -ne 0 ]; then
ocf_log err "FAILED to open device ${OCF_RESKEY_device_name}"
return $OCF_ERR_GENERIC
fi
# Set SCSI SN and t10 dev id
if [ ! ${OCF_RESKEY_scsi_sn} == "" ]; then
ocf_log info "Setting SCSI S/N ${OCF_RESKEY_scsi_sn}"
echo "${OCF_RESKEY_scsi_sn}" > "${SCST_BASE}/devices/${OCF_RESKEY_device_name}/usn"
if [ $? -ne 0 ]; then
ocf_log warn "FAILED to set SCSI S/N!"
fi
if [ ! ${OCF_RESKEY_scsi_id} == "" ]; then
ocf_log info "Setting SCSI ID ${OCF_RESKEY_scsi_sn}-${OCF_RESKEY_scsi_id}"
echo "${OCF_RESKEY_scsi_id}" > "${SCST_BASE}/devices/${OCF_RESKEY_device_name}/t10_dev_id"
if [ $? -ne 0 ]; then
ocf_log warn "FAILED to set SCSI ID!"
fi
fi
fi
# Assign Device to the Target
ocf_log info "Adding LUN ${OCF_RESKEY_lun}, device ${OCF_RESKEY_device_name}, target ${OCF_RESKEY_target_iqn}"
echo "add ${OCF_RESKEY_device_name} ${OCF_RESKEY_lun}" > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/mgmt"
if [ $? -ne 0 ]; then
ocf_log err "FAILED to add lun ${OCF_RESKEY_lun}"
return $OCF_ERR_GENERIC
fi
#Enable target
ocf_log info "Enabling target ${OCF_RESKEY_target_iqn}"
echo 1 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/enabled"
ocf_log info "Started lun ${OCF_RESKEY_lun} on target ${OCF_RESKEY_target_iqn}"
echo 32 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/${OCF_RESKEY_lun}/device/threads_num"
echo 1024 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/${OCF_RESKEY_lun}/device/max_tgt_dev_commands"
#Debugging purpose
#scstadmin -write_config /tmp/scst.conf.start
# scstadmin -write_config /etc/scst.conf >/dev/null 2>&1
# /etc/init.d/scst restart >/dev/null 2>&1
return $OCF_SUCCESS
}
SCSTLun_stop() {
SCSTLun_monitor
if [ $? = $OCF_SUCCESS ]; then
ERR_REMOVE=0
ocf_log info "Stopping lun ${OCF_RESKEY_lun} on target ${OCF_RESKEY_target_iqn}"
# Disable Target
ocf_log info "Disabling target ${OCF_RESKEY_target_iqn}"
echo 0 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/enabled"
# Drop connections, only if session is using lun
if [ ${OCF_RESKEY_force_close} ]; then
for i in $( ls "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/sessions" ) ; do
if [ -d "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/sessions/${i}/luns/${OCF_RESKEY_lun}" ]; then
ocf_log warn "Force closing session to initiator ${i}"
echo 1 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/sessions/${i}/force_close"
fi
done
fi
#Remove LUN
i=1
ERR_REMOVE=1
while : #Let the timeout stop control the loop
do
if [ -d "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/${OCF_RESKEY_lun}" ]; then
#If The LUN Exists, try to remove it
ocf_log info "Removing LUN ${OCF_RESKEY_lun} from target ${OCF_RESKEY_target_iqn} (Try ${i})"
echo "del ${OCF_RESKEY_lun}" >${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/mgmt
sleep 1
else
break
fi
i=$[$i+1]
done
#Close Device
i=1
while :
do
if [ -d "${SCST_BASE}/devices/${OCF_RESKEY_device_name}" ];then
ocf_log info "Closing device ${OCF_RESKEY_device_name} (Try ${i})"
echo "del_device ${OCF_RESKEY_device_name}" > "${SCST_BASE}/handlers/vdisk_fileio/mgmt"
sleep 1
else
ERR_REMOVE=0
break
fi
i=$[$i+1]
done
if [ $ERR_REMOVE -eq 1 ]; then
ocf_log info "FAILED to remove device ${OCF_RESKEY_device_name}"
return $OCF_ERR_GENERIC
fi
# Enable Target
ocf_log info "Enabling target ${OCF_RESKEY_target_iqn}"
echo 1 > "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/enabled"
#Debugging purpose
#scstadmin -write_config /tmp/scst.conf.stop
#Stop Handler
l_stop_handler || exit $OCF_ERR_GENERIC
fi
# scstadmin -write_config /etc/scst.conf
# /etc/init.d/scst reload >/dev/null 2>&1
return $OCF_SUCCESS
}
SCSTLun_monitor() {
#Check if underlying device is configured
if [ ! -e "${SCST_BASE}/handlers/${OCF_RESKEY_handler}/${OCF_RESKEY_device_name}/usn" ]; then
# Check if lun is running.
ocf_log warn "Device ${OCF_RESKEY_device_name} is not configured"
fi
if [ -e "${ISCSI_BASE}/${OCF_RESKEY_target_iqn}/luns/${OCF_RESKEY_lun}" ]; then
return $OCF_SUCCESS
fi
return $OCF_NOT_RUNNING
}
SCSTLun_validate() {
# Do we have all required variables?
# TODO: make this check more precise considering handler used!
for var in target_iqn lun device_name handler; do
param="OCF_RESKEY_${var}"
if [ -z "${!param}" ]; then
ocf_log error "Missing resource parameter \"$var\"!"
exit $OCF_ERR_CONFIGURED
fi
done
if ! ocf_is_probe; then
# Do we have all required binaries?
check_binary scstadmin
# Is the required kernel functionality available?
if [ ! -d ${SCST_BASE} ]; then
ocf_log err "${SCST_BASE} does not exist or is not a directory -- check if required modules are loaded."
exit $OCF_ERR_INSTALLED
fi
fi
return $OCF_SUCCESS
}
case $1 in
meta-data)
meta_data
exit $OCF_SUCCESS
;;
usage|help)
SCSTLun_usage
exit $OCF_SUCCESS
;;
esac
# Everything except usage and meta-data must pass the validate test
SCSTLun_validate
case $__OCF_ACTION in
start) SCSTLun_start;;
stop) SCSTLun_stop;;
monitor) SCSTLun_monitor;;
*"status"*) SCSTLun_monitor;;
reload) ocf_log err "Reloading..."
SCSTLun_start
;;
validate-all) ;;
*) SCSTLun_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc