diff --git a/PVE/Service/pvestatd.pm b/PVE/Service/pvestatd.pm index 74813877..e99d3c74 100755 --- a/PVE/Service/pvestatd.pm +++ b/PVE/Service/pvestatd.pm @@ -234,6 +234,29 @@ sub rebalance_lxc_containers { my @cpu_ctcount = (0) x $max_cpuid; my @balanced_cts; + my $modify_cpuset = sub { + my ($vmid, $cpuset, $newset) = @_; + + syslog('info', "modified cpu set for lxc/$vmid: " . + $newset->short_string()); + eval { + # allow all, so that we can set new cpuset in /ns + $all_cpus->write_to_cgroup("lxc/$vmid"); + eval { + $newset->write_to_cgroup("lxc/$vmid/ns"); + }; + if (my $err = $@) { + warn $err; + # restore original + $cpuset->write_to_cgroup("lxc/$vmid"); + } else { + # also apply to container root cgroup + $newset->write_to_cgroup("lxc/$vmid"); + } + }; + warn $@ if $@; + }; + my $ctlist = PVE::LXC::config_list(); foreach my $vmid (sort keys %$ctlist) { @@ -278,9 +301,7 @@ sub rebalance_lxc_containers { # Apply hot-plugged changes if any: if (!$newset->is_equal($cpuset)) { @cpuset_members = $newset->members(); - syslog('info', "detected changed cpu set for lxc/$vmid: " . - $newset->short_string()); - $newset->write_to_cgroup("lxc/$vmid"); + $modify_cpuset->($vmid, $cpuset, $newset); } # Note: no need to rebalance if we already use all cores @@ -332,24 +353,7 @@ sub rebalance_lxc_containers { } if (!$newset->is_equal($cpuset)) { - syslog('info', "modified cpu set for lxc/$vmid: " . - $newset->short_string()); - eval { - # allow all, so that we can set new cpuset in /ns - $all_cpus->write_to_cgroup("lxc/$vmid"); - eval { - $newset->write_to_cgroup("lxc/$vmid/ns"); - }; - if (my $err = $@) { - warn $err; - # restore original - $cpuset->write_to_cgroup("lxc/$vmid"); - } else { - # also apply to container root cgroup - $newset->write_to_cgroup("lxc/$vmid"); - } - }; - warn $@ if $@; + $modify_cpuset->($vmid, $cpuset, $newset); } } }