summaryrefslogtreecommitdiffstats
path: root/storage/zfcp.py
blob: 25f90b9cc16ded9dcbeaf779853cceda24bd17c6 (plain)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#
# zfcp.py - mainframe zfcp configuration install data
#
# Copyright (C) 2001, 2002, 2003, 2004  Red Hat, Inc.  All rights reserved.
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#
# Author(s): Karsten Hopp <karsten@redhat.com>
#

import string
import os
from constants import *
from udev import udev_settle

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

import logging
log = logging.getLogger("anaconda")
import warnings

def loggedWriteLineToFile(fn, value):
    f = open(fn, "w")
    log.debug("echo %s > %s" % (value, fn))
    f.write("%s\n" % (value))
    f.close()

zfcpsysfs = "/sys/bus/ccw/drivers/zfcp"
scsidevsysfs = "/sys/bus/scsi/devices"

class ZFCPDevice:
    def __init__(self, devnum, wwpn, fcplun):
        self.devnum = self.sanitizeDeviceInput(devnum)
        self.wwpn = self.sanitizeWWPNInput(wwpn)
        self.fcplun = self.sanitizeFCPLInput(fcplun)

        if not self.checkValidDevice(self.devnum):
            raise ValueError, _("You have not specified a device number or the number is invalid")
        if not self.checkValidWWPN(self.wwpn):
            raise ValueError, _("You have not specified a worldwide port name or the name is invalid.")
        if not self.checkValidFCPLun(self.fcplun):
            raise ValueError, _("You have not specified a FCP LUN or the number is invalid.")

    def __str__(self):
        return "%s %s %s" %(self.devnum, self.wwpn, self.fcplun)

    def sanitizeDeviceInput(self, dev):
        if dev is None or dev == "":
            return None
        dev = dev.lower()
        bus = dev[:string.rfind(dev, ".") + 1]
        dev = dev[string.rfind(dev, ".") + 1:]
        dev = "0" * (4 - len(dev)) + dev
        if not len(bus):
            return "0.0." + dev
        else:
            return bus + dev

    def sanitizeWWPNInput(self, id):
        if id is None or id == "":
            return None
        id = id.lower()
        if id[:2] != "0x":
            return "0x" + id
        return id

    # ZFCP LUNs are usually entered as 16 bit, sysfs accepts only 64 bit 
    # (#125632), expand with zeroes if necessary
    def sanitizeFCPLInput(self, lun):
        if lun is None or lun == "":
            return None
        lun = lun.lower()
        if lun[:2] == "0x":
            lun = lun[2:]
        lun = "0x" + "0" * (4 - len(lun)) + lun
        lun = lun + "0" * (16 - len(lun) + 2)
        return lun

    def _hextest(self, hex):
        try:
            int(hex, 16)
            return True
        except TypeError:
            return False

    def checkValidDevice(self, id):
        if id is None or id == "":
            return False
        if len(id) != 8:             # p.e. 0.0.0600
            return False
        if id[0] not in string.digits or id[2] not in string.digits:
            return False
        if id[1] != "." or id[3] != ".":
            return False
        return self._hextest(id[4:])

    def checkValid64BitHex(self, hex):
        if hex is None or hex == "":
            return False
        if len(hex) != 18:
            return False
        return self._hextest(hex)
    checkValidWWPN = checkValidFCPLun = checkValid64BitHex

    def onlineDevice(self):
        online = "%s/%s/online" %(zfcpsysfs, self.devnum)
        portadd = "%s/%s/port_add" %(zfcpsysfs, self.devnum)
        portdir = "%s/%s/%s" %(zfcpsysfs, self.devnum, self.wwpn)
        unitadd = "%s/unit_add" %(portdir)
        unitdir = "%s/%s" %(portdir, self.fcplun)
        failed = "%s/failed" %(unitdir)

        try:
            if not os.path.exists(online):
                loggedWriteLineToFile("/proc/cio_ignore",
                                      "free %s" %(self.devnum,))
                udev_settle()
        except IOError as e:
            raise ValueError, _(
                "Could not free zFCP device %s from device ignore list (%s)."
                %(self.devnum, e))

        if not os.path.exists(online):
            raise ValueError, _(
                "zFCP device %s not found, not even in device ignore list."
                %(self.devnum,))

        try:
            f = open(online, "r")
            devonline = f.readline().strip()
            f.close()
            if devonline != "1":
                loggedWriteLineToFile(online, "1")
            else:
                log.info("zFCP device %s already online." %(self.devnum,))
        except IOError as e:
            raise ValueError, _(
                "Could not set zFCP device %s online (%s)."
                %(self.devnum, e))

        if not os.path.exists(portdir):
            if os.path.exists(portadd):
                # older zfcp sysfs interface
                try:
                    loggedWriteLineToFile(portadd, self.wwpn)
                    udev_settle()
                except IOError as e:
                    raise ValueError, _(
                        "Could not add WWPN %s to zFCP device %s (%s)."
                        %(self.wwpn, self.devnum, e))
            else:
                # newer zfcp sysfs interface with auto port scan
                raise ValueError, _("WWPN %s not found at zFCP device %s."
                                    %(self.wwpn, self.devnum))
        else:
            if os.path.exists(portadd):
                # older zfcp sysfs interface
                log.info("WWPN %s at zFCP device %s already there."
                         %(self.wwpn, self.devnum))

        if not os.path.exists(unitdir):
            try:
                loggedWriteLineToFile(unitadd, self.fcplun)
                udev_settle()
            except IOError as e:
                raise ValueError, _(
                    "Could not add LUN %s to WWPN %s on zFCP device %s (%s)."
                    %(self.fcplun, self.wwpn, self.devnum, e))
        else:
            raise ValueError, _(
                "LUN %s at WWPN %s on zFCP device %s already configured."
                %(self.fcplun, self.wwpn, self.devnum))

        fail = "0"
        try:
            f = open(failed, "r")
            fail = f.readline().strip()
            f.close()
        except IOError as e:
            raise ValueError, _(
                "Could not read failed attribute of LUN %s at WWPN %s on zFCP device %s (%s)."
                %(self.fcplun, self.wwpn, self.devnum, e))
        if fail != "0":
            self.offlineDevice()
            raise ValueError, _(
                "Failed LUN %s at WWPN %s on zFCP device %s removed again."
                %(self.fcplun, self.wwpn, self.devnum))

        return True

    def offlineSCSIDevice(self):
        f = open("/proc/scsi/scsi", "r")
        lines = f.readlines()
        f.close()
        # alternatively iterate over /sys/bus/scsi/devices/*:0:*:*/

        for line in lines:
            if not line.startswith("Host"):
                continue
            scsihost = string.split(line)
            host = scsihost[1]
            channel = "0"
            id = scsihost[5]
            lun = scsihost[7]
            scsidev = "%s:%s:%s:%s" % (host[4:], channel, id, lun)
            fcpsysfs = "%s/%s" % (scsidevsysfs, scsidev)
            scsidel = "%s/%s/delete" % (scsidevsysfs, scsidev)

            f = open("%s/hba_id" %(fcpsysfs), "r")
            fcphbasysfs = f.readline().strip()
            f.close()
            f = open("%s/wwpn" %(fcpsysfs), "r")
            fcpwwpnsysfs = f.readline().strip()
            f.close()
            f = open("%s/fcp_lun" %(fcpsysfs), "r")
            fcplunsysfs = f.readline().strip()
            f.close()

            if fcphbasysfs == self.devnum \
                    and fcpwwpnsysfs == self.wwpn \
                    and fcplunsysfs == self.fcplun:
                loggedWriteLineToFile(scsidel, "1")
                udev_settle()
                return

        log.warn("no scsi device found to delete for zfcp %s %s %s"
                 %(self.devnum, self.wwpn, self.fcplun))

    def offlineDevice(self):
        offline = "%s/%s/online" %(zfcpsysfs, self.devnum)
        portadd = "%s/%s/port_add" %(zfcpsysfs, self.devnum)
        portremove = "%s/%s/port_remove" %(zfcpsysfs, self.devnum)
        unitremove = "%s/%s/%s/unit_remove" %(zfcpsysfs, self.devnum, self.wwpn)
        portdir = "%s/%s/%s" %(zfcpsysfs, self.devnum, self.wwpn)
        devdir = "%s/%s" %(zfcpsysfs, self.devnum)

        try:
            self.offlineSCSIDevice()
        except IOError as e:
            raise ValueError, _(
                "Could not correctly delete SCSI device of zFCP %s %s %s (%s)."
                %(self.devnum, self.wwpn, self.fcplun, e))

        try:
            loggedWriteLineToFile(unitremove, self.fcplun)
        except IOError as e:
            raise ValueError, _(
                "Could not remove LUN %s at WWPN %s on zFCP device %s (%s)."
                %(self.fcplun, self.wwpn, self.devnum, e))

        if os.path.exists(portadd):
            # only try to remove ports with older zfcp sysfs interface
            for lun in os.listdir(portdir):
                if lun.startswith("0x") and \
                        os.path.isdir(os.path.join(portdir, lun)):
                    log.info("Not removing WWPN %s at zFCP device %s since port still has other LUNs, e.g. %s."
                             %(self.wwpn, self.devnum, lun))
                    return True

            try:
                loggedWriteLineToFile(portremove, self.wwpn)
            except IOError as e:
                raise ValueError, _("Could not remove WWPN %s on zFCP device %s (%s)."
                                    %(self.wwpn, self.devnum, e))

        if os.path.exists(portadd):
            # older zfcp sysfs interface
            for port in os.listdir(devdir):
                if port.startswith("0x") and \
                        os.path.isdir(os.path.join(devdir, port)):
                    log.info("Not setting zFCP device %s offline since it still has other ports, e.g. %s."
                             %(self.devnum, port))
                    return True
        else:
            # newer zfcp sysfs interface with auto port scan
            import glob
            luns = glob.glob("%s/0x????????????????/0x????????????????"
                          %(devdir,))
            if len(luns) != 0:
                log.info("Not setting zFCP device %s offline since it still has other LUNs, e.g. %s."
                         %(self.devnum, luns[0]))
                return True

        try:
            loggedWriteLineToFile(offline, "0")
        except IOError as e:
            raise ValueError, _("Could not set zFCP device %s offline (%s)."
                                %(self.devnum, e))

        return True

class ZFCP:
    def __init__(self):
        self.fcpdevs = []
        self.hasReadConfig = False
        self.down = True

    def readConfig(self):
        try:
            f = open("/tmp/fcpconfig", "r")
        except IOError:
            log.info("no /tmp/fcpconfig; not configuring zfcp")
            return

        lines = f.readlines()
        f.close()
        for line in lines:
            # each line is a string separated list of values to describe a dev
            # there are two valid formats for the line:
            #   devnum scsiid wwpn scsilun fcplun    (scsiid + scsilun ignored)
            #   devnum wwpn fcplun
            line = string.strip(line).lower()
            if line.startswith("#"):
                continue
            fcpconf = string.split(line)
            if len(fcpconf) == 3:
                devnum = fcpconf[0]
                wwpn = fcpconf[1]
                fcplun = fcpconf[2]
            elif len(fcpconf) == 5:
                warnings.warn("SCSI ID and SCSI LUN values for ZFCP devices are ignored and deprecated.", DeprecationWarning)
                devnum = fcpconf[0]
                wwpn = fcpconf[2]
                fcplun = fcpconf[4]
            else:
                log.warn("Invalid line found in /tmp/fcpconfig!")
                continue

            try:
                self.addFCP(devnum, wwpn, fcplun)
            except ValueError, e:
                log.warn(str(e))
                continue

    def addFCP(self, devnum, wwpn, fcplun):
        d = ZFCPDevice(devnum, wwpn, fcplun)
        if d.onlineDevice():
            self.fcpdevs.append(d)

    def shutdown(self):
        if self.down:
            return
        self.down = True
        if len(self.fcpdevs) == 0:
            return
        for d in self.fcpdevs:
            try:
                d.offlineDevice()
            except ValueError, e:
                log.warn(str(e))

    def startup(self):
        if not self.down:
            return
        self.down = False
        if not self.hasReadConfig:
            self.readConfig()
            self.hasReadConfig = True
            # readConfig calls addFCP which calls onlineDevice already
            return
            
        if len(self.fcpdevs) == 0:
            return
        for d in self.fcpdevs:
            try:
                d.onlineDevice()
            except ValueError, e:
                log.warn(str(e))

    def writeKS(self, f):
        if len(self.fcpdevs) == 0:
            return
        for d in self.fcpdevs:
            f.write("zfcp --devnum %s --wwpn %s --fcplun %s\n" %(d.devnum,
                                                                 d.wwpn,
                                                                 d.fcplun))

    def write(self, instPath):
        if len(self.fcpdevs) == 0:
            return
        f = open(instPath + "/etc/zfcp.conf", "w")
        for d in self.fcpdevs:
            f.write("%s\n" %(d,))
        f.close()
        
        f = open(instPath + "/etc/modprobe.conf", "a")
        f.write("alias scsi_hostadapter zfcp\n")
        f.close()

# vim:tw=78:ts=4:et:sw=4