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
27def reset_sr(session, host_uuid, sr_uuid, is_sr_master):
28 cleanup.abort(sr_uuid)
30 gc_lock = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, sr_uuid)
31 sr_lock = lock.Lock(lock.LOCK_TYPE_SR, sr_uuid)
32 gc_lock.acquire()
33 sr_lock.acquire()
35 sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
37 host_ref = session.xenapi.host.get_by_uuid(host_uuid)
38 host_key = "host_%s" % host_ref
40 util.SMlog("RESET for SR %s (master: %s)" % (sr_uuid, is_sr_master))
42 vdi_recs = session.xenapi.VDI.get_all_records_where( \
43 "field \"SR\" = \"%s\"" % sr_ref)
45 for vdi_ref, vdi_rec in vdi_recs.items():
46 vdi_uuid = vdi_rec["uuid"]
47 sm_config = vdi_rec["sm_config"]
48 if sm_config.get(host_key):
49 util.SMlog("Clearing attached status for VDI %s" % vdi_uuid)
50 session.xenapi.VDI.remove_from_sm_config(vdi_ref, host_key)
51 if is_sr_master and sm_config.get("paused"):
52 util.SMlog("Clearing paused status for VDI %s" % vdi_uuid)
53 session.xenapi.VDI.remove_from_sm_config(vdi_ref, "paused")
55 sr_lock.release()
56 gc_lock.release()
59def reset_vdi(session, vdi_uuid, force, term_output=True, writable=True):
60 vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid)
61 vdi_rec = session.xenapi.VDI.get_record(vdi_ref)
62 sm_config = vdi_rec["sm_config"]
63 host_ref = None
64 clean = True
65 for key, val in sm_config.items():
66 if key.startswith("host_"):
67 host_ref = key[len("host_"):]
68 host_uuid = None
69 host_invalid = False
70 host_str = host_ref
71 try:
72 host_rec = session.xenapi.host.get_record(host_ref)
73 host_uuid = host_rec["uuid"]
74 host_str = "%s (%s)" % (host_uuid, host_rec["name_label"])
75 except XenAPI.Failure as e:
76 msg = "Invalid host: %s (%s)" % (host_ref, e)
77 util.SMlog(msg)
78 if term_output:
79 print(msg)
80 host_invalid = True
82 if host_invalid:
83 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
84 msg = "Invalid host: Force-cleared %s for %s on host %s" % \
85 (val, vdi_uuid, host_str)
86 util.SMlog(msg)
87 if term_output:
88 print(msg)
89 continue
91 if force:
92 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
93 msg = "Force-cleared %s for %s on host %s" % \
94 (val, vdi_uuid, host_str)
95 util.SMlog(msg)
96 if term_output:
97 print(msg)
98 continue
100 ret = session.xenapi.host.call_plugin(
101 host_ref, "on-slave", "is_open",
102 {"vdiUuid": vdi_uuid, "srRef": vdi_rec["SR"]})
103 if ret != "False":
104 util.SMlog("VDI %s is still open on host %s, not resetting" % \
105 (vdi_uuid, host_str))
106 if term_output:
107 print("ERROR: VDI %s is still open on host %s" % \
108 (vdi_uuid, host_str))
109 if writable:
110 return False
111 else:
112 clean = False
113 else:
114 session.xenapi.VDI.remove_from_sm_config(vdi_ref, key)
115 msg = "Cleared %s for %s on host %s" % \
116 (val, vdi_uuid, host_str)
117 util.SMlog(msg)
118 if term_output:
119 print(msg)
121 if not host_ref:
122 msg = "VDI %s is not marked as attached anywhere, nothing to do" \
123 % vdi_uuid
124 util.SMlog(msg)
125 if term_output:
126 print(msg)
127 return clean
130def usage():
131 print("Usage:")
132 print("all <HOST UUID> <SR UUID> [--master]")
133 print("single <VDI UUID> [--force]")
134 print()
135 print("*WARNING!* calling with 'all' on an attached SR, or using " + \
136 "--force may cause DATA CORRUPTION if the VDI is still " + \
137 "attached somewhere. Always manually double-check that " + \
138 "the VDI is not in use before running this script.")
139 sys.exit(1)
141if __name__ == '__main__': 141 ↛ 142line 141 didn't jump to line 142, because the condition on line 141 was never true
142 import atexit
144 if len(sys.argv) not in [3, 4, 5]:
145 usage()
147 session = XenAPI.xapi_local()
148 session.xenapi.login_with_password('root', '', '', 'SM')
149 atexit.register(session.xenapi.session.logout)
151 mode = sys.argv[1]
152 if mode == "all":
153 if len(sys.argv) not in [4, 5]:
154 usage()
155 host_uuid = sys.argv[2]
156 sr_uuid = sys.argv[3]
157 is_master = False
158 if len(sys.argv) == 5:
159 if sys.argv[4] == "--master":
160 is_master = True
161 else:
162 usage()
163 reset_sr(session, host_uuid, sr_uuid, is_master)
164 elif mode == "single":
165 vdi_uuid = sys.argv[2]
166 force = False
167 if len(sys.argv) == 4 and sys.argv[3] == "--force":
168 force = True
169 reset_vdi(session, vdi_uuid, force)
170 elif len(sys.argv) in [3, 4]:
171 # backwards compatibility: the arguments for the "all" case used to be
172 # just host_uuid, sr_uuid, [is_master] (i.e., no "all" string, since it
173 # was the only mode available). To avoid having to change XAPI, accept
174 # the old format here as well.
175 host_uuid = sys.argv[1]
176 sr_uuid = sys.argv[2]
177 is_master = False
178 if len(sys.argv) == 4:
179 if sys.argv[3] == "--master":
180 is_master = True
181 else:
182 usage()
183 reset_sr(session, host_uuid, sr_uuid, is_master)
184 else:
185 usage()