Coverage for drivers/mpath_cli.py : 26%

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
18import util
19import re
20import time
21from fairlock import Fairlock
24class MPathCLIFail(Exception):
25 def __init__(self):
26 return
28 def __str__(self):
29 return "MPath CLI failed"
31mpathcmd = ["/usr/sbin/multipathd", "-k"]
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
43def add_path(path):
44 mpexec("add path %s" % path)
47def remove_path(path):
48 mpexec("remove path %s" % path)
51def remove_map(m):
52 mpexec("remove map %s" % m)
55def resize_map(m):
56 mpexec("resize map %s" % m)
59def reconfigure():
60 mpexec("reconfigure")
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")
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
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
92def get_topology(scsi_id):
93 cmd = "show map %s topology" % scsi_id
94 return do_get_topology(cmd)
97def get_all_topologies():
98 cmd = "show topology"
99 return do_get_topology(cmd)
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
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]]
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)