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# FileSR: local-file storage repository 

19 

20import socket 

21 

22import SR 

23import SRCommand 

24import FileSR 

25import util 

26import errno 

27import os 

28import sys 

29import xmlrpc.client 

30import xs_errors 

31import nfs 

32import vhdutil 

33from lock import Lock 

34import cleanup 

35 

36CAPABILITIES = ["SR_PROBE", "SR_UPDATE", "SR_CACHING", 

37 "VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH", 

38 "VDI_UPDATE", "VDI_CLONE", "VDI_SNAPSHOT", "VDI_RESIZE", 

39 "VDI_GENERATE_CONFIG", "VDI_MIRROR", 

40 "VDI_RESET_ON_BOOT/2", "ATOMIC_PAUSE", "VDI_CONFIG_CBT", 

41 "VDI_ACTIVATE", "VDI_DEACTIVATE", "THIN_PROVISIONING", "VDI_READ_CACHING"] 

42 

43CONFIGURATION = [['server', 'hostname or IP address of NFS server (required)'], 

44 ['serverpath', 'path on remote server (required)'], 

45 nfs.NFS_VERSION] 

46 

47DRIVER_INFO = { 

48 'name': 'NFS VHD', 

49 'description': 'SR plugin which stores disks as VHD files on a remote NFS filesystem', 

50 'vendor': 'Citrix Systems Inc', 

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

52 'driver_version': '1.0', 

53 'required_api_version': '1.0', 

54 'capabilities': CAPABILITIES, 

55 'configuration': CONFIGURATION 

56 } 

57 

58DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True} 

59 

60# The mountpoint for the directory when performing an sr_probe. All probes 

61# are guaranteed to be serialised by xapi, so this single mountpoint is fine. 

62PROBE_MOUNTPOINT = "probe" 

63NFSPORT = 2049 

64DEFAULT_TRANSPORT = "tcp" 

65PROBEVERSION = 'probeversion' 

66 

67 

68class NFSSR(FileSR.SharedFileSR): 

69 """NFS file-based storage repository""" 

70 

71 def handles(type): 

72 return type == 'nfs' 

73 handles = staticmethod(handles) 

74 

75 def load(self, sr_uuid): 

76 self.ops_exclusive = FileSR.OPS_EXCLUSIVE 

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

78 self.sr_vditype = SR.DEFAULT_TAP 

79 self.driver_config = DRIVER_CONFIG 

80 if 'server' not in self.dconf: 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true

81 raise xs_errors.XenError('ConfigServerMissing') 

82 self.remoteserver = self.dconf['server'] 

83 self.nosubdir = False 

84 if self.sr_ref and self.session is not None: 84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never true

85 self.sm_config = self.session.xenapi.SR.get_sm_config(self.sr_ref) 

86 self.other_config = self.session.xenapi.SR.get_other_config(self.sr_ref) 

87 else: 

88 self.sm_config = self.srcmd.params.get('sr_sm_config') or {} 

89 self.other_config = self.srcmd.params.get('sr_other_config') or {} 

90 self.nosubdir = self.sm_config.get('nosubdir') == "true" 

91 serverpath = self.dconf.get('serverpath') 

92 if serverpath is not None: 92 ↛ 97line 92 didn't jump to line 97, because the condition on line 92 was never false

93 self.remotepath = os.path.join( 

94 serverpath, 

95 not self.nosubdir and sr_uuid or "" 

96 ) 

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

98 

99 # Handle optional dconf attributes 

100 self.set_transport() 

101 self.nfsversion = nfs.validate_nfsversion(self.dconf.get('nfsversion')) 

102 if 'options' in self.dconf: 

103 self.options = self.dconf['options'] 

104 else: 

105 self.options = '' 

106 

107 def validate_remotepath(self, scan): 

108 serverpath = self.dconf.get('serverpath') 

109 if serverpath is None: 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true

110 if scan: 

111 try: 

112 self.scan_exports(self.dconf['server']) 

113 except: 

114 pass 

115 raise xs_errors.XenError('ConfigServerPathMissing') 

116 

117 def check_server(self): 

118 try: 

119 if PROBEVERSION in self.dconf: 119 ↛ 120line 119 didn't jump to line 120, because the condition on line 119 was never true

120 sv = nfs.get_supported_nfs_versions(self.remoteserver) 

121 if len(sv): 

122 self.nfsversion = sv[0] 

