]> git.proxmox.com Git - pve-container.git/commitdiff
split PVE::API2::LXC into smaller parts
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 20 Aug 2015 11:20:06 +0000 (13:20 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 20 Aug 2015 11:20:06 +0000 (13:20 +0200)
src/PVE/API2/LXC.pm
src/PVE/API2/LXC/Config.pm [new file with mode: 0644]
src/PVE/API2/LXC/Makefile [new file with mode: 0644]
src/PVE/API2/LXC/Snapshot.pm [new file with mode: 0644]
src/PVE/API2/LXC/Status.pm [new file with mode: 0644]
src/PVE/API2/Makefile
src/PVE/LXC.pm
src/pct

index c212bbd6d668a08ee8ca95da24605f42dd55dccf..8431125b9f344b3b7c568cb89ba661aedb7921e6 100644 (file)
@@ -15,36 +15,35 @@ use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::LXC;
 use PVE::LXC::Create;
+use PVE::API2::LXC::Config;
+use PVE::API2::LXC::Status;
+use PVE::API2::LXC::Snapshot;
 use PVE::HA::Config;
 use PVE::JSONSchema qw(get_standard_option);
 use base qw(PVE::RESTHandler);
 
 use Data::Dumper; # fixme: remove
 
-my $check_ct_modify_config_perm = sub {
-    my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
-
-    return 1 if $authuser ne 'root@pam';
-
-    foreach my $opt (@$key_list) {
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::LXC::Config",
+    path => '{vmid}/config',                         
+});
 
-       if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
-       } elsif ($opt eq 'disk') {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
-       } elsif ($opt eq 'memory' || $opt eq 'swap') {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
-       } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
-                $opt eq 'searchdomain' || $opt eq 'hostname') {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
-       } else {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
-       }
-    }
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::LXC::Status",
+    path => '{vmid}/status',
+});
 
