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# EXTSR: Based on local-file storage repository, mounts ext3 partition 

19 

20import SR 

21from SR import deviceCheck 

22import SRCommand 

23import FileSR 

24import util 

25import lvutil 

26import scsiutil 

27 

28import os 

29import xs_errors 

30import vhdutil 

31from lock import Lock 

32from constants import EXT_PREFIX 

33 

34CAPABILITIES = ["SR_PROBE", "SR_UPDATE", "SR_SUPPORTS_LOCAL_CACHING", 

35 "VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH", 

36 "VDI_UPDATE", "VDI_CLONE", "VDI_SNAPSHOT", "VDI_RESIZE", "VDI_MIRROR", 

37 "VDI_GENERATE_CONFIG", 

38 "VDI_RESET_ON_BOOT/2", "ATOMIC_PAUSE", "VDI_CONFIG_CBT", 

39 "VDI_ACTIVATE", "VDI_DEACTIVATE", "THIN_PROVISIONING", "VDI_READ_CACHING"] 

40 

41CONFIGURATION = [['device', 'local device path (required) (e.g. /dev/sda3)']] 

42 

43DRIVER_INFO = { 

44 'name': 'Local EXT3 VHD', 

45 'description': 'SR plugin which represents disks as VHD files stored on a local EXT3 filesystem, created inside an LVM volume', 

46 'vendor': 'Citrix Systems Inc', 

47 'copyright': '(C) 2008 Citrix Systems Inc', 

48 'driver_version': '1.0', 

49 'required_api_version': '1.0', 

50 'capabilities': CAPABILITIES, 

51 'configuration': CONFIGURATION 

52 } 

53 

54DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True} 

55 

56 

57class EXTSR(FileSR.FileSR): 

58 """EXT3 Local file storage repository""" 

59 

60 def handles(srtype): 

61 return srtype == 'ext' 

62 handles = staticmethod(handles) 

63 

64 def load(self, sr_uuid): 

65 self.ops_exclusive = FileSR.OPS_EXCLUSIVE 

66 self.lock = Lock(vhdutil.LOCK_TYPE_SR, self.uuid) 

67 self.sr_vditype = SR.DEFAULT_TAP 

68 

69 self.path = os.path.join(SR.MOUNT_BASE, sr_uuid) 

70 self.vgname = EXT_PREFIX + sr_uuid 

71 self.remotepath = os.path.join("/dev", self.vgname, sr_uuid) 

72 self.attached = self._checkmount() 

73 self.driver_config = DRIVER_CONFIG 

74 

75 def delete(self, sr_uuid): 

76 super(EXTSR, self).delete(sr_uuid) 

77 

78 # Check PVs match VG 

79 try: 

80 for dev in self.dconf['device'].split(','): 

81 cmd = ["pvs", dev] 

82 txt = util.pread2(cmd) 

83 if txt.find(self.vgname) == -1: 

84 raise xs_errors.XenError('VolNotFound', 

85 opterr='volume is %s' % self.vgname) 

86 except util.CommandException as inst: 

87 raise xs_errors.XenError('PVSfailed', 

88 opterr='error is %d' % inst.code) 

89 

90 # Remove LV, VG and pv 

91 try: 

92 cmd = ["lvremove", "-f", self.remotepath] 

93 util.pread2(cmd) 

94 

95 cmd = ["vgremove", self.vgname] 

96 util.pread2(cmd) 

97 

98 for dev in self.dconf['device'].split(','): 

99 cmd = ["pvremove", dev] 

100 util.pread2(cmd) 

101 except util.CommandException as inst: 

102 raise xs_errors.XenError('LVMDelete', 

103 opterr='errno is %d' % inst.code) 

104 

105 def attach(self, sr_uuid): 

106 if not self._checkmount(): 

107 try: 

108 #Activate LV 

109 cmd = ['lvchange', '-ay', self.remotepath] 

110 util.pread2(cmd) 

111 except util.CommandException as inst: 

112 raise xs_errors.XenError( 

113 'LVMMount', 

114 opterr='Unable to activate LV. Errno is %d' % inst.code) 

115 

116 try: 

117 util.pread(["fsck", "-a", self.remotepath]) 

118 except util.CommandException as inst: 

119 if inst.code == 1: 

120 util.SMlog("FSCK detected and corrected FS errors. Not fatal.") 

121 else: 

122 raise xs_errors.XenError( 

123 'LVMMount', 

124 opterr='FSCK failed on %s. Errno is %d' % (self.remotepath, inst.code)) 

