This repository was archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathVirtualFilesystem.py
More file actions
197 lines (166 loc) · 6.48 KB
/
VirtualFilesystem.py
File metadata and controls
197 lines (166 loc) · 6.48 KB
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
#
# Copyright 2011 Red Hat, Inc.
# Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU 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.
import os
import VirtualDevice
from virtinst import _gettext as _
from XMLBuilderDomain import _xml_property
class VirtualFilesystem(VirtualDevice.VirtualDevice):
_virtual_device_type = VirtualDevice.VirtualDevice.VIRTUAL_DEV_FILESYSTEM
_target_props = ["dir", "name", "file", "dev"]
TYPE_MOUNT = "mount"
TYPE_TEMPLATE = "template"
TYPE_FILE = "file"
TYPE_BLOCK = "block"
TYPE_DEFAULT = "default"
TYPES = [TYPE_MOUNT, TYPE_TEMPLATE, TYPE_FILE, TYPE_BLOCK, TYPE_DEFAULT]
MODE_PASSTHROUGH = "passthrough"
MODE_MAPPED = "mapped"
MODE_SQUASH = "squash"
MODE_DEFAULT = "default"
MOUNT_MODES = [MODE_PASSTHROUGH, MODE_MAPPED, MODE_SQUASH, MODE_DEFAULT]
DRIVER_PATH = "path"
DRIVER_HANDLE = "handle"
DRIVER_DEFAULT = "default"
DRIVER_TYPES = [DRIVER_PATH, DRIVER_HANDLE, DRIVER_DEFAULT]
@staticmethod
def type_to_source_prop(fs_type):
"""
Convert a value of VirtualFilesystem.type to it's associated XML
source @prop name
"""
if (fs_type == VirtualFilesystem.TYPE_MOUNT or
fs_type == VirtualFilesystem.TYPE_DEFAULT or
fs_type is None):
return "dir"
elif fs_type == VirtualFilesystem.TYPE_TEMPLATE:
return "name"
elif fs_type == VirtualFilesystem.TYPE_FILE:
return "file"
elif fs_type == VirtualFilesystem.TYPE_BLOCK:
return "dev"
return "dir"
def __init__(self, conn, parsexml=None, parsexmlnode=None, caps=None):
VirtualDevice.VirtualDevice.__init__(self, conn, parsexml,
parsexmlnode, caps)
self._type = None
self._mode = None
self._driver = None
self._target = None
self._source = None
self._readonly = None
if self._is_parse():
return
self.mode = self.MODE_DEFAULT
self.type = self.TYPE_DEFAULT
self.driver = self.DRIVER_DEFAULT
def _get_type(self):
return self._type
def _set_type(self, val):
if val is not None and not self.TYPES.count(val):
raise ValueError(_("Unsupported filesystem type '%s'" % val))
self._type = val
type = _xml_property(_get_type, _set_type, xpath="./@type")
def _get_mode(self):
return self._mode
def _set_mode(self, val):
if val is not None and not self.MOUNT_MODES.count(val):
raise ValueError(_("Unsupported filesystem mode '%s'" % val))
self._mode = val
mode = _xml_property(_get_mode, _set_mode, xpath="./@accessmode")
def _get_readonly(self):
return self._readonly
def _set_readonly(self, val):
self._readonly = val
readonly = _xml_property(_get_readonly, _set_readonly,
xpath="./readonly", is_bool=True)
def _get_driver(self):
return self._driver
def _set_driver(self, val):
if val is not None and not self.DRIVER_TYPES.count(val):
raise ValueError(_("Unsupported filesystem driver '%s'" % val))
self._driver = val
driver = _xml_property(_get_driver, _set_driver, xpath="./driver/@type")
def _get_source(self):
return self._source
def _set_source(self, val):
if self.type != self.TYPE_TEMPLATE:
val = os.path.abspath(val)
self._source = val
def _xml_get_source_xpath(self):
xpath = None
ret = "./source/@dir"
for prop in self._target_props:
xpath = "./source/@" + prop
if self._xml_ctx.xpathEval(xpath):
ret = xpath
return ret
def _xml_set_source_xpath(self):
ret = "./source/@" + self.type_to_source_prop(self.type)
return ret
source = _xml_property(_get_source, _set_source,
xml_get_xpath=_xml_get_source_xpath,
xml_set_xpath=_xml_set_source_xpath)
def _get_target(self):
return self._target
def _set_target(self, val):
is_qemu = self.is_qemu()
# In case of qemu for default fs type (mount) target is not
# actually a directory, it is merely a arbitrary string tag
# that is exported to the guest as a hint for where to mount
if (is_qemu and
(self.type == self.TYPE_DEFAULT or
self.type == self.TYPE_MOUNT)):
pass
elif not os.path.isabs(val):
raise ValueError(_("Filesystem target '%s' must be an absolute "
"path") % val)
self._target = val
target = _xml_property(_get_target, _set_target, xpath="./target/@dir")
def _get_xml_config(self):
mode = self.mode
ftype = self.type
driver = self.driver
source = self.source
target = self.target
readonly = self.readonly
if mode == self.MODE_DEFAULT:
mode = None
if ftype == self.TYPE_DEFAULT:
ftype = None
if driver == self.DRIVER_DEFAULT:
driver = None
if not source or not target:
raise ValueError(
_("A filesystem source and target must be specified"))
fsxml = " <filesystem"
if ftype:
fsxml += " type='%s'" % ftype
if mode:
fsxml += " accessmode='%s'" % mode
fsxml += ">\n"
if driver:
fsxml += " <driver type='%s'/>\n" % driver
fsxml += " <source %s='%s'/>\n" % (
self.type_to_source_prop(ftype),
source)
fsxml += " <target dir='%s'/>\n" % target
if readonly:
fsxml += " <readonly/>\n"
fsxml += " </filesystem>"
return fsxml