New version.
[pykickstart.git] / pykickstart / commands / autopart.py
blobe19a89f0d6fd82331a824035df93e00cf2e79eea
2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2005, 2006, 2007, 2008, 2012 Red Hat, Inc.
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2. This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
20 from pykickstart.base import KickstartCommand
21 from pykickstart.constants import AUTOPART_TYPE_BTRFS, AUTOPART_TYPE_LVM, AUTOPART_TYPE_LVM_THINP, AUTOPART_TYPE_PLAIN
22 from pykickstart.errors import KickstartParseError, KickstartValueError, formatErrorMsg
23 from pykickstart.options import KSOptionParser
25 import gettext
26 _ = lambda x: gettext.ldgettext("pykickstart", x)
28 class FC3_AutoPart(KickstartCommand):
29 removedKeywords = KickstartCommand.removedKeywords
30 removedAttrs = KickstartCommand.removedAttrs
32 def __init__(self, writePriority=100, *args, **kwargs):
33 KickstartCommand.__init__(self, writePriority, *args, **kwargs)
34 self.autopart = kwargs.get("autopart", False)
36 def __str__(self):
37 retval = KickstartCommand.__str__(self)
39 if self.autopart:
40 retval += "autopart\n"
42 return retval
44 def parse(self, args):
45 if len(args) > 0:
46 raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("Kickstart command %s does not take any arguments") % "autopart"))
48 self.autopart = True
49 return self
51 class F9_AutoPart(FC3_AutoPart):
52 removedKeywords = FC3_AutoPart.removedKeywords
53 removedAttrs = FC3_AutoPart.removedAttrs
55 def __init__(self, writePriority=100, *args, **kwargs):
56 FC3_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
57 self.encrypted = kwargs.get("encrypted", False)
58 self.passphrase = kwargs.get("passphrase", "")
60 self.op = self._getParser()
62 def __str__(self):
63 retval = KickstartCommand.__str__(self)
65 if not self.autopart:
66 return retval
68 retval += "autopart"
70 if self.encrypted:
71 retval += " --encrypted"
73 if self.passphrase != "":
74 retval += " --passphrase=\"%s\""% self.passphrase
76 retval += "\n"
77 return retval
79 def _getParser(self):
80 op = KSOptionParser()
81 op.add_option("--encrypted", action="store_true", default=False)
82 op.add_option("--passphrase")
83 return op
85 def parse(self, args):
86 (opts, extra) = self.op.parse_args(args=args, lineno=self.lineno)
87 # Rely on any error handling from baseclass
88 FC3_AutoPart.parse(self, extra)
90 self._setToSelf(self.op, opts)
91 return self
93 class F12_AutoPart(F9_AutoPart):
94 removedKeywords = F9_AutoPart.removedKeywords
95 removedAttrs = F9_AutoPart.removedAttrs
97 def __init__(self, writePriority=100, *args, **kwargs):
98 F9_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
100 self.escrowcert = kwargs.get("escrowcert", "")
101 self.backuppassphrase = kwargs.get("backuppassphrase", False)
103 def __str__(self):
104 retval = F9_AutoPart.__str__(self)
106 if not self.autopart:
107 return retval
109 if self.encrypted and self.escrowcert != "":
110 retval = retval.strip()
112 retval += " --escrowcert=\"%s\"" % self.escrowcert
114 if self.backuppassphrase:
115 retval += " --backuppassphrase"
117 retval += "\n"
119 return retval
121 def _getParser(self):
122 op = F9_AutoPart._getParser(self)
123 op.add_option("--escrowcert")
124 op.add_option("--backuppassphrase", action="store_true", default=False)
125 return op
127 class RHEL6_AutoPart(F12_AutoPart):
128 removedKeywords = F12_AutoPart.removedKeywords
129 removedAttrs = F12_AutoPart.removedAttrs
131 def __init__(self, writePriority=100, *args, **kwargs):
132 F12_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
133 self.cipher = kwargs.get("cipher", "")
135 def __str__(self):
136 retval = F12_AutoPart.__str__(self)
137 if not self.autopart:
138 return retval
140 if self.encrypted and self.cipher:
141 # remove any trailing newline
142 retval = retval.strip()
143 retval += " --cipher=\"%s\"" % self.cipher
144 retval += "\n"
146 return retval
148 def _getParser(self):
149 op = F12_AutoPart._getParser(self)
150 op.add_option("--cipher")
151 return op
153 def parse(self, args):
154 # call the overriden command to do it's job first
155 retval = F12_AutoPart.parse(self, args)
157 # Using autopart together with other partitioning command such as
158 # part/partition, raid, logvol or volgroup can lead to hard to debug
159 # behavior that might among other result into an unbootable system.
161 # Therefore if any of those commands is detected in the same kickstart
162 # together with autopart, an error is raised and installation is
163 # aborted.
164 conflicting_command = ""
166 # seen indicates that the corresponding
167 # command has been seen in kickstart
168 if self.handler.partition.seen:
169 conflicting_command = "part/partition"
170 elif self.handler.raid.seen:
171 conflicting_command = "raid"
172 elif self.handler.volgroup.seen:
173 conflicting_command = "volgroup"
174 elif self.handler.logvol.seen:
175 conflicting_command = "logvol"
177 if conflicting_command:
178 # allow for translation of the error message
179 errorMsg = _("The %s and autopart commands can't be used at the same time") % \
180 conflicting_command
181 raise KickstartParseError(formatErrorMsg(self.lineno, msg=errorMsg))
182 return retval
185 class F16_AutoPart(F12_AutoPart):
186 removedKeywords = F12_AutoPart.removedKeywords
187 removedAttrs = F12_AutoPart.removedAttrs
189 def __init__(self, writePriority=100, *args, **kwargs):
190 F12_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
191 self.lvm = kwargs.get("lvm", True)
193 def __str__(self):
194 retval = F12_AutoPart.__str__(self)
195 if not self.autopart:
196 return retval
198 # If requested, disable LVM autopart
199 if not self.lvm:
200 # remove any trailing newline
201 retval = retval.strip()
202 retval += " --nolvm"
203 retval += "\n"
205 return retval
207 def _getParser(self):
208 op = F12_AutoPart._getParser(self)
209 op.add_option("--nolvm", action="store_false", dest="lvm",
210 default=True)
211 return op
213 class F17_AutoPart(F16_AutoPart):
214 def __init__(self, writePriority=100, *args, **kwargs):
215 F16_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
216 self.type = kwargs.get("type", None)
217 self.typeMap = { "lvm": AUTOPART_TYPE_LVM,
218 "btrfs": AUTOPART_TYPE_BTRFS,
219 "plain": AUTOPART_TYPE_PLAIN,
220 "partition": AUTOPART_TYPE_PLAIN }
222 def _typeAsStr(self):
223 retval = None
225 for (key, value) in self.typeMap.items():
226 if value == self.type:
227 retval = key
228 break
230 if retval == "partition":
231 retval = "plain"
233 return retval
235 def __str__(self):
236 retval = F16_AutoPart.__str__(self)
237 if not self.autopart:
238 return retval
240 ty = self._typeAsStr()
241 if ty:
242 # remove any trailing newline
243 retval = retval.strip()
244 retval += " --type=%s\n" % ty
246 return retval
248 def _getParser(self):
249 def type_cb(option, opt_str, value, parser):
250 if self.typeMap.has_key(value.lower()):
251 parser.values.ensure_value(option.dest,
252 self.typeMap[value.lower()])
254 def nolvm_cb(option, opt_str, value, parser):
255 parser.values.ensure_value(option.dest, AUTOPART_TYPE_PLAIN)
257 op = F16_AutoPart._getParser(self)
258 op.add_option("--nolvm", action="callback", callback=nolvm_cb,
259 dest="type", nargs=0)
261 op.add_option("--type", action="callback", callback=type_cb,
262 dest="type", nargs=1, type="string")
263 return op
265 def parse(self, args):
266 (opts, extra) = self.op.parse_args(args=args, lineno=self.lineno)
267 # Rely on any error handling from baseclass
268 F16_AutoPart.parse(self, extra)
270 self._setToSelf(self.op, opts)
272 # make this always True to avoid writing --nolvm
273 self.lvm = True
275 return self
277 class F18_AutoPart(F17_AutoPart):
278 removedKeywords = F17_AutoPart.removedKeywords
279 removedAttrs = F17_AutoPart.removedAttrs
281 def __init__(self, writePriority=100, *args, **kwargs):
282 F17_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
283 self.cipher = kwargs.get("cipher", "")
285 def __str__(self):
286 retval = F17_AutoPart.__str__(self)
287 if not self.autopart:
288 return retval
290 if self.encrypted and self.cipher:
291 # remove any trailing newline
292 retval = retval.strip()
293 retval += " --cipher=\"%s\"" % self.cipher
294 retval += "\n"
296 return retval
298 def _getParser(self):
299 op = F17_AutoPart._getParser(self)
300 op.add_option("--cipher")
301 return op
304 class F20_AutoPart(F18_AutoPart):
305 def __init__(self, writePriority=100, *args, **kwargs):
306 F18_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
307 self.typeMap["thinp"] = AUTOPART_TYPE_LVM_THINP
309 def parse(self, args):
310 # call the overriden command to do it's job first
311 retval = F18_AutoPart.parse(self, args)
313 # Using autopart together with other partitioning command such as
314 # part/partition, raid, logvol or volgroup can lead to hard to debug
315 # behavior that might among other result into an unbootable system.
317 # Therefore if any of those commands is detected in the same kickstart
318 # together with autopart, an error is raised and installation is
319 # aborted.
320 conflicting_command = ""
322 # seen indicates that the corresponding
323 # command has been seen in kickstart
324 if self.handler.partition.seen:
325 conflicting_command = "part/partition"
326 elif self.handler.raid.seen:
327 conflicting_command = "raid"
328 elif self.handler.volgroup.seen:
329 conflicting_command = "volgroup"
330 elif self.handler.logvol.seen:
331 conflicting_command = "logvol"
333 if conflicting_command:
334 # allow for translation of the error message
335 errorMsg = _("The %s and autopart commands can't be used at the same time") % \
336 conflicting_command
337 raise KickstartParseError(formatErrorMsg(self.lineno, msg=errorMsg))
338 return retval