-    return 1;
-};
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::LXC::Snapshot",
+    path => '{vmid}/snapshot',
+});
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Firewall::CT",
+    path => '{vmid}/firewall',
+});
 my $alloc_rootfs = sub {
     my ($storage_conf, $storage, $disk_size_gb, $vmid) = @_;
 
@@ -93,12 +92,6 @@ my $alloc_rootfs = sub {
     return $volid;
 };
 
-PVE::JSONSchema::register_standard_option('pve-lxc-snapshot-name', {
-    description => "The name of the snapshot.",
-    type => 'string', format => 'pve-configid',
-    maxLength => 40,
-});
-
 __PACKAGE__->register_method({
     name => 'vmlist',
     path => '',
@@ -267,7 +260,7 @@ __PACKAGE__->register_method({
            raise_perm_exc();
        }
 
-       &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
+       PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
 
        PVE::Storage::activate_storage($storage_cfg, $storage);
 
@@ -350,103 +343,6 @@ __PACKAGE__->register_method({
 
     }});
 
-my $vm_config_perm_list = [
-           'VM.Config.Disk',
-           'VM.Config.CPU',
-           'VM.Config.Memory',
-           'VM.Config.Network',
-           'VM.Config.Options',
-    ];
-
-__PACKAGE__->register_method({
-    name => 'update_vm',
-    path => '{vmid}/config',
-    method => 'PUT',
-    protected => 1,
-    proxyto => 'node',
-    description => "Set container options.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => PVE::LXC::json_config_properties(
-           {
-               node => get_standard_option('pve-node'),
-               vmid => get_standard_option('pve-vmid'),
-               delete => {
-                   type => 'string', format => 'pve-configid-list',
-                   description => "A list of settings you want to delete.",
-                   optional => 1,
-               },
-               digest => {
-                   type => 'string',
-                   description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
-                   maxLength => 40,
-                   optional => 1,
-               }
-           }),
-    },
-    returns => { type => 'null'},
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $digest = extract_param($param, 'digest');
-
-       die "no options specified\n" if !scalar(keys %$param);
-
-       my $delete_str = extract_param($param, 'delete');
-       my @delete = PVE::Tools::split_list($delete_str);
-
-       &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
-
-       foreach my $opt (@delete) {
-           raise_param_exc({ delete => "you can't use '-$opt' and " .
-                                 "-delete $opt' at the same time" })
-               if defined($param->{$opt});
-
-           if (!PVE::LXC::option_exists($opt)) {
-               raise_param_exc({ delete => "unknown option '$opt'" });
-           }
-       }
-
-       &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
-
-       my $storage_cfg = cfs_read_file("storage.cfg");
-
-       my $code = sub {
-
-           my $conf = PVE::LXC::load_config($vmid);
-           PVE::LXC::check_lock($conf);
-
-           PVE::Tools::assert_if_modified($digest, $conf->{digest});
-
-           my $running = PVE::LXC::check_running($vmid);
-
-           PVE::LXC::update_pct_config($vmid, $conf, $running, $param, \@delete);
-
-           PVE::LXC::write_config($vmid, $conf);
-           PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
-       };
-
-       PVE::LXC::lock_container($vmid, undef, $code);
-
-       return undef;
-    }});
-
-__PACKAGE__->register_method ({
-    subclass => "PVE::API2::Firewall::CT",
-    path => '{vmid}/firewall',
-});
-
 __PACKAGE__->register_method({
     name => 'vmdiridx',
     path => '{vmid}',
@@ -583,43 +479,6 @@ __PACKAGE__->register_method({
            "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
     }});
 
-
-__PACKAGE__->register_method({
-    name => 'vm_config',
-    path => '{vmid}/config',
-    method => 'GET',
-    proxyto => 'node',
-    description => "Get container configuration.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
-    },
-    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 $conf = PVE::LXC::load_config($param->{vmid});
-
-       delete $conf->{snapshots};
-       delete $conf->{lxc};
-
-       return $conf;
-    }});
-
 __PACKAGE__->register_method({
     name => 'destroy_vm',
     path => '{vmid}',
@@ -867,100 +726,33 @@ __PACKAGE__->register_method ({
        return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
     }});
 
-__PACKAGE__->register_method({
-    name => 'vmcmdidx',
-    path => '{vmid}/status',
-    method => 'GET',
-    proxyto => 'node',
-    description => "Directory index",
-    permissions => {
-       user => 'all',
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-       },
-    },
-    returns => {
-       type => 'array',
-       items => {
-           type => "object",
-           properties => {
-               subdir => { type => 'string' },
-           },
-       },
-       links => [ { rel => 'child', href => "{subdir}" } ],
-    },
-    code => sub {
-       my ($param) = @_;
-
-       # test if VM exists
-       my $conf = PVE::LXC::load_config($param->{vmid});
-
-       my $res = [
-           { subdir => 'current' },
-           { subdir => 'start' },
-           { subdir => 'stop' },
-           { subdir => 'shutdown' },
-           { subdir => 'migrate' },
-           ];
-
-       return $res;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'vm_status',
-    path => '{vmid}/status/current',
-    method => 'GET',
-    proxyto => 'node',
-    protected => 1, # openvz /proc entries are only readable by root
-    description => "Get virtual machine status.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-       },
-    },
-    returns => { type => 'object' },
-    code => sub {
-       my ($param) = @_;
-
-       # test if VM exists
-       my $conf = PVE::LXC::load_config($param->{vmid});
-
-       my $vmstatus =  PVE::LXC::vmstatus($param->{vmid});
-       my $status = $vmstatus->{$param->{vmid}};
-
-       $status->{ha} = PVE::HA::Config::vm_is_ha_managed($param->{vmid}) ? 1 : 0;
-
-       return $status;
-    }});
 
 __PACKAGE__->register_method({
-    name => 'vm_start',
-    path => '{vmid}/status/start',
+    name => 'migrate_vm',
+    path => '{vmid}/migrate',
     method => 'POST',
     protected => 1,
     proxyto => 'node',
-    description => "Start the container.",
+    description => "Migrate the container to another node. Creates a new migration task.",
     permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+       check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
     },
     parameters => {
        additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid'),
+           target => get_standard_option('pve-node', { description => "Target node." }),
+           online => {
+               type => 'boolean',
+               description => "Use online/live migration.",
+               optional => 1,
+           },
        },
     },
     returns => {
        type => 'string',
+       description => "the task ID.",
     },
     code => sub {
        my ($param) = @_;
@@ -969,89 +761,27 @@ __PACKAGE__->register_method({
 
        my $authuser = $rpcenv->get_user();
 
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
-
-       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
-           my $hacmd = sub {
-               my $upid = shift;
-
-               my $service = "ct:$vmid";
-
-               my $cmd = ['ha-manager', 'enable', $service];
-
-               print "Executing HA start for CT $vmid\n";
-
-               PVE::Tools::run_command($cmd);
-
-               return;
-           };
-
-           return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
-
-       } else {
-
-           my $realcmd = sub {
-               my $upid = shift;
-
-               syslog('info', "starting CT $vmid: $upid\n");
-
-               my $conf = PVE::LXC::load_config($vmid);
+       my $target = extract_param($param, 'target');
 
-               die "you can't start a CT if it's a template\n"
-                   if PVE::LXC::is_template($conf);
+       my $localnode = PVE::INotify::nodename();
+       raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
 
-               my $storage_cfg = cfs_read_file("storage.cfg");
+       PVE::Cluster::check_cfs_quorum();
 
-               PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
+       PVE::Cluster::check_node_exists($target);
 
-               my $cmd = ['lxc-start', '-n', $vmid];
+       my $targetip = PVE::Cluster::remote_node_ip($target);
 
-               run_command($cmd);
+       my $vmid = extract_param($param, 'vmid');
 
-               return;
-           };
+       # test if VM exists
+       PVE::LXC::load_config($vmid);
 
-           return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+       # try to detect errors early
+       if (PVE::LXC::check_running($vmid)) {
+           die "cant migrate running container without --online\n"
+               if !$param->{online};
        }
-    }});
-
-__PACKAGE__->register_method({
-    name => 'vm_stop',
-    path => '{vmid}/status/stop',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Stop the container.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-       },
-    },
-    returns => {
-       type => 'string',
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
 
        if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
 
@@ -1060,647 +790,32 @@ __PACKAGE__->register_method({
 
                my $service = "ct:$vmid";
 
-               my $cmd = ['ha-manager', 'disable', $service];
+               my $cmd = ['ha-manager', 'migrate', $service, $target];
 
-               print "Executing HA stop for CT $vmid\n";
+               print "Executing HA migrate for CT $vmid to node $target\n";
 
                PVE::Tools::run_command($cmd);
 
                return;
            };
 
-           return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
+           return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
 
        } else {
 
            my $realcmd = sub {
                my $upid = shift;
 
-               syslog('info', "stoping CT $vmid: $upid\n");
-
-               my $conf = PVE::LXC::load_config($vmid);
-
-               my $storage_cfg = PVE::Storage::config();
-
-               my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
-
-               run_command($cmd);
+               # fixme: implement lxc container migration
+               die "lxc container migration not implemented\n";
 
                return;
            };
 
-           return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
+           return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
        }
     }});
 
