-
Notifications
You must be signed in to change notification settings - Fork 118
/
nvdimm.py
187 lines (154 loc) · 8.29 KB
/
nvdimm.py
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
#
# Radek Vykydal <rvykydal@redhat.com>
#
# Copyright 2018 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. 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.
#
import warnings
from pykickstart.base import BaseData, KickstartCommand, DeprecatedCommand, RemovedCommand
from pykickstart.errors import KickstartParseError, KickstartParseWarning
from pykickstart.options import KSOptionParser, commaSplit
from pykickstart.constants import NVDIMM_MODE_SECTOR, NVDIMM_ACTION_RECONFIGURE, \
NVDIMM_ACTION_USE
from pykickstart.version import F28, F40, RHEL10, versionToLongString
from pykickstart.i18n import _
class F28_NvdimmData(BaseData):
removedKeywords = BaseData.removedKeywords
removedAttrs = BaseData.removedAttrs
def __init__(self, *args, **kwargs):
BaseData.__init__(self, *args, **kwargs)
self.action = kwargs.get("action", None)
self.namespace = kwargs.get("namespace", "")
self.mode = kwargs.get("mode", None)
self.sectorsize = kwargs.get("sectorsize", None)
self.blockdevs = kwargs.get("blockdevs", [])
def __eq__(self, y):
if not y:
return False
return (self.action == y.action and
self.namespace == y.namespace and
self.blockdevs == y.blockdevs)
def __ne__(self, y):
return not self == y
def _getArgsAsStr(self):
retval = "%s" % self.action
if self.action == NVDIMM_ACTION_RECONFIGURE:
if self.namespace:
retval += " --namespace=%s" % self.namespace
if self.mode == NVDIMM_MODE_SECTOR:
retval += " --mode=%s" % NVDIMM_MODE_SECTOR
if self.sectorsize:
retval += " --sectorsize=%d" % self.sectorsize
elif self.action == NVDIMM_ACTION_USE:
if self.namespace:
retval += " --namespace=%s" % self.namespace
if self.blockdevs:
retval += " --blockdevs=%s" % ",".join(self.blockdevs)
return retval
def __str__(self):
retval = BaseData.__str__(self)
retval += "nvdimm %s" % self._getArgsAsStr()
return retval.strip() + "\n"
class F28_Nvdimm(KickstartCommand):
removedKeywords = KickstartCommand.removedKeywords
removedAttrs = KickstartCommand.removedAttrs
validActions = [NVDIMM_ACTION_RECONFIGURE, NVDIMM_ACTION_USE]
validModes = [NVDIMM_MODE_SECTOR]
def __init__(self, writePriority=80, *args, **kwargs):
KickstartCommand.__init__(self, writePriority, *args, **kwargs)
self.actionList = kwargs.get("actionList", [])
self.op = self._getParser()
def __str__(self):
retval = ""
for action in self.actionList:
retval += action.__str__()
if retval != "":
retval = "# NVDIMM devices setup\n" + retval + "\n"
return retval
def _getParser(self):
op = KSOptionParser(prog="nvdimm", version=F28, description="""
Perform an action on an nvdimm device.""")
op.add_argument("action", choices=self.validActions, nargs=1, default=NVDIMM_ACTION_RECONFIGURE,
version=F28, help="""
The action to be performed on the device specified by further options.
The device can be specified by ``--namespace`` or ``--blockdevs`` options,
depending on the action.
Valid actions:
- ``reconfigure``: Reconfigures the device specified by ``--namespace``
into the mode specified by ``--mode`` and (depending on the mode)
``--sectorsize`` options. The device reconfigured into sector mode
will be allowed to be used for storage configuration.
- ``use``: Allow the device to be used for storage configuration. By default nvdimm
devices are ignored. Only devices in sector mode can be used.
""")
op.add_argument("--namespace", metavar="<namespace>", version=F28,
help="""The device specification by namespace.""")
op.add_argument("--blockdevs", metavar="<devspec1>,<devspec2>,...,<devspecN>", type=commaSplit, version=F28,
help="""Specification of devices by comma separated list of block device names.""")
op.add_argument("--mode", choices=self.validModes, default=NVDIMM_MODE_SECTOR,
version=F28, help="""The mode specification.""")
op.add_argument("--sectorsize", type=int, version=F28,
help="""Size of a sector for sector mode.""")
return op
def parse(self, args):
(ns, extra) = self.op.parse_known_args(args=args, lineno=self.lineno)
if extra:
raise KickstartParseError(_("Unexpected arguments to nvdimm command: %(arguments)s") %
{"arguments": extra}, lineno=self.lineno)
nvdimm_data = self.dataClass() # pylint: disable=not-callable
self.set_to_obj(ns, nvdimm_data)
nvdimm_data.lineno = self.lineno
action = ns.action[0]
nvdimm_data.action = action
if nvdimm_data.namespace and nvdimm_data.blockdevs:
raise KickstartParseError(_("Only one of --namespace and --blockdevs device specifications can be used"))
# Check for duplicates in the data list.
if nvdimm_data in self.dataList():
if nvdimm_data.namespace:
warnings.warn(_("An action %(action)s on namespace %(namespace)s has already been defined.")
% {"action": action, "namespace": nvdimm_data.namespace}, KickstartParseWarning)
if nvdimm_data.blockdevs:
warnings.warn(_("An action %(action)s on devices %(blockdevs)s has already been defined.")
% {"action": action, "blockdevs": nvdimm_data.blockdevs}, KickstartParseWarning)
if action == NVDIMM_ACTION_RECONFIGURE:
if not nvdimm_data.namespace:
raise KickstartParseError(_("Action %(action)s requires --namespace argument to be set")
% {"action": action})
if nvdimm_data.mode == NVDIMM_MODE_SECTOR and not nvdimm_data.sectorsize:
raise KickstartParseError(_("Action %(action)s with mode %(mode)s requires --sectorsize argument to be set")
% {"action": action, "mode": nvdimm_data.mode})
elif action == NVDIMM_ACTION_USE:
if not nvdimm_data.namespace and not nvdimm_data.blockdevs:
raise KickstartParseError(_("Action %(action)s requires --namespace or --blockdevs argument to be set")
% {"action": action})
return nvdimm_data
def dataList(self):
return self.actionList
@property
def dataClass(self):
return self.handler.NvdimmData
class F40_Nvdimm(DeprecatedCommand, F28_Nvdimm):
def __init__(self): # pylint: disable=super-init-not-called
DeprecatedCommand.__init__(self)
def _getParser(self):
op = F28_Nvdimm._getParser(self)
op.description += "\n\n.. deprecated:: %s" % versionToLongString(F40)
return op
class RHEL10_Nvdimm(RemovedCommand, F40_Nvdimm):
def _getParser(self):
op = F40_Nvdimm._getParser(self)
op.description += "\n\n.. versionremoved:: %s" % versionToLongString(RHEL10)
return op