示例#1
0
    def restore_image(self):
        """ """
        invalid_partitions = False

        if is_mounted(self.target_device):
            log.error("The partition {0} is mounted, please umount first, and try again".format(self.target_device))
            self.notify_status("mounted_partition_error",{"mounted_partition_error":self.target_device})
            raise DeviceIsMounted("Please umount first")

        self.active = True
        information = Information(self.image_path)
        information.load()
        if self.partitions:
            information.set_partitions(self.partitions)
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        partitions = information.get_partitions()

        # Get total size
        total_bytes = 0
        for part in partitions:
            total_bytes += part.size

        self.total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            log.error("Invalid target device %s" % device.path)
            self.notify_status("write_error", \
                               {"write_error":device_path})
            raise ErrorRestoringImage("Invalid target device")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                log.error("Unrecognized disk label")
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            if ("msdos" not in disk.getPedDisk().type.name):
                #se a tabela nao for msdos, recria ela como msdos para nao haver problemas
                d = disk_new_fresh(device.getPedDevice(), _ped.disk_type_get("msdos"))
                d.commit_to_dev()
                disk = Disk(device)

            #Get total disk target size
            disk_size = get_disk_size(self.target_device)
            if (total_bytes > disk_size):
                log.info("Total size of image is {0}".format(total_bytes))
                log.info("Total size of {0} is {1}".format(self.target_device,disk_size))
                log.error("The size of {0} is {1}, is not enough to apply the selected image".format(self.target_device, disk_size))
                disk_space_info = []
                disk_space_info.append(total_bytes)
                disk_space_info.append(disk_size)
                self.notify_status("no_enough_space", {"disk_minimum_size":disk_space_info})
                raise ErrorRestoringImage("No enough space on disk")

            log.info("Restoring MBR and Disk Layout")
            mbr = Mbr(self.image_path)
            try:
                mbr.restore_from_file(self.target_device)
            except Exception as e:
                log.error("Error to restore the Mbr file")
                image_path = self.image_path.split("/")[3] + "/mbr.bin"
                self.notify_status("file_not_found",{"file_not_found":image_path})
                raise ErrorFileNotFound("File not Found {0}".format(image_path))

            dlm = DiskLayoutManager(self.image_path)
            #try:
            if self.expand != 2:
                dlm.restore_from_file(disk, True)
            else:
                dlm.restore_from_file(disk, False)
            #except Exception as e:
            #        log.error("Error to restore the disk.dl file")
            #        image_path = self.image_path.split("/")[3] + "/disk.dl"
            #        self.notify_status("file_not_found",{"file_not_found":image_path})
            #        raise ErrorFileNotFound("File not found {0}".format(image_path))

        else:
            parent_path = get_parent_path(self.target_device)
            parent_device = Device(parent_path)
            parent_disk = Disk(parent_device)
            partition = parent_disk.get_partition_by_path(
                                        self.target_device,
                                        part.type)
            part_size = partition.getSize('b')
            if (total_bytes > part_size):
                 part_space_info = []
                 part_space_info.append(total_bytes)
                 part_space_info.append(part_size)
                 log.error("The partition selected is smaller than the image")
                 self.notify_status("no_enough_space_part", {"disk_minimum_size":part_space_info})
                 raise ErrorRestoringImage("No enought space on partition")

        self.timer.start()

        total_partitions = len(partitions)
        for part in partitions:
            total_partitions -= 1

            if not self.active: break

            if (self.expand == 3) and (total_partitions == 0): break

            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(part.number,
                                                         part.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                                            self.target_device,
                                            part.type)

            if partition is None:
                invalid_partitions = True
                continue

            log.info("Restoring partition {0}".format(partition.get_path()))
            self.notify_status("restore_partition",\
                               {"restoring_partition":partition.get_path()})

            invalid_partitions = False

            if hasattr(part, "uuid"):
                partition.filesystem.open_to_write(part.uuid)
            else:
                partition.filesystem.open_to_write()

            if hasattr(part, "label"):
                partition.filesystem.write_label(part.label)

            if partition.filesystem.is_swap():
                continue

            pattern = FILE_PATTERN.format(name=image_name,
                                          partition=part.number,
                                          volume="{volume}")
            volumes = 1
            if hasattr(part, "volumes"):
                volumes = part.volumes
            try:
                image_reader = ImageReaderFactory(self.image_path, pattern,
                                                  volumes, compressor_level,
                                                  self.notify_status)
            except Exception as e:
                log.info(e)

            extract_callback = None
            if compressor_level:
                compressor = Compressor(compressor_level)
                extract_callback = compressor.extract

            self.buffer_manager = BufferManagerFactory(image_reader.read_block,
                                                       self.notify_status,
                                                       extract_callback)

            # open the file after instantiating BufferManager, cause of a
            # problem on multiprocessing module, FD_CLOEXE doesn't work
            # (I don't want to dup the file descriptor).
            image_reader.open()
            self.buffer_manager.start()

            buffer = self.buffer_manager.output_buffer
            while self.active:
                try:
                    data = buffer.get()
                except IOError, e:
                    if e.errno == errno.EINTR:
                        self.notify_status("read_buffer_error",{"read_buffer_error":str(e)})
                        data = ""
                        self.cancel()
                        raise ErrorReadingFromDevice(e)
                        break

                if data == EOF:
                    break

                try:
                    partition.filesystem.write_block(data)
                except ErrorWritingToDevice, e:
                    self.notify_status("write_partition_error")
                    if not self.canceled:
                        self.stop()
                        raise e

                self.processed_blocks += 1
示例#2
0
class Cli:
    def __init__(self):
        self.parser = optparse.OptionParser(usage=self.usage(),
                                            version=get_version())
        self.lock = Lock()
        self._set_options()
        self._register_signals()
        self.operation = None

    def _register_signals(self):
        signal.signal(signal.SIGINT, self.cancel_operation)
        signal.signal(signal.SIGUSR1, self.cancel_operation)

    def cancel_operation(self, signal=None, frame=None):
        if self.operation is not None:
            self.operation.cancel()

    def usage(self):
        usage = "usage: %prog [options]\n"
        usage += "\nCreating: \n"
        usage += "\t%prog -s /dev/sda -o /media/external_hd\n"
        usage += "\t%prog -s /dev/sda1 -o /media/external_hd -r -z -c 0\n"
        usage += "\t%prog -s /dev/sda -o /media/external_hd -p 4G -m\n"
        usage += "\nRestoring: \n"
        usage += "\t%prog -t /dev/sda -i /media/external_hd\n"
        usage += "\t%prog -t /dev/sda -i /media/external_hd -x 1,3 -e\n"
        return usage

    def _set_options(self):
        """ """
        create_group = optparse.OptionGroup(
            self.parser,
            "Creating Image Options",
        )
        restore_group = optparse.OptionGroup(
            self.parser,
            "Restoring Image Options",
        )
        information_group = optparse.OptionGroup(
            self.parser,
            "Image Information Options",
        )

        create_group.add_option(
            "-s",
            "--source-device",
            dest="source_device",
        )
        create_group.add_option(
            "-o",
            "--output-folder",
            dest="output_folder",
        )
        create_group.add_option(
            "-n",
            "--image-name",
            dest="image_name",
            default="image",
            help="[default: %default]",
        )
        create_group.add_option(
            "-c",
            "--compressor-level",
            dest="compressor_level",
            type="int",
            default=6,
            help="An integer from 0 to 9 controlling "
            "the level of compression; 0 for no "
            "compression, 1 is fastest and produces "
            "the least compression, 9 is slowest and "
            "produces the most. "
            "[default: %default]",
        )
        create_group.add_option(
            "-r",
            "--raw",
            dest="raw",
            action="store_true",
            default=False,
            help="Create raw images.",
        )
        create_group.add_option(
            "-m",
            "--iso",
            dest="iso",
            action="store_true",
            default=False,
            help="Create iso(s) from file(s).",
        )
        create_group.add_option(
            "-z",
            "--fill-with-zeros",
            dest="fill_with_zeros",
            action="store_true",
            default=False,
            help="Fill the filesystem with zeros. "
            "Because Raw image copies every block of "
            "the filesystem this will aid compression "
            "of the image (If compression is enabled).",
        )
        create_group.add_option(
            "-p",
            "--split",
            dest="split_size",
            type="string",
            help="Split the image file into smaller "
            "chunks of required size.",
        )
        restore_group.add_option(
            "-t",
            "--target-device",
            dest="target_device",
        )
        restore_group.add_option(
            "-i",
            "--image-folder",
            dest="image_folder",
        )
        restore_group.add_option(
            "-x",
            "--image-partitions",
            dest="partition_numbers",
            help="Restore only the given "
            "partition number(s) (Comma separated). "
            "ONLY FOR DISK RECOVERIES. "
            "[Ex: 1,2,3]",
        )
        restore_group.add_option(
            "-e",
            "--expand",
            dest="expand",
            action="store_true",
            default=False,
            help="If possible resize the last partition "
            "until it fills the whole disk. "
            "ONLY FOR DISK RECOVERIES.",
        )
        information_group.add_option(
            "-q",
            "--image-information",
            dest="image_folder",
            help="Show the information about the image.",
        )

        self.parser.add_option_group(create_group)
        self.parser.add_option_group(restore_group)
        self.parser.add_option_group(information_group)

    def status(self, action, dict={}):
        """ """
        self.lock.acquire()
        response = None
        if action == "progress":
            sys.stdout.write("%d%%\r" % dict["percent"])

        elif action == "finish":
            sys.stdout.write("Finished.\r\n")

        elif action == "checking_filesystem":
            sys.stdout.write("Checking filesystem of %s...\n" % dict["device"])

        elif action == "expand":
            sys.stdout.write("Expanding filesystem of %s...\n" %
                             dict["device"])

        elif action == "filling_with_zeros":
            sys.stdout.write("Zeroing filesystem of %s...\n" % dict["device"])

        elif action == "iso":
            sys.stdout.write("Creating ISO %d of %d...\n" %
                             (dict["volume"], dict["total"]))

        elif action == "canceled":
            sys.stdout.write("\r%s operation canceled!\n" % dict["operation"])

        elif action == "base_files_not_found":
            sys.stdout.write("\nCarbono files cannt be found in %s.\n" \
                             % dict["device"])
            response = raw_input("Please type another device " + \
                                 "(or leave blank to cancel): ")

        elif action == "file_not_found":
            sys.stdout.write("\nThe file %s cannot be found at %s.\n" \
                             % (dict["file"], dict["path"]))
            response = raw_input("Type the file location " + \
                                 "(or leave blank to cancel): ")

        sys.stdout.flush()
        self.lock.release()
        return response

    def run(self):
        """ """
        opt, remainder = self.parser.parse_args()
        if opt.source_device is not None:
            if opt.output_folder is None:
                self.parser.print_help()
                sys.exit(1)

            if not check_if_root():
                sys.stderr.write("You need to run this option as root.\n")
                sys.exit(1)

            split_size = 0
            if opt.split_size:
                l = opt.split_size[-1]
                if l.isdigit():
                    split_size = float(opt.split_size)
                else:
                    try:
                        split_size = float(opt.split_size[:-1])
                    except ValueError:
                        raise Exception("Invalid split size")

                    l = l.upper()
                    if l == 'M':
                        split_size = int(split_size * 1024**2)
                    elif l == 'G':
                        split_size = int(split_size * 1024**3)
                    else:
                        raise Exception("Cannot determine split size")

            self.operation = ImageCreator(opt.source_device, opt.output_folder,
                                          self.status, opt.image_name,
                                          opt.compressor_level, opt.raw,
                                          split_size, opt.iso,
                                          opt.fill_with_zeros)
            self.operation.create_image()

        elif opt.target_device is not None:
            if opt.image_folder is None:
                self.parser.print_help()
                sys.exit(1)

            if not check_if_root():
                sys.stderr.write("You need to run this option as root.\n")
                sys.exit(1)

            partitions = None
            if opt.partition_numbers is not None:
                if not opt.partition_numbers.find(','):
                    partitions = list()
                    partitions.append(int(opt.partition_numbers))
                else:
                    plist = opt.partition_numbers.split(',')
                    partitions = map(lambda x: int(x), plist)

            self.operation = ImageRestorer(opt.image_folder, opt.target_device,
                                           self.status, partitions, opt.expand)
            try:
                self.operation.restore_image()
            except OSError, e:
                if e.errno == errno.ECHILD:
                    pass

        elif opt.image_folder is not None:
            image_path = adjust_path(opt.image_folder)
            inf = Information(image_path)
            inf.load()
            sys.stdout.write("Name:\t\t\t%s\n" % inf.get_image_name())
            sys.stdout.write("Is disk:\t\t%s\n" % inf.get_image_is_disk())
            sys.stdout.write("Compressor level:\t%d\n\n" %
                             inf.get_image_compressor_level())
            sys.stdout.write("Partitions:\n")
            sys.stdout.write("%-10s %-15s %-15s %-16s %-36s\n" %
                             ("Number", "Type", "Size", "Label", "UUID"))
            for part in inf.get_partitions():
                uuid = ''
                label = ''
                if hasattr(part, "uuid"):
                    uuid = part.uuid
                if hasattr(part, "label"):
                    label = part.label
                sys.stdout.write(
                    "%-10d %-15s %-15d %-16s %-36s\n" %
                    (part.number, part.type, part.size, label, uuid))
            sys.stdout.flush()
