summaryrefslogtreecommitdiffstats
path: root/iscsi.py
blob: f9f396d262cce0093276d9df88a466aae6a3ae16 (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
#
# 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
log = logging.getLogger("anaconda")


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

class iscsi:
    def __init__(self):
        if flags.iscsi:
            self.ipaddr = ""
            self.port = "3260"
            self.initiator = ""
            self.iscsidStarted = False



    def action(self, action):
        #
        # 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 = [ ISCSIADM, "-m", "node" ]
        records = iutil.execWithCapture(argv[0], argv, searchPath = 1)
        for line in records.split("\n"):
            if line:
                recnum = line.split()[0][1:-1]
                argv = [ ISCSIADM, "-m", "node", "-r", "%s" % (recnum,),
                         "%s" % (action,) ]
                iutil.execWithRedirect(argv[0], 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 = [ "ps", "--no-headers", "-C", "%s" % (ISCSID,) ]
        psout = iutil.execWithCapture(argv[0], argv, searchPath = 1)
        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 startup(self):
        log.info("iSCSI IP address %s, port %s" % (self.ipaddr, self.port))
        log.info("iSCSI initiator name %s", self.initiator)

        if flags.test:
            return

        self.shutdown()

        if not self.ipaddr:
            log.info("iSCSI: Not starting, no iscsi IP address specified")
            return

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

        if not os.path.exists(ISCSID_DB_DIR):
            iutil.mkdirChain(ISCSID_DB_DIR)

        argv = [ ISCSID ]
        iutil.execWithRedirect(argv[0], argv, searchPath = 1)

        argv = [ ISCSIADM, "-m", "discovery", "-t", "st", "-p", 
                 "%s:%s" % (self.ipaddr, self.port) ]
        iutil.execWithRedirect(argv[0], argv, searchPath = 1,
                               stdout = "/dev/tty5", stderr="/dev/tty5")

        self.action("--login")
        self.iscsidStarted = True;

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

    def write(self):
        # XXX copy config files and any files needed by mkinitrd, depends on
        # mkinitrd and open-iscsi work in progress.
        return

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