1 |
# swap.py |
2 |
# Device format classes for anaconda's storage configuration module. |
3 |
# |
4 |
# Copyright (C) 2009 Red Hat, Inc. |
5 |
# |
6 |
# This copyrighted material is made available to anyone wishing to use, |
7 |
# modify, copy, or redistribute it subject to the terms and conditions of |
8 |
# the GNU General Public License v.2, or (at your option) any later version. |
9 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
10 |
# ANY WARRANTY expressed or implied, including the implied warranties of |
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
12 |
# Public License for more details. You should have received a copy of the |
13 |
# GNU General Public License along with this program; if not, write to the |
14 |
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 |
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
16 |
# source code or documentation are not subject to the GNU General Public |
17 |
# License and may only be used or replicated with the express permission of |
18 |
# Red Hat, Inc. |
19 |
# |
20 |
# Red Hat Author(s): Dave Lehman <dlehman@redhat.com> |
21 |
# |
22 |
|
23 |
from iutil import numeric_type |
24 |
from parted import PARTITION_SWAP, fileSystemType |
25 |
from ..storage_log import log_method_call |
26 |
from ..errors import * |
27 |
from ..devicelibs import swap |
28 |
from . import DeviceFormat, register_device_format |
29 |
from constants import * |
30 |
|
31 |
import gettext |
32 |
_ = lambda x: gettext.ldgettext("anaconda", x) |
33 |
|
34 |
import logging |
35 |
log = logging.getLogger("storage") |
36 |
|
37 |
|
38 |
class SwapSpace(DeviceFormat): |
39 |
""" Swap space """ |
40 |
_type = "swap" |
41 |
_name = None |
42 |
_udevTypes = ["swap"] |
43 |
partedFlag = PARTITION_SWAP |
44 |
partedSystem = fileSystemType["linux-swap(v1)"] |
45 |
_formattable = True # can be formatted |
46 |
_supported = True # is supported |
47 |
_linuxNative = True # for clearpart |
48 |
_maxSize = SWAP_SIZE_LIMIT |
49 |
|
50 |
def __init__(self, *args, **kwargs): |
51 |
""" Create a SwapSpace instance. |
52 |
|
53 |
Keyword Arguments: |
54 |
|
55 |
device -- path to the underlying device |
56 |
uuid -- this swap space's uuid |
57 |
label -- this swap space's label |
58 |
priority -- this swap space's priority |
59 |
exists -- indicates whether this is an existing format |
60 |
|
61 |
""" |
62 |
log_method_call(self, *args, **kwargs) |
63 |
DeviceFormat.__init__(self, *args, **kwargs) |
64 |
|
65 |
self.priority = kwargs.get("priority") |
66 |
self.label = kwargs.get("label") |
67 |
|
68 |
def __str__(self): |
69 |
s = DeviceFormat.__str__(self) |
70 |
s += (" priority = %(priority)s label = %(label)s" % |
71 |
{"priority": self.priority, "label": self.label}) |
72 |
return s |
73 |
|
74 |
@property |
75 |
def dict(self): |
76 |
d = super(SwapSpace, self).dict |
77 |
d.update({"priority": self.priority, "label": self.label}) |
78 |
return d |
79 |
|
80 |
def _setPriority(self, priority): |
81 |
if priority is None: |
82 |
self._priority = None |
83 |
return |
84 |
|
85 |
if not isinstance(priority, int) or not 0 <= priority <= 32767: |
86 |
raise ValueError("swap priority must be an integer between 0 and 32767") |
87 |
|
88 |
self._priority = priority |
89 |
|
90 |
def _getPriority(self): |
91 |
return self._priority |
92 |
|
93 |
priority = property(_getPriority, _setPriority, |
94 |
doc="The priority of the swap device") |
95 |
|
96 |
def _getOptions(self): |
97 |
opts = "" |
98 |
if self.priority is not None: |
99 |
opts += "pri=%d" % self.priority |
100 |
|
101 |
return opts |
102 |
|
103 |
def _setOptions(self, opts): |
104 |
if not opts: |
105 |
self.priority = None |
106 |
return |
107 |
|
108 |
for option in opts.split(","): |
109 |
(opt, equals, arg) = option.partition("=") |
110 |
if equals and opt == "pri": |
111 |
try: |
112 |
self.priority = int(arg) |
113 |
except ValueError: |
114 |
log.info("invalid value for swap priority: %s" % arg) |
115 |
|
116 |
options = property(_getOptions, _setOptions, |
117 |
doc="The swap device's fstab options string") |
118 |
|
119 |
@property |
120 |
def status(self): |
121 |
""" Device status. """ |
122 |
return self.exists and swap.swapstatus(self.device) |
123 |
|
124 |
def setup(self, *args, **kwargs): |
125 |
""" Open, or set up, a device. """ |
126 |
log_method_call(self, device=self.device, |
127 |
type=self.type, status=self.status) |
128 |
if not self.exists: |
129 |
raise SwapSpaceError("format has not been created") |
130 |
|
131 |
if self.status: |
132 |
return |
133 |
|
134 |
DeviceFormat.setup(self, *args, **kwargs) |
135 |
swap.swapon(self.device, priority=self.priority) |
136 |
|
137 |
def teardown(self, *args, **kwargs): |
138 |
""" Close, or tear down, a device. """ |
139 |
log_method_call(self, device=self.device, |
140 |
type=self.type, status=self.status) |
141 |
if not self.exists: |
142 |
raise SwapSpaceError("format has not been created") |
143 |
|
144 |
if self.status: |
145 |
swap.swapoff(self.device) |
146 |
|
147 |
def create(self, *args, **kwargs): |
148 |
""" Create the device. """ |
149 |
log_method_call(self, device=self.device, |
150 |
type=self.type, status=self.status) |
151 |
intf = kwargs.get("intf") |
152 |
force = kwargs.get("force") |
153 |
if not force and self.exists: |
154 |
raise SwapSpaceError("format already exists") |
155 |
|
156 |
if force: |
157 |
self.teardown() |
158 |
elif self.status: |
159 |
raise SwapSpaceError("device exists and is active") |
160 |
|
161 |
w = None |
162 |
if intf: |
163 |
w = intf.progressWindow(_("Formatting"), |
164 |
_("Creating %s on %s") |
165 |
% (self.type, |
166 |
kwargs.get("device", self.device)), |
167 |
100, pulse = True) |
168 |
|
169 |
try: |
170 |
DeviceFormat.create(self, *args, **kwargs) |
171 |
swap.mkswap(self.device, label=self.label, progress=w) |
172 |
except Exception: |
173 |
raise |
174 |
else: |
175 |
self.exists = True |
176 |
finally: |
177 |
if w: |
178 |
w.pop() |
179 |
|
180 |
def writeKS(self, f): |
181 |
f.write("swap") |
182 |
|
183 |
if self.label: |
184 |
f.write(" --label=\"%s\"" % self.label) |
185 |
|
186 |
|
187 |
register_device_format(SwapSpace) |
188 |
|