This file is indexed.

/usr/bin/cinder-rtstool is in cinder-common 1:2014.1-0ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#! /usr/bin/python
# vim: et tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 - 2013 Red Hat, Inc.
# All Rights Reserved.
#
#    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.

import gettext
import re
import sys

import rtslib

gettext.install('cinder-rtstool', unicode=1)


class RtstoolError(Exception):
    pass


class RtstoolImportError(RtstoolError):
    pass


def create(backing_device, name, userid, password, initiator_iqns=None):
    try:
        rtsroot = rtslib.root.RTSRoot()
    except rtslib.utils.RTSLibError:
        print(_('Ensure that configfs is mounted at /sys/kernel/config.'))
        raise

    # Look to see if BlockStorageObject already exists
    for x in rtsroot.storage_objects:
        if x.dump()['name'] == name:
            # Already exists, use this one
            return

    so_new = rtslib.BlockStorageObject(name=name,
                                       dev=backing_device)

    target_new = rtslib.Target(rtslib.FabricModule('iscsi'), name, 'create')

    tpg_new = rtslib.TPG(target_new, mode='create')
    tpg_new.set_attribute('authentication', '1')

    lun_new = rtslib.LUN(tpg_new, storage_object=so_new)

    initiator_name = None
    name_file = '/etc/iscsi/initiatorname.iscsi'

    try:
        with open(name_file, 'r') as f:
            for line in f:
                m = re.match('InitiatorName=(.+)', line)
                if m != None:
                    initiator_name = m.group(1)
                    break
    except IOError:
        raise RtstoolError(_('Could not open %s') % name_file)

    if initiator_name == None:
        raise RtstoolError(_('Could not read InitiatorName from %s') %
                           name_file)

    acl_new = rtslib.NodeACL(tpg_new, initiator_name, mode='create')

    acl_new.chap_userid = userid
    acl_new.chap_password = password

    rtslib.MappedLUN(acl_new, lun_new.lun, lun_new.lun)

    if initiator_iqns:
        initiator_iqns = initiator_iqns.strip(' ')
        for i in initiator_iqns.split(','):
            acl_new = rtslib.NodeACL(tpg_new, i, mode='create')
            acl_new.chap_userid = userid
            acl_new.chap_password = password

            rtslib.MappedLUN(acl_new, lun_new.lun, lun_new.lun)

    tpg_new.enable = 1

    try:
        rtslib.NetworkPortal(tpg_new, '0.0.0.0', 3260, mode='any')
    except rtslib.utils.RTSLibError:
        print(_('Error creating NetworkPortal: ensure port 3260 '
                'is not in use by another service.'))
        raise

    try:
        rtslib.NetworkPortal(tpg_new, '::0', 3260, mode='any')
    except rtslib.utils.RTSLibError:
        # TODO(emh): Binding to IPv6 fails sometimes -- let pass for now.
        pass


def add_initiator(target_iqn, initiator_iqn, userid, password):
    try:
        rtsroot = rtslib.root.RTSRoot()
    except rtslib.utils.RTSLibError:
        print(_('Ensure that configfs is mounted at /sys/kernel/config.'))
        raise

    # Look for the target
    target = None
    for t in rtsroot.targets:
        if t.dump()['wwn'] == target_iqn:
            target = t
            break
    if target == None:
        raise RtstoolError(_('Could not find target %s') % target_iqn)

    tpg = target.tpgs.next()  # get the first one
    for acl in tpg.dump()['node_acls']:
        # See if this ACL configuration already exists
        if acl['node_wwn'] == initiator_iqn:
            # No further action required
            return

    acl_new = rtslib.NodeACL(tpg, initiator_iqn, mode='create')
    acl_new.chap_userid = userid
    acl_new.chap_password = password

    rtslib.MappedLUN(acl_new, 0, tpg_lun=0)


def get_targets():
    rtsroot = rtslib.root.RTSRoot()
    for x in rtsroot.targets:
        print(x.dump()['wwn'])


def delete(iqn):
    rtsroot = rtslib.root.RTSRoot()
    for x in rtsroot.targets:
        if x.dump()['wwn'] == iqn:
            x.delete()
            break

    for x in rtsroot.storage_objects:
        if x.dump()['name'] == iqn:
            x.delete()
            break


def verify_rtslib():
    for member in ['BlockStorageObject', 'FabricModule', 'LUN',
                   'MappedLUN', 'NetworkPortal', 'NodeACL', 'root',
                   'Target', 'TPG']:
        if not hasattr(rtslib, member):
            raise RtstoolImportError(_("rtslib is missing member %s: "
                                       "You may need a newer python-rtslib.") %
                                     member)


def usage():
    print("Usage:")
    print(sys.argv[0] +
          " create [device] [name] [userid] [password]" +
          " <initiator_iqn,iqn2,iqn3,...>")
    print(sys.argv[0] +
          " add-initiator [target_iqn] [userid] [password] [initiator_iqn]")
    print(sys.argv[0] + " get-targets")
    print(sys.argv[0] + " delete [iqn]")
    print(sys.argv[0] + " verify")
    sys.exit(1)


def main(argv=None):
    if argv is None:
        argv = sys.argv

    if len(argv) < 2:
        usage()

    if argv[1] == 'create':
        if len(argv) < 6:
            usage()

        if len(argv) > 7:
            usage()

        backing_device = argv[2]
        name = argv[3]
        userid = argv[4]
        password = argv[5]
        initiator_iqns = None

        if len(argv) > 6:
            initiator_iqns = argv[6]

        create(backing_device, name, userid, password, initiator_iqns)

    elif argv[1] == 'add-initiator':
        if len(argv) < 6:
            usage()

        target_iqn = argv[2]
        userid = argv[3]
        password = argv[4]
        initiator_iqn = argv[5]

        add_initiator(target_iqn, initiator_iqn, userid, password)

    elif argv[1] == 'get-targets':
        get_targets()

    elif argv[1] == 'delete':
        if len(argv) < 3:
            usage()

        iqn = argv[2]
        delete(iqn)

    elif argv[1] == 'verify':
        # This is used to verify that this script can be called by cinder,
        # and that rtslib is new enough to work.
        verify_rtslib()
        return 0

    else:
        usage()

    return 0

if __name__ == '__main__':
    sys.exit(main())