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

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

# 

# udevSR: represents VDIs which are hotplugged into dom0 via udev e.g. 

#         USB CDROM/disk devices 

 

import SR, VDI, SRCommand, util 

import os, time, stat 

import xs_errors 

import sysdevice 

 

CAPABILITIES = [ "VDI_INTRODUCE","VDI_ATTACH","VDI_DETACH","VDI_UPDATE", "SR_UPDATE" ] 

 

CONFIGURATION = \ 

    [ [ 'location', 'path to mount (required) (e.g. server:/path)' ] ] 

 

DRIVER_INFO = { 

    'name': 'udev', 

    'description': 'SR plugin which represents devices plugged in via udev as VDIs', 

    'vendor': 'Citrix Systems Inc', 

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

    'driver_version': '1.0', 

    'required_api_version': '1.1', 

    'capabilities': CAPABILITIES, 

    'configuration': CONFIGURATION 

    } 

 

TYPE = 'udev' 

 

class udevSR(SR.SR): 

    """udev-driven storage repository""" 

    def handles(type): 

        if type == TYPE: 

            return True 

        return False 

    handles = staticmethod(handles) 

 

    def content_type(self, sr_uuid): 

        return super(udevSR, self).content_type(sr_uuid) 

 

    def vdi(self, uuid): 

        util.SMlog("params = %s" % (self.srcmd.params.keys())) 

 

59        if 'vdi_location' in self.srcmd.params: 

            vdi_location = self.srcmd.params['vdi_location'] 

        else: 

            vdi_location = self.get_vdi_location(uuid) 

 

        return udevVDI(self, vdi_location) 

 

    def get_vdi_location(self, uuid): 

        vdi = self.session.xenapi.VDI 

        vdi_ref = vdi.get_by_uuid(uuid) 

        return vdi.get_location(vdi_ref) 

 

    def load(self, sr_uuid): 

        # First of all, check we've got the correct keys in dconf 

        if not self.dconf.has_key('location'): 

            raise xs_errors.XenError('ConfigLocationMissing') 

        self.sr_vditype = 'phy' 

        # Cache the sm_config  

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

 

    def update(self, sr_uuid): 

        # Return as much information as we have 

        sr_root =self.dconf['location'] 

 

        if util.pathexists(sr_root): 

            for filename in os.listdir(sr_root): 

                path = os.path.join(sr_root, filename) 

                x = udevVDI(self, path) 

                self.vdis[path] = x 

 

        the_sum = 0L 

        for vdi in self.vdis.values(): 

            the_sum = the_sum + vdi.size 

 

        self.physical_size = the_sum 

        self.physical_utilisation = the_sum 

        self.virtual_allocation = the_sum 

 

        self._db_update() 

 

    def scan(self, sr_uuid): 

        self.update(sr_uuid) 

 

        # base class scan does all the work: 

        return super(udevSR, self).scan(sr_uuid) 

 

    def create(self, sr_uuid, size): 

        pass 

 

    def delete(self, sr_uuid): 

        pass 

 

    def attach(self, sr_uuid): 

        pass 

 

    def detach(self, sr_uuid): 

        pass 

 

def read_whole_file(filename): 

    f = open(filename, 'r') 

    try: 

        return f.readlines() 

    finally: 

        f.close() 

 

class udevVDI(VDI.VDI): 

    def __init__(self, sr, location): 

        self.location = location 

        VDI.VDI.__init__(self, sr, None) 

 

    def load(self, location): 

        self.path = self.location 

        self.size = 0 

        self.utilisation = 0 

        self.label = self.path 

        self.sm_config = {} 

        try: 

            s = os.stat(self.path) 

            self.deleted = False 

 

            # Use the CTIME of the symlink to mean "time it was hotplugged" 

            iso8601 = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(s[stat.ST_CTIME])) 

            self.sm_config['hotplugged_at'] = iso8601 

 

            dev = os.path.basename(self.path) 

            info = sysdevice.stat(dev) 

            if "size" in info.keys(): 

                self.size = info["size"] 

                self.utilisation = self.size 

 

            self.label = "%s %s" % (info["bus"], info["bus_path"]) 

            self.description = info["hwinfo"] 

 

            # XXX: what other information can we recover? 

            if self.sr.sm_config.has_key('type'): 

                self.read_only = self.sr.sm_config['type'] == "cd" 

 

            usb_path = info.get("usb_path") 

            if usb_path: 

                self.sm_config["usb_path"] = info["usb_path"] 

                pusbs = self.session.xenapi.PUSB.get_all_records() 

                for pusb in pusbs.itervalues(): 

                    if usb_path == pusb.get("path"): 

                        if pusb.get("passthrough_enabled"): 

                            raise xs_errors.XenError('VDIUnavailable') 

                        break 

 

        except OSError, e: 

            self.deleted = True 

 

    def introduce(self, sr_uuid, vdi_uuid): 

        self.uuid = vdi_uuid 

        self.location = self.sr.srcmd.params['vdi_location'] 

        self._db_introduce() 

        # Update the physical_utilisation etc 

        self.sr.update(sr_uuid) 

        return super(udevVDI, self).get_params() 

 

    def update(self, sr_uuid, vdi_location): 

        self.load(vdi_location) 

        # _db_update requires self.uuid to be set 

        self.uuid = self.sr.srcmd.params['vdi_uuid'] 

        self._db_update() 

        # also reset the name-label and description since we're a bit of 

        # a special SR 

        # this would lead to an infinite loop as VDI.set_name_label now 

        # calls VDI.update  

        # temporarily commenting this to pass quicktest 

        #vdi = self.sr.session.xenapi.VDI.get_by_uuid(self.uuid)         

        #self.sr.session.xenapi.VDI.set_name_label(vdi, self.label) 

        #self.sr.session.xenapi.VDI.set_name_description(vdi, self.description)         

 

    def attach(self, sr_uuid, vdi_uuid): 

        if self.deleted: 

            raise xs_errors.XenError('VDIUnavailable') 

 

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

 

    def detach(self, sr_uuid, vdi_uuid): 

        pass 

 

200if __name__ == '__main__': 

    SRCommand.run(udevSR, DRIVER_INFO) 

else: 

    SR.registerSR(udevSR)