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

#!/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 

# 

# LVHDoISCSISR: LVHD over Hardware HBA LUN driver, e.g. Fibre Channel or  

# hardware based iSCSI 

# 

 

import SR, LVHDSR, SRCommand, lvutil, HBASR 

import os 

import re 

import sys 

import xs_errors 

import xmlrpclib 

import util 

import scsiutil 

import mpath_cli 

import glob 

 

CAPABILITIES = ["SR_PROBE", "SR_UPDATE", "SR_METADATA", "SR_TRIM", 

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

                "VDI_GENERATE_CONFIG", "VDI_SNAPSHOT", "VDI_CLONE", "VDI_MIRROR", 

                "VDI_RESIZE", "ATOMIC_PAUSE", "VDI_RESET_ON_BOOT/2", 

                "VDI_UPDATE", "VDI_CONFIG_CBT", "VDI_ACTIVATE", "VDI_DEACTIVATE"] 

 

CONFIGURATION = [ [ 'SCSIid', 'The scsi_id of the destination LUN' ], \ 

                  [ 'allocation', 'Valid values are thick or thin (optional, defaults to thick)'] ] 

 

DRIVER_INFO = { 

    'name': 'LVHD over FC', 

    'description': 'SR plugin which represents disks as VHDs on Logical Volumes within a Volume Group created on an HBA LUN, e.g. hardware-based iSCSI or FC support', 

    'vendor': 'Citrix Systems Inc', 

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

    'driver_version': '1.0', 

    'required_api_version': '1.0', 

    'capabilities': CAPABILITIES, 

    'configuration': CONFIGURATION 

    } 

 

class LVHDoHBASR(LVHDSR.LVHDSR): 

    """LVHD over HBA storage repository""" 

    def handles(type): 

        if __name__ == '__main__': 

            name = sys.argv[0] 

        else: 

            name = __name__ 

        if name.endswith("LVMoHBASR"): 

            return type == "lvmohba" # for the initial switch from LVM 

        if type == "lvhdohba": 

            return True 

        return False 

    handles = staticmethod(handles) 

 

    def load(self, sr_uuid): 

        driver = SR.driver('hba') 

        self.hbasr = driver(self.original_srcmd, sr_uuid) 

 

        # If this is a vdi command, don't initialise SR 

        if not (util.isVDICommand(self.original_srcmd.cmd)): 

            pbd = None 

            try: 

                pbd = util.find_my_pbd(self.session, self.host_ref, self.sr_ref) 

            except: 

                pass 

 

            try: 

                if not self.dconf.has_key('SCSIid') and self.dconf.has_key('device'): 

                    # UPGRADE FROM MIAMI: add SCSIid key to device_config 

                    util.SMlog("Performing upgrade from Miami") 

                    if not os.path.exists(self.dconf['device']): 

                        raise xs_errors.XenError('InvalidDev') 

                    SCSIid = scsiutil.getSCSIid(self.dconf['device']) 

                    self.dconf['SCSIid'] = SCSIid 

                    del self.dconf['device'] 

 

                    if pbd <> None: 

                        device_config = self.session.xenapi.PBD.get_device_config(pbd) 

                        device_config['SCSIid'] = SCSIid 

                        device_config['upgraded_from_miami'] = 'true' 

                        del device_config['device'] 

                        self.session.xenapi.PBD.set_device_config(pbd, device_config) 

            except: 

                pass 

 

            if not self.dconf.has_key('SCSIid') or not self.dconf['SCSIid']: 

                print >>sys.stderr,self.hbasr.print_devs() 

                raise xs_errors.XenError('ConfigSCSIid') 

 

        self.SCSIid = self.dconf['SCSIid'] 

        LVHDSR.LVHDSR.load(self, sr_uuid) 

 

    def create(self, sr_uuid, size): 

        self.hbasr.attach(sr_uuid) 

        if self.mpath == "true": 

            self.mpathmodule.refresh(self.SCSIid, 0) 

        self._pathrefresh(LVHDoHBASR) 

        try: 

            LVHDSR.LVHDSR.create(self, sr_uuid, size) 

        finally: 

            if self.mpath == "true": 

                self.mpathmodule.reset(self.SCSIid, True) # explicit unmap 

                util.remove_mpathcount_field(self.session, self.host_ref, \ 

                                             self.sr_ref, self.SCSIid) 

 

    def attach(self, sr_uuid): 

        self.hbasr.attach(sr_uuid) 

        if self.mpath == "true": 

            self.mpathmodule.refresh(self.SCSIid,0) 

            # set the device mapper's I/O scheduler 

            path = '/dev/disk/by-scsid/%s' % self.dconf['SCSIid'] 

            for file in os.listdir(path): 

                self.block_setscheduler('%s/%s' % (path,file)) 

 

        self._pathrefresh(LVHDoHBASR) 

        if not os.path.exists(self.dconf['device']): 

            # Force a rescan on the bus 

            self.hbasr._init_hbadict() 

            # Must re-initialise the multipath node 

            if self.mpath == "true": 

                self.mpathmodule.refresh(self.SCSIid,0) 

        LVHDSR.LVHDSR.attach(self, sr_uuid) 

        self._setMultipathableFlag(SCSIid=self.SCSIid) 

 

    def scan(self, sr_uuid): 

        # During a reboot, scan is called ahead of attach, which causes the MGT 

        # to point of the wrong device instead of dm-x. Running multipathing will 

        # take care of this scenario. 

        if self.mpath == "true": 

            if 'device' not in self.dconf or not os.path.exists(self.dconf['device']): 

                util.SMlog("@@@@@ path does not exists") 

                self.mpathmodule.refresh(self.SCSIid, 0) 

                self._pathrefresh(LVHDoHBASR) 

                self._setMultipathableFlag(SCSIid=self.SCSIid) 

        else: 

                self._pathrefresh(LVHDoHBASR) 

        LVHDSR.LVHDSR.scan(self, sr_uuid) 

 

    def probe(self): 

        if self.mpath == "true" and self.dconf.has_key('SCSIid'): 

