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, self.transport) 

121 if len(sv): 

122 self.nfsversion = sv[0] 

123 else: 

124 if not nfs.check_server_tcp(self.remoteserver, self.transport, self.nfsversion): 124 ↛ 125line 124 didn't jump to line 125, because the condition on line 124 was never true

125 raise nfs.NfsException("Unsupported NFS version: %s" % self.nfsversion) 

126 

127 except nfs.NfsException as exc: 

128 raise xs_errors.XenError('NFSVersion', 

129 opterr=exc.errstr) 

130 

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

132 try: 

133 nfs.soft_mount( 

134 mountpoint, self.remoteserver, remotepath, self.transport, 

135 useroptions=self.options, timeout=timeout, 

136 nfsversion=self.nfsversion, retrans=retrans) 

137 except nfs.NfsException as exc: 

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

139 

140 def attach(self, sr_uuid): 

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

142 self.validate_remotepath(False) 

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

144 self.mount_remotepath(sr_uuid) 

145 self._check_hardlinks() 

146 self.attached = True 

147 

148 def mount_remotepath(self, sr_uuid): 

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

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

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

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

153 # in mount instead 

154 self.check_server() 

155 # Extract timeout and retrans values, if any 

156 io_timeout = nfs.get_nfs_timeout(self.other_config) 

157 io_retrans = nfs.get_nfs_retrans(self.other_config) 

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

159 timeout=io_timeout, retrans=io_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.transport, 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): 

253 return NFSFileVDI(self, uuid) 

254 

255 def scan_exports(self, target): 

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

257 dom = nfs.scan_exports(target, self.transport) 

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

259 

260 def set_transport(self): 

261 self.transport = DEFAULT_TRANSPORT 

262 if self.remoteserver is None: 

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

264 return 

265 

266 try: 

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

268 except Exception: 

269 return 

270 

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

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

273 self.transport = 'tcp6' 

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

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

276 

277 

278class NFSFileVDI(FileSR.FileVDI): 

279 def attach(self, sr_uuid, vdi_uuid): 

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

281 self.xenstore_data = {} 

282 

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

284 

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

286 

287 def generate_config(self, sr_uuid, vdi_uuid): 

288 util.SMlog("NFSFileVDI.generate_config") 

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

290 raise xs_errors.XenError('VDIUnavailable') 

291 resp = {} 

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

293 resp['sr_uuid'] = sr_uuid 

294 resp['vdi_uuid'] = vdi_uuid 

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

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

297 resp['command'] = 'vdi_attach_from_config' 

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

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

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

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

302 

303 def attach_from_config(self, sr_uuid, vdi_uuid): 

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

305 also start a tapdisk on the file""" 

306 util.SMlog("NFSFileVDI.attach_from_config") 

307 try: 

308 self.sr.attach(sr_uuid) 

309 except: 

310 util.logException("NFSFileVDI.attach_from_config") 

311 raise xs_errors.XenError('SRUnavailable', \ 

312 opterr='Unable to attach from config') 

313 

314 

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

316 SRCommand.run(NFSSR, DRIVER_INFO) 

317else: 

318 SR.registerSR(NFSSR)