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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify 

# it under the terms of the GNU Lesser General Public License as published 

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# SMBSR: SMB filesystem based storage repository 

 

import SR, SRCommand, FileSR, util 

import errno 

import os 

import xmlrpclib 

import xs_errors 

import vhdutil 

from lock import Lock 

import cleanup 

import cifutils 

 

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

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

                "VDI_UPDATE", "VDI_CLONE","VDI_SNAPSHOT","VDI_RESIZE","VDI_MIRROR", 

                "VDI_GENERATE_CONFIG", 

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

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

 

CONFIGURATION = [ [ 'server', 'Full path to share root on SMB server (required)' ], \ 

                  [ 'username', 'The username to be used during SMB authentication' ], \ 

                  [ 'password', 'The password to be used during SMB authentication' ] ] 

 

DRIVER_INFO = { 

    'name': 'SMB VHD', 

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

    'vendor': 'Citrix Systems Inc', 

    'copyright': '(C) 2015 Citrix Systems Inc', 

    'driver_version': '1.0', 

    'required_api_version': '1.0', 

    'capabilities': CAPABILITIES, 

    'configuration': CONFIGURATION 

    } 

 

DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True} 

 

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

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

PROBE_MOUNTPOINT = os.path.join(SR.MOUNT_BASE, "probe") 

 

class SMBException(Exception): 

    def __init__(self, errstr): 

        self.errstr = errstr 

 

# server = //smb-server/vol1 - ie the export path on the SMB server 

# mountpoint = /var/run/sr-mount/SMB/<smb_server_name>/<share_name>/uuid 

# linkpath = mountpoint/uuid - path to SR directory on share 

# path = /var/run/sr-mount/uuid - symlink to SR directory on share 

class SMBSR(FileSR.SharedFileSR): 

    """SMB file-based storage repository""" 

    def handles(type): 

        return type == 'smb' 

    handles = staticmethod(handles) 

 

    def load(self, sr_uuid): 

        self.ops_exclusive = FileSR.OPS_EXCLUSIVE 

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

        self.sr_vditype = SR.DEFAULT_TAP 

        self.driver_config = DRIVER_CONFIG 

78        if not self.dconf.has_key('server'): 

            raise xs_errors.XenError('ConfigServerMissing') 

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

81        if self.sr_ref and self.session is not None : 

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

        else: 

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

        self.mountpoint = os.path.join(SR.MOUNT_BASE, 'SMB', self.__extract_server(), sr_uuid) 

        self.linkpath = os.path.join(self.mountpoint, 

                                           sr_uuid or "") 

        # Remotepath is the absolute path inside a share that is to be mounted 

        # For a SMB SR, only the root can be mounted. 

        self.remotepath = '' 

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

        self._check_o_direct() 

 

    def checkmount(self): 

        return util.ioretry(lambda: ((util.pathexists(self.mountpoint) and \ 

                                util.ismount(self.mountpoint)) and \ 

                                util.pathexists(self.linkpath))) 

 

    def makeMountPoint(self, mountpoint): 

        """Mount the remote SMB export at 'mountpoint'""" 

        if mountpoint == None: 

            mountpoint = self.mountpoint 

105        elif not util.is_string(mountpoint) or mountpoint == "": 

            raise SMBException("mountpoint not a string object") 

 

        try: 

107   111            if not util.ioretry(lambda: util.isdir(mountpoint)): 

                util.ioretry(lambda: util.makedirs(mountpoint)) 

        except util.CommandException, inst: 

            raise SMBException("Failed to make directory: code is %d" % 

                                inst.code) 

        return mountpoint 

 

    def mount(self, mountpoint=None): 

 

        mountpoint = self.makeMountPoint(mountpoint) 

 

        new_env, domain = cifutils.getCIFCredentials(self.dconf, self.session) 

 

        options = self.getMountOptions(domain) 

123        if options: 

            options = ",".join(str(x) for x in options if x) 

 

        try: 

 

            util.ioretry(lambda: 

                util.pread(["mount.cifs", self.remoteserver, 

                mountpoint, "-o", options], new_env=new_env), 

                errlist=[errno.EPIPE, errno.EIO], 

                maxretry=2, nofail=True) 

        except util.CommandException, inst: 

            raise SMBException("mount failed with return code %d" % inst.code) 

 

        # Sanity check to ensure that the user has at least RO access to the 

        # mounted share. Windows sharing and security settings can be tricky. 

        try: 

            util.listdir(mountpoint) 

        except util.CommandException: 

            try: 

                self.unmount(mountpoint, True) 

            except SMBException: 

                util.logException('SMBSR.unmount()') 

            raise SMBException("Permission denied. " 

                                "Please check user privileges.") 

 

    def getMountOptions(self, domain): 

        """Creates option string based on parameters provided""" 

        options = ['cache=loose', 

                'vers=3.0', 

                'actimeo=0' 

        ] 

 

        if domain: 

            options.append('domain='+domain) 

 

