Coverage for libs/sm/on_slave.py: 100%

84 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2026-07-10 07:59 +0000

1# Copyright (C) Citrix Systems Inc. 

2# 

3# This program is free software; you can redistribute it and/or modify 

4# it under the terms of the GNU Lesser General Public License as published 

5# by the Free Software Foundation; version 2.1 only. 

6# 

7# This program is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU Lesser General Public License for more details. 

11# 

12# You should have received a copy of the GNU Lesser General Public License 

13# along with this program; if not, write to the Free Software Foundation, Inc., 

14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

15 

16from sm.core import util 

17from sm.core import lock 

18from sm.lvmcache import LVMCache 

19from sm.core import scsiutil 

20 

21 

22def multi(session, args): 

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

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

25 vgName = args["vgName"] 

26 lvmCache = LVMCache(vgName) 

27 i = 1 

28 while True: 

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

30 if not action: 

31 break 

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

33 if action == "activate": 

34 try: 

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

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

37 except util.CommandException: 

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

39 raise 

40 elif action == "deactivate": 

41 try: 

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

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

44 except util.SMException: 

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

46 raise 

47 elif action == "deactivateNoRefcount": 

48 try: 

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

50 except util.SMException: 

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

52 raise 

53 elif action == "refresh": 

54 try: 

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

56 except util.CommandException: 

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

58 raise 

59 elif action == "cleanupLockAndRefcount": 

60 from sm.refcounter import RefCounter 

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

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

63 else: 

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

65 i += 1 

66 return str(True) 

67 

68 

69def _is_open(session, args): 

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

71 from sm import SRCommand 

72 from sm import SR 

73 from sm.drivers import NFSSR 

74 from sm.drivers import EXTSR 

75 from sm.drivers import LVHDSR 

76 from sm import blktap2 

77 

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

79 vdiUuid = args["vdiUuid"] 

80 srRef = args["srRef"] 

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

82 srType = srRec["type"] 

83 

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

85 # avoid having to refactor the core files 

86 if srType.startswith("lvm"): 

87 srType = "lvhd" 

88 cmd = SRCommand.SRCommand(None) 

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

90 cmd.dconf = {"server": None, "device": "/HACK"} 

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

92 

93 driver = SR.driver(srType) 

94 sr = driver(cmd, srRec["uuid"]) 

95 vdi = sr.vdi(vdiUuid) 

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

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

98 if tapdisk: 

99 return "True" 

100 return "False" 

101 

102 

103def is_open(session, args): 

104 try: 

105 return _is_open(session, args) 

106 except: 

107 util.logException("is_open") 

108 raise 

109 

110 

111def refresh_lun_size_by_SCSIid(session, args): 

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

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

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

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

116 % args) 

117 return "True" 

118 else: 

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

120 return "False"