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 GlusterFSSR 

82 import LinstorSR 

83 import LVHDSR 

84 import MooseFSSR 

85 import NFSSR 

86 import XFSSR 

87 import ZFSSR 

88 import blktap2 

89 

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

91 vdiUuid = args["vdiUuid"] 

92 srRef = args["srRef"] 

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

94 srType = srRec["type"] 

95 

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

97 # avoid having to refactor the core files 

98 if srType.startswith("lvm"): 

99 srType = "lvhd" 

100 cmd = SRCommand.SRCommand(None) 

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

102 cmd.dconf = { 

103 "server": None, 

104 "device": "/HACK", 

105 # Hack for custom XCP-ng drivers. 

106 "masterhost": None, # MooseFS 

107 "rootpath": None, # MooseFS 

108 "serverpath": None, # CephFS 

109 "location": "/HACK" # ZFS 

110 } 

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

112 

113 sr_uuid = srRec["uuid"] 

114 

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

116 # we can't fetch the VDI path. 

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

118 host_ref = util.get_this_host_ref(session) 

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

120 

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

122 if pbd is None: 

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

124 

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

126 

127 driver = SR.driver(srType) 

128 sr = driver(cmd, sr_uuid) 

129 

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

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

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

133 sr.session = session 

134 

135 vdi = sr.vdi(vdiUuid) 

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

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

138 if tapdisk: 

139 return "True" 

140 return "False" 

141 

142 

143def is_open(session, args): 

144 try: 

145 return _is_open(session, args) 

146 except: 

147 util.logException("is_open") 

148 raise 

149 

150 

151def refresh_lun_size_by_SCSIid(session, args): 

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

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

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

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

156 % args) 

157 return "True" 

158 else: 

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

160 return "False" 

161 

162 

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

164 import XenAPIPlugin 

165 XenAPIPlugin.dispatch({ 

166 "multi": multi, 

167 "is_open": is_open, 

168 "refresh_lun_size_by_SCSIid": refresh_lun_size_by_SCSIid})