[PATCH] Add support for kickstart's '--initlabel' option to clearpart.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Move disklabel initialization into a method of DiskDevice and
pass in the value of Storage.reinitializeDisks to the DiskDevice
constructor unless the disk contains protected partitions.
---
 storage/__init__.py   |    2 +
 storage/devices.py    |   41 +++++++++++++++++++++++++-----------
 storage/devicetree.py |   55 +++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/storage/__init__.py b/storage/__init__.py
index f0ed7d6..a52cba1 100644
--- a/storage/__init__.py
+++ b/storage/__init__.py
@@ -172,6 +172,7 @@ class Storage(object):
         self.devicetree = DeviceTree(intf=self.anaconda.intf,
                                      ignored=self.ignoredDisks,
                                      exclusive=self.exclusiveDisks,
+                                     protected=self.protectedPartitions,
                                      zeroMbr=self.zeroMbr,
                                      passphrase=self.encryptionPassphrase,
                                      luksDict=self.__luksDevs)
@@ -216,6 +217,7 @@ class Storage(object):
                                      ignored=self.ignoredDisks,
                                      exclusive=self.exclusiveDisks,
                                      zeroMbr=self.zeroMbr,
+                                     protected=self.protectedPartitions,
                                      passphrase=self.encryptionPassphrase,
                                      luksDict=self.__luksDevs)
         self.devicetree.populate()
diff --git a/storage/devices.py b/storage/devices.py
index ae1ad09..aea00ea 100644
--- a/storage/devices.py
+++ b/storage/devices.py
@@ -639,7 +639,7 @@ class DiskDevice(StorageDevice):
 
     def __init__(self, device, format=None,
                  size=None, major=None, minor=None, sysfsPath='', \
-                 parents=None, initcb=None):
+                 parents=None, initcb=None, initlabel=None):
         """ Create a DiskDevice instance.
 
             Arguments:
@@ -657,6 +657,7 @@ class DiskDevice(StorageDevice):
                 removable -- whether or not this is a removable device
 
                 initcb -- the call back to be used when initiating disk.
+                initlabel -- whether to start with a fresh disklabel
 
 
             DiskDevices always exist.
@@ -667,20 +668,24 @@ class DiskDevice(StorageDevice):
 
         self.partedDevice = None
         self.partedDisk = None
+
         log.debug("looking up parted Device: %s" % self.path)
         self.partedDevice = parted.Device(path=self.path)
         if not self.partedDevice:
             raise DeviceError("cannot find parted device instance")
+
         log.debug("creating parted Disk: %s" % self.path)
-        try:
-            self.partedDisk = parted.Disk(device=self.partedDevice)
-        except _ped.DiskLabelException:
-            # if we have a cb function use it. else an error.
-            if initcb is not None and initcb():
-                self.partedDisk = parted.freshDisk(device=self.partedDevice, \
-                        ty = platform.getPlatform(None).diskType)
-            else:
-                raise DeviceUserDeniedFormatError("User prefered to not format.")
+        if initlabel:
+            self.partedDisk = self.freshPartedDisk()
+        else:
+            try:
+                self.partedDisk = parted.Disk(device=self.partedDevice)
+            except _ped.DiskLabelException:
+                # if we have a cb function use it. else an error.
+                if initcb is not None and initcb():
+                    self.partedDisk = self.freshPartedDisk()
+                else:
+                    raise DeviceUserDeniedFormatError("User prefered to not format.")
 
         # We save the actual state of the disk here. Before the first
         # modification (addPartition or removePartition) to the partition
@@ -690,6 +695,11 @@ class DiskDevice(StorageDevice):
 
         self.probe()
 
+    def freshPartedDisk(self):
+        log_method_call(self, self.name)
+        labelType = platform.getPlatform(None).diskType
+        return parted.freshDisk(device=self.partedDevice, ty=labelType)
+
     @property
     def size(self):
         """ The disk's size in MB """
@@ -2123,8 +2133,9 @@ class DMRaidArrayDevice(DiskDevice):
     _packages = ["dmraid"]
     devDir = "/dev/mapper"
 
-    def __init__(self, name, raidSet=None, level=None, format=None, size=None,
-                 major=None, minor=None, parents=None, sysfsPath='', initcb=None):
+    def __init__(self, name, raidSet=None, level=None, format=None,
+                 size=None, major=None, minor=None, parents=None,
+                 sysfsPath='', initcb=None, initlabel=None):
         """ Create a DMRaidArrayDevice instance.
 
             Arguments:
@@ -2139,6 +2150,9 @@ class DMRaidArrayDevice(DiskDevice):
                 sysfsPath -- sysfs device path
                 size -- the device's size
                 format -- a DeviceFormat instance
+
+                initcb -- the call back to be used when initiating disk.
+                initlabel -- whether to start with a fresh disklabel
         """
         if isinstance(parents, list):
             for parent in parents:
@@ -2146,7 +2160,8 @@ class DMRaidArrayDevice(DiskDevice):
                     raise ValueError("parent devices must contain dmraidmember format")
         DiskDevice.__init__(self, name, format=format, size=size,
                             major=major, minor=minor, parents=parents,
-                            sysfsPath=sysfsPath, initcb=initcb)
+                            sysfsPath=sysfsPath, initcb=initcb,
+                            initlabel=initlabel)
 
         self.formatClass = get_device_format_class("dmraidmember")
         if not self.formatClass:
diff --git a/storage/devicetree.py b/storage/devicetree.py
index a2e4f15..08367db 100644
--- a/storage/devicetree.py
+++ b/storage/devicetree.py
@@ -156,7 +156,8 @@ class DeviceTree(object):
     """
 
     def __init__(self, intf=None, ignored=[], exclusive=[],
-                 zeroMbr=None, passphrase=None, luksDict=None):
+                 zeroMbr=None, reinitializeDisks=None, protected=[],
+                 passphrase=None, luksDict=None):
         # internal data members
         self._devices = []
         self._actions = []
@@ -165,6 +166,8 @@ class DeviceTree(object):
         self.ignoredDisks = ignored
         self.exclusiveDisks = exclusive
         self.zeroMbr = zeroMbr
+        self.reinitializeDisks = reinitializeDisks
+        self.protectedPartitions = protected
         self.__passphrase = passphrase
         self.__luksDevs = {}
         if luksDict and isinstance(luksDict, dict):
@@ -977,11 +980,21 @@ class DeviceTree(object):
                     else:
                         cb = lambda: questionInitializeDisk(self.intf, name)
 
+                    # if the disk contains protected partitions we will
+                    # not wipe the disklabel even if clearpart --initlabel
+                    # was specified
+                    initlabel = self.reinitalizeDisks
+                    for protected in self.protectedPartitions:
+                        _p = "/sys/%s/%s" % (sysfs_path, protected)
+                        if os.path.exists(os.path.normpath(_p)):
+                            initlabel = False
+                            break
+
                     device = DiskDevice(name,
                                     major=udev_device_get_major(info),
                                     minor=udev_device_get_minor(info),
                                     sysfsPath=sysfs_path,
-                                    initcb=cb)
+                                    initcb=cb, initlabel=initlabel)
                     self._addDevice(device)
                 except DeviceUserDeniedFormatError: #drive not initialized?
                     self.ignoredDisks.append(name)
@@ -1152,18 +1165,34 @@ class DeviceTree(object):
                         rs.activate(mknod=True)
 
                         # Create the DMRaidArray
+                        if self.zeroMbr:
+                            cb = lambda: True
+                        else:
+                            cb = lambda: questionInitializeDisk(self.intf,
+                                                                rs.name)
+
+                        # we'll need the array's sysfs path below
+                        dm_node = dm.dm_node_from_name(rs.name)
+                        _sysfs_path = "/class/block/%s" % dm_node
+
+                        # if the disk contains protected partitions we will
+                        # not wipe the disklabel even if clearpart --initlabel
+                        # was specified
+                        initlabel = self.reinitalizeDisks
+                        for protected in self.protectedPartitions:
+                            _p = "/sys/%s/%s" % (_sysfs_path, protected)
+                            if os.path.exists(os.path.normpath(_p)):
+                                initlabel = False
+                                break
+
                         try:
-                            if self.zeroMbr:
-                                cb = lambda: True
-                            else:
-                                cb = lambda: questionInitializeDisk(self.intf,
-                                                                    rs.name)
-                            dm_array = DMRaidArrayDevice(rs.name,
-                                                         major=major, minor=minor,
-                                                         raidSet=rs,
-                                                         level=rs.level,
-                                                         parents=[device],
-                                                         initcb=cb)
+                             dm_array = DMRaidArrayDevice(rs.name,
+                                             major=major, minor=minor,
+                                             raidSet=rs,
+                                             level=rs.level,
+                                             parents=[device],
+                                             initcb=cb,
+                                             initlabel=initlabel)
 
                             self._addDevice(dm_array)
                             # Use the rs's object on the device.
-- 
1.6.0.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux