Coverage for drivers/GlusterFSSR.py : 21%
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/env python3
2#
3# Original work copyright (C) Citrix systems
4# Modified work copyright (C) Vates SAS and XCP-ng community
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU Lesser General Public License as published
8# by the Free Software Foundation; version 2.1 only.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with this program; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19from sm_typing import override
21import errno
22import os
23import syslog as _syslog
24import xmlrpc.client
25from syslog import syslog
27# careful with the import order here
28# FileSR has a circular dependency: FileSR- > blktap2 -> lvutil -> EXTSR -> FileSR
29# importing in this order seems to avoid triggering the issue.
30import SR
31import SRCommand
32import FileSR
33# end of careful
34import VDI
35import cleanup
36import lock
37import util
38import xs_errors
40CAPABILITIES = ["SR_PROBE", "SR_UPDATE",
41 "VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH",
42 "VDI_UPDATE", "VDI_CLONE", "VDI_SNAPSHOT", "VDI_RESIZE", "VDI_MIRROR",
43 "VDI_GENERATE_CONFIG",
44 "VDI_RESET_ON_BOOT/2", "ATOMIC_PAUSE"]
46CONFIGURATION = [['server', 'Full path to share on gluster server (required, ex: "192.168.0.12:/gv0")'],
47 ['backupservers', 'list of servers separated by ":"'],
48 ['fetchattempts', 'number of attempts to fetch files before switching to the backup server']
49 ]
51DRIVER_INFO = {
52 'name': 'GlusterFS VHD and QCOW2',
53 'description': 'SR plugin which stores disks as VHD and QCOW2 files on a GlusterFS storage',
54 'vendor': 'Vates SAS',
55 'copyright': '(C) 2020 Vates SAS',
56 'driver_version': '1.0',
57 'required_api_version': '1.0',
58 'capabilities': CAPABILITIES,
59 'configuration': CONFIGURATION
60}
62DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True}
64# The mountpoint for the directory when performing an sr_probe. All probes
65# are guaranteed to be serialised by xapi, so this single mountpoint is fine.
66PROBE_MOUNTPOINT = os.path.join(SR.MOUNT_BASE, "probe")
69class GlusterFSException(Exception):
70 def __init__(self, errstr):
71 self.errstr = errstr
74# mountpoint = /var/run/sr-mount/GlusterFS/<glusterfs_server_name>/uuid
75# linkpath = mountpoint/uuid - path to SR directory on share
76# path = /var/run/sr-mount/uuid - symlink to SR directory on share
77class GlusterFSSR(FileSR.FileSR):
78 """Gluster file-based storage repository"""
80 DRIVER_TYPE = 'glusterfs'
82 @override
83 @staticmethod
84 def handles(sr_type) -> bool:
85 # fudge, because the parent class (FileSR) checks for smb to alter its behavior
86 return sr_type == GlusterFSSR.DRIVER_TYPE or sr_type == 'smb'
88 @override
89 def load(self, sr_uuid) -> None:
90 if not self._is_glusterfs_available():
91 raise xs_errors.XenError(
92 'SRUnavailable',
93 opterr='glusterfs is not installed'
94 )
96 self.ops_exclusive = FileSR.OPS_EXCLUSIVE
97 self.lock = lock.Lock(lock.LOCK_TYPE_SR, self.uuid)
98 self.sr_vditype = SR.DEFAULT_TAP
99 self.driver_config = DRIVER_CONFIG
100 if 'server' not in self.dconf:
101 raise xs_errors.XenError('ConfigServerMissing')
102 # Can be None => on-slave plugin hack (is_open function).
103 self.remoteserver = self.dconf['server'] or ''
104 if self.sr_ref and self.session is not None:
105 self.sm_config = self.session.xenapi.SR.get_sm_config(self.sr_ref)
106 else:
107 self.sm_config = self.srcmd.params.get('sr_sm_config') or {}
108 self.mountpoint = os.path.join(SR.MOUNT_BASE, 'GlusterFS', self.remoteserver.split(':')[0], sr_uuid)
109 self.linkpath = os.path.join(self.mountpoint, sr_uuid or "")
110 self.path = os.path.join(SR.MOUNT_BASE, sr_uuid)
111 self._check_o_direct()
113 def checkmount(self):
114 return util.ioretry(lambda: ((util.pathexists(self.mountpoint) and
115 util.ismount(self.mountpoint)) and
116 util.pathexists(self.linkpath)))
118 def mount(self, mountpoint=None):
119 """Mount the remote gluster export at 'mountpoint'"""
120 if mountpoint is None:
121 mountpoint = self.mountpoint
122 elif not util.is_string(mountpoint) or mountpoint == "":
123 raise GlusterFSException("mountpoint not a string object")
125 try:
126 if not util.ioretry(lambda: util.isdir(mountpoint)):
127 util.ioretry(lambda: util.makedirs(mountpoint))
128 except util.CommandException as inst:
129 raise GlusterFSException("Failed to make directory: code is %d" % inst.code)
130 try:
131 options = []
132 if 'backupservers' in self.dconf:
133 options.append('backup-volfile-servers=' + self.dconf['backupservers'])
134 if 'fetchattempts' in self.dconf:
135 options.append('fetch-attempts=' + self.dconf['fetchattempts'])
136 if options:
137 options = ['-o', ','.join(options)]
138 command = ["mount", '-t', 'glusterfs', self.remoteserver, mountpoint] + options
139 util.ioretry(lambda: util.pread(command), errlist=[errno.EPIPE, errno.EIO], maxretry=2, nofail=True)
140 except util.CommandException as inst:
141 syslog(_syslog.LOG_ERR, 'GlusterFS mount failed ' + inst.__str__())
142 raise GlusterFSException("mount failed with return code %d" % inst.code)
144 # Sanity check to ensure that the user has at least RO access to the
145 # mounted share. Windows sharing and security settings can be tricky.
146 try:
147 util.listdir(mountpoint)
148 except util.CommandException:
149 try:
150 self.unmount(mountpoint, True)
151 except GlusterFSException:
152 util.logException('GlusterFSSR.unmount()')
153 raise GlusterFSException("Permission denied. Please check user privileges.")
155 def unmount(self, mountpoint, rmmountpoint):
156 try:
157 util.pread(["umount", mountpoint])
158 except util.CommandException as inst:
159 raise GlusterFSException("umount failed with return code %d" % inst.code)
160 if rmmountpoint:
161 try:
162 os.rmdir(mountpoint)
163 except OSError as inst:
164 raise GlusterFSException("rmdir failed with error '%s'" % inst.strerror)
166 @override
167 def attach(self, sr_uuid) -> None:
168 if not self.checkmount():
169 try:
170 self.mount()
171 os.symlink(self.linkpath, self.path)
172 except GlusterFSException as exc:
173 raise xs_errors.SROSError(12, exc.errstr)
174 self.attached = True
176 @override
177 def probe(self) -> str:
178 try:
179 self.mount(PROBE_MOUNTPOINT)
180 sr_list = filter(util.match_uuid, util.listdir(PROBE_MOUNTPOINT))
181 self.unmount(PROBE_MOUNTPOINT, True)
182 except (util.CommandException, xs_errors.XenError):
183 raise
184 # Create a dictionary from the SR uuids to feed SRtoXML()
185 return util.SRtoXML({sr_uuid: {} for sr_uuid in sr_list})
187 @override
188 def detach(self, sr_uuid) -> None:
189 if not self.checkmount():
190 return
191 util.SMlog("Aborting GC/coalesce")
192 cleanup.abort(self.uuid)
193 # Change directory to avoid unmount conflicts
194 os.chdir(SR.MOUNT_BASE)
195 self.unmount(self.mountpoint, True)
196 os.unlink(self.path)
197 self.attached = False
199 @override
200 def create(self, sr_uuid, size) -> None:
201 if self.checkmount():
202 raise xs_errors.SROSError(113, 'GlusterFS mount point already attached')
204 try:
205 self.mount()
206 except GlusterFSException as exc:
207 # noinspection PyBroadException
208 try:
209 os.rmdir(self.mountpoint)
210 except:
211 # we have no recovery strategy
212 pass
213 raise xs_errors.SROSError(111, "GlusterFS mount error [opterr=%s]" % exc.errstr)
215 if util.ioretry(lambda: util.pathexists(self.linkpath)):
216 if len(util.ioretry(lambda: util.listdir(self.linkpath))) != 0:
217 self.detach(sr_uuid)
218 raise xs_errors.XenError('SRExists')
219 else:
220 try:
221 util.ioretry(lambda: util.makedirs(self.linkpath))
222 os.symlink(self.linkpath, self.path)
223 except util.CommandException as inst:
224 if inst.code != errno.EEXIST:
225 try:
226 self.unmount(self.mountpoint, True)
227 except GlusterFSException:
228 util.logException('GlusterFSSR.unmount()')
229 raise xs_errors.SROSError(116,
230 "Failed to create GlusterFS SR. remote directory creation error: {}".format(
231 os.strerror(inst.code)))
232 self.detach(sr_uuid)
234 @override
235 def delete(self, sr_uuid) -> None:
236 # try to remove/delete non VDI contents first
237 super(GlusterFSSR, self).delete(sr_uuid)
238 try:
239 if self.checkmount():
240 self.detach(sr_uuid)
241 self.mount()
242 if util.ioretry(lambda: util.pathexists(self.linkpath)):
243 util.ioretry(lambda: os.rmdir(self.linkpath))
244 self.unmount(self.mountpoint, True)
245 except util.CommandException as inst:
246 self.detach(sr_uuid)
247 if inst.code != errno.ENOENT:
248 raise xs_errors.SROSError(114, "Failed to remove GlusterFS mount point")
250 @override
251 def vdi(self, uuid) -> VDI.VDI:
252 return GlusterFSFileVDI(self, uuid)
254 @staticmethod
255 def _is_glusterfs_available():
256 return util.find_executable('glusterfs')
259class GlusterFSFileVDI(FileSR.FileVDI):
260 @override
261 def attach(self, sr_uuid, vdi_uuid) -> str:
262 if not hasattr(self, 'xenstore_data'):
263 self.xenstore_data = {}
265 self.xenstore_data['storage-type'] = GlusterFSSR.DRIVER_TYPE
267 return super(GlusterFSFileVDI, self).attach(sr_uuid, vdi_uuid)
269 @override
270 def generate_config(self, sr_uuid, vdi_uuid) -> str:
271 util.SMlog("SMBFileVDI.generate_config")
272 if not util.pathexists(self.path):
273 raise xs_errors.XenError('VDIUnavailable')
274 resp = {'device_config': self.sr.dconf,
275 'sr_uuid': sr_uuid,
276 'vdi_uuid': vdi_uuid,
277 'sr_sm_config': self.sr.sm_config,
278 'command': 'vdi_attach_from_config'}
279 # Return the 'config' encoded within a normal XMLRPC response so that
280 # we can use the regular response/error parsing code.
281 config = xmlrpc.client.dumps(tuple([resp]), "vdi_attach_from_config")
282 return xmlrpc.client.dumps((config,), "", True)
284 @override
285 def attach_from_config(self, sr_uuid, vdi_uuid) -> str:
286 try:
287 if not util.pathexists(self.sr.path):
288 return self.sr.attach(sr_uuid)
289 except:
290 util.logException("SMBFileVDI.attach_from_config")
291 raise xs_errors.XenError('SRUnavailable',
292 opterr='Unable to attach from config')
293 return ''
295if __name__ == '__main__': 295 ↛ 296line 295 didn't jump to line 296, because the condition on line 295 was never true
296 SRCommand.run(GlusterFSSR, DRIVER_INFO)
297else:
298 SR.registerSR(GlusterFSSR)