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 

21 

22 

23class MPathCLIFail(Exception): 

24 def __init__(self): 

25 return 

26 

27 def __str__(self): 

28 print("", "MPath CLI failed") 

29 

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

31 

32 

33def mpexec(cmd): 

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

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

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

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

38 raise MPathCLIFail 

39 

40 

41def add_path(path): 

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

43 

44 

45def remove_path(path): 

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

47 

48 

49def remove_map(m): 

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

51 

52 

53def resize_map(m): 

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

55 

56 

57def reconfigure(): 

58 mpexec("reconfigure") 

59 

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

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

62regex3 = re.compile("switchgroup") 

63 

64 

65def is_working(): 

66 cmd = "help" 

67 try: 

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

69 m = regex3.search(stdout) 

70 if m: 

71 return True 

72 else: 

73 return False 

74 except: 

75 return False 

76 

77 

78def do_get_topology(cmd): 

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

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

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

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

83 if len(lines): 

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

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

86 return lines 

87 

88 

89def get_topology(scsi_id): 

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

91 return do_get_topology(cmd) 

92 

93 

94def get_all_topologies(): 

95 cmd = "show topology" 

96 return do_get_topology(cmd) 

97 

98 

99def list_paths(scsi_id): 

100 lines = get_topology(scsi_id) 

101 matches = [] 

102 for line in lines: 

103 m = regex.search(line) 

104 if(m): 

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

106 return matches 

107 

108 

109def list_maps(): 

110 cmd = "list maps" 

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

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

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

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

115 

116 

117def ensure_map_gone(scsi_id): 

118 while True: 

119 paths = list_paths(scsi_id) 

120 util.SMlog("list_paths succeeded") 

121 if len(paths) == 0: 

122 return 

123 time.sleep(1)