示例#3
0
    def restore_image(self):
        """ """
        if is_mounted(self.target_device):
            raise DeviceIsMounted("Please umount first")

        self.active = True
        information = Information(self.image_path)
        information.load()
        if self.partitions:
            information.set_partitions(self.partitions)
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        partitions = information.get_partitions()

        # Get total size
        total_bytes = 0
        for part in partitions:
            total_bytes += part.size

        self.total_blocks = long(math.ceil(total_bytes / float(BLOCK_SIZE)))

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            log.error("Invalid target device %s" % device.path)
            raise ErrorRestoringImage("Invalid target device")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                log.error("Unrecognized disk label")
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            log.info("Restoring MBR and Disk Layout")
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk, self.expand)

        self.timer.start()
        for part in partitions:
            if not self.active: break

            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(
                    part.number, part.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                    self.target_device, part.type)

            log.info("Restoring partition {0}".format(partition.get_path()))

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if hasattr(part, "uuid"):
                partition.filesystem.open_to_write(part.uuid)
            else:
                partition.filesystem.open_to_write()

            if hasattr(part, "label"):
                partition.filesystem.write_label(part.label)

            if partition.filesystem.is_swap():
                continue

            pattern = FILE_PATTERN.format(name=image_name,
                                          partition=part.number,
                                          volume="{volume}")
            volumes = 1
            if hasattr(part, "volumes"):
                volumes = part.volumes

            image_reader = ImageReaderFactory(self.image_path, pattern,
                                              volumes, compressor_level,
                                              self.notify_status)

            extract_callback = None
            if compressor_level:
                compressor = Compressor(compressor_level)
                extract_callback = compressor.extract

            self.buffer_manager = BufferManagerFactory(image_reader.read_block,
                                                       extract_callback)

            # open the file after instantiating BufferManager, cause of a
            # problem on multiprocessing module, FD_CLOEXE doesn't work
            # (I don't want to dup the file descriptor).
            image_reader.open()
            self.buffer_manager.start()

            buffer = self.buffer_manager.output_buffer
            while self.active:
                try:
                    data = buffer.get()
                except IOError, e:
                    if e.errno == errno.EINTR:
                        self.cancel()
                        break

                if data == EOF:
                    break

                try:
                    partition.filesystem.write_block(data)
                except ErrorWritingToDevice, e:
                    if not self.canceled:
                        self.stop()
                        raise e

                self.processed_blocks += 1
