- try:
- image_disk = parted.Disk(image_dev)
- try:
- is_extended = lambda p: p.type == parted.PARTITION_EXTENDED
- is_logical = lambda p: p.type == parted.PARTITION_LOGICAL
-
- partitions = []
- for p in src_disk.partitions:
- g = p.geometry
- f = p.fileSystem
- partitions.append(self._Partition(p.number, g.start, g.end,
- p.type, f.type if f is not None else '',
- mount_options(p.path)))
-
- last = partitions[-1]
- new_end = src_disk.device.getLength()
- if last.fs == 'linux-swap(v1)':
- MB = 2 ** 20
- size = (last.end - last.start + 1) * \
- src_disk.device.sectorSize
- meta['SWAP'] = "%d:%s" % (last.num, (size + MB - 1) // MB)
-
- img_disk.deletePartition(
- image_disk.getPartitionBySector(last.start))
- img_disk.commit()
-
- if is_logical(last) and last.num == 5:
- extended = image_disk.getExtendedPartition()
- image_disk.deletePartition(extended)
- image_disk.commit()
- partitions.remove(filter(is_extended, partitions)[0])
-
- partitions.remove(last)
- last = partitions[-1]
-
- # Leave 2048 blocks at the end
- new_end = last.end + 2048
-
- if last.mpoint:
- stat = os.statvfs(last.mpoint)
- # Shrink the last partition. The new size should be the
- # size of the occupied blocks
- blcks = stat.f_blocks - stat.f_bavail
- new_size = (blcks * stat.f_frsize) // src_dev.sectorSize
-
- # Add 10% just to be on the safe side
- part_end = last.start + (new_size * 11) // 10
- # Alighn to 2048
- part_end = ((part_end + 2047) // 2048) * 2048
- last = last._replace(end=part_end)
- partitions[-1] = last
-
- # Leave 2048 blocks at the end.
- new_end = new_size + 2048
-
- image_disk.setPartitionGeometry(
- image_disk.getPartitionBySector(last.start),
- parted.Constraint(device=image_disk.device),
- start=last.start, end=last.end)
- image_disk.commit()
-
- if last.type == parted.PARTITION_LOGICAL:
- # Fix the extended partition
- extended = disk.getExtendedPartition()
-
- image_disk.setPartitionGeometry(extended,
- parted.Constraint(device=img_dev),
- ext.geometry.start, end=last.end)
- image_disk.commit()
- finally:
- image_disk.destroy()
- finally:
- image_dev.destroy()
+ image_disk = parted.Disk(image_dev)
+
+ is_extended = lambda p: p.type == parted.PARTITION_EXTENDED
+ is_logical = lambda p: p.type == parted.PARTITION_LOGICAL
+
+ partitions = []
+ for p in src_disk.partitions:
+ g = p.geometry
+ f = p.fileSystem
+ partitions.append(self._Partition(p.number, g.start, g.end,
+ p.type, f.type if f is not None else '',
+ self._mount_options(p.path)))
+
+ last = partitions[-1]
+ new_end = src_disk.device.getLength()
+ if last.fs == 'linux-swap(v1)':
+ MB = 2 ** 20
+ size = (last.end - last.start + 1) * src_disk.device.sectorSize
+ self.meta['SWAP'] = "%d:%s" % (last.num, (size + MB - 1) // MB)
+
+ image_disk.deletePartition(
+ image_disk.getPartitionBySector(last.start))
+ image_disk.commit()
+
+ if is_logical(last) and last.num == 5:
+ extended = image_disk.getExtendedPartition()
+ image_disk.deletePartition(extended)
+ image_disk.commit()
+ partitions.remove(filter(is_extended, partitions)[0])
+
+ partitions.remove(last)
+ last = partitions[-1]
+
+ # Leave 2048 blocks at the end
+ new_end = last.end + 2048
+
+ if last.mopts.mpoint:
+ stat = os.statvfs(last.mopts.mpoint)
+ # Shrink the last partition. The new size should be the
+ # size of the occupied blocks
+ blcks = stat.f_blocks - stat.f_bavail
+ new_size = (blcks * stat.f_frsize) // src_disk.device.sectorSize
+
+ # Add 10% just to be on the safe side
+ part_end = last.start + (new_size * 11) // 10
+ # Alighn to 2048
+ part_end = ((part_end + 2047) // 2048) * 2048
+ last = last._replace(end=part_end)
+ partitions[-1] = last
+
+ # Leave 2048 blocks at the end.
+ new_end = new_size + 2048
+
+ image_disk.setPartitionGeometry(
+ image_disk.getPartitionBySector(last.start),
+ parted.Constraint(device=image_disk.device),
+ start=last.start, end=last.end)
+ image_disk.commit()
+
+ if last.type == parted.PARTITION_LOGICAL:
+ # Fix the extended partition
+ extended = disk.getExtendedPartition()
+
+ image_disk.setPartitionGeometry(extended,
+ parted.Constraint(device=img_dev),
+ ext.geometry.start, end=last.end)
+ image_disk.commit()