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# Tool to do a vhd-util check on all VHDs belonging to a VG/SR. 

19# Usage is "./verifyVHDsOnSR.py <sr_uuid>". This tool verifies all the VHDs 

20# on a VHD based SR. (FC or iSCSI) 

21# 

22 

23import os 

24import sys 

25import util 

26import lvutil 

27 

28import VDI 

29 

30from constants import NS_PREFIX_LVM, VG_LOCATION, VG_PREFIX 

31from cowutil import getCowUtil 

32from lock import Lock 

33from lvmcowutil import LV_PREFIX, LvmCowUtil 

34from refcounter import RefCounter 

35from vditype import VdiType 

36 

37# Stores the vdi activated, comes handy while deactivating 

38VHDs_passed = 0 

39VHDs_failed = 0 

40 

41 

42def activateVdiChainAndCheck(vhd_info, vg_name): 

43 global VHDs_passed 

44 global VHDs_failed 

45 activated_list = [] 

46 vhd_path = os.path.join(VG_LOCATION, vg_name, vhd_info.path) 

47 if not activateVdi( 

48 vg_name.lstrip(VG_PREFIX), 

49 vhd_info.uuid, 

50 vhd_path): 

51 # If activation fails, do not run check, also no point on running 

52 # check on the VDIs down the chain 

53 util.SMlog("VHD activate failed for %s, skipping rest of VDH chain" % 

54 vg_name) 

55 return activated_list 

56 

57 activated_list.append([vhd_info.uuid, vhd_path]) 

58 

59 cowutil = None # TODO 

60 

61 # Do a vhdutil check with -i option, to ignore error in primary 

62 if cowutil.check(vhd_path, True) != cowutil.CheckResult.Success: 

63 util.SMlog("VHD check for %s failed, continuing with the rest!" % vg_name) 

64 VHDs_failed += 1 

65 else: 

66 VHDs_passed += 1 

67 

68 if hasattr(vhd_info, 'children'): 

69 for vhd_info_sub in vhd_info.children: 

70 activated_list.extend(activateVdiChainAndCheck(vhd_info_sub, vg_name)) 

71 

72 return activated_list 

73 

74 

75def activateVdi(sr_uuid, vdi_uuid, vhd_path): 

76 name_space = NS_PREFIX_LVM + sr_uuid 

77 lock = Lock(vdi_uuid, name_space) 

78 lock.acquire() 

79 try: 

80 count = RefCounter.get(vdi_uuid, False, name_space) 

81 if count == 1: 

82 try: 

83 lvutil.activateNoRefcount(vhd_path, False) 

84 except Exception as e: 

85 util.SMlog(" lv activate failed for %s with error %s" % 

86 (vhd_path, str(e))) 

87 RefCounter.put(vdi_uuid, True, name_space) 

88 return False 

89 finally: 

90 lock.release() 

91 

92 return True 

93 

94 

95def deactivateVdi(sr_uuid, vdi_uuid, vhd_path): 

96 name_space = NS_PREFIX_LVM + sr_uuid 

97 lock = Lock(vdi_uuid, name_space) 

98 lock.acquire() 

99 try: 

100 count = RefCounter.put(vdi_uuid, False, name_space) 

101 if count > 0: 

102 return 

103 try: 

104 lvutil.deactivateNoRefcount(vhd_path) 

105 except Exception as e: 

106 util.SMlog(" lv de-activate failed for %s with error %s" % 

107 (vhd_path, str(e))) 

108 RefCounter.get(vdi_uuid, False, name_space) 

109 finally: 

110 lock.release() 

111 

112 

113def checkAllVHD(sr_uuid): 

114 activated_list = [] 

115 vhd_trees = [] 

116 VHDs_total = 0 

117 

118 vg_name = VG_PREFIX + sr_uuid 

119 vdi_type = VdiType.VHD 

120 pattern = "%s*" % LV_PREFIX[vdi_type] 

121 

122 # Do a vhd scan and gets all the VHDs 

123 vhds = getCowUtil(vdi_type).getAllInfoFromVG(pattern, LvmCowUtil.extractUuid, vg_name) 

124 VHDs_total = len(vhds) 

125 

126 # Build VHD chain, that way it will be easier to activate all the VHDs 

127 # that belong to one chain, do check on the same and then deactivate 

128 for vhd in vhds: 

129 if vhds[vhd].parentUuid: 

130 parent_VHD_info = vhds.get(vhds[vhd].parentUuid) 

131 if not hasattr(parent_VHD_info, 'children'): 

132 parent_VHD_info.children = [] 

133 parent_VHD_info.children.append(vhds[vhd]) 

134 else: 

135 vhd_trees.append(vhds[vhd]) 

136 

137 # If needed, activate VHDs belonging to each VDI chain, do a check on 

138 # all VHDs and then set the state back. 

139 for vhd_chain in vhd_trees: 

140 activated_list = activateVdiChainAndCheck(vhd_chain, vg_name) 

141 

142 #Deactivate the LVs, states are maintained by Refcounter 

143 for item in activated_list: 

144 deactivateVdi(sr_uuid, item[0], item[1]) 

145 

146 print("VHD check passed on %d, failed on %d, not run on %d" % 

147 (VHDs_passed, VHDs_failed, VHDs_total - (VHDs_passed + VHDs_failed))) 

148 

149if __name__ == '__main__': 

150 if len(sys.argv) == 1: 

151 print("Usage:") 

152 print("/opt/xensource/sm/verifyVHDsOnSR.py <sr_uuid>") 

153 else: 

154 checkAllVHD(sys.argv[1])