-
Notifications
You must be signed in to change notification settings - Fork 83
/
deviceaction.py
961 lines (764 loc) · 33.2 KB
/
deviceaction.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# deviceaction.py
# Device modification action classes.
#
# Copyright (C) 2009-2014 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>
#
from . import util
from . import udev
from .util import get_current_entropy
from .devices import StorageDevice
from .devices import PartitionDevice, LVMLogicalVolumeDevice
from .formats import getFormat, luks
from parted import partitionFlag, PARTITION_LBA
from .i18n import _, N_
from .callbacks import CreateFormatPreData, CreateFormatPostData
from .callbacks import ResizeFormatPreData, ResizeFormatPostData
from .callbacks import WaitForEntropyData, ReportProgressData
from .size import Size
import logging
log = logging.getLogger("blivet")
# The values are just hints as to the ordering.
# Eg: fsmod and devmod ordering depends on the mod (shrink -v- grow)
ACTION_TYPE_NONE = 0
ACTION_TYPE_DESTROY = 1000
ACTION_TYPE_RESIZE = 500
ACTION_TYPE_CREATE = 100
ACTION_TYPE_ADD = 50
ACTION_TYPE_REMOVE = 10
action_strings = {ACTION_TYPE_NONE: "None",
ACTION_TYPE_DESTROY: "Destroy",
ACTION_TYPE_RESIZE: "Resize",
ACTION_TYPE_CREATE: "Create",
ACTION_TYPE_ADD: "Add",
ACTION_TYPE_REMOVE: "Remove"}
ACTION_OBJECT_NONE = 0
ACTION_OBJECT_FORMAT = 1
ACTION_OBJECT_DEVICE = 2
ACTION_OBJECT_CONTAINER = 3
object_strings = {ACTION_OBJECT_NONE: "None",
ACTION_OBJECT_FORMAT: "Format",
ACTION_OBJECT_DEVICE: "Device",
ACTION_OBJECT_CONTAINER: "Container"}
RESIZE_SHRINK = 88
RESIZE_GROW = 89
resize_strings = {RESIZE_SHRINK: "Shrink",
RESIZE_GROW: "Grow"}
def action_type_from_string(type_string):
if type_string is None:
return None
for (k,v) in action_strings.items():
if v.lower() == type_string.lower():
return k
return resize_type_from_string(type_string)
def action_object_from_string(type_string):
if type_string is None:
return None
for (k,v) in object_strings.items():
if v.lower() == type_string.lower():
return k
def resize_type_from_string(type_string):
if type_string is None:
return None
for (k,v) in resize_strings.items():
if v.lower() == type_string.lower():
return k
class DeviceAction(util.ObjectID):
""" An action that will be carried out in the future on a Device.
These classes represent actions to be performed on devices or
filesystems.
The operand Device instance will be modified according to the
action, but no changes will be made to the underlying device or
filesystem until the DeviceAction instance's execute method is
called. The DeviceAction instance's cancel method should reverse
any modifications made to the Device instance's attributes.
If the Device instance represents a pre-existing device, the
constructor should call any methods or set any attributes that the
action will eventually change. Device/DeviceFormat classes should verify
that the requested modifications are reasonable and raise an
exception if not.
Only one action of any given type/object pair can exist for any
given device at any given time. This is enforced by the
DeviceTree.
Basic usage:
a = DeviceAction(dev)
a.execute()
OR
a = DeviceAction(dev)
a.cancel()
XXX should we back up the device with a deep copy for forcibly
cancelling actions?
The downside is that we lose any checking or verification that
would get done when resetting the Device instance's attributes to
their original values.
The upside is that we would be guaranteed to achieve a total
reversal. No chance of, eg: resizes ending up altering Device
size due to rounding or other miscalculation.
"""
type = ACTION_TYPE_NONE
obj = ACTION_OBJECT_NONE
typeDescStr = ""
def __init__(self, device):
util.ObjectID.__init__(self)
if not isinstance(device, StorageDevice):
raise ValueError("arg 1 must be a StorageDevice instance")
unavailable_dependencies = device.unavailableDependencies
if unavailable_dependencies:
dependencies_str = ", ".join(str(d) for d in unavailable_dependencies)
raise ValueError("device type %s requires unavailable_dependencies: %s" % (device.type, dependencies_str))
self.device = device
self.container = getattr(self.device, "container", None)
self._applied = False
def apply(self):
""" apply changes related to the action to the device(s) """
self._applied = True
def execute(self, callbacks=None):
"""
Perform the action.
:param callbacks: callbacks to be run when matching actions are
executed (see :meth:`~.blivet.Blivet.doIt`)
"""
# pylint: disable=unused-argument
if not self._applied:
raise RuntimeError("cannot execute unapplied action")
if callbacks and callbacks.report_progress:
msg = _("Executing %(action)s") % {"action": str(self)}
callbacks.report_progress(ReportProgressData(msg))
def cancel(self):
""" cancel the action """
self._applied = False
@property
def isDestroy(self):
return self.type == ACTION_TYPE_DESTROY
@property
def isCreate(self):
return self.type == ACTION_TYPE_CREATE
@property
def isResize(self):
return self.type == ACTION_TYPE_RESIZE
@property
def isShrink(self):
return (self.isResize and self.dir == RESIZE_SHRINK) # pylint: disable=no-member
@property
def isGrow(self):
return (self.isResize and self.dir == RESIZE_GROW) # pylint: disable=no-member
@property
def isAdd(self):
return self.type == ACTION_TYPE_ADD
@property
def isRemove(self):
return self.type == ACTION_TYPE_REMOVE
@property
def isDevice(self):
return self.obj == ACTION_OBJECT_DEVICE
@property
def isContainer(self):
return self.obj == ACTION_OBJECT_CONTAINER
@property
def isFormat(self):
return self.obj == ACTION_OBJECT_FORMAT
@property
def format(self):
return self.device.format
@property
def typeString(self):
""" String indicating if this action is a create, destroy or resize. """
return action_strings[self.type]
@property
def objectString(self):
""" String indicating if this action's operand is device or format. """
return object_strings[self.obj]
@property
def resizeString(self):
""" String representing the direction of a resize action. """
s = ""
if self.isResize:
s = resize_strings[self.dir] # pylint: disable=no-member
return s
@property
def objectTypeString(self):
""" String representing the type of the operand device or format. """
if self.isFormat:
s = self.format.name
else:
s = self.device.type
return s
@property
def typeDesc(self):
return _(self.typeDescStr)
# Force str and unicode types since there's a good chance that the self.device.*
# strings are unicode.
def _toString(self):
s = "[%d] %s" % (self.id, self.typeDescStr)
if self.isResize:
s += " (%s)" % self.resizeString
if self.isFormat:
s += " %s on" % self.format.desc
s += " %s %s (id %d)" % (self.device.type, self.device.name,
self.device.id)
return s
def __str__(self):
return util.stringize(self._toString())
def __unicode__(self):
return util.unicodeize(self._toString())
def requires(self, action):
""" Return True if self requires action. """
return (not (self.isContainer or action.isContainer) and
self.type < action.type)
def obsoletes(self, action):
""" Return True is self obsoletes action.
DeviceAction instances obsolete other DeviceAction instances with
lower id and same device.
"""
return (self.device.id == action.device.id and
self.type == action.type and
self.obj == action.obj and
self.id > action.id)
class ActionCreateDevice(DeviceAction):
""" Action representing the creation of a new device. """
type = ACTION_TYPE_CREATE
obj = ACTION_OBJECT_DEVICE
typeDescStr = N_("create device")
def __init__(self, device):
if device.exists:
raise ValueError("device already exists")
# FIXME: assert device.fs is None
DeviceAction.__init__(self, device)
def execute(self, callbacks=None):
super(ActionCreateDevice, self).execute(callbacks=callbacks)
self.device.create()
def requires(self, action):
""" Return True if self requires action.
Device create actions require other actions when either of the
following is true:
- this action's device depends on the other action's device
- both actions are partition create actions on the same disk
and this partition has a higher number
- the other action adds a member to this device's container
"""
rc = super(ActionCreateDevice, self).requires(action)
if self.device.dependsOn(action.device):
rc = True
elif (action.isCreate and action.isDevice and
isinstance(self.device, PartitionDevice) and
isinstance(action.device, PartitionDevice) and
self.device.disk == action.device.disk):
# create partitions in ascending numerical order
selfNum = self.device.partedPartition.number
otherNum = action.device.partedPartition.number
if selfNum > otherNum:
rc = True
elif (action.isCreate and action.isDevice and
isinstance(self.device, LVMLogicalVolumeDevice) and
isinstance(action.device, LVMLogicalVolumeDevice) and
self.device.vg == action.device.vg):
# create cached LVs before non-cached LVs so that fast cache space
# is not taken by non-cached LVs
if not self.device.cached and action.device.cached:
rc = True
elif (action.isAdd and action.container == self.container):
rc = True
return rc
class ActionDestroyDevice(DeviceAction):
""" An action representing the deletion of an existing device. """
type = ACTION_TYPE_DESTROY
obj = ACTION_OBJECT_DEVICE
typeDescStr = N_("destroy device")
def __init__(self, device):
# XXX should we insist that device.fs be None?
DeviceAction.__init__(self, device)
def execute(self, callbacks=None):
super(ActionDestroyDevice, self).execute(callbacks=callbacks)
self.device.destroy()
def requires(self, action):
""" Return True if self requires action.
Device destroy actions require other actions when either of the
following is true:
- the other action's device depends on this action's device
- both actions are partition create actions on the same disk
and this partition has a lower number
- the other action removes this action's device from a container
"""
rc = super(ActionDestroyDevice, self).requires(action)
if action.device.dependsOn(self.device) and action.isDestroy:
rc = True
elif (action.isDestroy and action.isDevice and
isinstance(self.device, PartitionDevice) and
isinstance(action.device, PartitionDevice) and
self.device.disk == action.device.disk):
# remove partitions in descending numerical order
selfNum = self.device.partedPartition.number
otherNum = action.device.partedPartition.number
if selfNum < otherNum:
rc = True
elif (action.isDestroy and action.isFormat and
action.device.id == self.device.id):
# device destruction comes after destruction of device's format
rc = True
elif (action.isRemove and action.device == self.device):
rc = True
return rc
def obsoletes(self, action):
""" Return True if self obsoletes action.
- obsoletes all actions w/ lower id that act on the same device,
including self, if device does not exist
- obsoletes all but ActionDestroyFormat actions w/ lower id on the
same device if device exists
- obsoletes all actions that add a member to this action's
(container) device
"""
rc = False
if action.device.id == self.device.id:
if self.id >= action.id and not self.device.exists:
rc = True
elif self.id > action.id and \
self.device.exists and \
not (action.isDestroy and action.isFormat):
rc = True
elif action.isAdd and (action.device == self.device):
rc = True
elif action.isAdd and (action.container == self.device):
rc = True
return rc
class ActionResizeDevice(DeviceAction):
""" An action representing the resizing of an existing device. """
type = ACTION_TYPE_RESIZE
obj = ACTION_OBJECT_DEVICE
typeDescStr = N_("resize device")
def __init__(self, device, newsize):
if not device.resizable:
raise ValueError("device is not resizable")
if device.currentSize == newsize:
raise ValueError("new size same as old size")
if newsize < device.minSize:
raise ValueError("new size is too small")
if device.maxSize and newsize > device.maxSize:
raise ValueError("new size is too large")
DeviceAction.__init__(self, device)
if newsize > device.currentSize:
self.dir = RESIZE_GROW
else:
self.dir = RESIZE_SHRINK
if device.targetSize > Size(0):
self.origsize = device.targetSize
else:
self.origsize = device.size
self._targetSize = newsize
def apply(self):
""" apply changes related to the action to the device(s) """
if self._applied:
return
self.device.targetSize = self._targetSize
super(ActionResizeDevice, self).apply()
def execute(self, callbacks=None):
super(ActionResizeDevice, self).execute(callbacks=callbacks)
self.device.resize()
def cancel(self):
if not self._applied:
return
self.device.targetSize = self.origsize
super(ActionResizeDevice, self).cancel()
def requires(self, action):
""" Return True if self requires action.
A device resize action requires another action if:
- the other action is a format resize on the same device and
both are shrink operations
- the other action grows a device (or format it contains) that
this action's device depends on
- the other action shrinks a device (or format it contains)
that depends on this action's device
- the other action removes this action's device from a container
- the other action adds a member to this device's container
"""
retval = super(ActionResizeDevice, self).requires(action)
if action.isResize:
if self.device.id == action.device.id and \
self.dir == action.dir and \
action.isFormat and self.isShrink:
retval = True
elif action.isGrow and self.device.dependsOn(action.device):
retval = True
elif action.isShrink and action.device.dependsOn(self.device):
retval = True
elif (action.isRemove and action.device == self.device):
retval = True
elif (action.isAdd and action.container == self.container):
retval = True
return retval
class ActionCreateFormat(DeviceAction):
""" An action representing creation of a new filesystem. """
type = ACTION_TYPE_CREATE
obj = ACTION_OBJECT_FORMAT
typeDescStr = N_("create format")
def __init__(self, device, fmt=None):
"""
:param device: the device on which the format will be created
:type device: :class:`~.devices.StorageDevice`
:keyword fmt: the format to put on the device
:type fmt: :class:~.formats.DeviceFormat`
If no format is specified, it is assumed that the format is already
associated with the device.
"""
if device.formatImmutable:
raise ValueError("this device's formatting cannot be modified")
DeviceAction.__init__(self, device)
if fmt:
self.origFormat = device.format
else:
self.origFormat = getFormat(None)
self._format = fmt or device.format
if self._format.exists:
raise ValueError("specified format already exists")
if not self._format.formattable:
raise ValueError("resource to create this format %s is unavailable" % fmt)
def apply(self):
""" apply changes related to the action to the device(s) """
if self._applied:
return
self.device.format = self._format
super(ActionCreateFormat, self).apply()
def execute(self, callbacks=None):
super(ActionCreateFormat, self).execute(callbacks=callbacks)
if callbacks and callbacks.create_format_pre:
msg = _("Creating %(type)s on %(device)s") % {"type": self.device.format.type, "device": self.device.path}
callbacks.create_format_pre(CreateFormatPreData(msg))
if isinstance(self.device, PartitionDevice):
for flag in partitionFlag.keys():
# Keep the LBA flag on pre-existing partitions
if flag in [ PARTITION_LBA, self.format.partedFlag ]:
continue
self.device.unsetFlag(flag)
if self.format.partedFlag is not None:
self.device.setFlag(self.format.partedFlag)
if self.format.partedSystem is not None:
self.device.partedPartition.system = self.format.partedSystem
self.device.disk.format.commitToDisk()
udev.settle()
if isinstance(self.device.format, luks.LUKS):
# LUKS needs to wait for random data entropy if it is too low
min_required_entropy = self.device.format.min_luks_entropy
current_entropy = get_current_entropy()
if current_entropy < min_required_entropy:
force_cont = False
if callbacks and callbacks.wait_for_entropy:
msg = _("Not enough entropy to create LUKS format. "
"%d bits are needed.") % min_required_entropy
force_cont = callbacks.wait_for_entropy(WaitForEntropyData(msg, min_required_entropy))
if force_cont:
# log warning and set format's required entropy to 0
log.warning("Forcing LUKS creation regardless of enough "
"random data entropy (%d/%d)",
get_current_entropy(), min_required_entropy)
self.device.format.min_luks_entropy = 0
self.device.setup()
self.device.format.create(device=self.device.path,
options=self.device.formatArgs)
# Get the UUID now that the format is created
udev.settle()
self.device.updateSysfsPath()
info = udev.get_device(self.device.sysfsPath)
# only do this if the format has a device known to udev
# (the format might not have a normal device at all)
if info:
if self.device.format.type != "btrfs":
self.device.format.uuid = udev.device_get_uuid(info)
self.device.deviceLinks = udev.device_get_symlinks(info)
elif self.device.format.type != "tmpfs":
# udev lookup failing is a serious issue for anything other than tmpfs
log.error("udev lookup failed for device: %s", self.device)
if callbacks and callbacks.create_format_post:
msg = _("Created %(type)s on %(device)s") % {"type": self.device.format.type, "device": self.device.path}
callbacks.create_format_post(CreateFormatPostData(msg))
def cancel(self):
if not self._applied:
return
self.device.format = self.origFormat
super(ActionCreateFormat, self).cancel()
def requires(self, action):
""" Return True if self requires action.
Format create action can require another action if:
- this action's device depends on the other action's device
and the other action is not a device destroy action or a
container action
- the other action is a create or resize of this action's
device
"""
return (super(ActionCreateFormat, self).requires(action) or
(self.device.dependsOn(action.device) and
not ((action.isDestroy and action.isDevice) or
action.isContainer)) or
(action.isDevice and (action.isCreate or action.isResize) and
self.device.id == action.device.id))
def obsoletes(self, action):
""" Return True if this action obsoletes action.
Format create actions obsolete the following actions:
- format actions w/ lower id on this action's device, other
than those that destroy existing formats
"""
return (self.device.id == action.device.id and
self.obj == action.obj and
not (action.isDestroy and action.format.exists) and
self.id > action.id)
class ActionDestroyFormat(DeviceAction):
""" An action representing the removal of an existing filesystem. """
type = ACTION_TYPE_DESTROY
obj = ACTION_OBJECT_FORMAT
typeDescStr = N_("destroy format")
def __init__(self, device):
if device.formatImmutable:
raise ValueError("this device's formatting cannot be modified")
DeviceAction.__init__(self, device)
self.origFormat = self.device.format
if not device.format.destroyable:
raise ValueError("resource to destroy this format type %s is unavailable" % device.format.type)
def apply(self):
if self._applied:
return
self.device.format = None
super(ActionDestroyFormat, self).apply()
def execute(self, callbacks=None):
""" wipe the filesystem signature from the device """
super(ActionDestroyFormat, self).execute(callbacks=callbacks)
status = self.device.status
self.device.setup(orig=True)
self.format.destroy()
udev.settle()
if not status:
self.device.teardown()
def cancel(self):
if not self._applied:
return
self.device.format = self.origFormat
super(ActionDestroyFormat, self).cancel()
@property
def format(self):
return self.origFormat
def requires(self, action):
""" Return True if self requires action.
Format destroy actions require other actions when:
- the other action's device depends on this action's device
and the other action is a destroy action
- the other action removes this action's device from a container
"""
retval = super(ActionDestroyFormat, self).requires(action)
if action.device.dependsOn(self.device) and action.isDestroy:
retval = True
elif (action.isRemove and action.device == self.device):
retval = True
return retval
def obsoletes(self, action):
""" Return True if this action obsoletes action.
Format destroy actions obsolete the following actions:
- non-destroy format actions w/ lower id on same device, including
self if format does not exist
- destroy format action w/ higher id on same device
- format destroy action on a non-existent format shouldn't
obsolete a format destroy action on an existing one
"""
retval = False
same_device = self.device.id == action.device.id
format_action = self.obj == action.obj
if same_device and format_action:
if action.isDestroy:
if self.format.exists and not action.format.exists:
retval = True
elif not self.format.exists and action.format.exists:
retval = False
elif self.id == action.id and not self.format.exists:
retval = True
else:
retval = self.id < action.id
else:
retval = self.id > action.id
return retval
class ActionResizeFormat(DeviceAction):
""" An action representing the resizing of an existing filesystem.
XXX Do we even want to support resizing of a filesystem without
also resizing the device it resides on?
"""
type = ACTION_TYPE_RESIZE
obj = ACTION_OBJECT_FORMAT
typeDescStr = N_("resize format")
def __init__(self, device, newsize):
if device.formatImmutable:
raise ValueError("this device's formatting cannot be modified")
if not device.format.resizable:
raise ValueError("format is not resizable")
if device.format.currentSize == newsize:
raise ValueError("new size same as old size")
DeviceAction.__init__(self, device)
if newsize > device.format.currentSize:
self.dir = RESIZE_GROW
else:
self.dir = RESIZE_SHRINK
if device.format.targetSize > Size(0):
self.origSize = device.format.targetSize
# no targetSize -- original size for device was its currentSize
else:
self.origSize = device.format.currentSize
self._targetSize = newsize
def apply(self):
if self._applied:
return
self.device.format.targetSize = self._targetSize
super(ActionResizeFormat, self).apply()
def execute(self, callbacks=None):
super(ActionResizeFormat, self).execute(callbacks=callbacks)
if callbacks and callbacks.resize_format_pre:
msg = _("Resizing filesystem on %(device)s") % {"device": self.device.path}
callbacks.resize_format_pre(ResizeFormatPreData(msg))
self.device.setup(orig=True)
self.device.format.doResize()
if callbacks and callbacks.resize_format_post:
msg = _("Resized filesystem on %(device)s") % {"device": self.device.path}
callbacks.resize_format_post(ResizeFormatPostData(msg))
def cancel(self):
if not self._applied:
return
self.device.format.targetSize = self.origSize
super(ActionResizeFormat, self).cancel()
def requires(self, action):
""" Return True if self requires action.
A format resize action requires another action if:
- the other action is a device resize on the same device and
both are grow operations
- the other action shrinks a device (or format it contains)
that depends on this action's device
- the other action grows a device (or format) that this
action's device depends on
- the other action removes this action's device from a container
"""
retval = super(ActionResizeFormat, self).requires(action)
if action.isResize:
if self.device.id == action.device.id and \
self.dir == action.dir and \
action.isDevice and self.isGrow:
retval = True
elif action.isShrink and action.device.dependsOn(self.device):
retval = True
elif action.isGrow and self.device.dependsOn(action.device):
retval = True
elif (action.isRemove and action.device == self.device):
retval = True
return retval
class ActionAddMember(DeviceAction):
""" An action representing addition of a member device to a container. """
type = ACTION_TYPE_ADD
obj = ACTION_OBJECT_CONTAINER
typeDescStr = N_("add container member")
def __init__(self, container, device):
super(ActionAddMember, self).__init__(device)
self.container = container
def apply(self):
if self._applied:
return
self.container.parents.append(self.device)
super(ActionAddMember, self).apply()
def cancel(self):
if not self._applied:
return
self.container.parents.remove(self.device)
super(ActionAddMember, self).cancel()
def execute(self, callbacks=None):
super(ActionAddMember, self).execute(callbacks=callbacks)
self.container.add(self.device)
def requires(self, action):
"""
requires
- create/resize the same device
required by
- any create/grow action on a device in the same container
"""
return ((action.isCreate or action.isResize) and
action.device == self.device)
def obsoletes(self, action):
"""
obsoletes
- remove same member from same container
- add same member to same container w/ higher id
obsoleted by
- destroy the container
- destroy the device
- remove same member from same container
"""
retval = False
if (action.isRemove and
action.device == self.device and
action.container == self.container):
retval = True
elif (action.isAdd and
action.device == self.device and
action.container == self.container and
action.id > self.id):
retval = True
return retval
class ActionRemoveMember(DeviceAction):
""" An action representing removal of a member device from a container. """
type = ACTION_TYPE_REMOVE
obj = ACTION_OBJECT_CONTAINER
typeDescStr = N_("remove container member")
def __init__(self, container, device):
super(ActionRemoveMember, self).__init__(device)
self.container = container
def apply(self):
if self._applied:
return
self.container.parents.remove(self.device)
super(ActionRemoveMember, self).apply()
def cancel(self):
if not self._applied:
return
self.container.parents.append(self.device)
super(ActionRemoveMember, self).cancel()
def execute(self, callbacks=None):
super(ActionRemoveMember, self).execute(callbacks=callbacks)
self.container.remove(self.device)
def requires(self, action):
"""
requires
- any destroy/shrink action on a device in the same container
- any add action on this container
required by
- any destroy/resize action on the device
"""
retval = False
if ((action.isShrink or action.isDestroy) and
action.device.container == self.container):
retval = True
elif action.isAdd and action.container == self.container:
retval = True
return retval
def obsoletes(self, action):
"""
obsoletes
- add same member to same container
- remove same member from same container w/ higher id
obsoleted by
- add same member to same container
"""
retval = False
if (action.isAdd and
action.device == self.device and
action.container == self.container):
retval = True
elif (action.isRemove and
action.device == self.device and
action.container == self.container and
action.id > self.id):
retval = True
return retval