Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# 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 Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# Manipulation utilities for multipath.conf 

# 

 

 

import fileinput, shutil, sys, re 

import util, lock 

 

LOCK_TYPE_HOST = "host" 

LOCK_NS = "multipath.conf" 

# We always expect this file. 

# It must be a regular file pointed by /etc/multipath.conf 

CONF_FILE = "/etc/multipath.xenserver/multipath.conf" 

BELIST_TAG = "blacklist_exceptions" 

 

class WWIDException(Exception): 

    def __init__(self, errstr): 

        self.errstr = errstr 

 

 

def edit_wwid(wwid, remove=False): 

    """Add a wwid to the list of exceptions or remove if 

    remove is set to 1. 

 

    """ 

 

    tmp_file = CONF_FILE+"~" 

    filt_regex = re.compile('^\s*%s\s*{'%BELIST_TAG) 

    wwid_regex = re.compile('^\s*wwid\s+\"%s\"'%wwid) 

 

    conflock = lock.Lock(LOCK_TYPE_HOST, LOCK_NS) 

    conflock.acquire() 

 

    try: 

        shutil.copy2(CONF_FILE, tmp_file) 

    except: 

        util.SMlog("Failed to create temp file %s" %(tmp_file)) 

        raise 

 

    add_mode = True 

    for line in fileinput.input(tmp_file, inplace=1): 

        if add_mode: 

            print line, 

        else: 

            if wwid_regex.match(line): 

                add_mode = True 

            else: 

                print line, 

            continue 

 

        if filt_regex.match(line): 

            if remove: 

                # looking for the line to remove 

                add_mode = False 

                continue 

            else: 

                print "\twwid \"%s\""%wwid 

 

    shutil.move(tmp_file, CONF_FILE) 

 

def is_blacklisted(dev): 

    """This function returns True if the device is blacklisted according 

    to multipath.conf rules. 

 

    There is also the possibility that the path to the device we are 

    checking is unavailable for some reason. In that case, 

    is_blacklisted() cannot tell if the device is blacklisted or not 

    and a WWIDException is raised. 

 

    It cannot be used to check the current daemon rules because it 

    could be running with an old configuration file in memory 

 

    Input: 

        dev -- it is any string accepted by "multipath -c". A full path 

               is expected. 

 

    Return: 

        bool -- True or False, depending on whether the device is 

                blacklisted or not. 

 

    Raise: 

        WWIDException 

    """ 

 

    (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-v3','-c',dev]) 

 

    if "scope is nul" in stdout: 

        raise WWIDException("WARNING: Could not retrieve device's " 

                            "WWID. Path {} is down".format(dev)) 

 

    # If the device is blacklisted according to multipath.conf, the 

    # string "wwid blacklisted" appears in the verbose output. 

    # This is a very fragile mechanism and keeps changing. 

    # What we want is a method to tell immediately if a device is 

    # blacklisted according only to configuration file rules regardless 

    # of daemon in-memory configuration. 

    return "wwid blacklisted" in stdout 

 

 

def check_conf_file(): 

    (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-h']) 

    # Ugly way to check for malformed conf file 

    if len(stdout): 

        util.SMlog("Malformed multipath conf file") 

        return 1 

    return 0 

 

 

def usage(): 

    print "Usage: %s [-r] -d <device path> -w <device wwid>" % sys.argv[0] 

    print "Usage: %s -f [-r] -w <device wwid>" % sys.argv[0] 

    print "\tAdd a device wwid to multipath.conf whitelist" 

    print "\t-r: remove" 

    print "\t-f: if provided the operation will be performed anyway" 

    print "\t    otherwise it will be after checking <device> is" 

    print "\t    blacklisted (or not)" 

 

 

136if __name__ == "__main__": 

    import getopt 

    from operator import xor 

 

    try: 

        opts, args = getopt.getopt(sys.argv[1:], "fd:w:r") 

    except getopt.GetoptError: 

        usage() 

        sys.exit(1) 

 

    remove = False 

    device = "" 

    force = False 

    wwid = "" 

 

    for o, a in opts: 

        if o == "-r": 

            remove = True 

        elif o == "-d": 

            device = a 

        elif o == "-f": 

            force = True 

        elif o == "-w": 

            wwid = a 

 

    if not wwid: 

        usage() 

        sys.exit(1) 

 

    if not (device or force): 

        usage() 

        sys.exit(1) 

 

    try: 

        if force or xor(remove, is_blacklisted(device)): 

            try: 

                edit_wwid(wwid, remove) 

            except: 

                sys.exit(1) 

    except WWIDException as e: 

        util.SMlog(e.errstr) 

 

    sys.exit(0)