-__PACKAGE__->register_method({
-    name => 'vm_shutdown',
-    path => '{vmid}/status/shutdown',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Shutdown the container.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           timeout => {
-               description => "Wait maximal timeout seconds.",
-               type => 'integer',
-               minimum => 0,
-               optional => 1,
-               default => 60,
-           },
-           forceStop => {
-               description => "Make sure the Container stops.",
-               type => 'boolean',
-               optional => 1,
-               default => 0,
-           }
-       },
-    },
-    returns => {
-       type => 'string',
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $timeout = extract_param($param, 'timeout');
-
-       die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
-
-       my $realcmd = sub {
-           my $upid = shift;
-
-           syslog('info', "shutdown CT $vmid: $upid\n");
-
-           my $cmd = ['lxc-stop', '-n', $vmid];
-
-           $timeout = 60 if !defined($timeout);
-
-           my $conf = PVE::LXC::load_config($vmid);
-
-           my $storage_cfg = PVE::Storage::config();
-
-           push @$cmd, '--timeout', $timeout;
-
-           eval { run_command($cmd, timeout => $timeout+5); };
-           my $err = $@;
-           if ($err && $param->{forceStop}) {
-               $err = undef;
-               warn "shutdown failed - forcing stop now\n";
-
-               push @$cmd, '--kill';
-               run_command($cmd);
-               
-           }
-
-           die $err if !$err;
-
-           return;
-       };
-
-       my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
-
-       return $upid;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'vm_suspend',
-    path => '{vmid}/status/suspend',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Suspend the container.",
-    permissions => {
-        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
-    },
-    parameters => {
-        additionalProperties => 0,
-        properties => {
-            node => get_standard_option('pve-node'),
-            vmid => get_standard_option('pve-vmid'),
-        },
-    },
-    returns => {
-        type => 'string',
-    },
-    code => sub {
-        my ($param) = @_;
-
-        my $rpcenv = PVE::RPCEnvironment::get();
-
-        my $authuser = $rpcenv->get_user();
-
-        my $node = extract_param($param, 'node');
-
-        my $vmid = extract_param($param, 'vmid');
-
-        die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
-
-        my $realcmd = sub {
-            my $upid = shift;
-
-            syslog('info', "suspend CT $vmid: $upid\n");
-
-           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
-
-           run_command($cmd);
-
-            return;
-        };
-
-        my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
-
-        return $upid;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'vm_resume',
-    path => '{vmid}/status/resume',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Resume the container.",
-    permissions => {
-        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
-    },
-    parameters => {
-        additionalProperties => 0,
-        properties => {
-            node => get_standard_option('pve-node'),
-            vmid => get_standard_option('pve-vmid'),
-        },
-    },
-    returns => {
-        type => 'string',
-    },
-    code => sub {
-        my ($param) = @_;
-
-        my $rpcenv = PVE::RPCEnvironment::get();
-
-        my $authuser = $rpcenv->get_user();
-
-        my $node = extract_param($param, 'node');
-
-        my $vmid = extract_param($param, 'vmid');
-
-        die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
-
-        my $realcmd = sub {
-            my $upid = shift;
-
-            syslog('info', "resume CT $vmid: $upid\n");
-
-           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
-                      '-D', '/var/liv/vz/dump'];
-
-           run_command($cmd);
-
-            return;
-        };
-
-        my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
-
-        return $upid;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'migrate_vm',
-    path => '{vmid}/migrate',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Migrate the container to another node. Creates a new migration task.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           target => get_standard_option('pve-node', { description => "Target node." }),
-           online => {
-               type => 'boolean',
-               description => "Use online/live migration.",
-               optional => 1,
-           },
-       },
-    },
-    returns => {
-       type => 'string',
-       description => "the task ID.",
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $target = extract_param($param, 'target');
-
-       my $localnode = PVE::INotify::nodename();
-       raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
-
-       PVE::Cluster::check_cfs_quorum();
-
-       PVE::Cluster::check_node_exists($target);
-
-       my $targetip = PVE::Cluster::remote_node_ip($target);
-
-       my $vmid = extract_param($param, 'vmid');
-
-       # test if VM exists
-       PVE::LXC::load_config($vmid);
-
-       # try to detect errors early
-       if (PVE::LXC::check_running($vmid)) {
-           die "cant migrate running container without --online\n"
-               if !$param->{online};
-       }
-
-       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
-           my $hacmd = sub {
-               my $upid = shift;
-
-               my $service = "ct:$vmid";
-
-               my $cmd = ['ha-manager', 'migrate', $service, $target];
-
-               print "Executing HA migrate for CT $vmid to node $target\n";
-
-               PVE::Tools::run_command($cmd);
-
-               return;
-           };
-
-           return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
-
-       } else {
-
-           my $realcmd = sub {
-               my $upid = shift;
-
-               # fixme: implement lxc container migration
-               die "lxc container migration not implemented\n";
-
-               return;
-           };
-
-           return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
-       }
-    }});
-
-__PACKAGE__->register_method({
-    name => 'snapshot',
-    path => '{vmid}/snapshot',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Snapshot a container.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-#          vmstate => {
-#              optional => 1,
-#              type => 'boolean',
-#              description => "Save the vmstate",
-#          },
-           description => {
-               optional => 1,
-               type => 'string',
-               description => "A textual description or comment.",
-           },
-       },
-    },
-    returns => {
-       type => 'string',
-       description => "the task ID.",
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $snapname = extract_param($param, 'snapname');
-
-       die "unable to use snapshot name 'current' (reserved name)\n"
-           if $snapname eq 'current';
-
-       my $realcmd = sub {
-           PVE::Cluster::log_msg('info', $authuser, "snapshot container $vmid: $snapname");
-           PVE::LXC::snapshot_create($vmid, $snapname, $param->{description});
-       };
-
-       return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
-    }});
-
-__PACKAGE__->register_method({
-    name => 'delsnapshot',
-    path => '{vmid}/snapshot/{snapname}',
-    method => 'DELETE',
-    protected => 1,
-    proxyto => 'node',
-    description => "Delete a LXC snapshot.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-           force => {
-               optional => 1,
-               type => 'boolean',
-               description => "For removal from config file, even if removing disk snapshots fails.",
-           },
-       },
-    },
-    returns => {
-       type => 'string',
-       description => "the task ID.",
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $snapname = extract_param($param, 'snapname');
-
-       my $realcmd = sub {
-           PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
-           PVE::LXC::snapshot_delete($vmid, $snapname, $param->{force});
-       };
-
-       return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
-    }});
-
-__PACKAGE__->register_method({
-    name => 'rollback',
-    path => '{vmid}/snapshot/{snapname}/rollback',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Rollback LXC state to specified snapshot.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-       },
-    },
-    returns => {
-       type => 'string',
-       description => "the task ID.",
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $snapname = extract_param($param, 'snapname');
-
-       my $realcmd = sub {
-           PVE::Cluster::log_msg('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
-           PVE::LXC::snapshot_rollback($vmid, $snapname);
-       };
-
-       return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
-    }});
-
-__PACKAGE__->register_method({
-    name => 'snapshot_list',
-    path => '{vmid}/snapshot',
-    method => 'GET',
-    description => "List all snapshots.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
-    },
-    proxyto => 'node',
-    protected => 1, # lxc pid files are only readable by root
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           vmid => get_standard_option('pve-vmid'),
-           node => get_standard_option('pve-node'),
-       },
-    },
-    returns => {
-       type => 'array',
-       items => {
-           type => "object",
-           properties => {},
-       },
-       links => [ { rel => 'child', href => "{name}" } ],
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $vmid = $param->{vmid};
-
-       my $conf = PVE::LXC::load_config($vmid);
-       my $snaphash = $conf->{snapshots} || {};
-
-       my $res = [];
-
-       foreach my $name (keys %$snaphash) {
-           my $d = $snaphash->{$name};
-           my $item = {
-               name => $name,
-               snaptime => $d->{snaptime} || 0,
-               description => $d->{description} || '',
-           };
-           $item->{parent} = $d->{parent} if defined($d->{parent});
-           $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
-           push @$res, $item;
-       }
-
-       my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
-       my $current = { name => 'current', digest => $conf->{digest}, running => $running };
-       $current->{parent} = $conf->{parent} if defined($conf->{parent});
-
-       push @$res, $current;
-
-       return $res;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'snapshot_cmd_idx',
-    path => '{vmid}/snapshot/{snapname}',
-    description => '',
-    method => 'GET',
-    permissions => {
-       user => 'all',
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           vmid => get_standard_option('pve-vmid'),
-           node => get_standard_option('pve-node'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-       },
-    },
-    returns => {
-       type => 'array',
-       items => {
-           type => "object",
-           properties => {},
-       },
-       links => [ { rel => 'child', href => "{cmd}" } ],
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $res = [];
-
-       push @$res, { cmd => 'rollback' };
-       push @$res, { cmd => 'config' };
-
-       return $res;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'update_snapshot_config',
-    path => '{vmid}/snapshot/{snapname}/config',
-    method => 'PUT',
-    protected => 1,
-    proxyto => 'node',
-    description => "Update snapshot metadata.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-           description => {
-               optional => 1,
-               type => 'string',
-               description => "A textual description or comment.",
-           },
-       },
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $snapname = extract_param($param, 'snapname');
-
-       return undef if !defined($param->{description});
-
-       my $updatefn =  sub {
-
-           my $conf = PVE::LXC::load_config($vmid);
-           PVE::LXC::check_lock($conf);
-
-           my $snap = $conf->{snapshots}->{$snapname};
-
-           die "snapshot '$snapname' does not exist\n" if !defined($snap);
-
-           $snap->{description} = $param->{description} if defined($param->{description});
-
-           PVE::LXC::write_config($vmid, $conf, 1);
-       };
-
-       PVE::LXC::lock_container($vmid, 10, $updatefn);
-
-       return undef;
-    }});
-
-__PACKAGE__->register_method({
-    name => 'get_snapshot_config',
-    path => '{vmid}/snapshot/{snapname}/config',
-    method => 'GET',
-    proxyto => 'node',
-    description => "Get snapshot configuration",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           node => get_standard_option('pve-node'),
-           vmid => get_standard_option('pve-vmid'),
-           snapname => get_standard_option('pve-lxc-snapshot-name'),
-       },
-    },
-    returns => { type => "object" },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $snapname = extract_param($param, 'snapname');
-
-       my $conf = PVE::LXC::load_config($vmid);
-
-       my $snap = $conf->{snapshots}->{$snapname};
-
-       die "snapshot '$snapname' does not exist\n" if !defined($snap);
-
-       delete $snap->{lxc};
-       
-       return $snap;
-    }});
-
 __PACKAGE__->register_method({
     name => 'vm_feature',
     path => '{vmid}/feature',
diff --git a/src/PVE/API2/LXC/Config.pm b/src/PVE/API2/LXC/Config.pm
new file mode 100644 (file)
index 0000000..71da37b
--- /dev/null
@@ -0,0 +1,152 @@
+package PVE::API2::LXC::Config;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param run_command);
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::INotify;
+use PVE::Cluster qw(cfs_read_file);
+use PVE::AccessControl;
+use PVE::Firewall;
+use PVE::Storage;
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::LXC;
+use PVE::LXC::Create;
+use PVE::HA::Config;
+use PVE::JSONSchema qw(get_standard_option);
+use base qw(PVE::RESTHandler);
+
+use Data::Dumper; # fixme: remove
+
+__PACKAGE__->register_method({
+    name => 'vm_config',
+    path => '',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get container configuration.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    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 $conf = PVE::LXC::load_config($param->{vmid});
+
+       delete $conf->{snapshots};
+       delete $conf->{lxc};
+
+       return $conf;
+    }});
+
+my $vm_config_perm_list = [
+           'VM.Config.Disk',
+           'VM.Config.CPU',
+           'VM.Config.Memory',
+           'VM.Config.Network',
+           'VM.Config.Options',
+    ];
+
+__PACKAGE__->register_method({
+    name => 'update_vm',
+    path => '',
+    method => 'PUT',
+    protected => 1,
+    proxyto => 'node',
+    description => "Set container options.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => PVE::LXC::json_config_properties(
+           {
+               node => get_standard_option('pve-node'),
+               vmid => get_standard_option('pve-vmid'),
+               delete => {
+                   type => 'string', format => 'pve-configid-list',
+                   description => "A list of settings you want to delete.",
+                   optional => 1,
+               },
+               digest => {
+                   type => 'string',
+                   description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+                   maxLength => 40,
+                   optional => 1,
+               }
+           }),
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $digest = extract_param($param, 'digest');
+
+       die "no options specified\n" if !scalar(keys %$param);
+
+       my $delete_str = extract_param($param, 'delete');
+       my @delete = PVE::Tools::split_list($delete_str);
+
+       PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
+
+       foreach my $opt (@delete) {
+           raise_param_exc({ delete => "you can't use '-$opt' and " .
+                                 "-delete $opt' at the same time" })
+               if defined($param->{$opt});
+
+           if (!PVE::LXC::option_exists($opt)) {
+               raise_param_exc({ delete => "unknown option '$opt'" });
+           }
+       }
+
+       PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
+
+       my $storage_cfg = cfs_read_file("storage.cfg");
+
+       my $code = sub {
+
+           my $conf = PVE::LXC::load_config($vmid);
+           PVE::LXC::check_lock($conf);
+
+           PVE::Tools::assert_if_modified($digest, $conf->{digest});
+
+           my $running = PVE::LXC::check_running($vmid);
+
+           PVE::LXC::update_pct_config($vmid, $conf, $running, $param, \@delete);
+
+           PVE::LXC::write_config($vmid, $conf);
+           PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
+       };
+
+       PVE::LXC::lock_container($vmid, undef, $code);
+
+       return undef;
+    }});
+
+1;
diff --git a/src/PVE/API2/LXC/Makefile b/src/PVE/API2/LXC/Makefile
new file mode 100644 (file)
index 0000000..f372d95
--- /dev/null
@@ -0,0 +1,8 @@
+SOURCES=Config.pm Status.pm Snapshot.pm
+
+.PHONY: install
+install:
+       install -d -m 0755 ${PERLDIR}/PVE/API2/LXC
+       for i in ${SOURCES}; do install -D -m 0644 $$i ${PERLDIR}/PVE/API2/LXC/$$i; done
+
+
diff --git a/src/PVE/API2/LXC/Snapshot.pm b/src/PVE/API2/LXC/Snapshot.pm
new file mode 100644 (file)
index 0000000..7ed6614
--- /dev/null
@@ -0,0 +1,357 @@
+package PVE::API2::LXC::Snapshot;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param run_command);
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::INotify;
+use PVE::Cluster qw(cfs_read_file);
+use PVE::AccessControl;
+use PVE::Firewall;
+use PVE::Storage;
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::LXC;
+use PVE::LXC::Create;
+use PVE::HA::Config;
+use PVE::JSONSchema qw(get_standard_option);
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method({
+    name => 'snapshot_list',
+    path => '',
+    method => 'GET',
+    description => "List all snapshots.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    proxyto => 'node',
+    protected => 1, # lxc pid files are only readable by root
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid'),
+           node => get_standard_option('pve-node'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {},
+       },
+       links => [ { rel => 'child', href => "{name}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $vmid = $param->{vmid};
+
+       my $conf = PVE::LXC::load_config($vmid);
+       my $snaphash = $conf->{snapshots} || {};
+
+       my $res = [];
+
+       foreach my $name (keys %$snaphash) {
+           my $d = $snaphash->{$name};
+           my $item = {
+               name => $name,
+               snaptime => $d->{snaptime} || 0,
+               description => $d->{description} || '',
+           };
+           $item->{parent} = $d->{parent} if defined($d->{parent});
+           $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
+           push @$res, $item;
+       }
+
+       my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
+       my $current = { name => 'current', digest => $conf->{digest}, running => $running };
+       $current->{parent} = $conf->{parent} if defined($conf->{parent});
+
+       push @$res, $current;
+
+       return $res;
+    }});
+
+use Data::Dumper; # fixme: remove
+__PACKAGE__->register_method({
+    name => 'snapshot',
+    path => '',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Snapshot a container.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+#          vmstate => {
+#              optional => 1,
+#              type => 'boolean',
+#              description => "Save the vmstate",
+#          },
+           description => {
+               optional => 1,
+               type => 'string',
+               description => "A textual description or comment.",
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+       description => "the task ID.",
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $snapname = extract_param($param, 'snapname');
+
+       die "unable to use snapshot name 'current' (reserved name)\n"
+           if $snapname eq 'current';
+
+       my $realcmd = sub {
+           PVE::Cluster::log_msg('info', $authuser, "snapshot container $vmid: $snapname");
+           PVE::LXC::snapshot_create($vmid, $snapname, $param->{description});
+       };
+
+       return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
+    }});
+
+__PACKAGE__->register_method({
+    name => 'delsnapshot',
+    path => '{snapname}',
+    method => 'DELETE',
+    protected => 1,
+    proxyto => 'node',
+    description => "Delete a LXC snapshot.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+           force => {
+               optional => 1,
+               type => 'boolean',
+               description => "For removal from config file, even if removing disk snapshots fails.",
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+       description => "the task ID.",
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $snapname = extract_param($param, 'snapname');
+
+       my $realcmd = sub {
+           PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
+           PVE::LXC::snapshot_delete($vmid, $snapname, $param->{force});
+       };
+
+       return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
+    }});
+
+__PACKAGE__->register_method({
+    name => 'snapshot_cmd_idx',
+    path => '{snapname}',
+    description => '',
+    method => 'GET',
+    permissions => {
+       user => 'all',
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid'),
+           node => get_standard_option('pve-node'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {},
+       },
+       links => [ { rel => 'child', href => "{cmd}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $res = [];
+
+       push @$res, { cmd => 'rollback' };
+       push @$res, { cmd => 'config' };
+
+       return $res;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'rollback',
+    path => '{snapname}/rollback',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Rollback LXC state to specified snapshot.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+       },
+    },
+    returns => {
+       type => 'string',
+       description => "the task ID.",
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $snapname = extract_param($param, 'snapname');
+
+       my $realcmd = sub {
+           PVE::Cluster::log_msg('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
+           PVE::LXC::snapshot_rollback($vmid, $snapname);
+       };
+
+       return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
+    }});
+
+__PACKAGE__->register_method({
+    name => 'update_snapshot_config',
+    path => '{snapname}/config',
+    method => 'PUT',
+    protected => 1,
+    proxyto => 'node',
+    description => "Update snapshot metadata.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+           description => {
+               optional => 1,
+               type => 'string',
+               description => "A textual description or comment.",
+           },
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $snapname = extract_param($param, 'snapname');
+
+       return undef if !defined($param->{description});
+
+       my $updatefn =  sub {
+
+           my $conf = PVE::LXC::load_config($vmid);
+           PVE::LXC::check_lock($conf);
+
+           my $snap = $conf->{snapshots}->{$snapname};
+
+           die "snapshot '$snapname' does not exist\n" if !defined($snap);
+
+           $snap->{description} = $param->{description} if defined($param->{description});
+
+           PVE::LXC::write_config($vmid, $conf, 1);
+       };
+
+       PVE::LXC::lock_container($vmid, 10, $updatefn);
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'get_snapshot_config',
+    path => '{snapname}/config',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get snapshot configuration",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           snapname => get_standard_option('pve-lxc-snapshot-name'),
+       },
+    },
+    returns => { type => "object" },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $snapname = extract_param($param, 'snapname');
+
+       my $conf = PVE::LXC::load_config($vmid);
+
+       my $snap = $conf->{snapshots}->{$snapname};
+
+       die "snapshot '$snapname' does not exist\n" if !defined($snap);
+
+       delete $snap->{lxc};
+       
+       return $snap;
+    }});
+
+1;
diff --git a/src/PVE/API2/LXC/Status.pm b/src/PVE/API2/LXC/Status.pm
new file mode 100644 (file)
index 0000000..bfd851f
--- /dev/null
@@ -0,0 +1,435 @@
+package PVE::API2::LXC::Status;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param run_command);
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::INotify;
+use PVE::Cluster qw(cfs_read_file);
+use PVE::AccessControl;
+use PVE::Firewall;
+use PVE::Storage;
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::LXC;
+use PVE::LXC::Create;
+use PVE::HA::Config;
+use PVE::JSONSchema qw(get_standard_option);
+use base qw(PVE::RESTHandler);
+
+use Data::Dumper; # fixme: remove
+
+__PACKAGE__->register_method({
+    name => 'vmcmdidx',
+    path => '',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Directory index",
+    permissions => {
+       user => 'all',
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {
+               subdir => { type => 'string' },
+           },
+       },
+       links => [ { rel => 'child', href => "{subdir}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       # test if VM exists
+       my $conf = PVE::LXC::load_config($param->{vmid});
+
+       my $res = [
+           { subdir => 'current' },
+           { subdir => 'start' },
+           { subdir => 'stop' },
+           { subdir => 'shutdown' },
+           { subdir => 'migrate' },
+           ];
+
+       return $res;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_status',
+    path => 'current',
+    method => 'GET',
+    proxyto => 'node',
+    protected => 1, # openvz /proc entries are only readable by root
+    description => "Get virtual machine status.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => { type => 'object' },
+    code => sub {
+       my ($param) = @_;
+
+       # test if VM exists
+       my $conf = PVE::LXC::load_config($param->{vmid});
+
+       my $vmstatus =  PVE::LXC::vmstatus($param->{vmid});
+       my $status = $vmstatus->{$param->{vmid}};
+
+       $status->{ha} = PVE::HA::Config::vm_is_ha_managed($param->{vmid}) ? 1 : 0;
+
+       return $status;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_start',
+    path => 'start',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Start the container.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
+
+       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+
+           my $hacmd = sub {
+               my $upid = shift;
+
+               my $service = "ct:$vmid";
+
+               my $cmd = ['ha-manager', 'enable', $service];
+
+               print "Executing HA start for CT $vmid\n";
+
+               PVE::Tools::run_command($cmd);
+
+               return;
+           };
+
+           return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
+
+       } else {
+
+           my $realcmd = sub {
+               my $upid = shift;
+
+               syslog('info', "starting CT $vmid: $upid\n");
+
+               my $conf = PVE::LXC::load_config($vmid);
+
+               die "you can't start a CT if it's a template\n"
+                   if PVE::LXC::is_template($conf);
+
+               my $storage_cfg = cfs_read_file("storage.cfg");
+
+               PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
+
+               my $cmd = ['lxc-start', '-n', $vmid];
+
+               run_command($cmd);
+
+               return;
+           };
+
+           return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+       }
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_stop',
+    path => 'stop',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Stop the container.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+
+           my $hacmd = sub {
+               my $upid = shift;
+
+               my $service = "ct:$vmid";
+
+               my $cmd = ['ha-manager', 'disable', $service];
+
+               print "Executing HA stop for CT $vmid\n";
+
+               PVE::Tools::run_command($cmd);
+
+               return;
+           };
+
+           return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
+
+       } else {
+
+           my $realcmd = sub {
+               my $upid = shift;
+
+               syslog('info', "stoping CT $vmid: $upid\n");
+
+               my $conf = PVE::LXC::load_config($vmid);
+
+               my $storage_cfg = PVE::Storage::config();
+
+               my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+
+               run_command($cmd);
+
+               return;
+           };
+
+           return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
+       }
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_shutdown',
+    path => 'shutdown',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Shutdown the container.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           timeout => {
+               description => "Wait maximal timeout seconds.",
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+               default => 60,
+           },
+           forceStop => {
+               description => "Make sure the Container stops.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           }
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $timeout = extract_param($param, 'timeout');
+
+       die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+       my $realcmd = sub {
+           my $upid = shift;
+
+           syslog('info', "shutdown CT $vmid: $upid\n");
+
+           my $cmd = ['lxc-stop', '-n', $vmid];
+
+           $timeout = 60 if !defined($timeout);
+
+           my $conf = PVE::LXC::load_config($vmid);
+
+           my $storage_cfg = PVE::Storage::config();
+
+           push @$cmd, '--timeout', $timeout;
+
+           eval { run_command($cmd, timeout => $timeout+5); };
+           my $err = $@;
+           if ($err && $param->{forceStop}) {
+               $err = undef;
+               warn "shutdown failed - forcing stop now\n";
+
+               push @$cmd, '--kill';
+               run_command($cmd);
+               
+           }
+
+           die $err if !$err;
+
+           return;
+       };
+
+       my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
+
+       return $upid;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_suspend',
+    path => 'suspend',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Suspend the container.",
+    permissions => {
+        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            node => get_standard_option('pve-node'),
+            vmid => get_standard_option('pve-vmid'),
+        },
+    },
+    returns => {
+        type => 'string',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $rpcenv = PVE::RPCEnvironment::get();
+
+        my $authuser = $rpcenv->get_user();
+
+        my $node = extract_param($param, 'node');
+
+        my $vmid = extract_param($param, 'vmid');
+
+        die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+        my $realcmd = sub {
+            my $upid = shift;
+
+            syslog('info', "suspend CT $vmid: $upid\n");
+
+           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
+
+           run_command($cmd);
+
+            return;
+        };
+
+        my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
+
+        return $upid;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vm_resume',
+    path => 'resume',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Resume the container.",
+    permissions => {
+        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            node => get_standard_option('pve-node'),
+            vmid => get_standard_option('pve-vmid'),
+        },
+    },
+    returns => {
+        type => 'string',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $rpcenv = PVE::RPCEnvironment::get();
+
+        my $authuser = $rpcenv->get_user();
+
+        my $node = extract_param($param, 'node');
+
+        my $vmid = extract_param($param, 'vmid');
+
+        die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
+
+        my $realcmd = sub {
+            my $upid = shift;
+
+            syslog('info', "resume CT $vmid: $upid\n");
+
+           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
+                      '-D', '/var/liv/vz/dump'];
+
+           run_command($cmd);
+
+            return;
+        };
+
+        my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
+
+        return $upid;
+    }});
+
+1;
index 5fe8e5bd3d80042139abf6643bbdcf9a515134cf..98e44bf316c30b17b1247c651426f0a67a587cf4 100644 (file)
@@ -4,3 +4,5 @@ SOURCES=LXC.pm
 install:
        install -d -m 0755 ${PERLDIR}/PVE/API2
        for i in ${SOURCES}; do install -D -m 0644 $$i ${PERLDIR}/PVE/API2/$$i; done
