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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# __init__.py
# Entry point for anaconda storage formats subpackage.
#
# Copyright (C) 2009 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Dave Lehman <dlehman@redhat.com>
#
import os
from errors import *
from iutil import notify_kernel, get_sysfs_path_by_name, log_method_call
from dm import dm_node_from_name
import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)
import logging
log = logging.getLogger("storage")
device_formats = {}
def register_device_format(fmt_class):
if not issubclass(fmt_class, DeviceFormat):
raise ValueError("arg1 must be a subclass of DeviceFormat")
device_formats[fmt_class._type] = fmt_class
log.debug("registered device format class %s as %s" % (fmt_class.__name__,
fmt_class._type))
default_fstypes = ("ext4", "ext3", "ext2")
default_boot_fstypes = ("ext3", "ext2")
def get_default_filesystem_type(boot=None):
if boot:
fstypes = default_boot_filesystem_types
else:
fstypes = default_filesystem_types
for fstype in fstypes:
try:
supported = get_device_format_class(fstype).supported
except AttributeError:
supported = None
elif supported:
return fstype
raise DeviceFormatError("None of %s is supported by your kernel" % ",".join(fstypes))
def getFormat(fmt_type, *args, **kwargs):
""" Return a DeviceFormat instance based on fmt_type and args.
Given a device format type and a set of constructor arguments,
return a DeviceFormat instance.
Return None if no suitable format class is found.
Arguments:
fmt_type -- the name of the format type (eg: 'ext3', 'swap')
Keyword Arguments:
The keyword arguments may vary according to the format type,
but here is the common set:
device -- path to the device on which the format resides
uuid -- the UUID of the (preexisting) formatted device
exists -- whether or not the format exists on the device
"""
fmt_class = get_device_format_class(fmt_type)
fmt = None
if fmt_class:
fmt = fmt_class(*args, **kwargs)
try:
className = fmt.__class__.__name__
except AttributeError:
className = None
log.debug("getFormat('%s') returning %s instance" % (fmt_type, className))
return fmt
def collect_device_format_classes():
""" Pick up all device format classes from this directory.
Note: Modules must call register_device_format(FormatClass) in
order for the format class to be picked up.
"""
dir = os.path.dirname(__file__)
for module_file in os.listdir(dir):
if module_file.endswith(".py"):
mod_name = module_file[:-3]
# FIXME: use imputils here instead of exec
try:
exec("import %s" % mod_name)
except ImportError, e:
log.debug("import of device format module '%s' failed" % mod_name)
def get_device_format_class(fmt_type):
""" Return an appropriate format class based on fmt_type. """
if not device_formats:
collect_device_format_classes()
fmt = device_formats.get(fmt_type)
if not fmt:
for fmt_class in device_formats.values():
if fmt_type == fmt_class.name:
fmt = fmt_class
break
elif fmt_type in fmt_class._udevTypes:
fmt = fmt_class
break
# default to no formatting, AKA "Unknown"
if not fmt:
fmt = DeviceFormat
return fmt
class DeviceFormat(object):
""" Generic device format. """
_type = None
_name = "Unknown"
_udevTypes = []
_formattable = False # can be formatted
_supported = False # is supported
_linuxNative = False # for clearpart
_packages = [] # required packages
_resizable = False # can be resized
_bootable = False # can be used as boot
_maxsize = 0 # maximum size in MB
_minsize = 0 # minimum size in MB
_dump = False
_check = False
def __init__(self, *args, **kwargs):
""" Create a DeviceFormat instance.
Keyword Arguments:
device -- path to the underlying device
uuid -- this format's UUID
exists -- indicates whether this is an existing format
"""
self.device = kwargs.get("device")
self.uuid = kwargs.get("uuid")
self.exists = kwargs.get("exists")
self.options = kwargs.get("options")
def setDevice(self, devspec):
if devspec and not devspec.startswith("/"):
raise ValueError("device must be a fully qualified path")
self._device = devspec
def getDevice(self):
return self._device
device = property(lambda f: f.getDevice(),
lambda f,d: f.setDevice(d),
doc="Full path the device this format occupies")
@property
def name(self):
if self._name:
name = self._name
else:
name = self.type
return name
@property
def type(self):
return self._type
def probe(self):
log_method_call(self, device=os.path.basename(self.device),
type=self.type, status=self.status)
def notifyKernel(self):
log_method_call(self, device=os.path.basename(self.device),
type=self.type)
if self.device.startswith("/dev/mapper/"):
try:
name = dm_node_from_name(os.path.basename(self.device))
except Exception, e:
log.warning("failed to get dm node for %s" % self.device)
return
else:
name = os.path.basename(self.device)
path = get_sysfs_path_by_name(name)
try:
notify_kernel(path, action="change")
except Exception, e:
log.warning("failed to notify kernel of change: %s" % e)
def create(self, *args, **kwargs):
log_method_call(self, device=os.path.basename(self.device),
type=self.type, status=self.status)
pass
def destroy(self, *args, **kwargs):
log_method_call(self, device=os.path.basename(self.device),
type=self.type, status=self.status)
# zero out the 1MB at the beginning and end of the device in the
# hope that it will wipe any metadata from filesystems that
# previously occupied this device
log.debug("zeroing out beginning and end of %s..." % self.device)
try:
fd = os.open(self.device, os.O_RDWR)
buf = '\0' * 1024 * 1024
os.write(fd, buf)
os.lseek(fd, -1024 * 1024, 2)
os.write(fd, buf)
os.close(fd)
except Exception, e:
log.error("error zeroing out %s: %s" % (self.device, e))
def setup(self, *args, **kwargs):
log_method_call(self, device=os.path.basename(self.device),
type=self.type, status=self.status)
pass
def teardown(self, *args, **kwargs):
log_method_call(self, device=os.path.basename(self.device),
type=self.type, status=self.status)
pass
@property
def status(self):
if not self.exists:
return False
return os.path.exists(self.device)
@property
def formattable(self):
""" Can we create formats of this type? """
return self._formattable
@property
def supported(self):
""" Is this format a supported type? """
return self._supported
@property
def packages(self):
""" Packages required to manage formats of this type. """
return self._packages
@property
def resizable(self):
""" Can formats of this type be resized? """
return self._resizable
@property
def bootable(self):
""" Is this format type suitable for a boot partition? """
return self._bootable
@property
def linuxNative(self):
""" Is this format type native to linux? """
return self._linuxNative
@property
def mountable(self):
""" Is this something we can mount? """
return False
@property
def dump(self):
""" Whether or not this format will be dumped by dump(8). """
return self._dump
@property
def check(self):
""" Whether or not this format is checked on boot. """
return self._check
@property
def maxsize(self):
""" Maximum size (in MB) for this format type. """
return self._maxsize
@property
def minsize(self):
""" Minimum size (in MB) for this format type. """
return self._minsize
collect_device_format_classes()
|