-
Notifications
You must be signed in to change notification settings - Fork 117
/
autopart.py
632 lines (498 loc) · 23.3 KB
/
autopart.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#
# Chris Lumens <clumens@redhat.com>
#
# Copyright 2005, 2006, 2007, 2008, 2012 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.
#
from pykickstart.base import KickstartCommand
from pykickstart.version import versionToLongString, RHEL6, RHEL7, RHEL8
from pykickstart.version import FC3, F9, F12, F16, F17, F18, F20, F21, F26, F29, F38
from pykickstart.constants import AUTOPART_TYPE_BTRFS, AUTOPART_TYPE_LVM, AUTOPART_TYPE_LVM_THINP, AUTOPART_TYPE_PLAIN
from pykickstart.errors import KickstartParseError
from pykickstart.options import KSOptionParser
from pykickstart.i18n import _
class FC3_AutoPart(KickstartCommand):
removedKeywords = KickstartCommand.removedKeywords
removedAttrs = KickstartCommand.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
KickstartCommand.__init__(self, writePriority, *args, **kwargs)
self.autopart = kwargs.get("autopart", False)
def __str__(self):
retval = KickstartCommand.__str__(self)
if self.autopart:
retval += "autopart\n"
return retval
def parse(self, args):
if len(args) > 0:
raise KickstartParseError(_("Kickstart command %s does not take any arguments") % "autopart", lineno=self.lineno)
self.autopart = True
return self
def _getParser(self):
return KSOptionParser(prog="autopart", description="""
Automatically create partitions -- a root (``/``) partition,
a swap partition, and an appropriate boot partition
for the architecture. On large enough drives, this
will also create a /home partition.""",
version=FC3, conflicts=self.conflictingCommands)
class F9_AutoPart(FC3_AutoPart):
removedKeywords = FC3_AutoPart.removedKeywords
removedAttrs = FC3_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
FC3_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.encrypted = kwargs.get("encrypted", False)
self.passphrase = kwargs.get("passphrase", "")
self.op = self._getParser()
def __str__(self):
retval = KickstartCommand.__str__(self)
if not self.autopart:
return retval
retval += "autopart"
if self.encrypted:
retval += " --encrypted"
if self.passphrase:
retval += " --passphrase=\"%s\""% self.passphrase
retval += "\n"
return retval
def _getParser(self):
op = FC3_AutoPart._getParser(self)
op.add_argument("--encrypted", action="store_true", default=False,
version=F9, help="""
Should all devices with support be encrypted by default?
This is equivalent to checking the "Encrypt" checkbox on
the initial partitioning screen.""")
op.add_argument("--passphrase", version=F9, help="""
Only relevant if ``--encrypted`` is specified. Provide
a default system-wide passphrase for all encrypted
devices.""")
return op
def parse(self, args):
ns = self.op.parse_args(args=args, lineno=self.lineno)
self.set_to_self(ns)
self.autopart = True
return self
class F12_AutoPart(F9_AutoPart):
removedKeywords = F9_AutoPart.removedKeywords
removedAttrs = F9_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F9_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.escrowcert = kwargs.get("escrowcert", "")
self.backuppassphrase = kwargs.get("backuppassphrase", False)
def __str__(self):
retval = F9_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.encrypted and self.escrowcert:
retval = retval.strip()
retval += " --escrowcert=\"%s\"" % self.escrowcert
if self.backuppassphrase:
retval += " --backuppassphrase"
retval += "\n"
return retval
def _getParser(self):
op = F9_AutoPart._getParser(self)
op.add_argument("--escrowcert", metavar="<url>", version=F12, help="""
Only relevant if ``--encrypted`` is specified. Load an
X.509 certificate from ``<url>``. Store the data
encryption keys of all encrypted volumes created during
installation, encrypted using the certificate, as files
in ``/root``.""")
op.add_argument("--backuppassphrase", action="store_true",
default=False, version=F12, help="""
Only relevant if ``--escrowcert`` is specified. In
addition to storing the data encryption keys, generate
a random passphrase and add it to all encrypted volumes
created during installation. Then store the passphrase,
encrypted using the certificate specified by
``--escrowcert``, as files in ``/root`` (one file for
each encrypted volume).""")
return op
class RHEL6_AutoPart(F12_AutoPart):
removedKeywords = F12_AutoPart.removedKeywords
removedAttrs = F12_AutoPart.removedAttrs
conflictingCommands = ["partition", "raid", "volgroup", "logvol"]
def __init__(self, writePriority=100, *args, **kwargs):
F12_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.cipher = kwargs.get("cipher", "")
def __str__(self):
retval = F12_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.encrypted and self.cipher:
# remove any trailing newline
retval = retval.strip()
retval += " --cipher=\"%s\"" % self.cipher
retval += "\n"
return retval
def _getParser(self):
op = F12_AutoPart._getParser(self)
op.add_argument("--cipher", version=RHEL6, help="""
Only relevant if ``--encrypted`` is specified. Specifies
which encryption algorithm should be used to encrypt the
filesystem.""")
return op
class F16_AutoPart(F12_AutoPart):
removedKeywords = F12_AutoPart.removedKeywords
removedAttrs = F12_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F12_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.lvm = kwargs.get("lvm", True)
def __str__(self):
retval = F12_AutoPart.__str__(self)
if not self.autopart:
return retval
# If requested, disable LVM autopart
if not self.lvm:
# remove any trailing newline
retval = retval.strip()
retval += " --nolvm"
retval += "\n"
return retval
def _getParser(self):
op = F12_AutoPart._getParser(self)
op.add_argument("--nolvm", action="store_false", dest="lvm",
default=True, version=F16,
help="Don't use LVM when partitioning.")
return op
class F17_AutoPart(F16_AutoPart):
def __init__(self, writePriority=100, *args, **kwargs):
self.typeMap = {"lvm": AUTOPART_TYPE_LVM,
"btrfs": AUTOPART_TYPE_BTRFS,
"plain": AUTOPART_TYPE_PLAIN,
"partition": AUTOPART_TYPE_PLAIN}
F16_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.type = kwargs.get("type", None)
def _typeAsStr(self):
retval = None
for (key, value) in list(self.typeMap.items()):
if value == self.type:
retval = key
break
if retval == "partition":
retval = "plain"
return retval
def __str__(self):
retval = F16_AutoPart.__str__(self)
if not self.autopart:
return retval
ty = self._typeAsStr()
if ty:
# remove any trailing newline
retval = retval.strip()
retval += " --type=%s\n" % ty
return retval
def _type_cb(self, value):
if value.lower() in self.typeMap:
return self.typeMap[value.lower()]
else:
raise KickstartParseError(_("Invalid autopart type: %s") % value, lineno=self.lineno)
def _getParser(self):
op = F16_AutoPart._getParser(self)
op.add_argument("--nolvm", action="store_const", version=F17,
const=AUTOPART_TYPE_PLAIN, dest="type",
help="The same as ``--type=plain``")
op.add_argument("--type", type=self._type_cb, version=F17, help="""
Select automatic partitioning scheme. Must be one of the
following: %s. Plain means regular
partitions with no btrfs or lvm.""" % list(self.typeMap.keys()))
return op
def parse(self, args):
retval = F16_AutoPart.parse(self, args)
# make this always True to avoid writing --nolvm
self.lvm = True
return retval
class F18_AutoPart(F17_AutoPart):
removedKeywords = F17_AutoPart.removedKeywords
removedAttrs = F17_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F17_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.cipher = kwargs.get("cipher", "")
def __str__(self):
retval = F17_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.encrypted and self.cipher:
# remove any trailing newline
retval = retval.strip()
retval += " --cipher=\"%s\"" % self.cipher
retval += "\n"
return retval
def _getParser(self):
op = F17_AutoPart._getParser(self)
op.add_argument("--cipher", version=F18, help="""
Only relevant if ``--encrypted`` is specified. Specifies
which encryption algorithm should be used to encrypt the
filesystem.""")
return op
class F20_AutoPart(F18_AutoPart):
conflictingCommands = ["partition", "raid", "volgroup", "logvol"]
def __init__(self, writePriority=100, *args, **kwargs):
F18_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.typeMap["thinp"] = AUTOPART_TYPE_LVM_THINP
def _getParser(self):
"Only necessary for the type change documentation"
op = F18_AutoPart._getParser(self)
for action in op._actions:
if "--type" in action.option_strings:
action.help += """
.. versionchanged:: %s
Partitioning scheme 'thinp' was added.""" % versionToLongString(F20)
return op
class F21_AutoPart(F20_AutoPart):
removedKeywords = F20_AutoPart.removedKeywords
removedAttrs = F20_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F20_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.fstype = kwargs.get("fstype", "")
def __str__(self):
retval = F20_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.fstype:
# remove any trailing newline
retval = retval.strip()
retval += " --fstype=%s" % self.fstype
retval += "\n"
return retval
def _getParser(self):
op = F20_AutoPart._getParser(self)
op.add_argument("--fstype", version=F21, help="""
Use the specified filesystem type on the partitions.
Note that it cannot be used with ``--type=btrfs`` since
btrfs is both a partition scheme and a filesystem. eg.
``--fstype=ext4``.""")
return op
def parse(self, args):
# call the overriden command to do its job first
retval = F20_AutoPart.parse(self, args)
# btrfs is not a valid filesystem type
if self.fstype == "btrfs":
raise KickstartParseError(_("autopart --fstype=btrfs is not valid fstype, use --type=btrfs instead"), lineno=self.lineno)
if self._typeAsStr() == "btrfs" and self.fstype:
raise KickstartParseError(_("autopart --fstype cannot be used with --type=btrfs"), lineno=self.lineno)
return retval
class F23_AutoPart(F21_AutoPart):
conflictingCommands = ["partition", "raid", "volgroup", "logvol", "reqpart"]
class RHEL7_AutoPart(F21_AutoPart):
conflictingCommands = ["partition", "raid", "volgroup", "logvol", "reqpart"]
def __init__(self, writePriority=100, *args, **kwargs):
F21_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.nohome = kwargs.get("nohome", False)
def __str__(self):
retval = F21_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.nohome:
# remove any trailing newline
retval = retval.strip()
retval += " --nohome"
retval += "\n"
return retval
def _getParser(self):
op = F21_AutoPart._getParser(self)
op.add_argument("--nohome", action="store_true", default=False,
version=RHEL7, help="""
Do not create a /home partition.""")
return op
class F26_AutoPart(F23_AutoPart):
removedKeywords = F23_AutoPart.removedKeywords
removedAttrs = F23_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F23_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.nohome = kwargs.get("nohome", False)
self.noboot = kwargs.get("noboot", False)
self.noswap = kwargs.get("noswap", False)
def __str__(self):
retval = F23_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.nohome:
# remove any trailing newline
retval = retval.strip()
retval += " --nohome"
retval += "\n"
if self.noboot:
# remove any trailing newline
retval = retval.strip()
retval += " --noboot"
retval += "\n"
if self.noswap:
# remove any trailing newline
retval = retval.strip()
retval += " --noswap"
retval += "\n"
return retval
def _getParser(self):
op = F23_AutoPart._getParser(self)
op.add_argument("--nohome", action="store_true", default=False,
version=F26, help="""
Do not create a /home partition.""")
op.add_argument("--noboot", action="store_true", default=False,
version=F26, help="""
Do not create a /boot partition.""")
op.add_argument("--noswap", action="store_true", default=False,
version=F26, help="""
Do not create a swap partition. Only one of ``--noswap``
and ``--hibernation`` can be specified.""")
return op
class F29_AutoPart(F26_AutoPart):
removedKeywords = F26_AutoPart.removedKeywords
removedAttrs = F26_AutoPart.removedAttrs
def __init__(self, writePriority=100, *args, **kwargs):
F26_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.luks_version = kwargs.get("luks_version", "")
self.pbkdf = kwargs.get("pbkdf", "")
self.pbkdf_memory = kwargs.get("pbkdf_memory", 0)
self.pbkdf_time = kwargs.get("pbkdf_time", 0)
self.pbkdf_iterations = kwargs.get("pbkdf_iterations", 0)
def __str__(self):
retval = F26_AutoPart.__str__(self)
if not self.autopart:
return retval
if self.encrypted and self.luks_version:
retval = retval.strip()
retval += " --luks-version=%s" % self.luks_version
retval += "\n"
if self.encrypted and self.pbkdf:
retval = retval.strip()
retval += " --pbkdf=%s" % self.pbkdf
retval += "\n"
if self.encrypted and self.pbkdf_memory:
retval = retval.strip()
retval += " --pbkdf-memory=%s" % self.pbkdf_memory
retval += "\n"
if self.encrypted and self.pbkdf_time:
retval = retval.strip()
retval += " --pbkdf-time=%s" % self.pbkdf_time
retval += "\n"
if self.encrypted and self.pbkdf_iterations:
retval = retval.strip()
retval += " --pbkdf-iterations=%s" % self.pbkdf_iterations
retval += "\n"
return retval
def _getParser(self):
op = F26_AutoPart._getParser(self)
op.add_argument("--luks-version", dest="luks_version", version=F29, default="",
help="""
Only relevant if ``--encrypted`` is specified. Specifies
which version of LUKS format should be used to encrypt
the filesystem.""")
op.add_argument("--pbkdf", version=F29, default="", help="""
Only relevant if ``--encrypted`` is specified. Sets
Password-Based Key Derivation Function (PBKDF) algorithm
for LUKS keyslot. See ``man cryptsetup``.""")
op.add_argument("--pbkdf-memory", dest="pbkdf_memory", type=int, default=0,
version=F29, help="""
Only relevant if ``--encrypted`` is specified. Sets
the memory cost for PBKDF. See ``man cryptsetup``.""")
op.add_argument("--pbkdf-time", dest="pbkdf_time", type=int, default=0,
version=F29, help="""
Only relevant if ``--encrypted`` is specified. Sets
the number of milliseconds to spend with PBKDF passphrase
processing. See ``--iter-time`` in ``man cryptsetup``.
Only one of ``--pbkdf-time`` and ``--pbkdf-iterations``
can be specified.
""")
op.add_argument("--pbkdf-iterations", dest="pbkdf_iterations", type=int, default=0,
version=F29, help="""
Only relevant if ``--encrypted`` is specified. Sets
the number of iterations directly and avoids PBKDF benchmark.
See ``--pbkdf-force-iterations`` in ``man cryptsetup``.
Only one of ``--pbkdf-time`` and ``--pbkdf-iterations``
can be specified.
""")
return op
def parse(self, args):
retval = F26_AutoPart.parse(self, args)
if self.pbkdf_time and self.pbkdf_iterations:
msg = _("Only one of --pbkdf-time and --pbkdf-iterations can be specified.")
raise KickstartParseError(msg, lineno=self.lineno)
return retval
class RHEL8_AutoPart(F29_AutoPart):
removedKeywords = F29_AutoPart.removedKeywords
removedAttrs = F29_AutoPart.removedAttrs
def parse(self, args):
# call the overriden command to do it's job first
retval = F29_AutoPart.parse(self, args)
# btrfs is no more supported
if self._typeAsStr() == "btrfs":
raise KickstartParseError(_("autopart --type=btrfs is not supported"),
lineno=self.lineno)
return retval
def _getParser(self):
"Only necessary for the type change documentation"
op = F29_AutoPart._getParser(self)
for action in op._actions:
if "--type" in action.option_strings:
action.help += """
.. versionchanged:: %s
Partitioning scheme 'btrfs' was removed.""" % versionToLongString(RHEL8)
if "--fstype" in action.option_strings:
action.help += """
.. versionchanged:: %s
Partitioning scheme 'btrfs' was removed.""" % versionToLongString(RHEL8)
return op
class RHEL9_AutoPart(RHEL8_AutoPart):
pass
class F38_AutoPart(F29_AutoPart):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.hibernation = kwargs.get("hibernation", False)
def __str__(self):
retval = super().__str__()
if self.autopart and self.hibernation:
# remove any trailing newline
retval = retval.strip()
retval += " --hibernation"
retval += "\n"
return retval
def _getParser(self):
op = super()._getParser()
op.add_argument("--hibernation", action="store_true", default=False,
version=F38, help="""
Create a swap partition with an automatically determined
size that's big enough for hibernation. Only one of
``--noswap`` and ``--hibernation`` can be specified.""")
return op
def parse(self, args):
retval = super().parse(args)
if self.hibernation and self.noswap:
msg = _("Only one of --noswap and --hibernation can be specified.")
raise KickstartParseError(msg, lineno=self.lineno)
return retval
class RHEL10_AutoPart(F38_AutoPart):
removedKeywords = F38_AutoPart.removedKeywords
removedAttrs = F38_AutoPart.removedAttrs
def parse(self, args):
# call the overriden command to do it's job first
retval = F38_AutoPart.parse(self, args)
# btrfs is no more supported
if self._typeAsStr() == "btrfs":
raise KickstartParseError(_("autopart --type=btrfs is not supported"),
lineno=self.lineno)
return retval
def _getParser(self):
"Only necessary for the type change documentation"
op = F38_AutoPart._getParser(self)
for action in op._actions:
if "--type" in action.option_strings:
action.help += """
.. versionchanged:: %s
Partitioning scheme 'btrfs' was removed.""" % versionToLongString(RHEL8)
if "--fstype" in action.option_strings:
action.help += """
.. versionchanged:: %s
Partitioning scheme 'btrfs' was removed.""" % versionToLongString(RHEL8)
return op