Hide keyboard shortcuts

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# Copyright (C) Citrix Systems Inc. 

2# 

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

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

5# by the Free Software Foundation; version 2.1 only. 

6# 

7# This program is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU Lesser General Public License for more details. 

11# 

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

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

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

15# 

16# Talk to the multipathd cli 

17 

18import util 

19import re 

20import time 

21from fairlock import Fairlock 

22 

23 

24class MPathCLIFail(Exception): 

25 def __init__(self): 

26 return 

27 

28 def __str__(self): 

29 return "MPath CLI failed" 

30 

31mpathcmd = ["/usr/sbin/multipathd", "-k"] 

32 

33 

34def mpexec(cmd): 

35 util.SMlog("mpath cmd: %s" % cmd) 

36 with Fairlock("devicemapper"): 

37 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) 

38 if stdout != "multipathd> ok\nmultipathd> " \ 

39 and stdout != "multipathd> " + cmd + "\nok\nmultipathd> ": 

40 raise MPathCLIFail 

41 

42 

43def add_path(path): 

44 mpexec("add path %s" % path) 

45 

46 

47def remove_path(path): 

48 mpexec("remove path %s" % path) 

49 

50 

51def remove_map(m): 

52 mpexec("remove map %s" % m) 

53 

54 

55def resize_map(m): 

56 mpexec("resize map %s" % m) 

57 

58 

59def reconfigure(): 

60 mpexec("reconfigure") 

61 

62regex = re.compile(r"[0-9]+:[0-9]+:[0-9]+:[0-9]+\s*([a-z]*)") 

63regex2 = re.compile(r"multipathd>(\s*[^:]*:)?\s+(.*)") 

64regex3 = re.compile("switchgroup") 

65 

66 

67def is_working(): 

68 cmd = "help" 

69 try: 

70 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) 

71 m = regex3.search(stdout) 

72 if m: 

73 return True 

74 else: 

75 return False 

76 except: 

77 return False 

78 

79 

80def do_get_topology(cmd): 

81 util.SMlog("mpath cmd: %s" % cmd) 

82 with Fairlock("devicemapper"): 

83 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) 

84 util.SMlog("mpath output: %s" % stdout) 

85 lines = stdout.split('\n')[:-1] 

86 if len(lines): 

87 m = regex2.search(lines[0]) 

88 lines[0] = str(m.group(2)) 

89 return lines 

90 

91 

92def get_topology(scsi_id): 

93 cmd = "show map %s topology" % scsi_id 

94 return do_get_topology(cmd) 

95 

96 

97def get_all_topologies(): 

98 cmd = "show topology" 

99 return do_get_topology(cmd) 

100 

101 

102def list_paths(scsi_id): 

103 lines = get_topology(scsi_id) 

104 matches = [] 

105 for line in lines: 

106 m = regex.search(line) 

107 if(m): 

108 matches.append(m.group(1)) 

109 return matches 

110 

111 

112def list_maps(): 

113 cmd = "list maps" 

114 util.SMlog("mpath cmd: %s" % cmd) 

115 with Fairlock("devicemapper"): 

116 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) 

117 util.SMlog("mpath output: %s" % stdout) 

118 return [x.split(' ')[0] for x in stdout.split('\n')[2:-1]] 

119 

120 

121def ensure_map_gone(scsi_id): 

122 while True: 

123 paths = list_paths(scsi_id) 

124 util.SMlog("list_paths succeeded") 

125 if len(paths) == 0: 

126 return 

127 time.sleep(1)