]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC.pm
cleanup recover_config
[pve-container.git] / src / PVE / API2 / LXC.pm
1 package PVE::API2::LXC;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param run_command);
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Storage;
13 use PVE::RESTHandler;
14 use PVE::RPCEnvironment;
15 use PVE::LXC;
16 use PVE::LXCCreate;
17 use PVE::HA::Config;
18 use PVE::JSONSchema qw(get_standard_option);
19 use base qw(PVE::RESTHandler);
20
21 use Data::Dumper; # fixme: remove
22
23 my $get_container_storage = sub {
24 my ($stcfg, $vmid, $lxc_conf) = @_;
25
26 if (my $volid = $lxc_conf->{'pve.volid'}) {
27 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
28 return wantarray ? ($sid, $volname) : $sid;
29 } else {
30 my $path = $lxc_conf->{'lxc.rootfs'};
31 my ($vtype, $volid) = PVE::Storage::path_to_volume_id($stcfg, $path);
32 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
33 return wantarray ? ($sid, $volname, $path) : $sid;
34 }
35 };
36
37 my $check_ct_modify_config_perm = sub {
38 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
39
40 return 1 if $authuser ne 'root@pam';
41
42 foreach my $opt (@$key_list) {
43
44 if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
45 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
46 } elsif ($opt eq 'disk') {
47 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
48 } elsif ($opt eq 'memory' || $opt eq 'swap') {
49 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
50 } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
51 $opt eq 'searchdomain' || $opt eq 'hostname') {
52 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
53 } else {
54 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
55 }
56 }
57
58 return 1;
59 };
60
61 PVE::JSONSchema::register_standard_option('pve-lxc-snapshot-name', {
62 description => "The name of the snapshot.",
63 type => 'string', format => 'pve-configid',
64 maxLength => 40,
65 });
66
67 __PACKAGE__->register_method({
68 name => 'vmlist',
69 path => '',
70 method => 'GET',
71 description => "LXC container index (per node).",
72 permissions => {
73 description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
74 user => 'all',
75 },
76 proxyto => 'node',
77 protected => 1, # /proc files are only readable by root
78 parameters => {
79 additionalProperties => 0,
80 properties => {
81 node => get_standard_option('pve-node'),
82 },
83 },
84 returns => {
85 type => 'array',
86 items => {
87 type => "object",
88 properties => {},
89 },
90 links => [ { rel => 'child', href => "{vmid}" } ],
91 },
92 code => sub {
93 my ($param) = @_;
94
95 my $rpcenv = PVE::RPCEnvironment::get();
96 my $authuser = $rpcenv->get_user();
97
98 my $vmstatus = PVE::LXC::vmstatus();
99
100 my $res = [];
101 foreach my $vmid (keys %$vmstatus) {
102 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
103
104 my $data = $vmstatus->{$vmid};
105 $data->{vmid} = $vmid;
106 push @$res, $data;
107 }
108
109 return $res;
110
111 }});
112
113 __PACKAGE__->register_method({
114 name => 'create_vm',
115 path => '',
116 method => 'POST',
117 description => "Create or restore a container.",
118 permissions => {
119 user => 'all', # check inside
120 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
121 "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
122 "You also need 'Datastore.AllocateSpace' permissions on the storage.",
123 },
124 protected => 1,
125 proxyto => 'node',
126 parameters => {
127 additionalProperties => 0,
128 properties => PVE::LXC::json_config_properties({
129 node => get_standard_option('pve-node'),
130 vmid => get_standard_option('pve-vmid'),
131 ostemplate => {
132 description => "The OS template or backup file.",
133 type => 'string',
134 maxLength => 255,
135 },
136 password => {
137 optional => 1,
138 type => 'string',
139 description => "Sets root password inside container.",
140 minLength => 5,
141 },
142 storage => get_standard_option('pve-storage-id', {
143 description => "Target storage.",
144 default => 'local',
145 optional => 1,
146 }),
147 force => {
148 optional => 1,
149 type => 'boolean',
150 description => "Allow to overwrite existing container.",
151 },
152 restore => {
153 optional => 1,
154 type => 'boolean',
155 description => "Mark this as restore task.",
156 },
157 pool => {
158 optional => 1,
159 type => 'string', format => 'pve-poolid',
160 description => "Add the VM to the specified pool.",
161 },
162 }),
163 },
164 returns => {
165 type => 'string',
166 },
167 code => sub {
168 my ($param) = @_;
169
170 my $rpcenv = PVE::RPCEnvironment::get();
171
172 my $authuser = $rpcenv->get_user();
173
174 my $node = extract_param($param, 'node');
175
176 my $vmid = extract_param($param, 'vmid');
177
178 my $basecfg_fn = PVE::LXC::config_file($vmid);
179
180 my $same_container_exists = -f $basecfg_fn;
181
182 my $restore = extract_param($param, 'restore');
183
184 if ($restore) {
185 # fixme: limit allowed parameters
186
187 }
188
189 my $force = extract_param($param, 'force');
190
191 if (!($same_container_exists && $restore && $force)) {
192 PVE::Cluster::check_vmid_unused($vmid);
193 }
194
195 my $password = extract_param($param, 'password');
196
197 my $storage = extract_param($param, 'storage') || 'local';
198
199 my $pool = extract_param($param, 'pool');
200
201 my $storage_cfg = cfs_read_file("storage.cfg");
202
203 my $scfg = PVE::Storage::storage_check_node($storage_cfg, $storage, $node);
204
205 raise_param_exc({ storage => "storage '$storage' does not support container root directories"})
206 if !$scfg->{content}->{rootdir};
207
208 if (defined($pool)) {
209 $rpcenv->check_pool_exist($pool);
210 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
211 }
212
213 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
214 # OK
215 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
216 # OK
217 } elsif ($restore && $force && $same_container_exists &&
218 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
219 # OK: user has VM.Backup permissions, and want to restore an existing VM
220 } else {
221 raise_perm_exc();
222 }
223
224 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
225
226 PVE::Storage::activate_storage($storage_cfg, $storage);
227
228 my $ostemplate = extract_param($param, 'ostemplate');
229
230 my $archive;
231
232 if ($ostemplate eq '-') {
233 die "pipe requires cli environment\n"
234 if $rpcenv->{type} ne 'cli';
235 die "pipe can only be used with restore tasks\n"
236 if !$restore;
237 $archive = '-';
238 } else {
239 $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
240 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
241 }
242
243 my $conf = {};
244
245 if ($restore) {
246 $conf = PVE::LXCCreate::recover_config($archive);
247 PVE::LXC::lxc_config_change_vmid($conf, $vmid);
248 }
249
250 $param->{hostname} ||= "CT$vmid" if !defined($conf->{'lxc.utsname'});
251 $param->{memory} ||= 512 if !defined($conf->{'lxc.cgroup.memory.limit_in_bytes'});
252 $param->{swap} //= 512 if !defined($conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'});
253
254 PVE::LXC::update_lxc_config($vmid, $conf, 0, $param);
255
256 # assigng default names, so that we can configure network with LXCSetup
257 foreach my $k (keys %$conf) {
258 next if $k !~ m/^net(\d+)$/;
259 my $ind = $1;
260 $conf->{$k}->{name} ||= "eth$ind";
261 }
262
263 # use user namespace ?
264 # disable for now, because kernel 3.10.0 does not support it
265 #$conf->{'lxc.id_map'} = ["u 0 100000 65536", "g 0 100000 65536"];
266
267 my $check_vmid_usage = sub {
268 if ($force) {
269 die "cant overwrite running container\n"
270 if PVE::LXC::check_running($vmid);
271 } else {
272 PVE::Cluster::check_vmid_unused($vmid);
273 }
274 };
275
276 my $code = sub {
277 &$check_vmid_usage(); # final check after locking
278
279 PVE::Cluster::check_cfs_quorum();
280
281 PVE::LXCCreate::create_rootfs($storage_cfg, $storage, $param->{disk}, $vmid, $conf,
282 $archive, $password, $restore);
283 };
284
285 my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
286
287 &$check_vmid_usage(); # first check before locking
288
289 return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
290 $vmid, $authuser, $realcmd);
291
292 }});
293
294 my $vm_config_perm_list = [
295 'VM.Config.Disk',
296 'VM.Config.CPU',
297 'VM.Config.Memory',
298 'VM.Config.Network',
299 'VM.Config.Options',
300 ];
301
302 __PACKAGE__->register_method({
303 name => 'update_vm',
304 path => '{vmid}/config',
305 method => 'PUT',
306 protected => 1,
307 proxyto => 'node',
308 description => "Set container options.",
309 permissions => {
310 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
311 },
312 parameters => {
313 additionalProperties => 0,
314 properties => PVE::LXC::json_config_properties(
315 {
316 node => get_standard_option('pve-node'),
317 vmid => get_standard_option('pve-vmid'),
318 delete => {
319 type => 'string', format => 'pve-configid-list',
320 description => "A list of settings you want to delete.",
321 optional => 1,
322 },
323 digest => {
324 type => 'string',
325 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
326 maxLength => 40,
327 optional => 1,
328 }
329 }),
330 },
331 returns => { type => 'null'},
332 code => sub {
333 my ($param) = @_;
334
335 my $rpcenv = PVE::RPCEnvironment::get();
336
337 my $authuser = $rpcenv->get_user();
338
339 my $node = extract_param($param, 'node');
340
341 my $vmid = extract_param($param, 'vmid');
342
343 my $digest = extract_param($param, 'digest');
344
345 die "no options specified\n" if !scalar(keys %$param);
346
347 my $delete_str = extract_param($param, 'delete');
348 my @delete = PVE::Tools::split_list($delete_str);
349
350 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
351
352 foreach my $opt (@delete) {
353 raise_param_exc({ delete => "you can't use '-$opt' and " .
354 "-delete $opt' at the same time" })
355 if defined($param->{$opt});
356
357 if (!PVE::LXC::option_exists($opt)) {
358 raise_param_exc({ delete => "unknown option '$opt'" });
359 }
360 }
361
362 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
363
364 my $code = sub {
365
366 my $conf = PVE::LXC::load_config($vmid);
367 PVE::LXC::check_lock($conf);
368
369 PVE::Tools::assert_if_modified($digest, $conf->{digest});
370
371 my $running = PVE::LXC::check_running($vmid);
372
373 PVE::LXC::update_lxc_config($vmid, $conf, $running, $param, \@delete);
374
375 PVE::LXC::write_config($vmid, $conf);
376 };
377
378 PVE::LXC::lock_container($vmid, undef, $code);
379
380 return undef;
381 }});
382
383 __PACKAGE__->register_method ({
384 subclass => "PVE::API2::Firewall::CT",
385 path => '{vmid}/firewall',
386 });
387
388 __PACKAGE__->register_method({
389 name => 'vmdiridx',
390 path => '{vmid}',
391 method => 'GET',
392 proxyto => 'node',
393 description => "Directory index",
394 permissions => {
395 user => 'all',
396 },
397 parameters => {
398 additionalProperties => 0,
399 properties => {
400 node => get_standard_option('pve-node'),
401 vmid => get_standard_option('pve-vmid'),
402 },
403 },
404 returns => {
405 type => 'array',
406 items => {
407 type => "object",
408 properties => {
409 subdir => { type => 'string' },
410 },
411 },
412 links => [ { rel => 'child', href => "{subdir}" } ],
413 },
414 code => sub {
415 my ($param) = @_;
416
417 # test if VM exists
418 my $conf = PVE::LXC::load_config($param->{vmid});
419
420 my $res = [
421 { subdir => 'config' },
422 { subdir => 'status' },
423 { subdir => 'vncproxy' },
424 { subdir => 'vncwebsocket' },
425 { subdir => 'spiceproxy' },
426 { subdir => 'migrate' },
427 # { subdir => 'initlog' },
428 { subdir => 'rrd' },
429 { subdir => 'rrddata' },
430 { subdir => 'firewall' },
431 { subdir => 'snapshot' },
432 ];
433
434 return $res;
435 }});
436
437 __PACKAGE__->register_method({
438 name => 'rrd',
439 path => '{vmid}/rrd',
440 method => 'GET',
441 protected => 1, # fixme: can we avoid that?
442 permissions => {
443 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
444 },
445 description => "Read VM RRD statistics (returns PNG)",
446 parameters => {
447 additionalProperties => 0,
448 properties => {
449 node => get_standard_option('pve-node'),
450 vmid => get_standard_option('pve-vmid'),
451 timeframe => {
452 description => "Specify the time frame you are interested in.",
453 type => 'string',
454 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
455 },
456 ds => {
457 description => "The list of datasources you want to display.",
458 type => 'string', format => 'pve-configid-list',
459 },
460 cf => {
461 description => "The RRD consolidation function",
462 type => 'string',
463 enum => [ 'AVERAGE', 'MAX' ],
464 optional => 1,
465 },
466 },
467 },
468 returns => {
469 type => "object",
470 properties => {
471 filename => { type => 'string' },
472 },
473 },
474 code => sub {
475 my ($param) = @_;
476
477 return PVE::Cluster::create_rrd_graph(
478 "pve2-vm/$param->{vmid}", $param->{timeframe},
479 $param->{ds}, $param->{cf});
480
481 }});
482
483 __PACKAGE__->register_method({
484 name => 'rrddata',
485 path => '{vmid}/rrddata',
486 method => 'GET',
487 protected => 1, # fixme: can we avoid that?
488 permissions => {
489 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
490 },
491 description => "Read VM RRD statistics",
492 parameters => {
493 additionalProperties => 0,
494 properties => {
495 node => get_standard_option('pve-node'),
496 vmid => get_standard_option('pve-vmid'),
497 timeframe => {
498 description => "Specify the time frame you are interested in.",
499 type => 'string',
500 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
501 },
502 cf => {
503 description => "The RRD consolidation function",
504 type => 'string',
505 enum => [ 'AVERAGE', 'MAX' ],
506 optional => 1,
507 },
508 },
509 },
510 returns => {
511 type => "array",
512 items => {
513 type => "object",
514 properties => {},
515 },
516 },
517 code => sub {
518 my ($param) = @_;
519
520 return PVE::Cluster::create_rrd_data(
521 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
522 }});
523
524
525 __PACKAGE__->register_method({
526 name => 'vm_config',
527 path => '{vmid}/config',
528 method => 'GET',
529 proxyto => 'node',
530 description => "Get container configuration.",
531 permissions => {
532 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
533 },
534 parameters => {
535 additionalProperties => 0,
536 properties => {
537 node => get_standard_option('pve-node'),
538 vmid => get_standard_option('pve-vmid'),
539 },
540 },
541 returns => {
542 type => "object",
543 properties => {
544 digest => {
545 type => 'string',
546 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
547 }
548 },
549 },
550 code => sub {
551 my ($param) = @_;
552
553 my $lxc_conf = PVE::LXC::load_config($param->{vmid});
554
555 # NOTE: we only return selected/converted values
556
557 my $conf = PVE::LXC::lxc_conf_to_pve($param->{vmid}, $lxc_conf);
558
559 my $stcfg = PVE::Cluster::cfs_read_file("storage.cfg");
560
561 my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid}, $lxc_conf);
562 $conf->{storage} = $sid || $path;
563
564 return $conf;
565 }});
566
567 __PACKAGE__->register_method({
568 name => 'destroy_vm',
569 path => '{vmid}',
570 method => 'DELETE',
571 protected => 1,
572 proxyto => 'node',
573 description => "Destroy the container (also delete all uses files).",
574 permissions => {
575 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
576 },
577 parameters => {
578 additionalProperties => 0,
579 properties => {
580 node => get_standard_option('pve-node'),
581 vmid => get_standard_option('pve-vmid'),
582 },
583 },
584 returns => {
585 type => 'string',
586 },
587 code => sub {
588 my ($param) = @_;
589
590 my $rpcenv = PVE::RPCEnvironment::get();
591
592 my $authuser = $rpcenv->get_user();
593
594 my $vmid = $param->{vmid};
595
596 # test if container exists
597 my $conf = PVE::LXC::load_config($vmid);
598
599 my $storage_cfg = cfs_read_file("storage.cfg");
600
601 my $code = sub {
602 # reload config after lock
603 $conf = PVE::LXC::load_config($vmid);
604 PVE::LXC::check_lock($conf);
605
606 PVE::LXC::destory_lxc_container($storage_cfg, $vmid, $conf);
607 PVE::AccessControl::remove_vm_from_pool($vmid);
608 };
609
610 my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
611
612 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
613 }});
614
615 my $sslcert;
616
617 __PACKAGE__->register_method ({
618 name => 'vncproxy',
619 path => '{vmid}/vncproxy',
620 method => 'POST',
621 protected => 1,
622 permissions => {
623 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
624 },
625 description => "Creates a TCP VNC proxy connections.",
626 parameters => {
627 additionalProperties => 0,
628 properties => {
629 node => get_standard_option('pve-node'),
630 vmid => get_standard_option('pve-vmid'),
631 websocket => {
632 optional => 1,
633 type => 'boolean',
634 description => "use websocket instead of standard VNC.",
635 },
636 },
637 },
638 returns => {
639 additionalProperties => 0,
640 properties => {
641 user => { type => 'string' },
642 ticket => { type => 'string' },
643 cert => { type => 'string' },
644 port => { type => 'integer' },
645 upid => { type => 'string' },
646 },
647 },
648 code => sub {
649 my ($param) = @_;
650
651 my $rpcenv = PVE::RPCEnvironment::get();
652
653 my $authuser = $rpcenv->get_user();
654
655 my $vmid = $param->{vmid};
656 my $node = $param->{node};
657
658 my $authpath = "/vms/$vmid";
659
660 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
661
662 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
663 if !$sslcert;
664
665 my ($remip, $family);
666
667 if ($node ne PVE::INotify::nodename()) {
668 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
669 } else {
670 $family = PVE::Tools::get_host_address_family($node);
671 }
672
673 my $port = PVE::Tools::next_vnc_port($family);
674
675 # NOTE: vncterm VNC traffic is already TLS encrypted,
676 # so we select the fastest chipher here (or 'none'?)
677 my $remcmd = $remip ?
678 ['/usr/bin/ssh', '-t', $remip] : [];
679
680 my $shcmd = [ '/usr/bin/dtach', '-A',
681 "/var/run/dtach/vzctlconsole$vmid",
682 '-r', 'winch', '-z',
683 'lxc-console', '-n', $vmid ];
684
685 my $realcmd = sub {
686 my $upid = shift;
687
688 syslog ('info', "starting lxc vnc proxy $upid\n");
689
690 my $timeout = 10;
691
692 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
693 '-timeout', $timeout, '-authpath', $authpath,
694 '-perm', 'VM.Console'];
695
696 if ($param->{websocket}) {
697 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
698 push @$cmd, '-notls', '-listen', 'localhost';
699 }
700
701 push @$cmd, '-c', @$remcmd, @$shcmd;
702
703 run_command($cmd);
704
705 return;
706 };
707
708 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
709
710 PVE::Tools::wait_for_vnc_port($port);
711
712 return {
713 user => $authuser,
714 ticket => $ticket,
715 port => $port,
716 upid => $upid,
717 cert => $sslcert,
718 };
719 }});
720
721 __PACKAGE__->register_method({
722 name => 'vncwebsocket',
723 path => '{vmid}/vncwebsocket',
724 method => 'GET',
725 permissions => {
726 description => "You also need to pass a valid ticket (vncticket).",
727 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
728 },
729 description => "Opens a weksocket for VNC traffic.",
730 parameters => {
731 additionalProperties => 0,
732 properties => {
733 node => get_standard_option('pve-node'),
734 vmid => get_standard_option('pve-vmid'),
735 vncticket => {
736 description => "Ticket from previous call to vncproxy.",
737 type => 'string',
738 maxLength => 512,
739 },
740 port => {
741 description => "Port number returned by previous vncproxy call.",
742 type => 'integer',
743 minimum => 5900,
744 maximum => 5999,
745 },
746 },
747 },
748 returns => {
749 type => "object",
750 properties => {
751 port => { type => 'string' },
752 },
753 },
754 code => sub {
755 my ($param) = @_;
756
757 my $rpcenv = PVE::RPCEnvironment::get();
758
759 my $authuser = $rpcenv->get_user();
760
761 my $authpath = "/vms/$param->{vmid}";
762
763 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
764
765 my $port = $param->{port};
766
767 return { port => $port };
768 }});
769
770 __PACKAGE__->register_method ({
771 name => 'spiceproxy',
772 path => '{vmid}/spiceproxy',
773 method => 'POST',
774 protected => 1,
775 proxyto => 'node',
776 permissions => {
777 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
778 },
779 description => "Returns a SPICE configuration to connect to the CT.",
780 parameters => {
781 additionalProperties => 0,
782 properties => {
783 node => get_standard_option('pve-node'),
784 vmid => get_standard_option('pve-vmid'),
785 proxy => get_standard_option('spice-proxy', { optional => 1 }),
786 },
787 },
788 returns => get_standard_option('remote-viewer-config'),
789 code => sub {
790 my ($param) = @_;
791
792 my $vmid = $param->{vmid};
793 my $node = $param->{node};
794 my $proxy = $param->{proxy};
795
796 my $authpath = "/vms/$vmid";
797 my $permissions = 'VM.Console';
798
799 my $shcmd = ['/usr/bin/dtach', '-A',
800 "/var/run/dtach/vzctlconsole$vmid",
801 '-r', 'winch', '-z',
802 'lxc-console', '-n', $vmid];
803
804 my $title = "CT $vmid";
805
806 return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
807 }});
808
809 __PACKAGE__->register_method({
810 name => 'vmcmdidx',
811 path => '{vmid}/status',
812 method => 'GET',
813 proxyto => 'node',
814 description => "Directory index",
815 permissions => {
816 user => 'all',
817 },
818 parameters => {
819 additionalProperties => 0,
820 properties => {
821 node => get_standard_option('pve-node'),
822 vmid => get_standard_option('pve-vmid'),
823 },
824 },
825 returns => {
826 type => 'array',
827 items => {
828 type => "object",
829 properties => {
830 subdir => { type => 'string' },
831 },
832 },
833 links => [ { rel => 'child', href => "{subdir}" } ],
834 },
835 code => sub {
836 my ($param) = @_;
837
838 # test if VM exists
839 my $conf = PVE::LXC::load_config($param->{vmid});
840
841 my $res = [
842 { subdir => 'current' },
843 { subdir => 'start' },
844 { subdir => 'stop' },
845 { subdir => 'shutdown' },
846 { subdir => 'migrate' },
847 ];
848
849 return $res;
850 }});
851
852 __PACKAGE__->register_method({
853 name => 'vm_status',
854 path => '{vmid}/status/current',
855 method => 'GET',
856 proxyto => 'node',
857 protected => 1, # openvz /proc entries are only readable by root
858 description => "Get virtual machine status.",
859 permissions => {
860 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
861 },
862 parameters => {
863 additionalProperties => 0,
864 properties => {
865 node => get_standard_option('pve-node'),
866 vmid => get_standard_option('pve-vmid'),
867 },
868 },
869 returns => { type => 'object' },
870 code => sub {
871 my ($param) = @_;
872
873 # test if VM exists
874 my $conf = PVE::LXC::load_config($param->{vmid});
875
876 my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
877 my $status = $vmstatus->{$param->{vmid}};
878
879 $status->{ha} = PVE::HA::Config::vm_is_ha_managed($param->{vmid}) ? 1 : 0;
880
881 return $status;
882 }});
883
884 __PACKAGE__->register_method({
885 name => 'vm_start',
886 path => '{vmid}/status/start',
887 method => 'POST',
888 protected => 1,
889 proxyto => 'node',
890 description => "Start the container.",
891 permissions => {
892 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
893 },
894 parameters => {
895 additionalProperties => 0,
896 properties => {
897 node => get_standard_option('pve-node'),
898 vmid => get_standard_option('pve-vmid'),
899 },
900 },
901 returns => {
902 type => 'string',
903 },
904 code => sub {
905 my ($param) = @_;
906
907 my $rpcenv = PVE::RPCEnvironment::get();
908
909 my $authuser = $rpcenv->get_user();
910
911 my $node = extract_param($param, 'node');
912
913 my $vmid = extract_param($param, 'vmid');
914
915 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
916
917 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
918
919 my $hacmd = sub {
920 my $upid = shift;
921
922 my $service = "ct:$vmid";
923
924 my $cmd = ['ha-manager', 'enable', $service];
925
926 print "Executing HA start for CT $vmid\n";
927
928 PVE::Tools::run_command($cmd);
929
930 return;
931 };
932
933 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
934
935 } else {
936
937 my $realcmd = sub {
938 my $upid = shift;
939
940 syslog('info', "starting CT $vmid: $upid\n");
941
942 my $conf = PVE::LXC::load_config($vmid);
943 my $stcfg = cfs_read_file("storage.cfg");
944 if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
945 PVE::Storage::activate_storage($stcfg, $sid);
946 }
947
948 my $cmd = ['lxc-start', '-n', $vmid];
949
950 run_command($cmd);
951
952 return;
953 };
954
955 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
956 }
957 }});
958
959 __PACKAGE__->register_method({
960 name => 'vm_stop',
961 path => '{vmid}/status/stop',
962 method => 'POST',
963 protected => 1,
964 proxyto => 'node',
965 description => "Stop the container.",
966 permissions => {
967 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
968 },
969 parameters => {
970 additionalProperties => 0,
971 properties => {
972 node => get_standard_option('pve-node'),
973 vmid => get_standard_option('pve-vmid'),
974 },
975 },
976 returns => {
977 type => 'string',
978 },
979 code => sub {
980 my ($param) = @_;
981
982 my $rpcenv = PVE::RPCEnvironment::get();
983
984 my $authuser = $rpcenv->get_user();
985
986 my $node = extract_param($param, 'node');
987
988 my $vmid = extract_param($param, 'vmid');
989
990 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
991
992 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
993
994 my $hacmd = sub {
995 my $upid = shift;
996
997 my $service = "ct:$vmid";
998
999 my $cmd = ['ha-manager', 'disable', $service];
1000
1001 print "Executing HA stop for CT $vmid\n";
1002
1003 PVE::Tools::run_command($cmd);
1004
1005 return;
1006 };
1007
1008 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1009
1010 } else {
1011
1012 my $realcmd = sub {
1013 my $upid = shift;
1014
1015 syslog('info', "stoping CT $vmid: $upid\n");
1016
1017 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
1018
1019 run_command($cmd);
1020
1021 return;
1022 };
1023
1024 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
1025 }
1026 }});
1027
1028 __PACKAGE__->register_method({
1029 name => 'vm_shutdown',
1030 path => '{vmid}/status/shutdown',
1031 method => 'POST',
1032 protected => 1,
1033 proxyto => 'node',
1034 description => "Shutdown the container.",
1035 permissions => {
1036 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1037 },
1038 parameters => {
1039 additionalProperties => 0,
1040 properties => {
1041 node => get_standard_option('pve-node'),
1042 vmid => get_standard_option('pve-vmid'),
1043 timeout => {
1044 description => "Wait maximal timeout seconds.",
1045 type => 'integer',
1046 minimum => 0,
1047 optional => 1,
1048 default => 60,
1049 },
1050 forceStop => {
1051 description => "Make sure the Container stops.",
1052 type => 'boolean',
1053 optional => 1,
1054 default => 0,
1055 }
1056 },
1057 },
1058 returns => {
1059 type => 'string',
1060 },
1061 code => sub {
1062 my ($param) = @_;
1063
1064 my $rpcenv = PVE::RPCEnvironment::get();
1065
1066 my $authuser = $rpcenv->get_user();
1067
1068 my $node = extract_param($param, 'node');
1069
1070 my $vmid = extract_param($param, 'vmid');
1071
1072 my $timeout = extract_param($param, 'timeout');
1073
1074 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
1075
1076 my $realcmd = sub {
1077 my $upid = shift;
1078
1079 syslog('info', "shutdown CT $vmid: $upid\n");
1080
1081 my $cmd = ['lxc-stop', '-n', $vmid];
1082
1083 $timeout = 60 if !defined($timeout);
1084
1085 push @$cmd, '--timeout', $timeout;
1086
1087 eval { run_command($cmd, timeout => $timeout+5); };
1088 my $err = $@;
1089 return if !$err;
1090
1091 die $err if !$param->{forceStop};
1092
1093 warn "shutdown failed - forcing stop now\n";
1094
1095 push @$cmd, '--kill';
1096 run_command($cmd);
1097
1098 return;
1099 };
1100
1101 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
1102
1103 return $upid;
1104 }});
1105
1106 __PACKAGE__->register_method({
1107 name => 'vm_suspend',
1108 path => '{vmid}/status/suspend',
1109 method => 'POST',
1110 protected => 1,
1111 proxyto => 'node',
1112 description => "Suspend the container.",
1113 permissions => {
1114 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1115 },
1116 parameters => {
1117 additionalProperties => 0,
1118 properties => {
1119 node => get_standard_option('pve-node'),
1120 vmid => get_standard_option('pve-vmid'),
1121 },
1122 },
1123 returns => {
1124 type => 'string',
1125 },
1126 code => sub {
1127 my ($param) = @_;
1128
1129 my $rpcenv = PVE::RPCEnvironment::get();
1130
1131 my $authuser = $rpcenv->get_user();
1132
1133 my $node = extract_param($param, 'node');
1134
1135 my $vmid = extract_param($param, 'vmid');
1136
1137 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
1138
1139 my $realcmd = sub {
1140 my $upid = shift;
1141
1142 syslog('info', "suspend CT $vmid: $upid\n");
1143
1144 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
1145
1146 run_command($cmd);
1147
1148 return;
1149 };
1150
1151 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
1152
1153 return $upid;
1154 }});
1155
1156 __PACKAGE__->register_method({
1157 name => 'vm_resume',
1158 path => '{vmid}/status/resume',
1159 method => 'POST',
1160 protected => 1,
1161 proxyto => 'node',
1162 description => "Resume the container.",
1163 permissions => {
1164 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1165 },
1166 parameters => {
1167 additionalProperties => 0,
1168 properties => {
1169 node => get_standard_option('pve-node'),
1170 vmid => get_standard_option('pve-vmid'),
1171 },
1172 },
1173 returns => {
1174 type => 'string',
1175 },
1176 code => sub {
1177 my ($param) = @_;
1178
1179 my $rpcenv = PVE::RPCEnvironment::get();
1180
1181 my $authuser = $rpcenv->get_user();
1182
1183 my $node = extract_param($param, 'node');
1184
1185 my $vmid = extract_param($param, 'vmid');
1186
1187 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
1188
1189 my $realcmd = sub {
1190 my $upid = shift;
1191
1192 syslog('info', "resume CT $vmid: $upid\n");
1193
1194 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
1195 '-D', '/var/liv/vz/dump'];
1196
1197 run_command($cmd);
1198
1199 return;
1200 };
1201
1202 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
1203
1204 return $upid;
1205 }});
1206
1207 __PACKAGE__->register_method({
1208 name => 'migrate_vm',
1209 path => '{vmid}/migrate',
1210 method => 'POST',
1211 protected => 1,
1212 proxyto => 'node',
1213 description => "Migrate the container to another node. Creates a new migration task.",
1214 permissions => {
1215 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1216 },
1217 parameters => {
1218 additionalProperties => 0,
1219 properties => {
1220 node => get_standard_option('pve-node'),
1221 vmid => get_standard_option('pve-vmid'),
1222 target => get_standard_option('pve-node', { description => "Target node." }),
1223 online => {
1224 type => 'boolean',
1225 description => "Use online/live migration.",
1226 optional => 1,
1227 },
1228 },
1229 },
1230 returns => {
1231 type => 'string',
1232 description => "the task ID.",
1233 },
1234 code => sub {
1235 my ($param) = @_;
1236
1237 my $rpcenv = PVE::RPCEnvironment::get();
1238
1239 my $authuser = $rpcenv->get_user();
1240
1241 my $target = extract_param($param, 'target');
1242
1243 my $localnode = PVE::INotify::nodename();
1244 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1245
1246 PVE::Cluster::check_cfs_quorum();
1247
1248 PVE::Cluster::check_node_exists($target);
1249
1250 my $targetip = PVE::Cluster::remote_node_ip($target);
1251
1252 my $vmid = extract_param($param, 'vmid');
1253
1254 # test if VM exists
1255 PVE::LXC::load_config($vmid);
1256
1257 # try to detect errors early
1258 if (PVE::LXC::check_running($vmid)) {
1259 die "cant migrate running container without --online\n"
1260 if !$param->{online};
1261 }
1262
1263 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1264
1265 my $hacmd = sub {
1266 my $upid = shift;
1267
1268 my $service = "ct:$vmid";
1269
1270 my $cmd = ['ha-manager', 'migrate', $service, $target];
1271
1272 print "Executing HA migrate for CT $vmid to node $target\n";
1273
1274 PVE::Tools::run_command($cmd);
1275
1276 return;
1277 };
1278
1279 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1280
1281 } else {
1282
1283 my $realcmd = sub {
1284 my $upid = shift;
1285
1286 # fixme: implement lxc container migration
1287 die "lxc container migration not implemented\n";
1288
1289 return;
1290 };
1291
1292 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
1293 }
1294 }});
1295
1296 __PACKAGE__->register_method({
1297 name => 'snapshot',
1298 path => '{vmid}/snapshot',
1299 method => 'POST',
1300 protected => 1,
1301 proxyto => 'node',
1302 description => "Snapshot a container.",
1303 permissions => {
1304 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1305 },
1306 parameters => {
1307 additionalProperties => 0,
1308 properties => {
1309 node => get_standard_option('pve-node'),
1310 vmid => get_standard_option('pve-vmid'),
1311 snapname => get_standard_option('pve-lxc-snapshot-name'),
1312 vmstate => {
1313 optional => 1,
1314 type => 'boolean',
1315 description => "Save the vmstate",
1316 },
1317 description => {
1318 optional => 1,
1319 type => 'string',
1320 description => "A textual description or comment.",
1321 },
1322 },
1323 },
1324 returns => {
1325 type => 'string',
1326 description => "the task ID.",
1327 },
1328 code => sub {
1329 my ($param) = @_;
1330
1331 my $rpcenv = PVE::RPCEnvironment::get();
1332
1333 my $authuser = $rpcenv->get_user();
1334
1335 my $node = extract_param($param, 'node');
1336
1337 my $vmid = extract_param($param, 'vmid');
1338
1339 my $snapname = extract_param($param, 'snapname');
1340
1341 die "unable to use snapshot name 'current' (reserved name)\n"
1342 if $snapname eq 'current';
1343
1344 my $realcmd = sub {
1345 PVE::Cluster::log_msg('info', $authuser, "snapshot container $vmid: $snapname");
1346 PVE::LXC::snapshot_create($vmid, $snapname, $param->{description});
1347 };
1348
1349 return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
1350 }});
1351
1352 __PACKAGE__->register_method({
1353 name => 'delsnapshot',
1354 path => '{vmid}/snapshot/{snapname}',
1355 method => 'DELETE',
1356 protected => 1,
1357 proxyto => 'node',
1358 description => "Delete a LXC snapshot.",
1359 permissions => {
1360 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1361 },
1362 parameters => {
1363 additionalProperties => 0,
1364 properties => {
1365 node => get_standard_option('pve-node'),
1366 vmid => get_standard_option('pve-vmid'),
1367 snapname => get_standard_option('pve-lxc-snapshot-name'),
1368 force => {
1369 optional => 1,
1370 type => 'boolean',
1371 description => "For removal from config file, even if removing disk snapshots fails.",
1372 },
1373 },
1374 },
1375 returns => {
1376 type => 'string',
1377 description => "the task ID.",
1378 },
1379 code => sub {
1380 my ($param) = @_;
1381
1382 my $rpcenv = PVE::RPCEnvironment::get();
1383
1384 my $authuser = $rpcenv->get_user();
1385
1386 my $node = extract_param($param, 'node');
1387
1388 my $vmid = extract_param($param, 'vmid');
1389
1390 my $snapname = extract_param($param, 'snapname');
1391
1392 my $realcmd = sub {
1393 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
1394 PVE::LXC::snapshot_delete($vmid, $snapname, $param->{force});
1395 };
1396
1397 return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
1398 }});
1399
1400 __PACKAGE__->register_method({
1401 name => 'rollback',
1402 path => '{vmid}/snapshot/{snapname}/rollback',
1403 method => 'POST',
1404 protected => 1,
1405 proxyto => 'node',
1406 description => "Rollback LXC state to specified snapshot.",
1407 permissions => {
1408 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1409 },
1410 parameters => {
1411 additionalProperties => 0,
1412 properties => {
1413 node => get_standard_option('pve-node'),
1414 vmid => get_standard_option('pve-vmid'),
1415 snapname => get_standard_option('pve-lxc-snapshot-name'),
1416 },
1417 },
1418 returns => {
1419 type => 'string',
1420 description => "the task ID.",
1421 },
1422 code => sub {
1423 my ($param) = @_;
1424
1425 my $rpcenv = PVE::RPCEnvironment::get();
1426
1427 my $authuser = $rpcenv->get_user();
1428
1429 my $node = extract_param($param, 'node');
1430
1431 my $vmid = extract_param($param, 'vmid');
1432
1433 my $snapname = extract_param($param, 'snapname');
1434
1435 my $realcmd = sub {
1436 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
1437 PVE::LXC::snapshot_rollback($vmid, $snapname);
1438 };
1439
1440 return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
1441 }});
1442
1443 __PACKAGE__->register_method({
1444 name => 'snapshot_list',
1445 path => '{vmid}/snapshot',
1446 method => 'GET',
1447 description => "List all snapshots.",
1448 permissions => {
1449 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1450 },
1451 proxyto => 'node',
1452 protected => 1, # lxc pid files are only readable by root
1453 parameters => {
1454 additionalProperties => 0,
1455 properties => {
1456 vmid => get_standard_option('pve-vmid'),
1457 node => get_standard_option('pve-node'),
1458 },
1459 },
1460 returns => {
1461 type => 'array',
1462 items => {
1463 type => "object",
1464 properties => {},
1465 },
1466 links => [ { rel => 'child', href => "{name}" } ],
1467 },
1468 code => sub {
1469 my ($param) = @_;
1470
1471 my $vmid = $param->{vmid};
1472
1473 my $conf = PVE::LXC::load_config($vmid);
1474 my $snaphash = $conf->{snapshots} || {};
1475
1476 my $res = [];
1477
1478 foreach my $name (keys %$snaphash) {
1479 my $d = $snaphash->{$name};
1480 my $item = {
1481 name => $name,
1482 snaptime => $d->{'pve.snaptime'} || 0,
1483 description => $d->{'pve.snapcomment'} || '',
1484 };
1485 $item->{parent} = $d->{'pve.parent'} if $d->{'pve.parent'};
1486 $item->{snapstate} = $d->{'pve.snapstate'} if $d->{'pve.snapstate'};
1487 push @$res, $item;
1488 }
1489
1490 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
1491 my $current = { name => 'current', digest => $conf->{digest}, running => $running };
1492 $current->{parent} = $conf->{'pve.parent'} if $conf->{'pve.parent'};
1493
1494 push @$res, $current;
1495
1496 return $res;
1497 }});
1498
1499 __PACKAGE__->register_method({
1500 name => 'snapshot_cmd_idx',
1501 path => '{vmid}/snapshot/{snapname}',
1502 description => '',
1503 method => 'GET',
1504 permissions => {
1505 user => 'all',
1506 },
1507 parameters => {
1508 additionalProperties => 0,
1509 properties => {
1510 vmid => get_standard_option('pve-vmid'),
1511 node => get_standard_option('pve-node'),
1512 snapname => get_standard_option('pve-lxc-snapshot-name'),
1513 },
1514 },
1515 returns => {
1516 type => 'array',
1517 items => {
1518 type => "object",
1519 properties => {},
1520 },
1521 links => [ { rel => 'child', href => "{cmd}" } ],
1522 },
1523 code => sub {
1524 my ($param) = @_;
1525
1526 my $res = [];
1527
1528 push @$res, { cmd => 'rollback' };
1529 push @$res, { cmd => 'config' };
1530
1531 return $res;
1532 }});
1533
1534 __PACKAGE__->register_method({
1535 name => 'update_snapshot_config',
1536 path => '{vmid}/snapshot/{snapname}/config',
1537 method => 'PUT',
1538 protected => 1,
1539 proxyto => 'node',
1540 description => "Update snapshot metadata.",
1541 permissions => {
1542 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1543 },
1544 parameters => {
1545 additionalProperties => 0,
1546 properties => {
1547 node => get_standard_option('pve-node'),
1548 vmid => get_standard_option('pve-vmid'),
1549 snapname => get_standard_option('pve-lxc-snapshot-name'),
1550 description => {
1551 optional => 1,
1552 type => 'string',
1553 description => "A textual description or comment.",
1554 },
1555 },
1556 },
1557 returns => { type => 'null' },
1558 code => sub {
1559 my ($param) = @_;
1560
1561 my $rpcenv = PVE::RPCEnvironment::get();
1562
1563 my $authuser = $rpcenv->get_user();
1564
1565 my $vmid = extract_param($param, 'vmid');
1566
1567 my $snapname = extract_param($param, 'snapname');
1568
1569 return undef if !defined($param->{description});
1570
1571 my $updatefn = sub {
1572
1573 my $conf = PVE::LXC::load_config($vmid);
1574 PVE::LXC::check_lock($conf);
1575
1576 my $snap = $conf->{snapshots}->{$snapname};
1577
1578 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1579
1580 $snap->{'pve.snapcomment'} = $param->{description} if defined($param->{description});
1581
1582 PVE::LXC::write_config($vmid, $conf, 1);
1583 };
1584
1585 PVE::LXC::lock_container($vmid, 10, $updatefn);
1586
1587 return undef;
1588 }});
1589
1590 __PACKAGE__->register_method({
1591 name => 'get_snapshot_config',
1592 path => '{vmid}/snapshot/{snapname}/config',
1593 method => 'GET',
1594 proxyto => 'node',
1595 description => "Get snapshot configuration",
1596 permissions => {
1597 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1598 },
1599 parameters => {
1600 additionalProperties => 0,
1601 properties => {
1602 node => get_standard_option('pve-node'),
1603 vmid => get_standard_option('pve-vmid'),
1604 snapname => get_standard_option('pve-lxc-snapshot-name'),
1605 },
1606 },
1607 returns => { type => "object" },
1608 code => sub {
1609 my ($param) = @_;
1610
1611 my $rpcenv = PVE::RPCEnvironment::get();
1612
1613 my $authuser = $rpcenv->get_user();
1614
1615 my $vmid = extract_param($param, 'vmid');
1616
1617 my $snapname = extract_param($param, 'snapname');
1618
1619 my $lxc_conf = PVE::LXC::load_config($vmid);
1620
1621 my $snap = $lxc_conf->{snapshots}->{$snapname};
1622
1623 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1624
1625 my $conf = PVE::LXC::lxc_conf_to_pve($param->{vmid}, $snap);
1626
1627 return $conf;
1628 }});
1629
1630 __PACKAGE__->register_method({
1631 name => 'vm_feature',
1632 path => '{vmid}/feature',
1633 method => 'GET',
1634 proxyto => 'node',
1635 protected => 1,
1636 description => "Check if feature for virtual machine is available.",
1637 permissions => {
1638 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1639 },
1640 parameters => {
1641 additionalProperties => 0,
1642 properties => {
1643 node => get_standard_option('pve-node'),
1644 vmid => get_standard_option('pve-vmid'),
1645 feature => {
1646 description => "Feature to check.",
1647 type => 'string',
1648 enum => [ 'snapshot' ],
1649 },
1650 snapname => get_standard_option('pve-lxc-snapshot-name', {
1651 optional => 1,
1652 }),
1653 },
1654 },
1655 returns => {
1656 type => "object",
1657 properties => {
1658 hasFeature => { type => 'boolean' },
1659 #nodes => {
1660 #type => 'array',
1661 #items => { type => 'string' },
1662 #}
1663 },
1664 },
1665 code => sub {
1666 my ($param) = @_;
1667
1668 my $node = extract_param($param, 'node');
1669
1670 my $vmid = extract_param($param, 'vmid');
1671
1672 my $snapname = extract_param($param, 'snapname');
1673
1674 my $feature = extract_param($param, 'feature');
1675
1676 my $conf = PVE::LXC::load_config($vmid);
1677
1678 if($snapname){
1679 my $snap = $conf->{snapshots}->{$snapname};
1680 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1681 $conf = $snap;
1682 }
1683 my $storecfg = PVE::Storage::config();
1684 #Maybe include later
1685 #my $nodelist = PVE::LXC::shared_nodes($conf, $storecfg);
1686 my $hasFeature = PVE::LXC::has_feature($feature, $conf, $storecfg, $snapname);
1687
1688 return {
1689 hasFeature => $hasFeature,
1690 #nodes => [ keys %$nodelist ],
1691 };
1692 }});
1693 1;