125 

126 super(EXTSR, self).attach(sr_uuid, bind=False) 

127 

128 self.attached = True 

129 

130 #Update SCSIid string 

131 scsiutil.add_serial_record( 

132 self.session, self.sr_ref, 

133 scsiutil.devlist_to_serialstring(self.dconf['device'].split(','))) 

134 

135 # Set the block scheduler 

136 for dev in self.dconf['device'].split(','): 

137 self.block_setscheduler(dev) 

138 

139 def detach(self, sr_uuid): 

140 super(EXTSR, self).detach(sr_uuid) 

141 try: 

142 # deactivate SR 

143 cmd = ["lvchange", "-an", self.remotepath] 

144 util.pread2(cmd) 

145 except util.CommandException as inst: 

146 raise xs_errors.XenError( 

147 'LVMUnMount', 

148 opterr='lvm -an failed errno is %d' % inst.code) 

149 

150 @deviceCheck 

151 def probe(self): 

152 return lvutil.srlist_toxml(lvutil.scan_srlist(EXT_PREFIX, self.dconf['device']), 

153 EXT_PREFIX) 

154 

155 @deviceCheck 

156 def create(self, sr_uuid, size): 

157 if self._checkmount(): 

158 raise xs_errors.XenError('SRExists') 

159 

160 # Check none of the devices already in use by other PBDs 

161 if util.test_hostPBD_devs(self.session, sr_uuid, self.dconf['device']): 

162 raise xs_errors.XenError('SRInUse') 

163 

164 # Check serial number entry in SR records 

165 for dev in self.dconf['device'].split(','): 

166 if util.test_scsiserial(self.session, dev): 

167 raise xs_errors.XenError('SRInUse') 

168 

169 if not lvutil._checkVG(self.vgname): 

170 lvutil.createVG(self.dconf['device'], self.vgname) 

171 

172 if lvutil._checkLV(self.remotepath): 

173 raise xs_errors.XenError('SRExists') 

174 

175 try: 

176 numdevs = len(self.dconf['device'].split(',')) 

177 cmd = ["lvcreate", "-n", sr_uuid] 

178 if numdevs > 1: 

179 lowest = -1 

180 for dev in self.dconf['device'].split(','): 

181 stats = lvutil._getPVstats(dev) 

182 if lowest < 0 or stats['freespace'] < lowest: 

183 lowest = stats['freespace'] 

184 size_mb = (lowest // (1024 * 1024)) * numdevs 

185 

186 # Add stripe parameter to command 

187 cmd += ["-i", str(numdevs), "-I", "2048"] 

188 else: 

189 stats = lvutil._getVGstats(self.vgname) 

190 size_mb = stats['freespace'] // (1024 * 1024) 

191 assert(size_mb > 0) 

192 cmd += ["-L", str(size_mb), self.vgname] 

193 text = util.pread(cmd) 

194 

195 cmd = ["lvchange", "-ay", self.remotepath] 

196 text = util.pread(cmd) 

197 except util.CommandException as inst: 

198 raise xs_errors.XenError( 

199 'LVMCreate', 

200 opterr='lv operation, error %d' % inst.code) 

201 except AssertionError: 

202 raise xs_errors.XenError( 

203 'SRNoSpace', 

204 opterr='Insufficient space in VG %s' % self.vgname) 

205 

206 try: 

207 util.pread2(["mkfs.ext4", "-F", self.remotepath]) 

208 except util.CommandException as inst: 

209 raise xs_errors.XenError('LVMFilesystem', 

210 opterr='mkfs failed error %d' % inst.code) 

211 

212 #Update serial number string 

213 scsiutil.add_serial_record( 

214 self.session, self.sr_ref, 

215 scsiutil.devlist_to_serialstring(self.dconf['device'].split(','))) 

216 

217 def vdi(self, uuid): 

218 return EXTFileVDI(self, uuid) 

219 

220 

221class EXTFileVDI(FileSR.FileVDI): 

222 def attach(self, sr_uuid, vdi_uuid): 

223 if not hasattr(self, 'xenstore_data'): 

224 self.xenstore_data = {} 

225 

226 self.xenstore_data["storage-type"] = "ext" 

227 

228 return super(EXTFileVDI, self).attach(sr_uuid, vdi_uuid) 

229 

230 

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

232 SRCommand.run(EXTSR, DRIVER_INFO) 

233else: 

234 SR.registerSR(EXTSR)