Ejemplo n.º 1
0
    def create_image(self):
        """ """
        device = Device(self.device_path)
        disk = Disk(device)

        if device.is_disk():
            mbr = Mbr(self.target_path)
            mbr.save_to_file(self.device_path)
            dlm = DiskLayoutManager(self.target_path)
            dlm.save_to_file(disk)
        
        partition_list = disk.get_valid_partitions()
        
        total_bytes = 0
        for p in partition_list:
            total_bytes += p.filesystem.get_used_size()
       
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))
        self._print_informations(total_bytes)

        information = Information(self.target_path)
        information.set_image_is_disk(device.is_disk())
        information.set_image_name(self.image_name)
        information.set_image_total_bytes(total_bytes)
        information.set_image_compressor_level(self.compressor_level)

        compressor = Compressor(self.compressor_level)
        progress = Progress(total_blocks)
        progress.start()
        for p in partition_list:
            number = p.get_number()
            uuid = p.filesystem.uuid()
            type = p.filesystem.type
            information.add_partition(number, uuid, type)

            p.filesystem.open_to_read()

            file_name = FILE_PATTERN % (self.image_name, number)
            with open(self.target_path + file_name, 'wb') as f:
                # TODO: Work with parallelism
                while True: # Ugly
                    data = p.filesystem.read(BLOCK_SIZE)
                    if not len(data):
                        break
                    cdata = compressor.compact(data)
                    f.write(cdata)
                    progress.increment(1)
            p.filesystem.close()

        swap = disk.get_swap_partition()
        if swap is not None:
            number = swap.get_number()
            uuid = swap.filesystem.uuid()
            type = swap.filesystem.type
            information.add_partition(number, uuid, type)

        information.save()
        progress.stop()
        print "completed."
Ejemplo n.º 2
0
    def create_image(self):
        """ """
        device = Device(self.device_path)
        disk = Disk(device)

        if device.is_disk():
            mbr = Mbr(self.target_path)
            mbr.save_to_file(self.device_path)
            dlm = DiskLayoutManager(self.target_path)
            dlm.save_to_file(disk)
        
        partition_list = disk.get_valid_partitions(self.raw)

        # check partitions filesystem
        if not self.raw:
            for part in partition_list:
                if not part.filesystem.check():
                    raise ErrorCreatingImage("(%s) Filesystem is not clean" % part.path)       

        # get total size
        total_bytes = 0
        for part in partition_list:
            total_bytes += part.filesystem.get_used_size()
       
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))
        self._print_informations(total_bytes)

        information = Information(self.target_path)
        information.set_image_is_disk(device.is_disk())
        information.set_image_name(self.image_name)
        information.set_image_total_bytes(total_bytes)
        information.set_image_compressor_level(self.compressor_level)

        progress = Progress(total_blocks)
        progress.start()
        for part in partition_list:
            number = part.get_number()
            uuid = part.filesystem.uuid()
            type = part.filesystem.type

            part.filesystem.open_to_read()
            split_volume = 1
            while True:
                file_name = FILE_PATTERN % (self.image_name, number, split_volume)
                file_path = self.target_path + file_name

                writer = WriterFactory(file_path, self.compressor_level)
                writer.open()

                next_partition = False
                total_written = 0
                while True:
                    data = part.filesystem.read(BLOCK_SIZE)
                    if not len(data):
                        next_partition = True
                        break
                    bytes_written = writer.write(data)
                    total_written += bytes_written
                    progress.increment(1)
                    if self.split_size:
                        if (total_written + bytes_written) / 1024 / 1024 >= self.split_size:
                            break

                writer.close()
                if next_partition: break
                split_volume += 1
            part.filesystem.close()
            information.add_partition(number, uuid, type, split_volume)

        swap = disk.get_swap_partition()
        if swap is not None:
            number = swap.get_number()
            uuid = swap.filesystem.uuid()
            type = swap.filesystem.type
            information.add_partition(number, uuid, type, 0)

        information.save()
        progress.stop()
        print "completed."