示例#4
0
    def restore_image(self):
        """ """
        information = Information(self.image_path)
        information.load()
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        total_bytes = information.get_image_total_bytes()
        total_blocks = long(math.ceil(total_bytes / float(BLOCK_SIZE)))

        compressor = Compressor(compressor_level)

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            raise ErrorRestoringImage("Invalid target dispositive")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk)

        self._print_informations(total_bytes, image_name)
        progress = Progress(total_blocks)
        progress.start()
        partitions = information.get_partitions()
        for p in partitions:
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(p.number, p.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                    self.target_device, p.type)

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if p.uuid is not None:
                partition.filesystem.open_to_write(p.uuid)
            else:
                partition.filesystem.open_to_write()

            if partition.filesystem.is_swap():
                continue

            file_name = FILE_PATTERN % (image_name, p.number)
            with open(self.image_path + file_name, 'rb') as f:
                # TODO: Work with parallelism
                while True:  # Ugly
                    header = f.read(compressor.get_header_size())
                    if not len(header):
                        break
                    size = compressor.read_block_header(header)
                    cdata = f.read(size)
                    data = compressor.extract(cdata)
                    partition.filesystem.write(data)
                    progress.increment(1)
            partition.filesystem.close()

        progress.stop()
        print "completed."
示例#5
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."
示例#6
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."
示例#7
0
    def restore_image(self):
        """ """
        if is_mounted(self.target_device):
            raise DeviceIsMounted("Please umount first")

        self.active = True
        information = Information(self.image_path)
        information.load()
        if self.partitions:
            information.set_partitions(self.partitions)
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        partitions = information.get_partitions()

        # Get total size
        total_bytes = 0
        for part in partitions:
            total_bytes += part.size

        self.total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            log.error("Invalid target device %s" % device.path)
            raise ErrorRestoringImage("Invalid target device")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                log.error("Unrecognized disk label")
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            log.info("Restoring MBR and Disk Layout")
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk, self.expand)

        self.timer.start()
        for part in partitions:
            if not self.active: break
            
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(part.number,
                                                         part.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                                            self.target_device,
                                            part.type)

            log.info("Restoring partition {0}".format(partition.get_path()))

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if hasattr(part, "uuid"):
                partition.filesystem.open_to_write(part.uuid)
            else:
                partition.filesystem.open_to_write()

            if hasattr(part, "label"):
                partition.filesystem.write_label(part.label)

            if partition.filesystem.is_swap():
                continue

            pattern = FILE_PATTERN.format(name=image_name,
                                          partition=part.number,
                                          volume="{volume}")
            volumes = 1
            if hasattr(part, "volumes"):
                volumes = part.volumes

            image_reader = ImageReaderFactory(self.image_path, pattern,
                                              volumes, compressor_level,
                                              self.notify_status)

            extract_callback = None
            if compressor_level:
                compressor = Compressor(compressor_level)
                extract_callback = compressor.extract

            self.buffer_manager = BufferManagerFactory(image_reader.read_block,
                                                       extract_callback)

            # open the file after instantiating BufferManager, cause of a
            # problem on multiprocessing module, FD_CLOEXE doesn't work
            # (I don't want to dup the file descriptor).
            image_reader.open()
            self.buffer_manager.start()

            buffer = self.buffer_manager.output_buffer
            while self.active:
                try:
                    data = buffer.get()
                except IOError, e:
                    if e.errno == errno.EINTR:
                        self.cancel()
                        break

                if data == EOF:
                    break

                try:
                    partition.filesystem.write_block(data)
                except ErrorWritingToDevice, e:
                    if not self.canceled:
                        self.stop()
                        raise e

                self.processed_blocks += 1
