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 

112 # make a mountpoint: 

113 if not os.path.isdir(self.path): 

114 os.makedirs(self.path) 

115 except util.CommandException as inst: 

116 raise xs_errors.XenError( 

117 'LVMMount', 

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

119 

120 try: 

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

122 except util.CommandException as inst: 

123 if inst.code == 1: 

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

125 else: 

126 raise xs_errors.XenError( 

127 'LVMMount', 

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

129 

130 try: 

131 util.pread(["mount", self.remotepath, self.path]) 

132 except util.CommandException as inst: 

133 raise xs_errors.XenError( 

134 'LVMMount', 

135 opterr='Failed to mount FS. Errno is %d' % inst.code) 

136 

137 self.attached = True 

138 

139 #Update SCSIid string 

140 scsiutil.add_serial_record( 

141 self.session, self.sr_ref, 

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

143 

144 # Set the block scheduler 

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

146 self.block_setscheduler(dev) 

147 

148 def detach(self, sr_uuid): 

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

150 try: 

151 # deactivate SR 

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

153 util.pread2(cmd) 

154 except util.CommandException as inst: 

155 raise xs_errors.XenError( 

156 'LVMUnMount', 

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

158 

159 @deviceCheck 

160 def probe(self): 

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

162 EXT_PREFIX) 

163 

164 @deviceCheck 

165 def create(self, sr_uuid, size): 

166 if self._checkmount(): 

167 raise xs_errors.XenError('SRExists') 

168 

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

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

171 raise xs_errors.XenError('SRInUse') 

172 

173 # Check serial number entry in SR records 

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

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

176 raise xs_errors.XenError('SRInUse') 

177 

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

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

180 

181 if lvutil._checkLV(self.remotepath): 

182 raise xs_errors.XenError('SRExists') 

183 

184 try: 

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

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

187 if numdevs > 1: 

188 lowest = -1 

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

190 stats = lvutil._getPVstats(dev) 

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

192 lowest = stats['freespace'] 

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

194 

195 # Add stripe parameter to command 

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

197 else: 

198 stats = lvutil._getVGstats(self.vgname) 

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

200 assert(size_mb > 0) 

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

202 text = util.pread(cmd) 

203 

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

205 text = util.pread(cmd) 

206 except util.CommandException as inst: 

207 raise xs_errors.XenError( 

208 'LVMCreate', 

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

210 except AssertionError: 

211 raise xs_errors.XenError( 

212 'SRNoSpace', 

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

214 

215 try: 

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

217 except util.CommandException as inst: 

218 raise xs_errors.XenError('LVMFilesystem', 

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

220 

221 #Update serial number string 

222 scsiutil.add_serial_record( 

223 self.session, self.sr_ref, 

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

225 

226 def vdi(self, uuid): 

227 return EXTFileVDI(self, uuid) 

228 

229 

230class EXTFileVDI(FileSR.FileVDI): 

231 def attach(self, sr_uuid, vdi_uuid): 

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

233 self.xenstore_data = {} 

234 

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

236 

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

238 

239 

240if __name__ == '__main__': 

241 SRCommand.run(EXTSR, DRIVER_INFO) 

242else: 

243 SR.registerSR(EXTSR)