]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
snapshot: save state and implement rollback
[qemu-server.git] / PVE / API2 / Qemu.pm
index 468f138aef2ad606f895ad3a87a3abd8fc0effc3..ba54461d2bde21f68277e748df91048d67337225 100644 (file)
@@ -456,6 +456,7 @@ __PACKAGE__->register_method({
            { subdir => 'rrd' },
            { subdir => 'rrddata' },
            { subdir => 'monitor' },
+           { subdir => 'snapshot' },
            ];
 
        return $res;
@@ -1884,31 +1885,75 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'snapshot_vm',
+    name => 'snapshot_list',
     path => '{vmid}/snapshot',
-    method => 'PUT',
+    method => 'GET',
+    description => "List all snapshots.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    proxyto => 'node',
+    protected => 1, # qemu 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 $conf = PVE::QemuServer::load_config($param->{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 $d->{parent};
+           $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
+           push @$res, $item;
+       }
+
+       if ($conf->{parent}) {
+           push @$res, { name => '__current', parent => $conf->{parent} };
+       } else {
+           push @$res, { name => '__current' };
+       }
+
+       return $res;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'snapshot',
+    path => '{vmid}/snapshot',
+    method => 'POST',
     protected => 1,
     proxyto => 'node',
     description => "Snapshot a VM.",
     permissions => {
-       check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
+       check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
     },
     parameters => {
        additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid'),
-           skiplock => get_standard_option('skiplock'),
-           action => {
-               type => 'string',
-               description => "Action",
-               enum => [ 'create', 'delete', 'rollback' ],
-           },
-           snapname => {
-               type => 'string',
-               description => "The name of the snapshot",
-               maxLength => 40,
-           },
+           snapname => get_standard_option('pve-snapshot-name'),
            vmstate => {
                optional => 1,
                type => 'boolean',
@@ -1919,15 +1964,17 @@ __PACKAGE__->register_method({
                type => 'boolean',
                description => "Freeze the filesystem",
            },
-           digest => {
-               type => 'string',
-               description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
-               maxLength => 40,
+           description => {
                optional => 1,
+               type => 'string',
+               description => "A textual description or comment.",
            },
        },
     },
-    returns => { type => 'null'},
+    returns => {
+       type => 'string',
+       description => "the task ID.",
+    },
     code => sub {
        my ($param) = @_;
 
@@ -1939,38 +1986,236 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
-       my $digest = extract_param($param, 'digest');
+       my $snapname = extract_param($param, 'snapname');
 
-       my $action = extract_param($param, 'action');
+       my $realcmd = sub {
+           PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
+           PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate}, 
+                                            $param->{freezefs}, $param->{description});
+       };
+
+       return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
+    }});
+
+__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-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-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');
 
-       my $vmstate = extract_param($param, 'vmstate');
-       my $freezefs = extract_param($param, 'freezefs');
+       return undef if !defined($param->{description});
 
-       my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." })
-               if $skiplock && $authuser ne 'root@pam';
+       my $updatefn =  sub {
+
+           my $conf = PVE::QemuServer::load_config($vmid);
 
+           PVE::QemuServer::check_lock($conf);
 
-       # fixme: digest?
-       # fixme: access rights? 
-       # &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $conf);
-       # fixme: need to implement a check to see if all storages support snapshots
+           my $snap = $conf->{snapshots}->{$snapname};
 
-       if($action eq 'create') {
-           PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
-           PVE::QemuServer::snapshot_create($vmid, $snapname, $vmstate, $freezefs);
-       } elsif($action eq 'rollback'){
+           die "snapshot '$snapname' does not exist\n" if !defined($snap); 
+           
+           $snap->{description} = $param->{description} if defined($param->{description});
+
+            PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+       };
+
+       PVE::QemuServer::lock_config($vmid, $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-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::QemuServer::load_config($vmid);
+
+       my $snap = $conf->{snapshots}->{$snapname};
+
+       die "snapshot '$snapname' does not exist\n" if !defined($snap); 
+           
+       return $snap;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'rollback',
+    path => '{vmid}/snapshot/{snapname}/rollback',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Rollback VM 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-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 VM $vmid: $snapname");
            PVE::QemuServer::snapshot_rollback($vmid, $snapname);
-       } elsif($action eq 'delete') {
+       };
+
+       return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd);
+    }});
+
+__PACKAGE__->register_method({
+    name => 'delsnapshot',
+    path => '{vmid}/snapshot/{snapname}',
+    method => 'DELETE',
+    protected => 1,
+    proxyto => 'node',
+    description => "Delete a VM 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-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::QemuServer::snapshot_delete($vmid, $snapname, $vmstate);
-       }
+           PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force});
+       };
 
-       return undef;
+       return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
     }});
 
 1;