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 

27import lvhdutil 

28import vhdutil 

29 

30from lock import Lock 

31from refcounter import RefCounter 

32 

33# Stores the vdi activated, comes handy while deactivating 

34VHDs_passed = 0 

35VHDs_failed = 0 

36 

37 

38def activateVdiChainAndCheck(vhd_info, vg_name): 

39 global VHDs_passed 

40 global VHDs_failed 

41 activated_list = [] 

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

43 if not activateVdi( 

44 vg_name.lstrip(lvhdutil.VG_PREFIX), 

45 vhd_info.uuid, 

46 vhd_path): 

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

48 # check on the VDIs down the chain 

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

50 vg_name) 

51 return activated_list 

52 

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

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

55 if not vhdutil.check(vhd_path, True): 

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

57 VHDs_failed += 1 

58 else: 

59 VHDs_passed += 1 

60 

61 if hasattr(vhd_info, 'children'): 

62 for vhd_info_sub in vhd_info.children: 

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

64 

65 return activated_list 

66 

67 

68def activateVdi(sr_uuid, vdi_uuid, vhd_path): 

69 name_space = lvhdutil.NS_PREFIX_LVM + sr_uuid 

70 lock = Lock(vdi_uuid, name_space) 

71 lock.acquire() 

72 try: 

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

74 if count == 1: 

75 try: 

76 lvutil.activateNoRefcount(vhd_path, False) 

77 except Exception as e: 

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

79 (vhd_path, str(e))) 

80 RefCounter.put(vdi_uuid, True, name_space) 

81 return False 

82 finally: 

83 lock.release() 

84 

85 return True 

86 

87 

88def deactivateVdi(sr_uuid, vdi_uuid, vhd_path): 

89 name_space = lvhdutil.NS_PREFIX_LVM + sr_uuid 

90 lock = Lock(vdi_uuid, name_space) 

91 lock.acquire() 

92 try: 

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

94 if count > 0: 

95 return 

96 try: 

97 lvutil.deactivateNoRefcount(vhd_path) 

98 except Exception as e: 

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

100 (vhd_path, str(e))) 

101 RefCounter.get(vdi_uuid, False, name_space) 

102 finally: 

103 lock.release() 

104 

105 

106def checkAllVHD(sr_uuid): 

107 activated_list = [] 

108 vhd_trees = [] 

109 VHDs_total = 0 

110 

111 vg_name = lvhdutil.VG_PREFIX + sr_uuid 

112 pattern = "%s*" % lvhdutil.LV_PREFIX[vhdutil.VDI_TYPE_VHD] 

113 

114 # Do a vhd scan and gets all the VHDs 

115 vhds = vhdutil.getAllVHDs(pattern, lvhdutil.extractUuid, vg_name) 

116 VHDs_total = len(vhds) 

117 

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

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

120 for vhd in vhds: 

121 if vhds[vhd].parentUuid: 

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

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

124 parent_VHD_info.children = [] 

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

126 else: 

127 vhd_trees.append(vhds[vhd]) 

128 

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

130 # all VHDs and then set the state back. 

131 for vhd_chain in vhd_trees: 

132 activated_list = activateVdiChainAndCheck(vhd_chain, vg_name) 

133 

134 #Deactivate the LVs, states are maintained by Refcounter 

135 for item in activated_list: 

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

137 

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

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

140 

141if __name__ == '__main__': 

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

143 print("Usage:") 

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

145 else: 

146 checkAllVHD(sys.argv[1])