Coverage for drivers/mpath_cli.py : 28%

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
18from sm_typing import override
20import util
21import re
22import time
23from fairlock import Fairlock
26class MPathCLIFail(Exception):
27 def __init__(self):
28 return
30 @override
31 def __str__(self) -> str:
32 return "MPath CLI failed"
34mpathcmd = ["/usr/sbin/multipathd", "-k"]
37def mpexec(cmd):
38 util.SMlog("mpath cmd: %s" % cmd)
39 with Fairlock("devicemapper"):
40 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd)
41 if stdout != "multipathd> ok\nmultipathd> " \
42 and stdout != "multipathd> " + cmd + "\nok\nmultipathd> ":
43 raise MPathCLIFail
46def add_path(path):
47 mpexec("add path %s" % path)
50def remove_path(path):
51 mpexec("remove path %s" % path)
54def remove_map(m):
55 mpexec("remove map %s" % m)
58def resize_map(m):
59 mpexec("resize map %s" % m)
62def reconfigure():
63 mpexec("reconfigure")
65regex = re.compile(r"[0-9]+:[0-9]+:[0-9]+:[0-9]+\s*([a-z]*)")
66regex2 = re.compile(r"multipathd>(\s*[^:]*:)?\s+(.*)")
67regex3 = re.compile("switchgroup")
70def is_working():
71 cmd = "help"
72 try:
73 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd)
74 m = regex3.search(stdout)
75 if m:
76 return True
77 else:
78 return False
79 except:
80 return False
83def do_get_topology(cmd):
84 util.SMlog("mpath cmd: %s" % cmd)
85 with Fairlock("devicemapper"):
86 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd)
87 util.SMlog("mpath output: %s" % stdout)
88 lines = stdout.split('\n')[:-1]
89 if len(lines):
90 m = regex2.search(lines[0])
91 lines[0] = str(m.group(2))
92 return lines
95def get_topology(scsi_id):
96 cmd = "show map %s topology" % scsi_id
97 return do_get_topology(cmd)
100def get_all_topologies():
101 cmd = "show topology"
102 return do_get_topology(cmd)
105def list_paths(scsi_id):
106 lines = get_topology(scsi_id)
107 matches = []
108 for line in lines:
109 m = regex.search(line)
110 if(m):
111 matches.append(m.group(1))
112 return matches
115def list_maps():
116 cmd = "list maps"
117 util.SMlog("mpath cmd: %s" % cmd)
118 with Fairlock("devicemapper"):
119 (rc, stdout, stderr) = util.doexec(mpathcmd, cmd)
120 util.SMlog("mpath output: %s" % stdout)
121 return [x.split(' ')[0] for x in stdout.split('\n')[2:-1]]
124def ensure_map_gone(scsi_id):
125 while True:
126 paths = list_paths(scsi_id)
127 util.SMlog("list_paths succeeded")
128 if len(paths) == 0:
129 return
130 time.sleep(1)