Coverage for drivers/resetvdis.py : 6%
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#!/usr/bin/python3
2#
3# Copyright (C) Citrix Systems Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published
7# by the Free Software Foundation; version 2.1 only.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17#
18# Clear the attach status for all VDIs in the given SR on this host.
19# Additionally, reset the paused state if this host is the master.
21import cleanup
22import util
23import lock
24import sys
25import XenAPI # pylint: disable=import-error
28def reset_sr(session, host_uuid, sr_uuid, is_sr_master):
29 cleanup.abort(sr_uuid)
31 gc_lock = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, sr_uuid)
32 sr_lock = lock.Lock(lock.LOCK_TYPE_SR, sr_uuid)
33 gc_lock.acquire()
34 sr_lock.acquire()
36 sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
38 host_ref = session.xenapi.host.get_by_uuid(host_uuid)
39 host_key = "host_%s" % host_ref
41 util.SMlog("RESET for SR %s (master: %s)" % (sr_uuid, is_sr_master))
43 vdi_recs = session.xenapi.VDI.get_all_records_where( \
44 "field \"SR\" = \"%s\"" % sr_ref)
46 for vdi_ref, vdi_rec in vdi_recs.items():
47 vdi_uuid = vdi_rec["uuid"]
48 sm_config = vdi_rec["sm_config"]
49 if sm_config.get(host_key):
50 util.SMlog("Clearing attached status for VDI %s" % vdi_uuid)
51 session.xenapi.VDI.remove_from_sm_config(vdi_ref, host_key)
52 if is_sr_master and sm_config.get("paused"):
53 util.SMlog("Clearing paused status for VDI %s" % vdi_uuid)
54 session.xenapi.VDI.remove_from_sm_config(vdi_ref, "paused")
56 sr_lock.release()
57 gc_lock.release()
60def reset_vdi(session, vdi_uuid, force, term_output=True, writable=True):
61 vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid)
62 vdi_rec = session.xenapi.VDI.get_record(vdi_ref)
63 sm_config = vdi_rec["sm_config"]
64 host_ref = None
65 clean = True
66 for key, val in sm_config.items():
67 if key.startswith("host_"):
68 host_ref = key[len("host_"):]
69 host_uuid = None
70 host_invalid = False
71 host_str = host_ref
72 try:
73 host_rec = session.xenapi.host.get_record(host_ref)
74 host_uuid = host_rec["uuid"]
75 host_str = "%s (%s)" % (host_uuid, host_rec["name_label"])
76 except XenAPI.Failure as e:
77 msg = "Invalid host: %s (%s)" % (host_ref, e)
78 util.SMlog(msg)
79 if term_output:
80 print(msg)
81 host_invalid = True
83 if host_invalid:
84 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
85 msg = "Invalid host: Force-cleared %s for %s on host %s" % \
86 (val, vdi_uuid, host_str)
87 util.SMlog(msg)
88 if term_output:
89 print(msg)
90 continue
92 if force:
93 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
94 msg = "Force-cleared %s for %s on host %s" % \
95 (val, vdi_uuid, host_str)
96 util.SMlog(msg)
97 if term_output:
98 print(msg)
99 continue
101 ret = session.xenapi.host.call_plugin(
102 host_ref, "on-slave", "is_open",
103 {"vdiUuid": vdi_uuid, "srRef": vdi_rec["SR"]})
104 if ret != "False":
105 util.SMlog("VDI %s is still open on host %s, not resetting" % \
106 (vdi_uuid, host_str))
107 if term_output:
108 print("ERROR: VDI %s is still open on host %s" % \
109 (vdi_uuid, host_str))
110 if writable:
111 return False
112 else:
113 clean = False
114 else:
115 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
116 msg = "Cleared %s for %s on host %s" % \
117 (val, vdi_uuid, host_str)
118 util.SMlog(msg)
119 if term_output:
120 print(msg)
122 if not host_ref:
123 msg = "VDI %s is not marked as attached anywhere, nothing to do" \
124 % vdi_uuid
125 util.SMlog(msg)
126 if term_output:
127 print(msg)
128 return clean
131def usage():
132 print("Usage:")
133 print("all <HOST UUID> <SR UUID> [--master]")
134 print("single <VDI UUID> [--force]")
135 print()
136 print("*WARNING!* calling with 'all' on an attached SR, or using " + \
137 "--force may cause DATA CORRUPTION if the VDI is still " + \
138 "attached somewhere. Always manually double-check that " + \
139 "the VDI is not in use before running this script.")
140 sys.exit(1)
142if __name__ == '__main__': 142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true
143 import atexit
145 if len(sys.argv) not in [3, 4, 5]:
146 usage()
148 session = XenAPI.xapi_local()
149 session.xenapi.login_with_password('root', '', '', 'SM')
150 atexit.register(session.xenapi.session.logout)
152 mode = sys.argv[1]
153 if mode == "all":
154 if len(sys.argv) not in [4, 5]:
155 usage()
156 host_uuid = sys.argv[2]
157 sr_uuid = sys.argv[3]
158 is_master = False
159 if len(sys.argv) == 5:
160 if sys.argv[4] == "--master":
161 is_master = True
162 else:
163 usage()
164 reset_sr(session, host_uuid, sr_uuid, is_master)
165 elif mode == "single":
166 vdi_uuid = sys.argv[2]
167 force = False
168 if len(sys.argv) == 4 and sys.argv[3] == "--force":
169 force = True
170 reset_vdi(session, vdi_uuid, force)
171 elif len(sys.argv) in [3, 4]:
172 # backwards compatibility: the arguments for the "all" case used to be
173 # just host_uuid, sr_uuid, [is_master] (i.e., no "all" string, since it
174 # was the only mode available). To avoid having to change XAPI, accept
175 # the old format here as well.
176 host_uuid = sys.argv[1]
177 sr_uuid = sys.argv[2]
178 is_master = False
179 if len(sys.argv) == 4:
180 if sys.argv[3] == "--master":
181 is_master = True
182 else:
183 usage()
184 reset_sr(session, host_uuid, sr_uuid, is_master)
185 else:
186 usage()