Coverage for drivers/SHMSR.py : 0%

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
18from sm_typing import override
20import SR
21import VDI
22import SRCommand
23import util
24import os
25import xs_errors
27CAPABILITIES = ["VDI_ATTACH", "VDI_DETACH", "VDI_CLONE", "VDI_SNAPSHOT",
28 "SR_SCAN", "SR_ATTACH", "SR_DETACH"]
29CONFIGURATION = ['location', '/dev/shm subdirectory']
30DRIVER_INFO = {
31 'name': 'SHM',
32 'description': 'Handles shared memory virtual disks',
33 'vendor': 'Citrix Systems Inc.',
34 'copyright': '(c) 2009 Citrix Systems, Inc.',
35 'driver_version': '1.0',
36 'required_api_version': '1.0',
37 'capabilities': CAPABILITIES,
38 'configuration': CONFIGURATION
39 }
41TYPE = "shm"
44class SHMSR(SR.SR):
45 """Shared memory storage repository"""
47 def _loadvdis(self):
48 """Scan the location directory."""
49 if self.vdis:
50 return
52 try:
53 for name in util.listdir(self.dconf['location']):
54 if name != "":
55 self.vdis[name] = SHMVDI(self, util.gen_uuid(), name)
56 except:
57 pass
59 @override
60 @staticmethod
61 def handles(type) -> bool:
62 """Do we handle this type?"""
63 if type == TYPE:
64 return True
65 return False
67 @override
68 def content_type(self, sr_uuid) -> str:
69 """Returns the content_type XML"""
70 return super(SHMSR, self).content_type(sr_uuid)
72 @override
73 def vdi(self, uuid) -> VDI.VDI:
74 """Create a VDI class"""
75 if 'vdi_location' in self.srcmd.params:
76 return SHMVDI(self, uuid, self.srcmd.params['vdi_location'])
77 else:
78 return SHMVDI(self, uuid, self.srcmd.params['device_config']['location'])
80 @override
81 def load(self, sr_uuid) -> None:
82 """Initialises the SR"""
83 if 'location' not in self.dconf:
84 raise xs_errors.XenError('ConfigLocationMissing')
86 self.sr_vditype = 'file'
87 self.physical_size = 0
88 self.physical_utilisation = 0
89 self.virtual_allocation = 0
91 @override
92 def attach(self, sr_uuid) -> None:
93 """Std. attach"""
94 self._loadvdis()
96 @override
97 def detach(self, sr_uuid) -> None:
98 """Std. detach"""
99 pass
101 @override
102 def scan(self, sr_uuid) -> None:
103 """Scan"""
104 self._loadvdis()
105 super(SHMSR, self).scan(sr_uuid)
107 @override
108 def create(self, sr_uuid, size) -> None:
109 self.attach(sr_uuid)
110 self.detach(sr_uuid)
113class SHMVDI(VDI.VDI):
114 @override
115 def load(self, vdi_uuid) -> None:
116 try:
117 stat = os.stat(self.path)
118 self.utilisation = int(stat.st_size)
119 self.size = int(stat.st_size)
120 except:
121 pass
123 def __init__(self, mysr, uuid, filename):
124 self.uuid = uuid
125 self.path = os.path.join(mysr.dconf['location'], filename)
126 VDI.VDI.__init__(self, mysr, None)
127 self.label = filename
128 self.location = filename
129 self.vdi_type = 'file'
130 self.read_only = True
131 self.shareable = True
132 self.sm_config = {}
134 @override
135 def detach(self, sr_uuid, vdi_uuid) -> None:
136 pass
138 @override
139 def clone(self, sr_uuid, vdi_uuid) -> str:
140 return self.get_params()
142 @override
143 def snapshot(self, sr_uuid, vdi_uuid) -> str:
144 return self.get_params()
146if __name__ == '__main__':
147 SRCommand.run(SHMSR, DRIVER_INFO)
148else:
149 SR.registerSR(SHMSR)