[pve-devel] [PATCH 1/4] Add CT suspend/resume support via PVE2 API

Daniel Hunsaker danhunsaker at gmail.com
Fri Oct 3 20:58:53 CEST 2014


From: Dan Hunsaker <danhunsaker at gmail.com>

Suspend/resume support for VMs has been in the PVE2 API for some time,
but even though vzctl supports suspend/resume (what they call checkpoint/
restore), the API doesn't yet support suspend/resume for CTs.  This patch
adds that support.

Signed-off-by: Dan Hunsaker <danhunsaker at gmail.com>
---
 PVE/API2/OpenVZ.pm | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/OpenVZ.pm      | 26 ++++++++++++++-
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/OpenVZ.pm b/PVE/API2/OpenVZ.pm
index 184ebdf..5d8c0c6 100644
--- a/PVE/API2/OpenVZ.pm
+++ b/PVE/API2/OpenVZ.pm
@@ -1459,6 +1459,102 @@ __PACKAGE__->register_method({
     }});
 
 __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::OpenVZ::check_running($vmid);
+
+               my $realcmd = sub {
+                       my $upid = shift;
+
+                       syslog('info', "suspend CT $vmid: $upid\n");
+
+                       PVE::OpenVZ::vm_suspend($vmid);
+
+                       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::OpenVZ::check_running($vmid);
+
+               my $realcmd = sub {
+                       my $upid = shift;
+
+                       syslog('info', "resume CT $vmid: $upid\n");
+
+                       PVE::OpenVZ::vm_resume($vmid);
+
+                       return;
+               };
+
+               my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
+
+               return $upid;
+       }});
+
+__PACKAGE__->register_method({
     name => 'migrate_vm',
     path => '{vmid}/migrate',
     method => 'POST',
diff --git a/PVE/OpenVZ.pm b/PVE/OpenVZ.pm
index aa6f502..fcfb0c2 100644
--- a/PVE/OpenVZ.pm
+++ b/PVE/OpenVZ.pm
@@ -6,7 +6,7 @@ use File::stat qw();
 use POSIX qw (LONG_MAX);
 use IO::Dir;
 use IO::File;
-use PVE::Tools qw(extract_param $IPV6RE $IPV4RE);
+use PVE::Tools qw(run_command extract_param $IPV6RE $IPV4RE);
 use PVE::ProcFSTools;
 use PVE::Cluster qw(cfs_register_file cfs_read_file);
 use PVE::SafeSyslog;
@@ -1220,6 +1220,30 @@ sub lock_container {
     return $res;
 }
 
+sub vm_suspend {
+    my ($vmid) = @_;
+
+    my $cmd = ['vzctl', 'chkpnt', $vmid];
+
+    eval { run_command($cmd); };
+    if (my $err = $@) {
+        syslog("err", "CT $vmid suspend failed - $err");
+        die $err;
+    }
+}
+
+sub vm_resume {
+    my ($vmid) = @_;
+
+    my $cmd = ['vzctl', 'restore', $vmid];
+
+    eval { run_command($cmd); };
+    if (my $err = $@) {
+        syslog("err", "CT $vmid resume failed - $err");
+        die $err;
+    }
+}
+
 sub replacepw {
     my ($file, $epw) = @_;
 
-- 
1.9.1




More information about the pve-devel mailing list