+       make -C LXC install
+
index 8ed2bdbe6c451bb2dd029fa2dbe1c7cce43bb3a9..3ba8d51b58c62c41f18062f56dca4cf683f51f80 100644 (file)
@@ -14,6 +14,7 @@ use PVE::INotify;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Tools qw($IPV6RE $IPV4RE dir_glob_foreach);
 use PVE::Network;
+use PVE::AccessControl;
 
 use Data::Dumper;
 
@@ -50,6 +51,12 @@ PVE::JSONSchema::register_standard_option('pve-ct-rootfs', {
     optional => 1,
 });
 
+PVE::JSONSchema::register_standard_option('pve-lxc-snapshot-name', {
+    description => "The name of the snapshot.",
+    type => 'string', format => 'pve-configid',
+    maxLength => 40,
+});
+
 my $confdesc = {
     lock => {
        optional => 1,
@@ -1836,4 +1843,28 @@ sub find_loopdev {
     }
 }
 
+sub check_ct_modify_config_perm {
+    my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
+
+    return 1 if $authuser ne 'root@pam';
+
+    foreach my $opt (@$key_list) {
+
+       if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
+       } elsif ($opt eq 'disk') {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
+       } elsif ($opt eq 'memory' || $opt eq 'swap') {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
+       } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
+                $opt eq 'searchdomain' || $opt eq 'hostname') {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
+       } else {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
+       }
+    }
+
+    return 1;
+}
+
 1;