示例#8
0
    def create_image(self):
        """ """
        if is_mounted(self.device_path):
            raise DeviceIsMounted("Please umount first")

        self.active = True
        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)
        if not partition_list:
            raise ErrorCreatingImage("Partition(s) hasn't a " +\
                                     "valid filesystem")

        # check partitions filesystem
        if not self.raw:
            for part in partition_list:
                log.info("Checking filesystem of {0}".format(part.get_path()))
                self.notify_status("checking_filesystem",
                                   {"device": part.get_path()})
                if not part.filesystem.check():
                    log.error("{0} Filesystem is not clean".\
                              format(part.get_path()))
                    raise ErrorCreatingImage("{0} Filesystem is not clean".\
                                             format(part.get_path()))

        # fill partitions with zeroes
        if self.raw and self.fill_with_zeros:
            for part in partition_list:
                log.info("{0} Filling with zeros".format(part.get_path()))
                self.notify_status("filling_with_zeros", {"device":
                                                          part.get_path()})
                part.filesystem.fill_with_zeros()

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

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

        # TODO: Abstract this whole part, when creating isos,
        # splitting in files, etc...

        self.timer.start()
        remaining_size = self.split_size
        if self.create_iso:
            remaining_size -= BASE_SYSTEM_SIZE
        slices = dict()                  # Used when creating iso
        iso_volume = 1                   # Used when creating iso
        for part in partition_list:
            if not self.active: break

            log.info("Creating image of {0}".format(part.get_path()))
            number = part.get_number()
            uuid = part.filesystem.uuid()
            label = part.filesystem.read_label()
            type = part.filesystem.type

            part.filesystem.open_to_read()

            compact_callback = None
            if self.compressor_level:
                compressor = Compressor(self.compressor_level)
                compact_callback = compressor.compact

            self.buffer_manager = BufferManagerFactory(
                                  part.filesystem.read_block,
                                  compact_callback)
            self.buffer_manager.start()

            buffer = self.buffer_manager.output_buffer 
            volumes = 1
            while self.active:
                total_written = 0 # Used to help splitting the file
                pattern = FILE_PATTERN.format(name=self.image_name,
                                              partition=number,
                                              volume=volumes)
                file_path = self.target_path + pattern
                fd = open(file_path, "wb")

                next_partition = False
                while self.active:
                    try:
                        data = buffer.get()
                    except IOError, e:
                        if e.errno == errno.EINTR:
                            self.cancel()
                            break

                    if data == EOF:
                        next_partition = True
                        if self.create_iso:
                            remaining_size -= total_written
                            if not slices.has_key(iso_volume):
                                slices[iso_volume] = list()
                            slices[iso_volume].append(file_path)
                        break

                    fd.write(data)
                    self.processed_blocks += 1

                    if self.split_size:
                        bytes_written = len(data)
                        total_written += bytes_written

                        if (total_written + bytes_written) >= remaining_size:
                            volumes += 1
                            remaining_size = self.split_size
                            if self.create_iso:
                                if not slices.has_key(iso_volume):
                                    slices[iso_volume] = list()
                                slices[iso_volume].append(file_path)
                                iso_volume += 1
                            break

                fd.close()
                if next_partition: break

            self.buffer_manager.join()
            part.filesystem.close()
            information.add_partition(number, type, volumes,
                                      part.filesystem.get_used_size(),
                                      uuid, label)
