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