SECTOR_SIZE = 512
class PartitionedMount(Mount):
- def __init__(self, disks, mountdir, skipformat = False):
+ def __init__(self, mountdir, skipformat = False):
Mount.__init__(self, mountdir)
self.disks = {}
- for name in disks.keys():
- self.disks[name] = { 'disk': disks[name], # Disk object
- 'mapped': False, # True if kpartx mapping exists
- 'numpart': 0, # Number of allocate partitions
- 'partitions': [], # indexes to self.partitions
- # Partitions with part num higher than 3 will
- # be put inside extended partition.
- 'extended': 0, # Size of extended partition
- # Offset of next partition (in sectors)
- 'offset': 0 }
-
self.partitions = []
self.subvolumes = []
self.mapped = False
# Size of a sector used in calculations
self.sector_size = SECTOR_SIZE
+ def add_disks(self, disks):
+ """ Add the disks which have to be partitioned. """
+
+ for name in disks.keys():
+ self.disks[name] = { 'disk': disks[name], # Disk object
+ 'mapped': False, # True if kpartx mapping exists
+ 'numpart': 0, # Number of allocate partitions
+ 'partitions': [], # indexes to self.partitions
+ # Partitions with part num higher than 3 will
+ # be put inside extended partition.
+ 'extended': 0, # Size of extended partition
+ # Offset of next partition (in sectors)
+ 'offset': 0 }
+
+
def add_partition(self, size, disk, mountpoint, fstype = None, label=None, fsopts = None, boot = False, align = None):
# Converting MB to sectors for parted
size = size * 1024 * 1024 / self.sector_size
def cleanup(self):
Mount.cleanup(self)
- self.__unmap_partitions()
- for dev in self.disks.keys():
- d = self.disks[dev]
- try:
- d['disk'].cleanup()
- except:
- pass
+ if self.disks:
+ self.__unmap_partitions()
+ for dev in self.disks.keys():
+ d = self.disks[dev]
+ try:
+ d['disk'].cleanup()
+ except:
+ pass
def unmount(self):
self.__unmount_subvolumes()
imgsize = misc.get_file_size(img) * 1024L * 1024L
imgmnt = misc.mkdtemp()
disk = fs_related.SparseLoopbackDisk(img, imgsize)
- imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
+ imgloop = PartitionedMount(imgmnt, skipformat = True)
+ imgloop.add_disks({'/dev/sdb':disk})
imgloop.add_partition(imgsize/1024/1024, "/dev/sdb", "/", "vfat", boot=False)
try:
imgloop.mount()
partedcmd = fs_related.find_binary_path("parted")
disk = fs_related.SparseLoopbackDisk(img, imgsize)
imgmnt = misc.mkdtemp()
- imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
+ imgloop = PartitionedMount(imgmnt, skipformat = True)
+ imgloop.add_disks({'/dev/sdb':disk})
img_fstype = "ext3"
msger.info("Partition Table:")
srcimgsize = (misc.get_file_size(srcimg)) * 1024L * 1024L
srcmnt = misc.mkdtemp("srcmnt")
disk = fs_related.SparseLoopbackDisk(srcimg, srcimgsize)
- srcloop = PartitionedMount({'/dev/sdb':disk}, srcmnt, skipformat = True)
+ srcloop = PartitionedMount(srcmnt, skipformat = True)
+ srcloop.add_disks({'/dev/sdb':disk})
srcloop.add_partition(srcimgsize/1024/1024, "/dev/sdb", "/", "ext3", boot=False)
try:
srcloop.mount()