123 else: 

124 nfs.check_server_tcp(self.remoteserver, self.nfsversion) 

125 except nfs.NfsException as exc: 

126 raise xs_errors.XenError('NFSVersion', 

127 opterr=exc.errstr) 

128 

129 def mount(self, mountpoint, remotepath, timeout=None, retrans=None): 

130 try: 

131 nfs.soft_mount( 

132 mountpoint, self.remoteserver, remotepath, self.transport, 

133 useroptions=self.options, timeout=timeout, 

134 nfsversion=self.nfsversion, retrans=retrans) 

135 except nfs.NfsException as exc: 

136 raise xs_errors.XenError('NFSMount', opterr=exc.errstr) 

137 

138 def attach(self, sr_uuid): 

139 if not self._checkmount(): 139 ↛ 149line 139 didn't jump to line 149, because the condition on line 139 was never false

140 self.validate_remotepath(False) 

141 util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget') 

142 #Extract timeout and retrans values, if any 

143 io_timeout = nfs.get_nfs_timeout(self.other_config) 

144 io_retrans = nfs.get_nfs_retrans(self.other_config) 

145 self.mount_remotepath(sr_uuid, timeout=io_timeout, 

146 retrans=io_retrans) 

147 

148 self._check_hardlinks() 

149 self.attached = True 

150 

151 def mount_remotepath(self, sr_uuid, timeout=5, retrans=5): 

152 if not self._checkmount(): 152 ↛ exitline 152 didn't return from function 'mount_remotepath', because the condition on line 152 was never false

153 # FIXME: What is the purpose of this check_server? 

154 # It doesn't stop us from continuing if the server 

155 # doesn't support the requested version. We fail 

156 # in mount instead 

157 self.check_server() 

158 self.mount(self.path, self.remotepath, 

159 timeout=timeout, retrans=retrans) 

160 

161 def probe(self): 

162 # Verify NFS target and port 

163 util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget') 

164 

165 self.validate_remotepath(True) 

166 self.check_server() 

167 

168 temppath = os.path.join(SR.MOUNT_BASE, PROBE_MOUNTPOINT) 

169 

170 self.mount(temppath, self.remotepath) 

171 try: 

172 return nfs.scan_srlist(temppath, self.dconf) 

173 finally: 

174 try: 

175 nfs.unmount(temppath, True) 

176 except: 

177 pass 

178 

179 def detach(self, sr_uuid): 

180 """Detach the SR: Unmounts and removes the mountpoint""" 

181 if not self._checkmount(): 181 ↛ 183line 181 didn't jump to line 183, because the condition on line 181 was never false

182 return 

183 util.SMlog("Aborting GC/coalesce") 

184 cleanup.abort(self.uuid) 

185 

186 # Change directory to avoid unmount conflicts 

187 os.chdir(SR.MOUNT_BASE) 

188 

189 try: 

190 nfs.unmount(self.path, True) 

191 except nfs.NfsException as exc: 

192 raise xs_errors.XenError('NFSUnMount', opterr=exc.errstr) 

193 

194 self.attached = False 

195 

196 def create(self, sr_uuid, size): 

197 util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget') 

198 self.validate_remotepath(True) 

199 if self._checkmount(): 199 ↛ 200line 199 didn't jump to line 200, because the condition on line 199 was never true

200 raise xs_errors.XenError('NFSAttached') 

201 

202 # Set the target path temporarily to the base dir 

203 # so that we can create the target SR directory 

204 self.remotepath = self.dconf['serverpath'] 

205 try: 

206 self.mount_remotepath(sr_uuid) 

207 except Exception as exn: 

208 try: 

209 os.rmdir(self.path) 

210 except: 

211 pass 

212 raise 

213 

214 if not self.nosubdir: 214 ↛ 229line 214 didn't jump to line 229, because the condition on line 214 was never false

215 newpath = os.path.join(self.path, sr_uuid) 

216 if util.ioretry(lambda: util.pathexists(newpath)): 216 ↛ 217line 216 didn't jump to line 217, because the condition on line 216 was never true

217 if len(util.ioretry(lambda: util.listdir(newpath))) != 0: 

218 self.detach(sr_uuid) 

219 raise xs_errors.XenError('SRExists') 

220 else: 

221 try: 

222 util.ioretry(lambda: util.makedirs(newpath)) 

223 except util.CommandException as inst: 