157        if not cifutils.containsCredentials(self.dconf): 

            # No login details provided. 

            options.append('guest') 

 

        return options 

 

 

    def unmount(self, mountpoint, rmmountpoint): 

        """Unmount the remote SMB export at 'mountpoint'""" 

        try: 

            util.pread(["umount", mountpoint]) 

        except util.CommandException, inst: 

            raise SMBException("umount failed with return code %d" % inst.code) 

 

        if rmmountpoint: 

            try: 

                os.rmdir(mountpoint) 

            except OSError, inst: 

                raise SMBException("rmdir failed with error '%s'" % inst.strerror) 

 

    def __extract_server(self): 

        return self.remoteserver[2:].replace('\\', '/') 

 

    def __check_license(self): 

        """Raises an exception if SMB is not licensed.""" 

        if self.session is None or (isinstance(self.session, str) and \ 

                self.session == ""): 

            raise xs_errors.XenError('NoSMBLicense', 

                    'No session object to talk to XAPI') 

        restrictions = util.get_pool_restrictions(self.session) 

        if 'restrict_cifs' in restrictions and \ 

                restrictions['restrict_cifs'] == "true": 

            raise xs_errors.XenError('NoSMBLicense') 

 

    def attach(self, sr_uuid): 

        if not self.checkmount(): 

            try: 

                self.mount() 

                os.symlink(self.linkpath, self.path) 

            except SMBException, exc: 

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

            self._check_hardlinks() 

        self.attached = True 

 

    def probe(self): 

        try: 

            err = "SMBMount" 

            self.mount(PROBE_MOUNTPOINT) 

            sr_list = filter(util.match_uuid, util.listdir(PROBE_MOUNTPOINT)) 

            err = "SMBUnMount" 

            self.unmount(PROBE_MOUNTPOINT, True) 

        except SMBException, inst: 

            raise xs_errors.XenError(err, opterr=inst.errstr) 

        except (util.CommandException, xs_errors.XenError): 

            raise 

 

        # Create a dictionary from the SR uuids to feed SRtoXML() 

        sr_dict = {sr_uuid : {} for sr_uuid in sr_list} 

 

        return util.SRtoXML(sr_dict) 

 

    def detach(self, sr_uuid): 

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

        if not self.checkmount(): 

            return 

        util.SMlog("Aborting GC/coalesce") 

        cleanup.abort(self.uuid) 

 

        # Change directory to avoid unmount conflicts 

        os.chdir(SR.MOUNT_BASE) 

 

        try: 

            self.unmount(self.mountpoint, True) 

            os.unlink(self.path) 

        except SMBException, exc: 

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

 

        self.attached = False 

 

    def create(self, sr_uuid, size): 

        self.__check_license() 

 

        if self.checkmount(): 

            raise xs_errors.XenError('SMBAttached') 

 

        try: 

            self.mount() 

        except SMBException, exc: 

            try: 

                os.rmdir(self.mountpoint) 

            except: 

                pass 

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

 

        if util.ioretry(lambda: util.pathexists(self.linkpath)): 

            if len(util.ioretry(lambda: util.listdir(self.linkpath))) != 0: 

                self.detach(sr_uuid) 

                raise xs_errors.XenError('SRExists') 

        else: 

            try: 

                util.ioretry(lambda: util.makedirs(self.linkpath)) 

                os.symlink(self.linkpath, self.path) 

            except util.CommandException, inst: 

                if inst.code != errno.EEXIST: 

                    try: 

                        self.unmount(self.mountpoint, True) 

                    except SMBException: 

                        util.logException('SMBSR.unmount()') 

                    raise xs_errors.XenError( 

                            'SMBCreate', 

                            opterr="remote directory creation error: {}" 

                                    .format(os.strerror(inst.code)) 

                    ) 

        self.detach(sr_uuid) 

 

    def delete(self, sr_uuid): 

        # try to remove/delete non VDI contents first 

        super(SMBSR, self).delete(sr_uuid) 

        try: 

            if self.checkmount(): 

                self.detach(sr_uuid) 

 

            self.mount() 

            if util.ioretry(lambda: util.pathexists(self.linkpath)): 

                util.ioretry(lambda: os.rmdir(self.linkpath)) 

            self.unmount(self.mountpoint, True) 

        except util.CommandException, inst: 

            self.detach(sr_uuid) 

            if inst.code != errno.ENOENT: 

                raise xs_errors.XenError('SMBDelete') 

 

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

        if not loadLocked: 

            return SMBFileVDI(self, uuid) 

        return SMBFileVDI(self, uuid) 

 

class SMBFileVDI(FileSR.FileVDI): 

    def attach(self, sr_uuid, vdi_uuid): 

        if not hasattr(self,'xenstore_data'): 

            self.xenstore_data = {} 

 

        self.xenstore_data["storage-type"]="smb" 

 

        return super(SMBFileVDI, self).attach(sr_uuid, vdi_uuid) 

 

    def generate_config(self, sr_uuid, vdi_uuid): 

        util.SMlog("SMBFileVDI.generate_config") 

        if not util.pathexists(self.path): 

                raise xs_errors.XenError('VDIUnavailable') 

        resp = {} 

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

        resp['sr_uuid'] = sr_uuid 

        resp['vdi_uuid'] = vdi_uuid 

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

        resp['command'] = 'vdi_attach_from_config' 

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

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

        config = xmlrpclib.dumps(tuple([resp]), "vdi_attach_from_config") 

        return xmlrpclib.dumps((config,), "", True) 

 

    def attach_from_config(self, sr_uuid, vdi_uuid): 

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

        also start a tapdisk on the file""" 

        util.SMlog("SMBFileVDI.attach_from_config") 

        try: 

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

                self.sr.attach(sr_uuid) 

        except: 

            util.logException("SMBFileVDI.attach_from_config") 

            raise xs_errors.XenError('SRUnavailable', \ 

                        opterr='Unable to attach from config') 

 

 

329if __name__ == '__main__': 

    SRCommand.run(SMBSR, DRIVER_INFO) 

else: 

    SR.registerSR(SMBSR) 

#