Coverage for libs/sm/resetvdis.py: 5%
82 statements
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
1#
2# Copyright (C) Citrix Systems Inc.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU Lesser General Public License as published
6# by the Free Software Foundation; version 2.1 only.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public License
14# along with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16#
17# Clear the attach status for all VDIs in the given SR on this host.
18# Additionally, reset the paused state if this host is the master.
20import XenAPI # pylint: disable=import-error
21from sm import cleanup
22from sm.core import util
23from sm.core import lock
26def reset_sr(session, host_uuid, sr_uuid, is_sr_master):
27 from sm.vhdutil import LOCK_TYPE_SR
29 cleanup.abort(sr_uuid)
31 gc_lock = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, sr_uuid)
32 sr_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