Example #1
0
    def runTest(self):
        # The DOS disklabel does not support naming.
        self.assertRaises(_ped.PartitionException, self._part.set_name, "blah")

        # These should work.
        self._disk = _ped.disk_new_fresh(self._device, _ped.disk_type_get("mac"))
        self._part = _ped.Partition(self._disk, _ped.PARTITION_NORMAL, 0, 100, _ped.file_system_type_get("fat32"))
        self.assertTrue(self._part.set_name("blah"))
        self.assertEqual(self._part.get_name(), "blah")

        # Partitions that are inactive won't work.
        self._part = _ped.Partition(self._disk, _ped.PARTITION_FREESPACE, 0, 100)
        self.assertRaises(_ped.PartitionException, self._part.get_name)
Example #2
0
    def runTest(self):
        # The DOS disklabel does not support naming.
        self.assertRaises(_ped.PartitionException, self._part.set_name, "blah")

        # These should work.
        self._disk = _ped.disk_new_fresh(self._device, _ped.disk_type_get("mac"))
        self._part = _ped.Partition(self._disk, _ped.PARTITION_NORMAL, 0, 100,
                                    _ped.file_system_type_get("fat32"))
        self.assertTrue(self._part.set_name("blah"))
        self.assertEqual(self._part.get_name(), "blah")

        # Partitions that are inactive won't work.
        self._part = _ped.Partition(self._disk, _ped.PARTITION_FREESPACE, 0, 100)
        self.assertRaises(_ped.PartitionException, self._part.get_name)
Example #3
0
def freshDisk(device, ty):
    """Return a Disk object for this Device and using this DiskType.
       The type should be a member of the parted.diskType hash,
       either a key or a value.

       The new label is not written to disk until commitToDevice()
       is called on the Disk."""
    from _ped import disk_new_fresh, DiskType

    if isinstance(ty, string_types):
        ty = diskType[ty]
    elif not isinstance(ty, DiskType):
        raise TypeError("type must be a key or value in parted.diskType", ty)

    peddisk = disk_new_fresh(device.getPedDevice(), ty)
    return Disk(PedDisk=peddisk)
Example #4
0
def freshDisk(device, ty):
    """Return a Disk object for this Device and using this DiskType.
       The type should be a member of the parted.diskType hash,
       either a key or a value.

       The new label is not written to disk until commitToDevice()
       is called on the Disk."""
    from _ped import disk_new_fresh, DiskType

    if type(ty) == str:
        ty = diskType[ty]
    elif not isinstance(ty, DiskType):
        raise SyntaxError("type must be a key or value in parted.diskType")

    peddisk = disk_new_fresh(device.getPedDevice(), ty)
    return Disk(PedDisk=peddisk)
Example #5
0
    def runTest(self):
        # The DOS disklabel does not support naming.
        self.assertRaises(_ped.PartitionException, self._part.get_name)

        # Partitions that are inactive won't work either.
        self._part = _ped.Partition(self._disk, _ped.PARTITION_FREESPACE, 0, 100)
        self.assertRaises(_ped.PartitionException, self._part.get_name)

        # Mac disk labels do support naming, but there still has to be a name.
        self._disk = _ped.disk_new_fresh(self._device, _ped.disk_type_get("mac"))
        self._part = _ped.Partition(self._disk, _ped.PARTITION_NORMAL, 0, 100, _ped.file_system_type_get("fat32"))
        self.assertEqual(self._part.get_name(), "untitled")

        # Finally, Mac disk labels with a name will work.
        self._part.set_name("blah")
        self.assertEqual(self._part.get_name(), "blah")
Example #6
0
    def runTest(self):
        # The DOS disklabel does not support naming.
        self.assertRaises(_ped.PartitionException, self._part.get_name)

        # Partitions that are inactive won't work either.
        self._part = _ped.Partition(self._disk, _ped.PARTITION_FREESPACE, 0, 100)
        self.assertRaises(_ped.PartitionException, self._part.get_name)

        # Mac disk labels do support naming, but there still has to be a name.
        self._disk = _ped.disk_new_fresh(self._device, _ped.disk_type_get("mac"))
        self._part = _ped.Partition(self._disk, _ped.PARTITION_NORMAL, 0, 100,
                                    _ped.file_system_type_get("fat32"))
        self.assertEqual(self._part.get_name(), "untitled")

        # Finally, Mac disk labels with a name will work.
        self._part.set_name("blah")
        self.assertEqual(self._part.get_name(), "blah")
Example #7
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
Example #8
0
 def setUp(self):
     RequiresDevice.setUp(self)
     self._disk = _ped.disk_new_fresh(self._device,
                                      _ped.disk_type_get("msdos"))
     self.disk = parted.Disk(PedDisk=self._disk)
Example #9
0
 def setUp(self):
     RequiresDevice.setUp(self)
     self._disk = _ped.disk_new_fresh(self._device, _ped.disk_type_get("msdos"))
     self.disk = parted.Disk(PedDisk=self._disk)