# When multipathing is enabled, since we don't refcount the multipath maps, 

# we should not attempt to do the iscsi.attach/detach when the map is already present, 

# as this will remove it (which may well be in use). 

            maps = [] 

            try: 

                maps = mpath_cli.list_maps() 

            except: 

                pass 

 

            if self.dconf['SCSIid'] in maps: 

                raise xs_errors.XenError('SRInUse') 

 

            self.mpathmodule.refresh(self.SCSIid,0) 

 

        try: 

            self._pathrefresh(LVHDoHBASR) 

            result = LVHDSR.LVHDSR.probe(self) 

            if self.mpath == "true": 

                self.mpathmodule.reset(self.SCSIid,True) 

            return result 

        except: 

           if self.mpath == "true": 

                self.mpathmodule.reset(self.SCSIid,True) 

           raise 

 

    def detach(self, sr_uuid): 

        LVHDSR.LVHDSR.detach(self, sr_uuid) 

        self.mpathmodule.reset(self.SCSIid,True,True) # explicit_unmap 

        try: 

            pbdref = util.find_my_pbd(self.session, self.host_ref, self.sr_ref) 

        except: 

            pass 

        for key in ["mpath-" + self.SCSIid, "multipathed", "MPPEnabled"]: 

            try: 

                self.session.xenapi.PBD.remove_from_other_config(pbdref, key) 

            except: 

                pass 

 

    def _remove_device_nodes(self): 

        """ 

        Remove the kernel device nodes 

        """ 

        nodes = glob.glob('/dev/disk/by-scsid/%s/*' % self.SCSIid) 

        util.SMlog('Remove_nodes, nodes are %s' % nodes) 

        for node in nodes: 

            with open('/sys/block/%s/device/delete' % 

                      (os.path.basename(node)), 'w') as f: 

                f.write('1\n') 

 

    def delete(self, sr_uuid): 

        self._pathrefresh(LVHDoHBASR) 

        try: 

            LVHDSR.LVHDSR.delete(self, sr_uuid) 

        finally: 

            if self.mpath == "true": 

                self.mpathmodule.reset(self.SCSIid, explicit_unmap=True) 

            self._remove_device_nodes() 

 

    def vdi(self, uuid): 

        return LVHDoHBAVDI(self, uuid) 

 

class LVHDoHBAVDI(LVHDSR.LVHDVDI): 

    def generate_config(self, sr_uuid, vdi_uuid): 

        util.SMlog("LVHDoHBAVDI.generate_config") 

        if not lvutil._checkLV(self.path): 

            raise xs_errors.XenError('VDIUnavailable') 

        dict = {} 

        self.sr.dconf['multipathing'] = self.sr.mpath 

        self.sr.dconf['multipathhandle'] = self.sr.mpathhandle 

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

        dict['sr_uuid'] = sr_uuid 

        dict['vdi_uuid'] = vdi_uuid 

        dict['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([dict]), "vdi_attach_from_config") 

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

 

    def attach_from_config(self, sr_uuid, vdi_uuid): 

        util.SMlog("LVHDoHBAVDI.attach_from_config") 

        self.sr.hbasr.attach(sr_uuid) 

        if self.sr.mpath == "true": 

            self.sr.mpathmodule.refresh(self.sr.SCSIid,0) 

        try: 

            return self.attach(sr_uuid, vdi_uuid) 

        except: 

            util.logException("LVHDoHBAVDI.attach_from_config") 

            raise xs_errors.XenError('SRUnavailable', \ 

                        opterr='Unable to attach the heartbeat disk') 

 

def match_scsidev(s): 

    regex = re.compile("^/dev/disk/by-id|^/dev/mapper") 

    return regex.search(s, 0) 

 

248if __name__ == '__main__': 

    SRCommand.run(LVHDoHBASR, DRIVER_INFO) 

else: 

    SR.registerSR(LVHDoHBASR)