Hide keyboard shortcuts

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# A plugin for synchronizing slaves when something changes on the Master 

19 

20import sys 

21sys.path.append("/opt/xensource/sm/") 

22import util 

23import lock 

24from lvmcache import LVMCache 

25import scsiutil 

26 

27 

28def multi(session, args): 

29 """Perform several actions in one call (to save on round trips)""" 

30 util.SMlog("on-slave.multi: %s" % args) 

31 vgName = args["vgName"] 

32 lvmCache = LVMCache(vgName) 

33 i = 1 

34 while True: 

35 action = args.get("action%d" % i) 

36 if not action: 

37 break 

38 util.SMlog("on-slave.action %d: %s" % (i, action)) 

39 if action == "activate": 

40 try: 

41 lvmCache.activate(args["ns%d" % i], args["uuid%d" % i], 

42 args["lvName%d" % i], False) 

43 except util.CommandException: 

44 util.SMlog("on-slave.activate failed") 

45 raise 

46 elif action == "deactivate": 

47 try: 

48 lvmCache.deactivate(args["ns%d" % i], args["uuid%d" % i], 

49 args["lvName%d" % i], False) 

50 except util.SMException: 

51 util.SMlog("on-slave.deactivate failed") 

52 raise 

53 elif action == "deactivateNoRefcount": 

54 try: 

55 lvmCache.deactivateNoRefcount(args["lvName%d" % i]) 

56 except util.SMException: 

57 util.SMlog("on-slave.deactivateNoRefcount failed") 

58 raise 

59 elif action == "refresh": 

60 try: 

61 lvmCache.activateNoRefcount(args["lvName%d" % i], True) 

62 except util.CommandException: 

63 util.SMlog("on-slave.refresh failed") 

64 raise 

65 elif action == "cleanupLockAndRefcount": 

66 from refcounter import RefCounter 

67 lock.Lock.cleanup(args["uuid%d" % i], args["ns%d" % i]) 

68 RefCounter.reset(args["uuid%d" % i], args["ns%d" % i]) 

69 else: 

70 raise util.SMException("unrecognized action: %s" % action) 

71 i += 1 

72 return str(True) 

73 

74 

75def _is_open(session, args): 

76 """Check if VDI <args["vdiUuid"]> is open by a tapdisk on this host""" 

77 import SRCommand 

78 import SR 

79 import CephFSSR 

80 import EXTSR 

81 import LargeBlockSR 

82 import GlusterFSSR 

83 import LinstorSR 

84 import LVHDSR 

85 import MooseFSSR 

86 import NFSSR 

87 import XFSSR 

88 import ZFSSR 

89 import blktap2 

90 

91 util.SMlog("on-slave.is_open: %s" % args) 

92 vdiUuid = args["vdiUuid"] 

93 srRef = args["srRef"] 

94 srRec = session.xenapi.SR.get_record(srRef) 

95 srType = srRec["type"] 

96 

97 # FIXME: ugly hacks to create a VDI object without a real SRCommand to 

98 # avoid having to refactor the core files 

99 if srType.startswith("lvm"): 

100 srType = "lvhd" 

101 cmd = SRCommand.SRCommand(None) 

102 cmd.driver_info = {"capabilities": None} 

103 cmd.dconf = { 

104 "server": None, 

105 "device": "/HACK", 

106 # Hack for custom XCP-ng drivers. 

107 "masterhost": None, # MooseFS 

108 "rootpath": None, # MooseFS 

109 "serverpath": None, # CephFS 

110 "location": "/HACK" # ZFS 

111 } 

112 cmd.params = {"command": None} 

113 

114 sr_uuid = srRec["uuid"] 

115 

116 # Another ugly piece of code to load a real Linstor SR, otherwise 

117 # we can't fetch the VDI path. 

118 if srType == 'linstor': 118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never true

119 host_ref = util.get_this_host_ref(session) 

120 sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid) 

121 

122 pbd = util.find_my_pbd(session, host_ref, sr_ref) 

123 if pbd is None: 

124 raise util.SMException('Failed to find Linstor PBD') 

125 

126 cmd.dconf = session.xenapi.PBD.get_device_config(pbd) 

127 

128 driver = SR.driver(srType) 

129 sr = driver(cmd, sr_uuid) 

130 

131 # session_ref param is required to have a valid session when SR object is created. 

132 # It's not the case here, so attach the current session object to make LinstorSR happy. 

133 if srType == 'linstor': 133 ↛ 134line 133 didn't jump to line 134, because the condition on line 133 was never true

134 sr.session = session 

135 

136 vdi = sr.vdi(vdiUuid) 

137 tapdisk = blktap2.Tapdisk.find_by_path(vdi.path) 

138 util.SMlog("Tapdisk for %s: %s" % (vdi.path, tapdisk)) 

139 if tapdisk: 

140 return "True" 

141 return "False" 

142 

143 

144def is_open(session, args): 

145 try: 

146 return _is_open(session, args) 

147 except: 

148 util.logException("is_open") 

149 raise 

150 

151 

152def refresh_lun_size_by_SCSIid(session, args): 

153 """Refresh the size of LUNs backing the SCSIid on the local node.""" 

154 util.SMlog("on-slave.refresh_lun_size_by_SCSIid(,%s)" % args) 

155 if scsiutil.refresh_lun_size_by_SCSIid(args['SCSIid']): 

156 util.SMlog("on-slave.refresh_lun_size_by_SCSIid with %s succeeded" 

157 % args) 

158 return "True" 

159 else: 

160 util.SMlog("on-slave.refresh_lun_size_by_SCSIid with %s failed" % args) 

161 return "False" 

162 

163 

164if __name__ == "__main__": 164 ↛ 165line 164 didn't jump to line 165, because the condition on line 164 was never true

165 import XenAPIPlugin 

166 XenAPIPlugin.dispatch({ 

167 "multi": multi, 

168 "is_open": is_open, 

169 "refresh_lun_size_by_SCSIid": refresh_lun_size_by_SCSIid})