[PATCH] Wipe partitions before they are created (#950145)

Brian C. Lane bcl at redhat.com
Tue May 14 23:33:29 UTC 2013


From: "Brian C. Lane" <bcl at redhat.com>

If a partition is created in the same position as a previous filesystem,
eg. a LVM pv, udev will activate it and cause the device to be busy,
preventing us from using it.

This uses the parted information from parted to calculate the start of
the partition and calls dd to write zeros to the first 10MiB of the
partition.

This also rescans the pv to make sure things stay in sync with the
filesystem when creating and removing pvs.
---
 blivet/devicelibs/lvm.py | 10 ++++++++++
 blivet/devices.py        | 21 +++++++++++++++++++++
 blivet/formats/lvmpv.py  | 12 ++++++++----
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/blivet/devicelibs/lvm.py b/blivet/devicelibs/lvm.py
index d16b659..eaafad1 100644
--- a/blivet/devicelibs/lvm.py
+++ b/blivet/devicelibs/lvm.py
@@ -192,6 +192,16 @@ def pvremove(device):
     except LVMError as msg:
         raise LVMError("pvremove failed for %s: %s" % (device, msg))
 
+def pvscan(device):
+    args = ["pvscan", "--cache",] + \
+            _getConfigArgs() + \
+            [device]
+
+    try:
+        lvm(args)
+    except LVMError as msg:
+        raise LVMError("pvscan failed for %s: %s" % (device, msg))
+
 def pvinfo(device):
     """
         If the PV was created with '--metadacopies 0', lvm will do some
diff --git a/blivet/devices.py b/blivet/devices.py
index ef5db99..9bd0d79 100644
--- a/blivet/devices.py
+++ b/blivet/devices.py
@@ -1461,12 +1461,33 @@ class PartitionDevice(StorageDevice):
 
         self._bootable = self.getFlag(parted.PARTITION_BOOT)
 
+    def _wipe(self):
+        """ Wipe the partition. """
+        log_method_call(self, self.name, status=self.status)
+
+        start = self.partedPartition.geometry.start
+        part_len = self.partedPartition.geometry.end - start
+        bs = self.partedPartition.geometry.device.sectorSize
+        device = self.partedPartition.geometry.device.path
+
+        # Erase 10M or to end of partition
+        count = 10 * 1024 * 1024 / bs
+        count = min(count, part_len)
+
+        cmd = ["dd", "if=/dev/zero", "of=%s" % device, "bs=%s" % bs,
+               "seek=%s" % start, "count=%s" % count]
+        try:
+            util.run_program(cmd)
+        except OSError as e:
+            log.error(str(e))
+
     def _create(self):
         """ Create the device. """
         log_method_call(self, self.name, status=self.status)
         self.disk.format.addPartition(self.partedPartition)
 
         try:
+            self._wipe()
             self.disk.format.commit()
         except DiskLabelCommitError:
             part = self.disk.format.partedDisk.getPartitionByPath(self.path)
diff --git a/blivet/formats/lvmpv.py b/blivet/formats/lvmpv.py
index e610f66..300b849 100644
--- a/blivet/formats/lvmpv.py
+++ b/blivet/formats/lvmpv.py
@@ -109,13 +109,15 @@ class LVMPhysicalVolume(DeviceFormat):
             # lvm has issues with persistence of metadata, so here comes the
             # hammer...
             DeviceFormat.destroy(self, *args, **kwargs)
-
+            lvm.pvscan(self.device)
             lvm.pvcreate(self.device)
         except Exception:
             raise
-        else:
-            self.exists = True
-            self.notifyKernel()
+        finally:
+            lvm.pvscan(self.device)
+
+        self.exists = True
+        self.notifyKernel()
 
     def destroy(self, *args, **kwargs):
         """ Destroy the format. """
@@ -132,6 +134,8 @@ class LVMPhysicalVolume(DeviceFormat):
             lvm.pvremove(self.device)
         except LVMError:
             DeviceFormat.destroy(self, *args, **kwargs)
+        finally:
+            lvm.pvscan(self.device)
 
         self.exists = False
         self.notifyKernel()
-- 
1.8.1.4



More information about the anaconda-patches mailing list