- # 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',
- method => 'GET',
- proxyto => 'node',
- protected => 1,
- description => "Check if feature for virtual machine is available.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- feature => {
- description => "Feature to check.",
- type => 'string',
- enum => [ 'snapshot' ],
- },
- snapname => get_standard_option('pve-lxc-snapshot-name', {
- optional => 1,
- }),
- },
- },
- returns => {
- type => "object",
- properties => {
- hasFeature => { type => 'boolean' },
- #nodes => {
- #type => 'array',
- #items => { type => 'string' },
- #}
- },
- },
- code => sub {
- my ($param) = @_;
-
- my $node = extract_param($param, 'node');
-
- my $vmid = extract_param($param, 'vmid');
-
- my $snapname = extract_param($param, 'snapname');