]> git.proxmox.com Git - pve-container.git/commitdiff
implement cpuunits, cleanup code
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 22 Apr 2015 06:40:13 +0000 (08:40 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 22 Apr 2015 06:40:13 +0000 (08:40 +0200)
src/PVE/API2/LXC.pm
src/PVE/LXC.pm

index 15a9e735cd9c98fe2000b8843566af23553234ee..ad84d66c4a6291244e689e3125dc0fbefccf4169 100644 (file)
@@ -520,48 +520,13 @@ __PACKAGE__->register_method({
 
        # NOTE: we only return selected/converted values
 
-       my $conf = { digest => $lxc_conf->{digest} };
+       my $conf = PVE::LXC::lxc_conf_to_pve($param->{vmid}, $lxc_conf);
 
        my $stcfg = PVE::Cluster::cfs_read_file("storage.cfg");
 
        my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid}, $lxc_conf);
        $conf->{storage} = $sid || $path;
 
-       my $properties = PVE::LXC::json_config_properties();
-
-       foreach my $k (keys %$properties) {
-
-           if ($k eq 'description') {
-               if (my $raw = $lxc_conf->{'pve.comment'}) {
-                   $conf->{$k} = PVE::Tools::decode_text($raw);
-               }
-           } elsif ($k eq 'hostname') {
-               $conf->{$k} = $lxc_conf->{'lxc.utsname'} if $lxc_conf->{'lxc.utsname'};
-           } elsif ($k eq 'memory') {
-               if (my $value = $lxc_conf->{'lxc.cgroup.memory.limit_in_bytes'}) {
-                   $conf->{$k} = int($value / (1024*1024));
-               }
-           } elsif ($k eq 'swap') {
-               if (my $value = $lxc_conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'}) {
-                   my $mem = $lxc_conf->{'lxc.cgroup.memory.limit_in_bytes'} || 0;
-                   $conf->{$k} = int(($value -$mem) / (1024*1024));
-               }
-           } elsif ($k eq 'cpus') {
-               my $cfs_period_us = $lxc_conf->{'lxc.cgroup.cpu.cfs_period_us'};
-               my $cfs_quota_us = $lxc_conf->{'lxc.cgroup.cpu.cfs_quota_us'};
-
-               if ($cfs_period_us && $cfs_quota_us) {
-                   $conf->{$k} = int($cfs_quota_us/$cfs_period_us);
-               } else {
-                   $conf->{$k} = 0;
-               }
-           } elsif ($k =~ m/^net\d$/) {
-               my $net = $lxc_conf->{$k};
-               next if !$net;
-               $conf->{$k} = PVE::LXC::print_lxc_network($net);
-           }
-       }
-
        return $conf;
     }});
 
index 9f88cd5c40f0c8a5009fb0d1280c98c0027e2eee..6b38f7527969467e218984cf4975542e82d343cd 100644 (file)
@@ -54,8 +54,9 @@ my $valid_lxc_keys = {
     
     'lxc.cgroup.memory.limit_in_bytes' => \&parse_lxc_size,
     'lxc.cgroup.memory.memsw.limit_in_bytes' => \&parse_lxc_size,
-    'lxc.cgroup.cpu.cfs_period_us' => 1,
-    'lxc.cgroup.cpu.cfs_quota_us' => 1,
+    'lxc.cgroup.cpu.cfs_period_us' => '\d+',
+    'lxc.cgroup.cpu.cfs_quota_us' => '\d+',
+    'lxc.cgroup.cpu.shares' => '\d+',
     
     # mount related
     'lxc.mount' => 1,
@@ -63,7 +64,7 @@ my $valid_lxc_keys = {
     'lxc.mount.auto' => 1,
 
     # not used by pve
-    'lxc.tty' => 1,
+    'lxc.tty' => '\d+',
     'lxc.pts' => 1,
     'lxc.haltsignal' => 1,
     'lxc.rebootsignal' => 1,   
@@ -692,6 +693,51 @@ sub parse_ipv4_cidr {
     die "unable to parse ipv4 address/mask\n";
 }
 
+sub lxc_conf_to_pve {
+    my ($vmid, $lxc_conf) = @_;
+
+    my $properties = json_config_properties();
+
+    my $conf = { digest => $lxc_conf->{digest} };
+
+    foreach my $k (keys %$properties) {
+
+       if ($k eq 'description') {
+           if (my $raw = $lxc_conf->{'pve.comment'}) {
+               $conf->{$k} = PVE::Tools::decode_text($raw);
+           }
+       } elsif ($k eq 'hostname') {
+           $conf->{$k} = $lxc_conf->{'lxc.utsname'} if $lxc_conf->{'lxc.utsname'};
+       } elsif ($k eq 'memory') {
+           if (my $value = $lxc_conf->{'lxc.cgroup.memory.limit_in_bytes'}) {
+               $conf->{$k} = int($value / (1024*1024));
+           }
+       } elsif ($k eq 'swap') {
+           if (my $value = $lxc_conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'}) {
+               my $mem = $lxc_conf->{'lxc.cgroup.memory.limit_in_bytes'} || 0;
+               $conf->{$k} = int(($value -$mem) / (1024*1024));
+           }
+       } elsif ($k eq 'cpus') {
+           my $cfs_period_us = $lxc_conf->{'lxc.cgroup.cpu.cfs_period_us'};
+           my $cfs_quota_us = $lxc_conf->{'lxc.cgroup.cpu.cfs_quota_us'};
+           
+           if ($cfs_period_us && $cfs_quota_us) {
+               $conf->{$k} = int($cfs_quota_us/$cfs_period_us);
+           } else {
+               $conf->{$k} = 0;
+           }
+       } elsif ($k eq 'cpuunits') {
+           $conf->{$k} = $lxc_conf->{'lxc.cgroup.cpu.shares'} || 1024;
+       } elsif ($k =~ m/^net\d$/) {
+           my $net = $lxc_conf->{$k};
+           next if !$net;
+           $conf->{$k} = print_lxc_network($net);
+       }
+    }
+  
+    return $conf;
+}
+
 sub update_lxc_config {
     my ($vmid, $conf, $running, $param, $delete) = @_;
 
@@ -733,6 +779,8 @@ sub update_lxc_config {
                delete $conf->{'lxc.cgroup.cpu.cfs_period_us'};
                delete $conf->{'lxc.cgroup.cpu.cfs_quota_us'};
            }
+       } elsif ($opt eq 'cpuunits') {
+           $conf->{'lxc.cgroup.cpu.shares'} = $value;      
        } elsif ($opt eq 'description') {
            $conf->{'pve.comment'} = PVE::Tools::encode_text($value);
        } elsif ($opt =~ m/^net(\d+)$/) {