summaryrefslogtreecommitdiffstats
path: root/iscsi.py
blob: 8ac050f2245969409f8491b6f1fc2f6553cf434f (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
#
# iscsi.py - iscsi class
#
# Copyright 2005, 2006 IBM, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import string
import signal
import iutil
from flags import flags
import logging
import shutil
import time
log = logging.getLogger("anaconda")

from rhpl.translate import _, N_

# Note that stage2 copies all files under /sbin to /usr/sbin
ISCSID="/usr/sbin/iscsid"
ISCSIADM = "/usr/sbin/iscsiadm"
INITIATOR_FILE="/etc/iscsi/initiatorname.iscsi"

class iscsi:
    def __init__(self):
        self.targets = []
        self.initiator = ""
        self.iscsidStarted = False


    def action(self, action, ipaddr = None):
        #
        # run action for all iSCSI targets.
        #
        # For each record (line of output) in:
        #     iscsiadm -m node
        #
        # Where each line in the output is of the form:
        #     [recnum] stuff
        #
        # Issue the "action" request to recnum.
        #
        argv = [ "-m", "node" ]

        if ipaddr is not None:
            argv.extend(["-p", ipaddr])

        log.info("going to run iscsiadm: %s" %(argv,))
        records = iutil.execWithCapture(ISCSIADM, argv)
        for line in records.split("\n"):
            if line and line.find("no records found!") == -1:
                (portal, node) = line.split()
                argv = [ "-m", "node", "-T", node, "-p", portal, action ]
                log.info("going to run iscsiadm: %s" %(argv,))
                rc = iutil.execWithRedirect(ISCSIADM, argv, searchPath = 1,
                                            stdout = "/dev/tty5",
                                            stderr = "/dev/tty5")
                if rc != 0:
                    log.info("iscsiadm failed!")
                    continue

                if action != "--login":
                    continue
                
                # ... and now we have to make it start automatically
                argv = [ "-m", "node", "-T", node, "-p", portal,
                         "-o", "update", "-n", "node.startup",
                         "-v", "automatic" ]
                iutil.execWithRedirect(ISCSIADM, argv, searchPath = 1,
                                       stdout = "/dev/tty5",
                                       stderr = "/dev/tty5")

    def shutdown(self):
        if not self.iscsidStarted:
            return

        log.info("iSCSI shutdown")
        self.action("--logout")

        # XXX use iscsiadm shutdown when it's available.
        argv = [ "--no-headers", "-C", "%s" % (ISCSID,) ]
        psout = iutil.execWithCapture("/usr/bin/ps", argv)
        for line in psout.split("\n"):
            if line:
                pid = string.atoi(string.split(line)[0])
                log.info("Killing %s %d" % (ISCSID, pid))
                os.kill(pid, signal.SIGKILL)
        self.iscsidStarted = False;

    def discoverTarget(self, ipaddr, port = "3260", intf = None):
        if not self.iscsidStarted:
            self.startup(intf)
        if flags.test:
            return
            
        argv = [ "-m", "discovery", "-t", "st", "-p", 
                 "%s:%s" % (ipaddr, port) ]
        log.info("going to run with args: %s" %(argv,))
        iutil.execWithRedirect(ISCSIADM, argv,
                               stdout = "/dev/tty5", stderr="/dev/tty5")

    def loginTarget(self, ipaddr = None):
        if flags.test:
            return
        self.action("--login", ipaddr)

    def startup(self, intf = None):
        if flags.test:
            return
        if not self.initiator:
            log.info("no initiator set")
            return
        if self.iscsidStarted:
            return
        
        log.info("iSCSI initiator name %s", self.initiator)

        if intf:
            w = intf.waitWindow(_("Initializing iSCSI initiator"),
                                _("Initializing iSCSI initiator"))

        log.debug("Setting up %s" % (INITIATOR_FILE, ))
        if os.path.exists(INITIATOR_FILE):
            os.unlink(INITIATOR_FILE)
        if not os.path.isdir("/etc/iscsi"):
            os.makedirs("/etc/iscsi", 0755)
        fd = os.open(INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
        os.write(fd, "InitiatorName=%s\n" %(self.initiator))
        os.close(fd)

        log.info("ISCSID is %s", ISCSID)
        iutil.execWithRedirect(ISCSID, [],
                               stdout="/dev/tty5", stderr="/dev/tty5")
        self.iscsidStarted = True
        time.sleep(2)

        for t in self.targets:
            idx = t.rfind(":")
            if idx == -1:
                ipaddr = t
                port = "3260"
            else:
                ipaddr = t[:idx]
                port = t[idx+1:]

            self.discoverTarget(ipaddr, port, intf)
            self.loginTarget(ipaddr)

        if intf:
            w.pop()

    def writeKS(self):
        # XXX Useful if we have auto-generated kickstart files.
        return

    def write(self, instPath):
        if not self.initiator:
            return

        if not os.path.isdir(instPath + "/etc/iscsi"):
            os.makedirs(instPath + "/etc/iscsi", 0755)
        fd = os.open(instPath + INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
        os.write(fd, "InitiatorName=%s\n" %(self.initiator))
        os.close(fd)

        # copy "db" files.  *sigh*
        for d in ("/etc/iscsi/nodes", "/etc/iscsi/send_targets"):
            if os.path.isdir(d):
                shutil.copytree(d, instPath + d)

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