[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.
 - Add a new keyword argument, initlabel, to the DiskDevice and
   DMRaidArrayDevice constructors.
 - Include Storage.protectedPartitions, Storage.reinitializeDisks,
   and Storage.clearPartDisks in DeviceTree's constructor argument
   list.
 - Pass the value of reinitializeDisks to the DiskDevice and
   DMRaidArrayDevice constructors unless the disk contains protected
   partitions or the disk is not in clearPartDisks.
---
 storage/__init__.py   |    6 ++++
 storage/devices.py    |   41 ++++++++++++++++++++++----------
 storage/devicetree.py |   62 ++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 83 insertions(+), 26 deletions(-)

diff --git a/storage/__init__.py b/storage/__init__.py
index f0ed7d6..b886dc0 100644
--- a/storage/__init__.py
+++ b/storage/__init__.py
@@ -172,6 +172,9 @@ class Storage(object):
         self.devicetree = DeviceTree(intf=self.anaconda.intf,
                                      ignored=self.ignoredDisks,
                                      exclusive=self.exclusiveDisks,
+                                     clear=self.clearPartDisks,
+                                     reinitializeDisks=self.reinitializeDisks,
+                                     protected=self.protectedPartitions,
                                      zeroMbr=self.zeroMbr,
                                      passphrase=self.encryptionPassphrase,
                                      luksDict=self.__luksDevs)
@@ -215,6 +218,9 @@ class Storage(object):
         self.devicetree = DeviceTree(intf=self.anaconda.intf,
                                      ignored=self.ignoredDisks,
                                      exclusive=self.exclusiveDisks,
+                                     clear=self.clearPartDisks,
+                                     reinitializeDisks=self.reinitializeDisks,
+                                     protected=self.protectedPartitions,
                                      zeroMbr=self.zeroMbr,
                                      passphrase=self.encryptionPassphrase,
                                      luksDict=self.__luksDevs)
diff --git a/storage/devices.py b/storage/devices.py
index 39578c0..d236d15 100644
--- a/storage/devices.py
+++ b/storage/devices.py
@@ -658,7 +658,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:
@@ -676,6 +676,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.
@@ -686,20 +687,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
@@ -709,6 +714,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 """
@@ -2143,8 +2153,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:
@@ -2159,6 +2170,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:
@@ -2166,7 +2180,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 df5a6ed..66d053e 100644
--- a/storage/devicetree.py
+++ b/storage/devicetree.py
@@ -22,6 +22,7 @@
 
 import os
 import block
+import re
 
 from errors import *
 from devices import *
@@ -155,15 +156,19 @@ class DeviceTree(object):
         except for resize actions.
     """
 
-    def __init__(self, intf=None, ignored=[], exclusive=[],
-                 zeroMbr=None, passphrase=None, luksDict=None):
+    def __init__(self, intf=None, ignored=[], exclusive=[], clear=[],
+                 zeroMbr=None, reinitializeDisks=None, protected=[],
+                 passphrase=None, luksDict=None):
         # internal data members
         self._devices = []
         self._actions = []
 
         self.intf = intf
         self.exclusiveDisks = exclusive
+        self.clearPartDisks = clear
         self.zeroMbr = zeroMbr
+        self.reinitializeDisks = reinitializeDisks
+        self.protectedPartitions = protected
         self.__passphrase = passphrase
         self.__luksDevs = {}
         if luksDict and isinstance(luksDict, dict):
@@ -985,11 +990,25 @@ 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
+                    if not self.clearPartDisks or name in self.clearPartDisks:
+                        initlabel = self.reinitializeDisks
+
+                        for protected in self.protectedPartitions:
+                            _p = "/sys/%s/%s" % (sysfs_path, protected)
+                            if os.path.exists(os.path.normpath(_p)):
+                                initlabel = False
+                                break
+                    else:
+                        initlabel = False
+
                     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.addIgnoredDisk(name)
@@ -1162,18 +1181,35 @@ class DeviceTree(object):
                         rs.activate(mknod=True)
 
                         # Create the DMRaidArray
+                        if self.zeroMbr:
+                            cb = lambda: True
+                        else:
+                            cb = lambda: questionInitializeDisk(self.intf,
+                                                                rs.name)
+
+                        if not self.clearPartDisks or \
+                           rs.name in self.clearPartDisks:
+                            # if the disk contains protected partitions
+                            # we will not wipe the disklabel even if
+                            # clearpart --initlabel was specified
+                            initlabel = self.reinitializeDisks
+                            for protected in self.protectedPartitions:
+                                disk_name = re.sub(r'p\d+$', protected)
+                                if disk_name != protected and \
+                                   disk_name == rs.name:
+                                    initlabel = False
+                                    break
+                        else:
+                            initlabel = False
+
                         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)
+                                                    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