rebalance_lxc_containers: fix hotplug

factor out code to modify cpusets into $modify_cpuset->()
This commit is contained in:
Dietmar Maurer 2016-12-21 11:13:16 +01:00
parent 193146f8b0
commit 0b959507c1

View File

@ -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);
}
}
}