[pve-devel] [PATCH] lxc: add hotplug memory && cpus cgroups value V3

Alexandre Derumier aderumier at odiso.com
Mon Jun 22 08:48:22 CEST 2015


changelog:

- rename cgroups_write to write_cgroup_value  (to match the existing read_cgroup_value)
- write config after each $opt if vm is running. (In case of hotplug fail, when nic hotplug will be implemented)
- push nohotplug $opt in an array, and display them at the end

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/LXC.pm | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 068b46f..2f4d9eb 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -804,6 +804,14 @@ sub read_cgroup_value {
     return PVE::Tools::file_read_firstline($path);
 }
 
+sub write_cgroup_value {
+   my ($group, $vmid, $name, $value) = @_;
+
+   my $path = "/sys/fs/cgroup/$group/lxc/$vmid/$name";
+   PVE::ProcFSTools::write_proc_entry($path, $value) if -e $path;
+
+}
+
 sub find_lxc_console_pids {
 
     my $res = {};
@@ -964,8 +972,7 @@ sub verify_searchdomain_list {
 sub update_lxc_config {
     my ($vmid, $conf, $running, $param, $delete) = @_;
 
-    # fixme: hotplug
-    die "unable to modify config while container is running\n" if $running;
+    my @nohotplug;
 
     if (defined($delete)) {
 	foreach my $opt (@$delete) {
@@ -973,6 +980,7 @@ sub update_lxc_config {
 		die "unable to delete required option '$opt'\n";
 	    } elsif ($opt eq 'swap') {
 		delete $conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'};
+		write_cgroup_value("memory", $vmid, "memory.memsw.limit_in_bytes", -1);
 	    } elsif ($opt eq 'description') {
 		delete $conf->{'pve.comment'};
 	    } elsif ($opt eq 'onboot') {
@@ -981,13 +989,20 @@ sub update_lxc_config {
 		delete $conf->{'pve.startup'};
 	    } elsif ($opt eq 'nameserver') {
 		delete $conf->{'pve.nameserver'};
+		push @nohotplug, $opt;
+		next if $running;
 	    } elsif ($opt eq 'searchdomain') {
 		delete $conf->{'pve.searchdomain'};
+		push @nohotplug, $opt;
+		next if $running;
 	    } elsif ($opt =~ m/^net\d$/) {
 		delete $conf->{$opt};
+		push @nohotplug, $opt;
+		next if $running;
 	    } else {
 		die "implement me"
 	    }
+	    PVE::LXC::write_config($vmid, $conf) if $running;
 	}
     }
 
@@ -1002,39 +1017,56 @@ sub update_lxc_config {
 	} elsif ($opt eq 'nameserver') {
 	    my $list = verify_nameserver_list($value);
 	    $conf->{'pve.nameserver'} = $list;
+	    push @nohotplug, $opt;
+	    next if $running;
 	} elsif ($opt eq 'searchdomain') {
 	    my $list = verify_searchdomain_list($value);
 	    $conf->{'pve.searchdomain'} = $list;
+	    push @nohotplug, $opt;
+	    next if $running;
 	} elsif ($opt eq 'memory') {
 	    $conf->{'lxc.cgroup.memory.limit_in_bytes'} = $value*1024*1024;
+	    write_cgroup_value("memory", $vmid, "memory.limit_in_bytes", $value*1024*1024);
 	} elsif ($opt eq 'swap') {
 	    my $mem =  $conf->{'lxc.cgroup.memory.limit_in_bytes'};
 	    $mem = $param->{memory}*1024*1024 if $param->{memory};
 	    $conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'} = $mem + $value*1024*1024;
+	    write_cgroup_value("memory", $vmid, "memory.memsw.limit_in_bytes", $mem + $value*1024*1024);
+
 	} elsif ($opt eq 'cpulimit') {
 	    if ($value > 0) {
 		my $cfs_period_us = 100000;
 		$conf->{'lxc.cgroup.cpu.cfs_period_us'} = $cfs_period_us;
 		$conf->{'lxc.cgroup.cpu.cfs_quota_us'} = $cfs_period_us*$value;
+		write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", $cfs_period_us*$value);
 	    } else {
 		delete $conf->{'lxc.cgroup.cpu.cfs_period_us'};
 		delete $conf->{'lxc.cgroup.cpu.cfs_quota_us'};
+		write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", -1);
 	    }
 	} elsif ($opt eq 'cpuunits') {
 	    $conf->{'lxc.cgroup.cpu.shares'} = $value;	    
+	    write_cgroup_value("cpu", $vmid, "cpu.shares", $value);
 	} elsif ($opt eq 'description') {
 	    $conf->{'pve.comment'} = PVE::Tools::encode_text($value);
 	} elsif ($opt eq 'disk') {
 	    $conf->{'pve.disksize'} = $value;
+	    push @nohotplug, $opt;
+	    next if $running;
 	} elsif ($opt =~ m/^net(\d+)$/) {
 	    my $netid = $1;
 	    my $net = PVE::LXC::parse_lxc_network($value);
 	    $net->{'veth.pair'} = "veth${vmid}.$netid";
 	    $conf->{$opt} = $net;
+	    push @nohotplug, $opt;
+	    next if $running;
 	} else {
 	    die "implement me"
 	}
+	PVE::LXC::write_config($vmid, $conf) if $running;
     }
+
+    die "unable to modify ".join(",", at nohotplug)." while container is running\n" if @nohotplug > 0 && $running;
 }
 
 sub get_primary_ips {
@@ -1071,5 +1103,6 @@ sub destory_lxc_container {
 	PVE::Tools::run_command($cmd);
     }
 }
+
     
 1;
-- 
2.1.4




More information about the pve-devel mailing list