Skip to content

Commit c4db395

Browse files
phsmDaanHoogland
andcommitted
fix: enforce the cpu shares within allowed range
To be compatible with older libvirt versions Co-authored-by: dahn <[email protected]>
1 parent a163831 commit c4db395

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
320320
public static final String WINDOWS_GUEST_CONVERSION_SUPPORTED_CHECK_CMD = "rpm -qa | grep -i virtio-win";
321321
public static final String UBUNTU_WINDOWS_GUEST_CONVERSION_SUPPORTED_CHECK_CMD = "dpkg -l virtio-win";
322322
public static final String UBUNTU_NBDKIT_PKG_CHECK_CMD = "dpkg -l nbdkit";
323+
324+
// Libvirt < 9.1.0 cgroupv1 CPU shares values range
325+
public static final int LIBVIRT_CGROUPV1_CPU_SHARES_MIN = 2;
326+
public static final int LIBVIRT_CGROUPV1_CPU_SHARES_MAX = 262144;
323327

324328
private String modifyVlanPath;
325329
private String versionStringPath;
@@ -2796,12 +2800,23 @@ public int calculateCpuShares(VirtualMachineTO vmTO) {
27962800

27972801
if (hostCpuMaxCapacity > 0) {
27982802
int updatedCpuShares = (int) Math.ceil((requestedCpuShares * CGROUP_V2_UPPER_LIMIT) / (double) hostCpuMaxCapacity);
2799-
LOGGER.debug(String.format("This host utilizes cgroupv2 (as the max shares value is [%s]), thus, the VM requested shares of [%s] will be converted to " +
2800-
"consider the host limits; the new CPU shares value is [%s].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares));
2801-
return updatedCpuShares;
2802-
}
2803-
LOGGER.debug(String.format("This host does not have a maximum CPU shares set; therefore, this host utilizes cgroupv1 and the VM requested CPU shares [%s] will not be " +
2804-
"converted.", requestedCpuShares));
2803+
2804+
LOGGER.debug("This host utilizes cgroupv2 (as the max shares value is [{}]), thus, the VM requested shares of [{}] will be converted to " +
2805+
"consider the host limits; the new CPU shares value is [{}].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares);
2806+
requestedCpuShares = updatedCpuShares;
2807+
} else {
2808+
LOGGER.debug("This host does not have a maximum CPU shares set; therefore, this host utilizes cgroupv1 and the VM requested CPU shares [{}] will not be " +
2809+
"converted.", requestedCpuShares);
2810+
}
2811+
2812+
/**
2813+
* Libvirt < 9.1.0 enforces the same value range to both cgroupv1 and cgroupv2.
2814+
* Therefore, if the shares value is determined to be outside of boundaries,
2815+
* then bring it to the minimum or maximum allowed value.
2816+
* See: https://github.com/libvirt/libvirt/commit/38af6497610075e5fe386734b87186731d4c17ac
2817+
*/
2818+
if (requestedCpuShares < LIBVIRT_CGROUPV1_CPU_SHARES_MIN) requestedCpuShares = LIBVIRT_CGROUPV1_CPU_SHARES_MIN;
2819+
if (requestedCpuShares > LIBVIRT_CGROUPV1_CPU_SHARES_MAX) requestedCpuShares = LIBVIRT_CGROUPV1_CPU_SHARES_MAX;
28052820
return requestedCpuShares;
28062821
}
28072822

0 commit comments

Comments
 (0)