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