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# 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# 

16# A plugin for enabling trim on LVM based SRs to free up storage space 

17# in Storage arrays. 

18 

19import xml 

20import os 

21import time 

22import util 

23import lock 

24import lvhdutil 

25import vhdutil 

26import lvutil 

27 

28TRIM_LV_TAG = "_trim_lv" 

29TRIM_CAP = "SR_TRIM" 

30LOCK_RETRY_ATTEMPTS = 3 

31LOCK_RETRY_INTERVAL = 1 

32ERROR_CODE_KEY = "errcode" 

33ERROR_MSG_KEY = "errmsg" 

34 

35TRIM_LAST_TRIGGERED_KEY = "trim_last_triggered" 

36MASTER_LVM_CONF = '/etc/lvm/master' 

37 

38 

39def _vg_by_sr_uuid(sr_uuid): 

40 return lvhdutil.VG_PREFIX + sr_uuid 

41 

42 

43def _lvpath_by_vg_lv_name(vg_name, lv_name): 

44 return os.path.join(lvhdutil.VG_LOCATION, vg_name, lv_name) 

45 

46 

47def to_xml(d): 

48 

49 dom = xml.dom.minidom.Document() 

50 trim_response = dom.createElement("trim_response") 

51 dom.appendChild(trim_response) 

52 

53 for key, value in sorted(d.items()): 

54 key_value_element = dom.createElement("key_value_pair") 

55 trim_response.appendChild(key_value_element) 

56 

57 key_element = dom.createElement("key") 

58 key_text_node = dom.createTextNode(key) 

59 key_element.appendChild(key_text_node) 

60 key_value_element.appendChild(key_element) 

61 

62 value_element = dom.createElement("value") 

63 value_text_mode = dom.createTextNode(value) 

64 value_element.appendChild(value_text_mode) 

65 key_value_element.appendChild(value_element) 

66 

67 return dom.toxml() 

68 

69 

70# Note: This function is expected to be called from a context where 

71# the SR is locked by the thread calling the function; therefore removing 

72# any risk of a race condition updating the LAST_TRIGGERED value. 

73def _log_last_triggered(session, sr_uuid): 

74 try: 

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

76 other_config = session.xenapi.SR.get_other_config(sr_ref) 

77 if TRIM_LAST_TRIGGERED_KEY in other_config: 

78 session.xenapi.SR.remove_from_other_config(sr_ref, TRIM_LAST_TRIGGERED_KEY) 

79 session.xenapi.SR.add_to_other_config(sr_ref, TRIM_LAST_TRIGGERED_KEY, str(time.time())) 

80 except: 

81 util.logException("Unable to set other-config:%s" % TRIM_LAST_TRIGGERED_KEY) 

82 

83 

84def do_trim(session, args): 

85 """Attempt to trim the given LVHDSR""" 

86 util.SMlog("do_trim: %s" % args) 

87 sr_uuid = args["sr_uuid"] 

88 os.environ['LVM_SYSTEM_DIR'] = MASTER_LVM_CONF 

89 

90 if TRIM_CAP not in util.sr_get_capability(sr_uuid): 

91 util.SMlog("Trim command ignored on unsupported SR %s" % sr_uuid) 

92 err_msg = {ERROR_CODE_KEY: 'UnsupportedSRForTrim', 

93 ERROR_MSG_KEY: 'Trim on [%s] not supported' % sr_uuid} 

94 return to_xml(err_msg) 

95 

96 # Lock SR, get vg empty space details 

97 sr_lock = lock.Lock(vhdutil.LOCK_TYPE_SR, sr_uuid) 

98 got_lock = False 

99 for i in range(LOCK_RETRY_ATTEMPTS): 

100 got_lock = sr_lock.acquireNoblock() 

101 if got_lock: 

102 break 

103 time.sleep(LOCK_RETRY_INTERVAL) 

104 

105 if got_lock: 

106 try: 

107 vg_name = _vg_by_sr_uuid(sr_uuid) 

108 lv_name = sr_uuid + TRIM_LV_TAG 

109 lv_path = _lvpath_by_vg_lv_name(vg_name, lv_name) 

110 

111 # Clean trim LV in case the previous trim attemp failed 

112 if lvutil.exists(lv_path): 

113 lvutil.remove(lv_path) 

114 

115 #Check if VG limits are enough for creating LV. 

116 stats = lvutil._getVGstats(vg_name) 

117 if (stats['freespace'] < lvutil.LVM_SIZE_INCREMENT): 

118 util.SMlog("No space to claim on a full SR %s" % sr_uuid) 

119 err_msg = {ERROR_CODE_KEY: 'Trim failed on full SR', 

120 ERROR_MSG_KEY: 'No space to claim on a full SR'} 

121 result = to_xml(err_msg) 

122 else: 

123 # Perform a lvcreate, blkdiscard and lvremove to 

124 # trigger trim on the array 

125 lvutil.create(lv_name, 0, vg_name, size_in_percentage="100%F") 

126 cmd = ["/usr/sbin/blkdiscard", "-v", lv_path] 

127 stdout = util.pread2(cmd) 

128 util.SMlog("Stdout is %s" % stdout) 

129 util.SMlog("Trim on SR: %s complete. " % sr_uuid) 

130 result = str(True) 

131 except util.CommandException as e: 131 ↛ 132line 131 didn't jump to line 132

132 err_msg = { 

133 ERROR_CODE_KEY: 'TrimException', 

134 ERROR_MSG_KEY: e.reason 

135 } 

136 result = to_xml(err_msg) 

137 except: 

138 err_msg = { 

139 ERROR_CODE_KEY: 'UnknownTrimException', 

140 ERROR_MSG_KEY: 'Unknown Exception: trim failed on SR [%s]' 

141 % sr_uuid 

142 } 

143 result = to_xml(err_msg) 

144 finally: 

145 if lvutil.exists(lv_path): 

146 lvutil.remove(lv_path) 

147 

148 _log_last_triggered(session, sr_uuid) 

149 

150 sr_lock.release() 

151 return result 

152 else: 

153 util.SMlog("Could not complete Trim on %s, Lock unavailable !" \ 

154 % sr_uuid) 

155 err_msg = {ERROR_CODE_KEY: 'SRUnavailable', 

156 ERROR_MSG_KEY: 'Unable to get SR lock [%s]' % sr_uuid} 

157 return to_xml(err_msg)