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 ↛ 144line 139 didn't jump to line 144, because the condition on line 139 was never false

140 self.validate_remotepath(False) 

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

142 self.mount_remotepath(sr_uuid) 

143 self._check_hardlinks() 

144 self.attached = True 

145 

146 def mount_remotepath(self, sr_uuid): 

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

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

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

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

151 # in mount instead 

152 self.check_server() 

153 # Extract timeout and retrans values, if any 

154 io_timeout = nfs.get_nfs_timeout(self.other_config) 

155 io_retrans = nfs.get_nfs_retrans(self.other_config) 

156 self.mount(self.path, self.remotepath, 

157 timeout=io_timeout, retrans=io_retrans) 

158 

159 def probe(self): 

160 # Verify NFS target and port 

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

162 

163 self.validate_remotepath(True) 

164 self.check_server() 

165 

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

167 

168 self.mount(temppath, self.remotepath) 

169 try: 

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

171 finally: 

172 try: 

173 nfs.unmount(temppath, True) 

174 except: 

175 pass 

176 

177 def detach(self, sr_uuid): 

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

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

180 return 

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

182 cleanup.abort(self.uuid) 

183 

184 # Change directory to avoid unmount conflicts 

185 os.chdir(SR.MOUNT_BASE) 

186 

187 try: 

188 nfs.unmount(self.path, True) 

189 except nfs.NfsException as exc: 

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

191 

192 self.attached = False 

193 

194 def create(self, sr_uuid, size): 

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

196 self.validate_remotepath(True) 

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

198 raise xs_errors.XenError('NFSAttached') 

199 

200 # Set the target path temporarily to the base dir 

201 # so that we can create the target SR directory 

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

203 try: 

204 self.mount_remotepath(sr_uuid) 

205 except Exception as exn: 

206 try: 

207 os.rmdir(self.path) 

208 except: 

209 pass 

210 raise 

211 

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

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

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

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

216 self.detach(sr_uuid) 

217 raise xs_errors.XenError('SRExists') 

218 else: 

219 try: 

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

221 except util.CommandException as inst: 

222 if inst.code != errno.EEXIST: 

223 self.detach(sr_uuid) 

224 raise xs_errors.XenError('NFSCreate', 

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

226 % inst.code) 

227 self.detach(sr_uuid) 

228 

229 def delete(self, sr_uuid): 

230 # try to remove/delete non VDI contents first 

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

232 try: 

233 if self._checkmount(): 

234 self.detach(sr_uuid) 

235 

236 # Set the target path temporarily to the base dir 

237 # so that we can remove the target SR directory 

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

239 self.mount_remotepath(sr_uuid) 

240 if not self.nosubdir: 

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

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

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

244 self.detach(sr_uuid) 

245 except util.CommandException as inst: 

246 self.detach(sr_uuid) 

247 if inst.code != errno.ENOENT: 

248 raise xs_errors.XenError('NFSDelete') 

249 

250 def vdi(self, uuid): 

251 return NFSFileVDI(self, uuid) 

252 

253 def scan_exports(self, target): 

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

255 dom = nfs.scan_exports(target) 

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

257 

258 def set_transport(self): 

259 self.transport = DEFAULT_TRANSPORT 

260 if self.remoteserver is None: 

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

262 return 

263 

264 try: 

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

266 except Exception: 

267 return 

268 

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

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

271 self.transport = 'tcp6' 

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

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

274 

275 

276class NFSFileVDI(FileSR.FileVDI): 

277 def attach(self, sr_uuid, vdi_uuid): 

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

279 self.xenstore_data = {} 

280 

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

282 

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

284 

285 def generate_config(self, sr_uuid, vdi_uuid): 

286 util.SMlog("NFSFileVDI.generate_config") 

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

288 raise xs_errors.XenError('VDIUnavailable') 

289 resp = {} 

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

291 resp['sr_uuid'] = sr_uuid 

292 resp['vdi_uuid'] = vdi_uuid 

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

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

295 resp['command'] = 'vdi_attach_from_config' 

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

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

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

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

300 

301 def attach_from_config(self, sr_uuid, vdi_uuid): 

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

303 also start a tapdisk on the file""" 

304 util.SMlog("NFSFileVDI.attach_from_config") 

305 try: 

306 self.sr.attach(sr_uuid) 

307 except: 

308 util.logException("NFSFileVDI.attach_from_config") 

309 raise xs_errors.XenError('SRUnavailable', \ 

310 opterr='Unable to attach from config') 

311 

312 

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

314 SRCommand.run(NFSSR, DRIVER_INFO) 

315else: 

316 SR.registerSR(NFSSR)