def _create_lv(self, pool, name, size):
        """
            Really create the logical volume, all parameters were checked.
        """
        args = {}
        args['parents'] = [pool]
        args['size'] = pool.align(float(size) / units.MEGABYTE, True)
        if name:
            args['name'] = name

        storage.log_storage_call("CREATE LV", args)

        lv = self.storage.newLV(**args)
        action = blivet.deviceaction.ActionCreateDevice(lv)
        storage.do_storage_action(self.storage, action)

        newsize = lv.size * units.MEGABYTE
        outparams = [
                pywbem.CIMParameter(
                        name='theelement',
                        type='reference',
                        value=self.provider_manager.get_name_for_device(lv)),
                pywbem.CIMParameter(
                    name="Size",
                    type="uint64",
                    value=pywbem.Uint64(newsize))
        ]
        ret = self.Values.CreateOrModifyElementFromStoragePool \
                .Job_Completed_with_No_Error
        return (ret, outparams)
    def _create_mdraid(self, level, goal, devices, name):
        """
            Create new  MD RAID. The parameters were already checked.
        """
        # TODO: check if devices are unused!
        args = {}
        args['parents'] = devices
        if name:
            args['name'] = name
        args['level'] = str(level)
        args['memberDevices'] = len(devices)

        storage.log_storage_call("CREATE MDRAID", args)

        raid = self.storage.newMDArray(**args)
        action = blivet.ActionCreateDevice(raid)
        storage.do_storage_action(self.storage, action)

        newsize = raid.size * units.MEGABYTE
        outparams = [
                pywbem.CIMParameter(
                        name='theelement',
                        type='reference',
                        value=self.provider_manager.get_name_for_device(raid)),
                pywbem.CIMParameter(
                    name="size",
                    type="uint64",
                    value=pywbem.Uint64(newsize))
        ]
        retval = self.Values.CreateOrModifyMDRAID.Completed_with_No_Error
        return (retval, outparams)
 def do_delete_instance(self, device):
     storage.log_storage_call("DELETE MDRAID",
             {'device': device})
     actions = []
     actions.append(blivet.deviceaction.ActionDestroyDevice(device))
     # Destroy also all formats on member devices.
     # TODO: remove when Blivet does this automatically.
     for parent in device.parents:
         actions.append(blivet.deviceaction.ActionDestroyFormat(parent))
     storage.do_storage_action(self.storage, actions)
    def _create_vg(self, goal, devices, name):
        """
            Create new  Volume Group. The parameters were already checked.
        """
        for device in devices:
            # TODO: check if it is unused!
            if not (device.format
                    and isinstance(device.format,
                        blivet.formats.lvmpv.LVMPhysicalVolume)):
                # create the pv format there
                pv = blivet.formats.getFormat('lvmpv')
                self.storage.formatDevice(device, pv)

        args = {}
        args['parents'] = devices
        if goal and goal['ExtentSize']:
            args['peSize'] = float(goal['ExtentSize']) / units.MEGABYTE
        if name:
            args['name'] = name

        storage.log_storage_call("CREATE VG", args)

        vg = self.storage.newVG(**args)
        action = blivet.ActionCreateDevice(vg)
        storage.do_storage_action(self.storage, action)

        newsize = vg.size * units.MEGABYTE
        outparams = [
                pywbem.CIMParameter(
                        name='pool',
                        type='reference',
                        value=self.provider_manager.get_name_for_device(vg)),
                pywbem.CIMParameter(
                    name="size",
                    type="uint64",
                    value=pywbem.Uint64(newsize))
        ]
        retval = self.Values.CreateOrModifyVG.Job_Completed_with_No_Error
        return (retval, outparams)
    def _setpartitionstyle(self, device, capabilities, capabilities_provider):
        """
            Really set the partition style, all parameters were successfully
            checked.
        """
        part_styles = capabilities_provider.Values.PartitionStyle
        if capabilities['PartitionStyle'] == part_styles.MBR:
            label = "msdos"
        elif capabilities['PartitionStyle'] == part_styles.GPT:
            label = "gpt"
        else:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Unsupported PartitionStyle:"
                    + str(capabilities['PartitionStyle']) + ".")

        storage.log_storage_call("CREATE DISKLABEL",
                {'label': label, 'device': device.path})

        fmt = blivet.formats.getFormat('disklabel', labelType=label)
        action = blivet.deviceaction.ActionCreateFormat(device, fmt)
        storage.do_storage_action(self.storage, [action])

        return self.Values.SetPartitionStyle.Success
    def _modify_lv(self, device, name, size):
        """
            Really modify the logical volume, all parameters were checked.
        """
        outparams = []
        if name is not None:
            # rename
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Rename of logical volume is not yet supported.")

        if size is not None:
            # resize

            # check PE size
            newsize = device.vg.align(float(size) / units.MEGABYTE, True)
            oldsize = device.vg.align(device.size, False)
            if newsize != oldsize:
                action = blivet.deviceaction.ActionResizeDevice(
                        device, newsize)
                storage.do_storage_action(self.storage, action)
                self.storage.devicetree.processActions(dryRun=False)
                self.storage.reset()

        newsize = device.size * units.MEGABYTE
        outparams.append(pywbem.CIMParameter(
                name="Size",
                type="uint64",
                value=pywbem.Uint64(newsize)))

        outparams.append(pywbem.CIMParameter(
                name='theelement',
                type='reference',
                value=self.provider_manager.get_name_for_device(device)))
        ret = self.Values.CreateOrModifyElementFromStoragePool \
                .Job_Completed_with_No_Error
        return (ret, outparams)
    def _delete_partition(self, job, devicename):
        """
            Delete given partition.
            This method is called from JobManager worker thread!
        """
        device = self.provider_manager.get_device_for_name(devicename)
        if not device:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "The devices disappeared: " + devicename)
        # Check the device is unused
        storage.assert_unused(self.storage, [device])

        action = blivet.ActionDestroyDevice(device)
        storage.do_storage_action(self.storage, [action])

        ret = self.Values.LMI_DeletePartition.Job_Completed_with_No_Error

        job.finish_method(
                Job.STATE_FINISHED_OK,
                return_value=ret,
                return_type=Job.ReturnValueType.Uint32,
                output_arguments=[],
                affected_elements=[],
                error=None)
    def _lmi_create_partition(self, job, devicename, goal, size):
        """
            Create partition on given device with  given goal and size.
            Size can be null, which means the largest possible size.
            Return (retval, partition, size).
            This method is called from JobManager worker thread!
        """
        device = self.provider_manager.get_device_for_name(devicename)
        if not device:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "The devices disappeared: " + devicename)

        bootable = None
        part_type = None
        hidden = None
        primary = False

        if not isinstance(device.format,
                blivet.formats.disklabel.DiskLabel):
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find partition table on the extent.")

        # check goal and set appropriate partition parameters
        if goal:
            bootable = goal['Bootable']
            hidden = goal['Hidden']
            part_type = self._calculate_partition_type(device, goal)
            if part_type is not None:
                if part_type == parted.PARTITION_LOGICAL:
                    primary = False
                else:
                    primary = True

        # check size and grow it if necessary
        if size is None:
            grow = True
            size = 1
        else:
            # check maximum size
            max_partition = self._get_max_partition_size(device, part_type)
            max_partition = max_partition * device.partedDevice.sectorSize
            if max_partition < size:
                ret = self.Values.LMI_CreateOrModifyPartition.Size_Not_Supported
                outparams = {'Size' : pywbem.Uint64(max_partition)}
                job.finish_method(
                        Job.STATE_FINISHED_OK,
                        return_value=ret,
                        return_type=Job.ReturnValueType.Uint32,
                        output_arguments=outparams,
                        affected_elements=[],
                        error=None)

            # Ok, the partition will fit. Continue.
            grow = False
            size = size / units.MEGABYTE

        args = {
                'parents': [device],
                'size': size,
                'partType': part_type,
                'bootable': bootable,
                'grow': grow,
                'primary': primary
        }
        storage.log_storage_call("CREATE PARTITION", args)

        partition = self.storage.newPartition(**args)
        partition.disk = device

        if hidden is not None:
            if partition.flagAvailable(parted.PARTITION_HIDDEN):
                if hidden:
                    partition.setFlag(parted.PARTITION_HIDDEN)
            else:
                raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                        "Goal.Hidden cannot be set for this Extent.")

        # finally, do the dirty job
        action = blivet.deviceaction.ActionCreateDevice(partition)
        storage.do_storage_action(self.storage, [action])
        size = partition.size * units.MEGABYTE

        ret = self.Values.LMI_CreateOrModifyPartition\
                .Job_Completed_with_No_Error
        # re-read the partition from blivet, it should have all device links
        partition = self.storage.devicetree.getDeviceByPath(partition.path)
        partition_name = self.provider_manager.get_name_for_device(partition)
        outparams = {
                'Size': pywbem.Uint64(size),
                'Partition': partition_name
        }
        job.finish_method(
                Job.STATE_FINISHED_OK,
                return_value=ret,
                return_type=Job.ReturnValueType.Uint32,
                output_arguments=outparams,
                affected_elements=[partition_name, ],
                error=None)
 def do_delete_instance(self, device):
     cmpi_logging.logger.info("DELETE LV: %s" % (device.path))
     action = blivet.deviceaction.ActionDestroyDevice(device)
     storage.do_storage_action(self.storage, action)
 def do_delete_instance(self, device):
     storage.log_storage_call("DELETE VG",
             {'device': device})
     action = blivet.deviceaction.ActionDestroyDevice(device)
     storage.do_storage_action(self.storage, action)
    def _lmi_create_partition(self, device, goal, size):
        """
            Create partition on given device with  given goal and size.
            Size can be null, which means the largest possible size.
            Return (retval, partition, size).
        """
        bootable = None
        part_type = None
        hidden = None
        primary = False

        if not isinstance(device.format,
                blivet.formats.disklabel.DiskLabel):
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find partition table on the extent.")

        # check goal and set appropriate partition parameters
        if goal:
            bootable = goal['Bootable']
            hidden = goal['Hidden']
            part_type = self._calculate_partition_type(device, goal)
            if part_type is not None:
                if part_type == parted.PARTITION_LOGICAL:
                    primary = False
                else:
                    primary = True

        # check size and grow it if necessary
        if size is None:
            grow = True
            size = 1
        else:
            # check maximum size
            max_partition = self._get_max_partition_size(device, part_type)
            max_partition = max_partition * device.partedDevice.sectorSize
            if max_partition < size:
                ret = self.Values.LMI_CreateOrModifyPartition.Size_Not_Supported
                return (ret, None, max_partition)
            # Ok, the partition will fit. Continue.
            grow = False
            size = size / units.MEGABYTE

        args = {
                'parents': [device],
                'size': size,
                'partType': part_type,
                'bootable': bootable,
                'grow': grow,
                'primary': primary
        }
        storage.log_storage_call("CREATE PARTITION", args)

        partition = self.storage.newPartition(**args)
        partition.disk = device

        if hidden is not None:
            if partition.flagAvailable(parted.PARTITION_HIDDEN):
                if hidden:
                    partition.setFlag(parted.PARTITION_HIDDEN)
            else:
                raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                        "Goal.Hidden cannot be set for this Extent.")

        # finally, do the dirty job
        action = blivet.deviceaction.ActionCreateDevice(partition)
        storage.do_storage_action(self.storage, action)
        size = partition.size * units.MEGABYTE

        ret = self.Values.LMI_CreateOrModifyPartition\
                .Job_Completed_with_No_Error
        return (ret, partition, size)