Commit c6a69e0b authored by Dietmar Maurer's avatar Dietmar Maurer

implemented vm_config for openvz

parent 3e86d7f6
......@@ -3,6 +3,7 @@ package PVE::API2::OpenVZ;
use strict;
use warnings;
use File::Basename;
use POSIX qw (LONG_MAX);
use PVE::SafeSyslog;
use PVE::Tools qw(extract_param);
......@@ -53,12 +54,6 @@ CPUUNITS="1000"
CPUS="1"
__EOD
my $get_config_path = sub {
my $vmid = shift;
return "/etc/pve/openvz/${vmid}.conf";
};
__PACKAGE__->register_method({
name => 'vmlist',
path => '',
......@@ -131,7 +126,7 @@ __PACKAGE__->register_method({
my $code = sub {
my $basecfg_fn = &$get_config_path($vmid);
my $basecfg_fn = PVE::OpenVZ::get_config_path($vmid);
die "container $vmid already exists\n" if -f $basecfg_fn;
......@@ -209,12 +204,7 @@ __PACKAGE__->register_method({
my $code = sub {
my $basecfg_fn = &$get_config_path($vmid);
my $basecfg = PVE::Tools::file_get_contents($basecfg_fn);
die "container $vmid does not exists\n" if !$basecfg;
my $conf = PVE::OpenVZ::parse_ovz_config($basecfg_fn, $basecfg);
my $conf = PVE::OpenVZ::load_config($vmid);
die "checksum missmatch (file change by other user?)\n"
if $digest && $digest ne $conf->{digest};
......@@ -232,6 +222,58 @@ __PACKAGE__->register_method({
return undef;
}});
__PACKAGE__->register_method({
name => 'vm_config',
path => '{vmid}/config',
method => 'GET',
proxyto => 'node',
description => "Get virtual machine configuration.",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
vmid => get_standard_option('pve-vmid'),
},
},
returns => {
type => "object",
properties => {
digest => {
type => 'string',
description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
}
},
},
code => sub {
my ($param) = @_;
my $veconf = PVE::OpenVZ::load_config($param->{vmid});
# we only return selected/converted values
my $conf = { digest => $veconf->{digest} };
my $properties = PVE::OpenVZ::json_config_properties();
foreach my $k (keys %$properties) {
next if $k eq 'memory';
next if $k eq 'swap';
next if $k eq 'disk';
$conf->{$k} = $veconf->{$k}->{value} if $veconf->{$k} && defined($veconf->{$k}->{value});
}
$conf->{memory} = $veconf->{physpages}->{lim} ?
int($veconf->{physpages}->{lim} * 4096) : 512*1024*1024;
$conf->{swap} = $veconf->{swappages}->{lim} ?
int($veconf->{swappages}->{lim} * 4096) : 0;
my $diskspace = $veconf->{diskspace}->{bar} || LONG_MAX;
if ($diskspace == LONG_MAX) {
$conf->{disk} = 0;
} else {
$conf->{disk} = int($diskspace*1024);
}
return $conf;
}});
__PACKAGE__->register_method({
name => 'destroy_vm',
path => '{vmid}',
......
......@@ -39,6 +39,24 @@ sub cfs_config_path {
return "nodes/$node/openvz/$vmid.conf";
}
sub get_config_path {
my $vmid = shift;
return "/etc/pve/openvz/${vmid}.conf";
};
sub load_config {
my ($vmid) = @_;
my $basecfg_fn = get_config_path($vmid);
my $basecfg = PVE::Tools::file_get_contents($basecfg_fn);
die "container $vmid does not exists\n" if !$basecfg;
my $conf = PVE::OpenVZ::parse_ovz_config($basecfg_fn, $basecfg);
return wantarray ? ($conf, $basecfg) : $conf;
}
my $last_proc_vestat = {};
sub vmstatus {
......@@ -214,8 +232,8 @@ my $confdesc = {
disk => {
optional => 1,
type => 'number',
description => "Amount of disk space for the VM in GB.",
minimum => 0.5,
description => "Amount of disk space for the VM in GB. A zero indicates no limits.",
minimum => 0,
default => 2,
},
quotatime => {
......@@ -696,11 +714,12 @@ sub update_ovz_config {
}
};
my $mem = int (($veconf->{physpages}->{lim} * 4) / 1024) || 512;
my $swap = int (($veconf->{swappages}->{lim} * 4) / 1024) || 0;
my $mem = $veconf->{physpages}->{lim} ?
int (($veconf->{physpages}->{lim} * 4) / 1024) : 512;
my $swap = $veconf->{swappages}->{lim} ?
int (($veconf->{swappages}->{lim} * 4) / 1024) : 0;
my $disk = ($veconf->{diskspace}->{bar} / (1024*1024)) || $res_unlimited;
my $disk = ($veconf->{diskspace}->{bar} || $res_unlimited) / (1024*1024);
my $cpuunits = $veconf->{cpuunits}->{value} || 1000;
my $quotatime = $veconf->{quotatime}->{value} || 0;
my $quotaugidlimit = $veconf->{quotaugidlimit}->{value} || 0;
......@@ -758,7 +777,7 @@ sub update_ovz_config {
# disk quota parameters
if (($disk * 1.1) >= ($res_unlimited / (1024 * 1024))) {
if (!$disk || ($disk * 1.1) >= ($res_unlimited / (1024 * 1024))) {
&$push_bl_changes('diskspace', $res_unlimited, $res_unlimited);
&$push_bl_changes('diskinodes', $res_unlimited, $res_unlimited);
} else {
......
......@@ -37,7 +37,7 @@ my $cmddef = {
exit 0 if (!scalar(@$vmlist));
printf "%10s %-20s %-10s %-10s %12s %-10s\n",
printf "%10s %-20s %-10s %-10s %-12s %-10s\n",
qw(VMID NAME STATUS MEM(MB) DISK(GB));
foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
......@@ -53,6 +53,18 @@ my $cmddef = {
set => [ "PVE::API2::OpenVZ", 'update_vm', ['vmid'], { node => $nodename } ],
config => [ "PVE::API2::OpenVZ", 'vm_config', ['vmid'],
{ node => $nodename }, sub {
my $config = shift;
foreach my $k (sort (keys %$config)) {
next if $k eq 'digest';
my $v = $config->{$k};
if ($k eq 'description') {
$v = PVE::Tools::encode_text($v);
}
print "$k: $v\n";
}
}],
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment