[pve-devel] [RFC container 7/7] api: create/update vm: clamp cpuunit value

Fiona Ebner f.ebner at proxmox.com
Fri Oct 7 14:41:47 CEST 2022


While the clamping already happens before setting the actual
cpu.weight lxc config key, it can be done here too, to avoid writing
new out-of-range values into the config.

Can't use a validator enforcing this, because existing out-of-range
values should not become errors on parsing the config.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---
 src/PVE/API2/LXC.pm        | 3 +++
 src/PVE/API2/LXC/Config.pm | 4 ++++
 2 files changed, 7 insertions(+)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 589f96f..8282850 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -221,6 +221,9 @@ __PACKAGE__->register_method({
 	my $restore = extract_param($param, 'restore');
 	my $unique = extract_param($param, 'unique');
 
+	$param->{cpuunits} = PVE::GuestHelpers::get_cpuunits($param->{cpuunits})
+	    if defined($param->{cpuunits}); # clamp value depending on cgroup version
+
 	# used to skip firewall config restore if user lacks permission
 	my $skip_fw_config_restore = 0;
 
diff --git a/src/PVE/API2/LXC/Config.pm b/src/PVE/API2/LXC/Config.pm
index 1fec048..a27be56 100644
--- a/src/PVE/API2/LXC/Config.pm
+++ b/src/PVE/API2/LXC/Config.pm
@@ -10,6 +10,7 @@ use PVE::INotify;
 use PVE::Cluster qw(cfs_read_file);
 use PVE::AccessControl;
 use PVE::Firewall;
+use PVE::GuestHelpers;
 use PVE::Storage;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
@@ -144,6 +145,9 @@ __PACKAGE__->register_method({
 	my $revert_str = extract_param($param, 'revert');
 	my @revert = PVE::Tools::split_list($revert_str);
 
+	$param->{cpuunits} = PVE::GuestHelpers::get_cpuunits($param->{cpuunits})
+	    if defined($param->{cpuunits}); # clamp value depending on cgroup version
+
 	my $code = sub {
 
 	    my $conf = PVE::LXC::Config->load_config($vmid);
-- 
2.30.2






More information about the pve-devel mailing list