示例#9
0
    def create_image(self):
        """ """
        if is_mounted(self.device_path):
            log.error("The partition {0} is mounted, please umount first, and try again".format(self.device_path))
            self.notify_status("mounted_partition_error",{"mounted_partition_error":self.device_path})
            raise DeviceIsMounted("Please umount first")

        self.active = True
        device = Device(self.device_path)
        disk = Disk(device)

        if device.is_disk():
            try:
                mbr = Mbr(self.target_path)
                mbr.save_to_file(self.device_path)
                dlm = DiskLayoutManager(self.target_path)
                dlm.save_to_file(disk)
            except Exception as e:
                log.info(e)
                self.notify_status("write_error", \
                                   {"write_error":self.target_path})
                raise ErrorWritingToDevice("Write error in {0}".format(self.target_path))

        partition_list = disk.get_valid_partitions(self.raw)
        if not partition_list:
            raise ErrorCreatingImage("Partition(s) hasn't a " +\
                                     "valid filesystem")

        # check partitions filesystem
        if not self.raw:
            for part in partition_list:
                log.info("Checking filesystem of {0}".format(part.get_path()))
                self.notify_status("checking_filesystem",
                                   {"device": part.get_path()})
                if not part.filesystem.check():
                    log.error("{0} Filesystem is not clean".\
                              format(part.get_path()))
                    raise ErrorCreatingImage("{0} Filesystem is not clean".\
                                             format(part.get_path()))

        # fill partitions with zeroes
        if self.raw and self.fill_with_zeros:
            for part in partition_list:
                log.info("{0} Filling with zeros".format(part.get_path()))
                self.notify_status("filling_with_zeros", {"device":
                                                          part.get_path()})
                part.filesystem.fill_with_zeros()

        # get total size
        total_bytes = 0
        for part in partition_list:
            total_bytes += part.filesystem.get_used_size()

        self.total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))
        information = Information(self.target_path)
        information.set_image_is_disk(device.is_disk())
        information.set_image_name(self.image_name)
        information.set_image_compressor_level(self.compressor_level)
        if device.is_disk():
            disk_info = DiskInfo()
            disk_dict = disk_info.formated_disk(self.device_path)
            information.set_disk_size(disk_dict["size"])
        # TODO: Abstract this whole part, when creating isos,
        # splitting in files, etc...

        self.timer.start()
        remaining_size = self.split_size
        if self.create_iso:
            remaining_size -= BASE_SYSTEM_SIZE
        slices = dict()                  # Used when creating iso
        iso_volume = 1                   # Used when creating iso

        for part in partition_list:
            if not self.active: break

            log.info("Creating image of {0}".format(part.get_path()))
            self.notify_status("image_creator", \
                               {"image_creator ": part.get_path()})
            number = part.get_number()
            uuid = part.filesystem.uuid()
            label = part.filesystem.read_label()
            type = part.filesystem.type
            part.filesystem.open_to_read()

            #check if partclone is running
            if type in  ("ext2","ext3","ext4"):
                self.partclone_stderr = part.filesystem.get_error_ext()

            compact_callback = None
            if self.compressor_level:
                compressor = Compressor(self.compressor_level)
                compact_callback = compressor.compact

            self.buffer_manager = BufferManagerFactory(
                                  part.filesystem.read_block,
                                  self.notify_status,
                                  compact_callback)
            self.buffer_manager.start()

            buffer = self.buffer_manager.output_buffer
            volumes = 1
            while self.active:
                total_written = 0 # Used to help splitting the file
                pattern = FILE_PATTERN.format(name=self.image_name,
                                              partition=number,
                                              volume=volumes)
                file_path = self.target_path + pattern
                try:
                    fd = open(file_path, "wb")
                except Exception as e:
                    log.info(e)
                    self.notify_status("open_file", \
                                       {"open_file":file_path})
                    raise ImageNotFound("The file wasn't found {0}". \
                                        format(file_path))
                next_partition = False
                while self.active:
                    try:
                        data = buffer.get()
                    except IOError, e:
                        #self.notify_status("read_buffer_error", \
                        #           {"read_buffer_error":str(e)})
                        if e.errno == errno.EINTR:
                            self.notify_status("read_buffer_error", \
                                       {"read_buffer_error":str(e)})
                            data = ""
                            self.cancel()
                            raise ErrorReadingFromDevice(e)
                            break


                    if data == EOF:
                        if (self.partclone_stderr != None):
                            self.data_is_eof = True
                            while self.partclone_sucess == False:
                                pass

                        self.partclone_stderr = None
                        self.partclone_sucess = False
                        self.data_is_eof = False

                        next_partition = True
                        if self.create_iso:
                            remaining_size -= total_written
                            if not slices.has_key(iso_volume):
                                slices[iso_volume] = list()
                            slices[iso_volume].append(file_path)
                        break
                    try:
                        fd.write(data)
                    except Exception as e:
                        log.info("{0}".format(e))
                        self.notify_status("disk_full")
                        self.cancel()
                        raise ErrorWritingToDevice("Error in write file {0}".\
                                                   format(file_path))

                    self.processed_blocks += 1

                    if self.split_size:
                        bytes_written = len(data)
                        total_written += bytes_written

                        if (total_written + bytes_written) >= remaining_size:
                            volumes += 1
                            remaining_size = self.split_size
                            if self.create_iso:
                                if not slices.has_key(iso_volume):
                                    slices[iso_volume] = list()
                                slices[iso_volume].append(file_path)
                                iso_volume += 1
                            break
                try:
                    fd.close()
                except Exception as e:
                    log.info(e)
                    self.notify_status("write_error",{"write_error":e})
                    raise ErrorCloseToWrite("Close Error {0}".format(e))

                if next_partition: break

            self.buffer_manager.join()
            part.filesystem.close()
            information.add_partition(number, type, volumes,
                                      part.filesystem.get_used_size(),
                                      uuid, label)
示例#10
0
    def restore_image(self):
        """ """
        information = Information(self.image_path)
        information.load()
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        total_bytes = information.get_image_total_bytes()
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            raise ErrorRestoringImage("Invalid target dispositive")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk)

        self._print_informations(total_bytes, image_name)
        progress = Progress(total_blocks)
        progress.start()
        partitions = information.get_partitions()
        for part in partitions:
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(part.number, part.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                                            self.target_device,
                                            part.type)

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if hasattr(part, "uuid"):
                partition.filesystem.open_to_write(part.uuid)
            else:
                partition.filesystem.open_to_write()

            if partition.filesystem.is_swap():
                continue

            current_volume = 1
            while True:
                file_name = FILE_PATTERN % (image_name, part.number, current_volume)
                file_path = self.image_path + file_name

                reader = ReaderFactory(file_path, compressor_level)

                for data in reader:
                    partition.filesystem.write(data)
                    progress.increment(1)

                if hasattr(part, "volumes"):
                    if current_volume < part.volumes:
                        current_volume += 1
                        continue
                break
            partition.filesystem.close()

        progress.stop()
        print "completed."
示例#11
0
    def restore_image(self):
        """ """
        information = Information(self.image_path)
        information.load()
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        total_bytes = information.get_image_total_bytes()
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))

        compressor = Compressor(compressor_level)

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            raise ErrorRestoringImage("Invalid target dispositive")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk)

        self._print_informations(total_bytes, image_name)
        progress = Progress(total_blocks)
        progress.start()
        partitions = information.get_partitions()
        for p in partitions:
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(p.number, p.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                                            self.target_device,
                                            p.type)

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if p.uuid is not None:
                partition.filesystem.open_to_write(p.uuid)
            else:
                partition.filesystem.open_to_write()

            if partition.filesystem.is_swap():
                continue

            file_name = FILE_PATTERN % (image_name, p.number)
            with open(self.image_path + file_name, 'rb') as f:
                # TODO: Work with parallelism
                while True: # Ugly
                    header = f.read(compressor.get_header_size())
                    if not len(header):
                        break
                    size = compressor.read_block_header(header)
                    cdata = f.read(size)
                    data = compressor.extract(cdata)
                    partition.filesystem.write(data)
                    progress.increment(1)
            partition.filesystem.close()

        progress.stop()
        print "completed."