224 if inst.code != errno.EEXIST: 

225 self.detach(sr_uuid) 

226 raise xs_errors.XenError('NFSCreate', 

227 opterr='remote directory creation error is %d' 

228 % inst.code) 

229 self.detach(sr_uuid) 

230 

231 def delete(self, sr_uuid): 

232 # try to remove/delete non VDI contents first 

233 super(NFSSR, self).delete(sr_uuid) 

234 try: 

235 if self._checkmount(): 

236 self.detach(sr_uuid) 

237 

238 # Set the target path temporarily to the base dir 

239 # so that we can remove the target SR directory 

240 self.remotepath = self.dconf['serverpath'] 

241 self.mount_remotepath(sr_uuid) 

242 if not self.nosubdir: 

243 newpath = os.path.join(self.path, sr_uuid) 

244 if util.ioretry(lambda: util.pathexists(newpath)): 

245 util.ioretry(lambda: os.rmdir(newpath)) 

246 self.detach(sr_uuid) 

247 except util.CommandException as inst: 

248 self.detach(sr_uuid) 

249 if inst.code != errno.ENOENT: 

250 raise xs_errors.XenError('NFSDelete') 

251 

252 def vdi(self, uuid, loadLocked=False): 

253 if not loadLocked: 

254 return NFSFileVDI(self, uuid) 

255 return NFSFileVDI(self, uuid) 

256 

257 def scan_exports(self, target): 

258 util.SMlog("scanning2 (target=%s)" % target) 

259 dom = nfs.scan_exports(target) 

260 print(dom.toprettyxml(), file=sys.stderr) 

261 

262 def set_transport(self): 

263 self.transport = DEFAULT_TRANSPORT 

264 if self.remoteserver is None: 

265 # CA-365359: on_slave.is_open sends a dconf with {"server": None} 

266 return 

267 

268 try: 

269 addr_info = socket.getaddrinfo(self.remoteserver, 0)[0] 

270 except Exception: 

271 return 

272 

273 use_ipv6 = addr_info[0] == socket.AF_INET6 

274 if use_ipv6: 274 ↛ 276line 274 didn't jump to line 276, because the condition on line 274 was never false

275 self.transport = 'tcp6' 

276 if 'useUDP' in self.dconf and self.dconf['useUDP'] == 'true': 276 ↛ 277line 276 didn't jump to line 277, because the condition on line 276 was never true

277 self.transport = 'udp6' if use_ipv6 else 'udp' 

278 

279 

280class NFSFileVDI(FileSR.FileVDI): 

281 def attach(self, sr_uuid, vdi_uuid): 

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

283 self.xenstore_data = {} 

284 

285 self.xenstore_data["storage-type"] = "nfs" 

286 

287 return super(NFSFileVDI, self).attach(sr_uuid, vdi_uuid) 

288 

289 def generate_config(self, sr_uuid, vdi_uuid): 

290 util.SMlog("NFSFileVDI.generate_config") 

291 if not util.pathexists(self.path): 

292 raise xs_errors.XenError('VDIUnavailable') 

293 resp = {} 

294 resp['device_config'] = self.sr.dconf 

295 resp['sr_uuid'] = sr_uuid 

296 resp['vdi_uuid'] = vdi_uuid 

297 resp['sr_sm_config'] = self.sr.sm_config 

298 resp['sr_other_config'] = self.sr.other_config 

299 resp['command'] = 'vdi_attach_from_config' 

300 # Return the 'config' encoded within a normal XMLRPC response so that 

301 # we can use the regular response/error parsing code. 

302 config = xmlrpc.client.dumps(tuple([resp]), "vdi_attach_from_config") 

303 return xmlrpc.client.dumps((config, ), "", True) 

304 

305 def attach_from_config(self, sr_uuid, vdi_uuid): 

306 """Used for HA State-file only. Will not just attach the VDI but 

307 also start a tapdisk on the file""" 

308 util.SMlog("NFSFileVDI.attach_from_config") 

309 try: 

310 self.sr.attach(sr_uuid) 

311 except: 

312 util.logException("NFSFileVDI.attach_from_config") 

313 raise xs_errors.XenError('SRUnavailable', \ 

314 opterr='Unable to attach from config') 

315 

316 

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

318 SRCommand.run(NFSSR, DRIVER_INFO) 

319else: 

320 SR.registerSR(NFSSR)