def configure_vm_reservation(options,
                             vm_name,
                             cpu_reservation=None,
                             memory_reservation=None,
                             mhz_per_core=None):
    """
    Routine to run configure VMs reservations for cpu and memory when given
    in percents of total allocated
    @type vm_name: string
    @param vm_name: name of the VM
    @type cpu_reservation: int
    @param cpu_reservation: percent of the CPU to be reserved per allocated
    @type memory_reservation: int
    @param memory_reservation: percent of the memory to be reserved per
                               allocated
    @type mhz_per_core: int
    @param mhz_per_core: MHz per core as exposed to VM
    @raise ValueError: if vm is not found
    @raise Exception: if updating of cpu/memory reservation errors
    """
    log = logging.getLogger('vdnet')
    vm = nsx_network.get_vm_by_name(options, vm_name)
    if vm is not None:
        resource_spec = Vim.ResourceConfigSpec()
        resource_spec.entity = vm

        if cpu_reservation is not None:
            if mhz_per_core is None:
                mhz_per_core = 1000  # BUG: 1420892 Assuming each core is 1GHz
            cpu_reservation = int((vm.config.hardware.numCPU * mhz_per_core *
                                   int(cpu_reservation)) / 100)
            resource_spec.cpuAllocation = Vim.ResourceAllocationInfo()
            resource_spec.cpuAllocation.reservation = cpu_reservation
        if memory_reservation is not None:
            memory_reservation = int(
                (vm.config.hardware.memoryMB * int(memory_reservation)) / 100)
            resource_spec.memoryAllocation = Vim.ResourceAllocationInfo()
            resource_spec.memoryAllocation.reservation = memory_reservation

        if ((cpu_reservation is not None or memory_reservation is not None)):
            vc_vm_pool = vm.resourcePool
            try:
                vc_vm_pool.UpdateChildResourceConfiguration([resource_spec])
            except Exception:
                log.exception(
                    "Failed to update cpu/memory resources for vm %r" %
                    vm_name)
                raise
            new_cpu_res = vm.resourceConfig.cpuAllocation.reservation
            new_memory_res = vm.resourceConfig.memoryAllocation.reservation
            log.info("Updated %r with reservations: cpu=%sMHz, memory=%sMB" %
                     (vm_name, new_cpu_res, new_memory_res))
    else:
        raise ValueError("Failed to find vm %r for resources allocation" %
                         vm_name)
Exemple #2
0
    def createResourceConfigSpec(self):
        spec = Vim.ResourceConfigSpec()
        spec.cpuAllocation = Vim.ResourceAllocationInfo()
        spec.cpuAllocation.shares = Vim.SharesInfo()
        spec.cpuAllocation.shares.level = Vim.SharesInfo.Level.normal
        spec.cpuAllocation.shares.shares = 4000
        spec.cpuAllocation.reservation = long(0)
        spec.cpuAllocation.limit = long(-1)
        spec.cpuAllocation.expandableReservation = True

        spec.memoryAllocation = Vim.ResourceAllocationInfo()
        spec.memoryAllocation.shares = Vim.SharesInfo()
        spec.memoryAllocation.shares.level = Vim.SharesInfo.Level.normal
        spec.memoryAllocation.shares.shares = 4000
        spec.memoryAllocation.reservation = long(0)
        spec.memoryAllocation.limit = long(-1)
        spec.memoryAllocation.expandableReservation = True

        return spec