diff --git a/src/pct b/src/pct
index 1d67efd84f55f4717a3564056a8cf816bbda753d..0cacb3e06e003a71ccbe0cb0d18bc33a55778549 100755 (executable)
--- a/src/pct
+++ b/src/pct
@@ -12,6 +12,9 @@ use PVE::RPCEnvironment;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::CLIHandler;
 use PVE::API2::LXC;
+use PVE::API2::LXC::Config;
+use PVE::API2::LXC::Status;
+use PVE::API2::LXC::Snapshot;
 
 use Data::Dumper;
 
@@ -94,7 +97,7 @@ my $cmddef = {
            printf($format, $d->{vmid}, $d->{status}, $d->{name});
        }
     }],
-    config => [ "PVE::API2::LXC", 'vm_config', ['vmid'], 
+    config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], 
                { node => $nodename }, sub {
                    my $config = shift;
                    foreach my $k (sort (keys %$config)) {
@@ -106,16 +109,17 @@ my $cmddef = {
                        print "$k: $v\n";
                    }
                }],
-    set => [ 'PVE::API2::LXC', 'update_vm', ['vmid'], { node => $nodename }],
+    set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
     
     create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
     restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
 
-    start => [ 'PVE::API2::LXC', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
-    suspend => [ 'PVE::API2::LXC', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
-    resume => [ 'PVE::API2::LXC', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
-    shutdown => [ 'PVE::API2::LXC', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
-    stop => [ 'PVE::API2::LXC', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
+    start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
+    suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
+    resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
+    shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
+    stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
+    
     migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
     
     console => [ __PACKAGE__, 'console', ['vmid']],
@@ -124,12 +128,12 @@ my $cmddef = {
     destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], 
                 { node => $nodename }, $upid_exit ],
 
-    snapshot => [ "PVE::API2::LXC", 'snapshot', ['vmid', 'snapname'],
+    snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
                  { node => $nodename } , $upid_exit ],
 
-    delsnapshot => [ "PVE::API2::LXC", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+    delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
 
-    rollback => [ "PVE::API2::LXC", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+    rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
 
     template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
 };