]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC.pm
implement restore command
[pve-container.git] / src / PVE / API2 / LXC.pm
index 121173bb12386e5abecc83e7246b82b1f83d4a13..e22843c1d05bde27bea90581e499af5a4b39aaf1 100644 (file)
@@ -13,6 +13,8 @@ use PVE::Storage;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::LXC;
+use PVE::LXCCreate;
+use PVE::HA::Config;
 use PVE::JSONSchema qw(get_standard_option);
 use base qw(PVE::RESTHandler);
 
@@ -29,7 +31,7 @@ my $get_container_storage = sub {
 
 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) {
@@ -40,7 +42,7 @@ my $check_ct_modify_config_perm = sub {
            $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' || 
+       } 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 {
@@ -53,8 +55,8 @@ my $check_ct_modify_config_perm = sub {
 
 
 __PACKAGE__->register_method({
-    name => 'vmlist', 
-    path => '', 
+    name => 'vmlist',
+    path => '',
     method => 'GET',
     description => "LXC container index (per node).",
     permissions => {
@@ -95,12 +97,12 @@ __PACKAGE__->register_method({
        }
 
        return $res;
-  
+
     }});
 
 __PACKAGE__->register_method({
-    name => 'create_vm', 
-    path => '', 
+    name => 'create_vm',
+    path => '',
     method => 'POST',
     description => "Create or restore a container.",
     permissions => {
@@ -118,13 +120,14 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
            ostemplate => {
                description => "The OS template or backup file.",
-               type => 'string', 
+               type => 'string',
                maxLength => 255,
            },
-           password => { 
-               optional => 1, 
+           password => {
+               optional => 1,
                type => 'string',
                description => "Sets root password inside container.",
+               minLength => 5,
            },
            storage => get_standard_option('pve-storage-id', {
                description => "Target storage.",
@@ -132,23 +135,23 @@ __PACKAGE__->register_method({
                optional => 1,
            }),
            force => {
-               optional => 1, 
+               optional => 1,
                type => 'boolean',
                description => "Allow to overwrite existing container.",
            },
            restore => {
-               optional => 1, 
+               optional => 1,
                type => 'boolean',
                description => "Mark this as restore task.",
            },
-           pool => { 
+           pool => {
                optional => 1,
                type => 'string', format => 'pve-poolid',
                description => "Add the VM to the specified pool.",
            },
        }),
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -168,18 +171,23 @@ __PACKAGE__->register_method({
 
        my $restore = extract_param($param, 'restore');
 
+       if ($restore) {
+           # fixme: limit allowed parameters
+
+       }
+       
        my $force = extract_param($param, 'force');
 
        if (!($same_container_exists && $restore && $force)) {
            PVE::Cluster::check_vmid_unused($vmid);
        }
-       
+
        my $password = extract_param($param, 'password');
 
        my $storage = extract_param($param, 'storage') || 'local';
 
        my $pool = extract_param($param, 'pool');
-       
+
        my $storage_cfg = cfs_read_file("storage.cfg");
 
        my $scfg = PVE::Storage::storage_check_node($storage_cfg, $storage, $node);
@@ -192,7 +200,7 @@ __PACKAGE__->register_method({
        if (defined($pool)) {
            $rpcenv->check_pool_exist($pool);
            $rpcenv->check_perm_modify($authuser, "/pool/$pool");
-       } 
+       }
 
        if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
            # OK
@@ -210,56 +218,81 @@ __PACKAGE__->register_method({
        PVE::Storage::activate_storage($storage_cfg, $storage);
 
        my $ostemplate = extract_param($param, 'ostemplate');
-       
+
        my $archive;
 
        if ($ostemplate eq '-') {
-           die "archive pipe not implemented\n" 
-           # $archive = '-';
+           die "pipe requires cli environment\n" 
+               if $rpcenv->{type} ne 'cli'; 
+           die "pipe can only be used with restore tasks\n" 
+               if !$restore;
+           $archive = '-';
        } else {
            $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
            $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
        }
 
-       my $memory = $param->{memory} || 512;
-       my $hostname = $param->{hostname} || "T$vmid";
        my $conf = {};
-       
-       $conf->{'lxc.utsname'} = $param->{hostname} || "CT$vmid";
-       $conf->{'lxc.cgroup.memory.limit_in_bytes'} = "${memory}M";
 
-       my $code = sub {
-           my $temp_conf_fn = PVE::LXC::write_temp_config($vmid, $conf);
+       $param->{hostname} ||= "CT$vmid";
+       $param->{memory} ||= 512;
+       $param->{swap} = 512 if !defined($param->{swap});
 
-           my $cmd = ['lxc-create', '-f', $temp_conf_fn, '-t', 'pve', '-n', $vmid,
-                      '--', '--archive', $archive];
+       PVE::LXC::update_lxc_config($vmid, $conf, 0, $param);
 
-           eval { PVE::Tools::run_command($cmd); };
-           my $err = $@;
+       # assigng default names, so that we can configure network with LXCSetup
+       foreach my $k (keys %$conf) {
+           next if $k !~ m/^net(\d+)$/;
+           my $d = $conf->{$k};
+           my $ind = $1;
+           $d->{name} = "eth$ind"; # fixme: do not overwrite settings!
+       }
+
+       $conf->{'lxc.hook.mount'} = "/usr/share/lxc/hooks/lxc-pve-mount-hook";
 
-           unlink $temp_conf_fn;
+       # use user namespace ?
+       # disable for now, because kernel 3.10.0 does not support it
+       #$conf->{'lxc.id_map'} = ["u 0 100000 65536", "g 0 100000 65536"];
 
-           die $err if $err;
+       my $check_vmid_usage = sub {
+           if ($force) {
+               die "cant overwrite running container\n"
+                   if PVE::LXC::check_running($vmid);
+           } else {
+               PVE::Cluster::check_vmid_unused($vmid);
+           }
        };
        
+       my $code = sub {
+           
+           &$check_vmid_usage(); # final check after locking
+
+           PVE::Cluster::check_cfs_quorum();
+
+           PVE::LXCCreate::create_rootfs($storage_cfg, $storage, $param->{disk}, $vmid, $conf, 
+                                         $archive, $password, $restore);
+       };
+
        my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
 
-       return $rpcenv->fork_worker($param->{restore} ? 'vzrestore' : 'vzcreate', 
+       &$check_vmid_usage(); # first check before locking
+
+       return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
                                    $vmid, $authuser, $realcmd);
-           
+
     }});
 
 my $vm_config_perm_list = [
-           'VM.Config.Disk', 
-           'VM.Config.CPU', 
-           'VM.Config.Memory', 
-           'VM.Config.Network', 
+           'VM.Config.Disk',
+           'VM.Config.CPU',
+           'VM.Config.Memory',
+           'VM.Config.Network',
            'VM.Config.Options',
     ];
 
 __PACKAGE__->register_method({
-    name => 'update_vm', 
-    path => '{vmid}/config', 
+    name => 'update_vm',
+    path => '{vmid}/config',
     method => 'PUT',
     protected => 1,
     proxyto => 'node',
@@ -282,7 +315,7 @@ __PACKAGE__->register_method({
                    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,                  
+                   optional => 1,
                }
            }),
     },
@@ -304,14 +337,14 @@ __PACKAGE__->register_method({
 
        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'" });
            }
@@ -325,31 +358,9 @@ __PACKAGE__->register_method({
 
            PVE::Tools::assert_if_modified($digest, $conf->{digest});
 
-           # die if running
+           my $running = PVE::LXC::check_running($vmid);
 
-           foreach my $opt (@delete) {
-               if ($opt eq 'hostname') {
-                   die "unable to delete required option '$opt'\n";
-               } elsif ($opt =~ m/^net\d$/) {
-                   delete $conf->{$opt};
-               } else {
-                   die "implement me"
-               }
-           }
-
-           foreach my $opt (keys %$param) {
-               my $value = $param->{$opt};
-               if ($opt eq 'hostname') {
-                   $conf->{'lxc.utsname'} = $value;
-               } if ($opt =~ m/^net(\d+)$/) {
-                   my $netid = $1;
-                   my $net = PVE::LXC::parse_lxc_network($value);
-                   $net->{'veth.pair'} = "veth${vmid}.$netid";
-                   $conf->{$opt} = $net;
-               } else {
-                   die "implement me"
-               }
-           }
+           PVE::LXC::update_lxc_config($vmid, $conf, $running, $param, \@delete);
 
            PVE::LXC::write_config($vmid, $conf);
        };
@@ -360,13 +371,13 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Firewall::CT",  
+    subclass => "PVE::API2::Firewall::CT",
     path => '{vmid}/firewall',
 });
 
 __PACKAGE__->register_method({
     name => 'vmdiridx',
-    path => '{vmid}', 
+    path => '{vmid}',
     method => 'GET',
     proxyto => 'node',
     description => "Directory index",
@@ -394,26 +405,27 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        # test if VM exists
-       my $conf = PVE::OpenVZ::load_config($param->{vmid});
+       my $conf = PVE::LXC::load_config($param->{vmid});
 
        my $res = [
            { subdir => 'config' },
-#          { subdir => 'status' },
-#          { subdir => 'vncproxy' },
-#          { subdir => 'spiceproxy' },
-#          { subdir => 'migrate' },
+           { subdir => 'status' },
+           { subdir => 'vncproxy' },
+           { subdir => 'vncwebsocket' },
+           { subdir => 'spiceproxy' },
+           { subdir => 'migrate' },
 #          { subdir => 'initlog' },
            { subdir => 'rrd' },
            { subdir => 'rrddata' },
            { subdir => 'firewall' },
            ];
-       
+
        return $res;
     }});
 
 __PACKAGE__->register_method({
-    name => 'rrd', 
-    path => '{vmid}/rrd', 
+    name => 'rrd',
+    path => '{vmid}/rrd',
     method => 'GET',
     protected => 1, # fixme: can we avoid that?
     permissions => {
@@ -452,14 +464,14 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        return PVE::Cluster::create_rrd_graph(
-           "pve2-vm/$param->{vmid}", $param->{timeframe}, 
+           "pve2-vm/$param->{vmid}", $param->{timeframe},
            $param->{ds}, $param->{cf});
-                                             
+
     }});
 
 __PACKAGE__->register_method({
-    name => 'rrddata', 
-    path => '{vmid}/rrddata', 
+    name => 'rrddata',
+    path => '{vmid}/rrddata',
     method => 'GET',
     protected => 1, # fixme: can we avoid that?
     permissions => {
@@ -500,8 +512,8 @@ __PACKAGE__->register_method({
 
 
 __PACKAGE__->register_method({
-    name => 'vm_config', 
-    path => '{vmid}/config', 
+    name => 'vm_config',
+    path => '{vmid}/config',
     method => 'GET',
     proxyto => 'node',
     description => "Get container configuration.",
@@ -515,7 +527,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        type => "object",
        properties => {
            digest => {
@@ -530,37 +542,20 @@ __PACKAGE__->register_method({
        my $lxc_conf = PVE::LXC::load_config($param->{vmid});
 
        # 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 =~ m/^net\d$/) {
-               my $net = $lxc_conf->{$k};
-               next if !$net;
-               $conf->{$k} = PVE::LXC::print_lxc_network($net);
-           }
-       }
-
        return $conf;
     }});
 
 __PACKAGE__->register_method({
-    name => 'destroy_vm', 
-    path => '{vmid}', 
+    name => 'destroy_vm',
+    path => '{vmid}',
     method => 'DELETE',
     protected => 1,
     proxyto => 'node',
@@ -575,7 +570,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -590,15 +585,695 @@ __PACKAGE__->register_method({
        # test if container exists
        my $conf = PVE::LXC::load_config($param->{vmid});
 
+       my $storage_cfg = cfs_read_file("storage.cfg");
+
+       my $code = sub {
+           PVE::LXC::destory_lxc_container($storage_cfg, $vmid, $conf);
+           PVE::AccessControl::remove_vm_from_pool($vmid);
+       };
+
+       my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
+       
+       return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
+    }});
+
+my $sslcert;
+
+__PACKAGE__->register_method ({
+    name => 'vncproxy',
+    path => '{vmid}/vncproxy',
+    method => 'POST',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
+    },
+    description => "Creates a TCP VNC proxy connections.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           websocket => {
+               optional => 1,
+               type => 'boolean',
+               description => "use websocket instead of standard VNC.",
+           },
+       },
+    },
+    returns => {
+       additionalProperties => 0,
+       properties => {
+           user => { type => 'string' },
+           ticket => { type => 'string' },
+           cert => { type => 'string' },
+           port => { type => 'integer' },
+           upid => { type => 'string' },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = $param->{vmid};
+       my $node = $param->{node};
+
+       my $authpath = "/vms/$vmid";
+
+       my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
+
+       $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
+           if !$sslcert;
+
+       my $port = PVE::Tools::next_vnc_port();
+
+       my $remip;
+
+       if ($node ne PVE::INotify::nodename()) {
+           $remip = PVE::Cluster::remote_node_ip($node);
+       }
+
+       # NOTE: vncterm VNC traffic is already TLS encrypted,
+       # so we select the fastest chipher here (or 'none'?)
+       my $remcmd = $remip ?
+           ['/usr/bin/ssh', '-t', $remip] : [];
+
+       my $shcmd = [ '/usr/bin/dtach', '-A',
+                     "/var/run/dtach/vzctlconsole$vmid",
+                     '-r', 'winch', '-z',
+                     'lxc-console', '-n', $vmid ];
+
        my $realcmd = sub {
-           my $cmd = ['lxc-destroy', '-n', $vmid ];
+           my $upid = shift;
+
+           syslog ('info', "starting lxc vnc proxy $upid\n");
+
+           my $timeout = 10;
+
+           my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
+                      '-timeout', $timeout, '-authpath', $authpath,
+                      '-perm', 'VM.Console'];
+
+           if ($param->{websocket}) {
+               $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
+               push @$cmd, '-notls', '-listen', 'localhost';
+           }
+
+           push @$cmd, '-c', @$remcmd, @$shcmd;
 
            run_command($cmd);
 
-           PVE::AccessControl::remove_vm_from_pool($vmid);
+           return;
        };
 
-       return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
+       my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
+
+       PVE::Tools::wait_for_vnc_port($port);
+
+       return {
+           user => $authuser,
+           ticket => $ticket,
+           port => $port,
+           upid => $upid,
+           cert => $sslcert,
+       };
+    }});
+
+__PACKAGE__->register_method({
+    name => 'vncwebsocket',
+    path => '{vmid}/vncwebsocket',
+    method => 'GET',
+    permissions => {
+       description => "You also need to pass a valid ticket (vncticket).",
+       check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
+    },
+    description => "Opens a weksocket for VNC traffic.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           vncticket => {
+               description => "Ticket from previous call to vncproxy.",
+               type => 'string',
+               maxLength => 512,
+           },
+           port => {
+               description => "Port number returned by previous vncproxy call.",
+               type => 'integer',
+               minimum => 5900,
+               maximum => 5999,
+           },
+       },
+    },
+    returns => {
+       type => "object",
+       properties => {
+           port => { type => 'string' },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $authpath = "/vms/$param->{vmid}";
+
+       PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
+
+       my $port = $param->{port};
+
+       return { port => $port };
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'spiceproxy',
+    path => '{vmid}/spiceproxy',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
+    },
+    description => "Returns a SPICE configuration to connect to the CT.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           proxy => get_standard_option('spice-proxy', { optional => 1 }),
+       },
+    },
+    returns => get_standard_option('remote-viewer-config'),
+    code => sub {
+       my ($param) = @_;
+
+       my $vmid = $param->{vmid};
+       my $node = $param->{node};
+       my $proxy = $param->{proxy};
+
+       my $authpath = "/vms/$vmid";
+       my $permissions = 'VM.Console';
+
+       my $shcmd = ['/usr/bin/dtach', '-A',
+                    "/var/run/dtach/vzctlconsole$vmid",
+                    '-r', 'winch', '-z',
+                    'lxc-console', '-n', $vmid];
+
+       my $title = "CT $vmid";
+
+       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::OpenVZ::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',
+    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);
+               my $stcfg = cfs_read_file("storage.cfg");
+               if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
+                   PVE::Storage::activate_storage($stcfg, $sid);
+               }
+
+               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 => '{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') {
+
+           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 $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 => '{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);
+
+           push @$cmd, '--timeout', $timeout;
+
+           eval { run_command($cmd, timeout => $timeout+5); };
+           my $err = $@;
+           return if !$err;
+
+           die $err if !$param->{forceStop};
+
+           warn "shutdown failed - forcing stop now\n";
+
+           push @$cmd, '--kill';
+           run_command($cmd);
+
+           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);
+       }
     }});
 
 1;