From a5e501b3cbdf2fb69a8351f38cfd490f1343633f Mon Sep 17 00:00:00 2001 From: David Lehman Date: Thu, 17 Apr 2014 12:40:12 -0500 Subject: [PATCH 3/9] Change signature of DiskLabel.addPartition to be more useful. The new method accepts start sector, end sector, and partition type. The previous method required you to pass in a parted.Partition instance. It did not simply add that parted.Partition instance. Instead, it created a new parted.Partition instance based on the one passed in and added that to the parted.Disk. (cherry picked from commit 6a9c2ed8d1c32b1062f44b6ae99fb4e08083e8b7) --- blivet/devices.py | 13 ++++++++++--- blivet/formats/disklabel.py | 42 ++++++++++++++++++++---------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/blivet/devices.py b/blivet/devices.py index 8ffa553..c008924 100644 --- a/blivet/devices.py +++ b/blivet/devices.py @@ -1766,7 +1766,9 @@ class PartitionDevice(StorageDevice): self.partedPartition in self.disk.format.partitions: return - self.disk.format.addPartition(self.partedPartition) + self.disk.format.addPartition(self.partedPartition.geometry.start, + self.partedPartition.geometry.end, + self.partedPartition.type) # Look up the path by start sector to deal with automatic renumbering of # logical partitions on msdos disklabels. @@ -1823,7 +1825,9 @@ class PartitionDevice(StorageDevice): def _create(self): """ Create the device. """ log_method_call(self, self.name, status=self.status) - self.disk.format.addPartition(self.partedPartition) + self.disk.format.addPartition(self.partedPartition.geometry.start, + self.partedPartition.geometry.end, + self.partedPartition.type) self._wipe() try: @@ -1916,7 +1920,10 @@ class PartitionDevice(StorageDevice): try: self.disk.originalFormat.commit() except errors.DiskLabelCommitError: - self.disk.originalFormat.addPartition(self.partedPartition) + self.disk.originalFormat.addPartition( + self.partedPartition.geometry.start, + self.partedPartition.geometry.end, + self.partedPartition.type) self.partedPartition = self.disk.originalFormat.partedDisk.getPartitionByPath(self.path) raise diff --git a/blivet/formats/disklabel.py b/blivet/formats/disklabel.py index 71c9a25..c69aa04 100644 --- a/blivet/formats/disklabel.py +++ b/blivet/formats/disklabel.py @@ -285,28 +285,32 @@ class DiskLabel(DeviceFormat): else: self.updateOrigPartedDisk() - def addPartition(self, partition, constraint=None): + def addPartition(self, start, end, ptype=None): """ Add a partition to the disklabel. - :param partition: partition from which to get geom, type (required) - :type partition: parted.Partition. - :keyword constraint: constraint to use - :type constraint: parted.Constraint. + :param int start: start sector + :param int end: end sector + :param ptype: partition type or None + :type ptype: int (parted partition type constant) or NoneType - .. note:: - - This does not add the :class:`parted.Partition` instance you - pass in. The geometry and type are taken from that instance and - used to create a new :class:`parted.Partition` to add to the - disklabel. + Partition type will default to either PARTITION_NORMAL or + PARTITION_LOGICAL, depending on whether the start sector is within + an extended partition. """ - geometry = partition.geometry - if not constraint: - constraint = parted.Constraint(exactGeom=geometry) + if ptype is None: + extended = self.extendedPartition + if extended and extended.geometry.contains(start): + ptype = parted.PARTITION_LOGICAL + else: + ptype = parted.PARTITION_NORMAL + geometry = parted.Geometry(device=self.partedDevice, + start=start, end=end) new_partition = parted.Partition(disk=self.partedDisk, - type=partition.type, + type=ptype, geometry=geometry) + + constraint = parted.Constraint(exactGeom=geometry) self.partedDisk.addPartition(partition=new_partition, constraint=constraint) @@ -314,13 +318,7 @@ class DiskLabel(DeviceFormat): """ Remove a partition from the disklabel. :param partition: the partition to remove - :type partition: parted.Partition. - - .. note:: - - Unlike :meth:`addPartition`, the actual partition instance - passed is what will be removed from the disklabel. - + :type partition: :class:`parted.Partition` """ self.partedDisk.removePartition(partition) -- 1.9.3