]> git.proxmox.com Git - pve-container.git/blame - src/PVE/API2/LXC.pm
Typo in resize_vm subroutine
[pve-container.git] / src / PVE / API2 / LXC.pm
CommitLineData
f76a2828
DM
1package PVE::API2::LXC;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Tools qw(extract_param run_command);
8use PVE::Exception qw(raise raise_param_exc);
9use PVE::INotify;
9c2d4ce9 10use PVE::Cluster qw(cfs_read_file);
f76a2828 11use PVE::AccessControl;
2f9b5ead 12use PVE::Firewall;
f76a2828
DM
13use PVE::Storage;
14use PVE::RESTHandler;
15use PVE::RPCEnvironment;
16use PVE::LXC;
7af97ad5 17use PVE::LXC::Create;
6f42807e 18use PVE::LXC::Migrate;
52389a07
DM
19use PVE::API2::LXC::Config;
20use PVE::API2::LXC::Status;
21use PVE::API2::LXC::Snapshot;
5c752bbf 22use PVE::HA::Config;
f76a2828
DM
23use PVE::JSONSchema qw(get_standard_option);
24use base qw(PVE::RESTHandler);
25
26use Data::Dumper; # fixme: remove
27
52389a07
DM
28__PACKAGE__->register_method ({
29 subclass => "PVE::API2::LXC::Config",
30 path => '{vmid}/config',
31});
f76a2828 32
52389a07
DM
33__PACKAGE__->register_method ({
34 subclass => "PVE::API2::LXC::Status",
35 path => '{vmid}/status',
36});
f76a2828 37
52389a07
DM
38__PACKAGE__->register_method ({
39 subclass => "PVE::API2::LXC::Snapshot",
40 path => '{vmid}/snapshot',
41});
f76a2828 42
52389a07
DM
43__PACKAGE__->register_method ({
44 subclass => "PVE::API2::Firewall::CT",
45 path => '{vmid}/firewall',
46});
1e6c8d5b 47
f76a2828 48__PACKAGE__->register_method({
5c752bbf
DM
49 name => 'vmlist',
50 path => '',
f76a2828
DM
51 method => 'GET',
52 description => "LXC container index (per node).",
53 permissions => {
54 description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
55 user => 'all',
56 },
57 proxyto => 'node',
58 protected => 1, # /proc files are only readable by root
59 parameters => {
60 additionalProperties => 0,
61 properties => {
62 node => get_standard_option('pve-node'),
63 },
64 },
65 returns => {
66 type => 'array',
67 items => {
68 type => "object",
69 properties => {},
70 },
71 links => [ { rel => 'child', href => "{vmid}" } ],
72 },
73 code => sub {
74 my ($param) = @_;
75
76 my $rpcenv = PVE::RPCEnvironment::get();
77 my $authuser = $rpcenv->get_user();
78
79 my $vmstatus = PVE::LXC::vmstatus();
80
81 my $res = [];
82 foreach my $vmid (keys %$vmstatus) {
83 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
84
85 my $data = $vmstatus->{$vmid};
86 $data->{vmid} = $vmid;
87 push @$res, $data;
88 }
89
90 return $res;
5c752bbf 91
f76a2828
DM
92 }});
93
9c2d4ce9 94__PACKAGE__->register_method({
5c752bbf
DM
95 name => 'create_vm',
96 path => '',
9c2d4ce9
DM
97 method => 'POST',
98 description => "Create or restore a container.",
99 permissions => {
100 user => 'all', # check inside
101 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
102 "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
103 "You also need 'Datastore.AllocateSpace' permissions on the storage.",
104 },
105 protected => 1,
106 proxyto => 'node',
107 parameters => {
108 additionalProperties => 0,
eb35f9c0 109 properties => PVE::LXC::json_config_properties({
9c2d4ce9 110 node => get_standard_option('pve-node'),
781e26b2 111 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
9c2d4ce9
DM
112 ostemplate => {
113 description => "The OS template or backup file.",
5c752bbf 114 type => 'string',
9c2d4ce9 115 maxLength => 255,
68e8f3c5 116 completion => \&PVE::LXC::complete_os_templates,
9c2d4ce9 117 },
5c752bbf
DM
118 password => {
119 optional => 1,
9c2d4ce9
DM
120 type => 'string',
121 description => "Sets root password inside container.",
168d6b07 122 minLength => 5,
9c2d4ce9
DM
123 },
124 storage => get_standard_option('pve-storage-id', {
eb35f9c0 125 description => "Default Storage.",
9c2d4ce9
DM
126 default => 'local',
127 optional => 1,
c5362cda 128 completion => \&PVE::Storage::complete_storage_enabled,
9c2d4ce9
DM
129 }),
130 force => {
5c752bbf 131 optional => 1,
9c2d4ce9
DM
132 type => 'boolean',
133 description => "Allow to overwrite existing container.",
134 },
135 restore => {
5c752bbf 136 optional => 1,
9c2d4ce9
DM
137 type => 'boolean',
138 description => "Mark this as restore task.",
139 },
5c752bbf 140 pool => {
9c2d4ce9
DM
141 optional => 1,
142 type => 'string', format => 'pve-poolid',
143 description => "Add the VM to the specified pool.",
144 },
145 }),
146 },
5c752bbf 147 returns => {
9c2d4ce9
DM
148 type => 'string',
149 },
150 code => sub {
151 my ($param) = @_;
152
153 my $rpcenv = PVE::RPCEnvironment::get();
154
155 my $authuser = $rpcenv->get_user();
156
157 my $node = extract_param($param, 'node');
158
159 my $vmid = extract_param($param, 'vmid');
160
161 my $basecfg_fn = PVE::LXC::config_file($vmid);
162
163 my $same_container_exists = -f $basecfg_fn;
164
165 my $restore = extract_param($param, 'restore');
166
148d1cb4
DM
167 if ($restore) {
168 # fixme: limit allowed parameters
169
170 }
171
9c2d4ce9
DM
172 my $force = extract_param($param, 'force');
173
174 if (!($same_container_exists && $restore && $force)) {
175 PVE::Cluster::check_vmid_unused($vmid);
e22af68f
AG
176 } else {
177 my $conf = PVE::LXC::load_config($vmid);
178 PVE::LXC::check_protection($conf, "unable to restore CT $vmid");
9c2d4ce9 179 }
5c752bbf 180
9c2d4ce9
DM
181 my $password = extract_param($param, 'password');
182
27916659
DM
183 my $storage = extract_param($param, 'storage') // 'local';
184
9c2d4ce9
DM
185 my $storage_cfg = cfs_read_file("storage.cfg");
186
187 my $scfg = PVE::Storage::storage_check_node($storage_cfg, $storage, $node);
188
189 raise_param_exc({ storage => "storage '$storage' does not support container root directories"})
644f2f8f 190 if !($scfg->{content}->{images} || $scfg->{content}->{rootdir});
9c2d4ce9 191
27916659
DM
192 my $pool = extract_param($param, 'pool');
193
9c2d4ce9
DM
194 if (defined($pool)) {
195 $rpcenv->check_pool_exist($pool);
196 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
5c752bbf 197 }
9c2d4ce9 198
27916659
DM
199 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
200
9c2d4ce9
DM
201 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
202 # OK
203 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
204 # OK
205 } elsif ($restore && $force && $same_container_exists &&
206 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
207 # OK: user has VM.Backup permissions, and want to restore an existing VM
208 } else {
209 raise_perm_exc();
210 }
211
52389a07 212 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
9c2d4ce9
DM
213
214 PVE::Storage::activate_storage($storage_cfg, $storage);
215
216 my $ostemplate = extract_param($param, 'ostemplate');
5c752bbf 217
9c2d4ce9
DM
218 my $archive;
219
220 if ($ostemplate eq '-') {
148d1cb4
DM
221 die "pipe requires cli environment\n"
222 if $rpcenv->{type} ne 'cli';
223 die "pipe can only be used with restore tasks\n"
224 if !$restore;
225 $archive = '-';
b51a98d4 226 die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs});
9c2d4ce9
DM
227 } else {
228 $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
229 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
230 }
231
9c2d4ce9 232 my $conf = {};
5b4657d0 233
b51a98d4
DM
234 my $no_disk_param = {};
235 foreach my $opt (keys %$param) {
78ccc99b
DM
236 my $value = $param->{$opt};
237 if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) {
238 # allow to use simple numbers (add default storage in that case)
239 $param->{$opt} = "$storage:$value" if $value =~ m/^\d+(\.\d+)?$/;
240 } else {
241 $no_disk_param->{$opt} = $value;
242 }
b51a98d4
DM
243 }
244 PVE::LXC::update_pct_config($vmid, $conf, 0, $no_disk_param);
9c2d4ce9 245
148d1cb4
DM
246 my $check_vmid_usage = sub {
247 if ($force) {
5c45496e 248 die "can't overwrite running container\n"
148d1cb4
DM
249 if PVE::LXC::check_running($vmid);
250 } else {
251 PVE::Cluster::check_vmid_unused($vmid);
252 }
253 };
f507c3a7 254
5b4657d0 255 my $code = sub {
148d1cb4 256 &$check_vmid_usage(); # final check after locking
87273b2b 257
148d1cb4 258 PVE::Cluster::check_cfs_quorum();
eb35f9c0 259 my $vollist = [];
27916659
DM
260
261 eval {
b51a98d4
DM
262 if (!defined($param->{rootfs})) {
263 if ($restore) {
264 my (undef, $disksize) = PVE::LXC::Create::recover_config($archive);
8ed5ff9d 265 $disksize /= 1024 * 1024 * 1024; # create_disks expects GB as unit size
b51a98d4
DM
266 die "unable to detect disk size - please specify rootfs (size)\n"
267 if !$disksize;
78ccc99b 268 $param->{rootfs} = "$storage:$disksize";
b51a98d4 269 } else {
78ccc99b 270 $param->{rootfs} = "$storage:4"; # defaults to 4GB
b51a98d4
DM
271 }
272 }
273
6c871c36 274 $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $param, $conf);
eb35f9c0
AD
275
276 PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf, $archive, $password, $restore);
27916659
DM
277 # set some defaults
278 $conf->{hostname} ||= "CT$vmid";
279 $conf->{memory} ||= 512;
280 $conf->{swap} //= 512;
27916659
DM
281 PVE::LXC::create_config($vmid, $conf);
282 };
283 if (my $err = $@) {
6c871c36 284 PVE::LXC::destroy_disks($storage_cfg, $vollist);
27916659
DM
285 PVE::LXC::destroy_config($vmid);
286 die $err;
6d098bf4 287 }
87273b2b 288 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
9c2d4ce9 289 };
5c752bbf 290
9c2d4ce9
DM
291 my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
292
148d1cb4
DM
293 &$check_vmid_usage(); # first check before locking
294
295 return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
9c2d4ce9 296 $vmid, $authuser, $realcmd);
5c752bbf 297
9c2d4ce9
DM
298 }});
299
f76a2828
DM
300__PACKAGE__->register_method({
301 name => 'vmdiridx',
5c752bbf 302 path => '{vmid}',
f76a2828
DM
303 method => 'GET',
304 proxyto => 'node',
305 description => "Directory index",
306 permissions => {
307 user => 'all',
308 },
309 parameters => {
310 additionalProperties => 0,
311 properties => {
312 node => get_standard_option('pve-node'),
313 vmid => get_standard_option('pve-vmid'),
314 },
315 },
316 returns => {
317 type => 'array',
318 items => {
319 type => "object",
320 properties => {
321 subdir => { type => 'string' },
322 },
323 },
324 links => [ { rel => 'child', href => "{subdir}" } ],
325 },
326 code => sub {
327 my ($param) = @_;
328
329 # test if VM exists
e901d418 330 my $conf = PVE::LXC::load_config($param->{vmid});
f76a2828
DM
331
332 my $res = [
333 { subdir => 'config' },
fff3a342
DM
334 { subdir => 'status' },
335 { subdir => 'vncproxy' },
336 { subdir => 'vncwebsocket' },
337 { subdir => 'spiceproxy' },
338 { subdir => 'migrate' },
f76a2828
DM
339# { subdir => 'initlog' },
340 { subdir => 'rrd' },
341 { subdir => 'rrddata' },
342 { subdir => 'firewall' },
cc5392c8 343 { subdir => 'snapshot' },
f76a2828 344 ];
5c752bbf 345
f76a2828
DM
346 return $res;
347 }});
348
349__PACKAGE__->register_method({
5c752bbf
DM
350 name => 'rrd',
351 path => '{vmid}/rrd',
f76a2828
DM
352 method => 'GET',
353 protected => 1, # fixme: can we avoid that?
354 permissions => {
355 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
356 },
357 description => "Read VM RRD statistics (returns PNG)",
358 parameters => {
359 additionalProperties => 0,
360 properties => {
361 node => get_standard_option('pve-node'),
362 vmid => get_standard_option('pve-vmid'),
363 timeframe => {
364 description => "Specify the time frame you are interested in.",
365 type => 'string',
366 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
367 },
368 ds => {
369 description => "The list of datasources you want to display.",
370 type => 'string', format => 'pve-configid-list',
371 },
372 cf => {
373 description => "The RRD consolidation function",
374 type => 'string',
375 enum => [ 'AVERAGE', 'MAX' ],
376 optional => 1,
377 },
378 },
379 },
380 returns => {
381 type => "object",
382 properties => {
383 filename => { type => 'string' },
384 },
385 },
386 code => sub {
387 my ($param) = @_;
388
389 return PVE::Cluster::create_rrd_graph(
5c752bbf 390 "pve2-vm/$param->{vmid}", $param->{timeframe},
f76a2828 391 $param->{ds}, $param->{cf});
5c752bbf 392
f76a2828
DM
393 }});
394
395__PACKAGE__->register_method({
5c752bbf
DM
396 name => 'rrddata',
397 path => '{vmid}/rrddata',
f76a2828
DM
398 method => 'GET',
399 protected => 1, # fixme: can we avoid that?
400 permissions => {
401 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
402 },
403 description => "Read VM RRD statistics",
404 parameters => {
405 additionalProperties => 0,
406 properties => {
407 node => get_standard_option('pve-node'),
408 vmid => get_standard_option('pve-vmid'),
409 timeframe => {
410 description => "Specify the time frame you are interested in.",
411 type => 'string',
412 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
413 },
414 cf => {
415 description => "The RRD consolidation function",
416 type => 'string',
417 enum => [ 'AVERAGE', 'MAX' ],
418 optional => 1,
419 },
420 },
421 },
422 returns => {
423 type => "array",
424 items => {
425 type => "object",
426 properties => {},
427 },
428 },
429 code => sub {
430 my ($param) = @_;
431
432 return PVE::Cluster::create_rrd_data(
433 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
434 }});
435
f76a2828 436__PACKAGE__->register_method({
5c752bbf
DM
437 name => 'destroy_vm',
438 path => '{vmid}',
f76a2828
DM
439 method => 'DELETE',
440 protected => 1,
441 proxyto => 'node',
442 description => "Destroy the container (also delete all uses files).",
443 permissions => {
444 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
445 },
446 parameters => {
447 additionalProperties => 0,
448 properties => {
449 node => get_standard_option('pve-node'),
68e8f3c5 450 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
f76a2828
DM
451 },
452 },
5c752bbf 453 returns => {
f76a2828
DM
454 type => 'string',
455 },
456 code => sub {
457 my ($param) = @_;
458
459 my $rpcenv = PVE::RPCEnvironment::get();
460
461 my $authuser = $rpcenv->get_user();
462
463 my $vmid = $param->{vmid};
464
465 # test if container exists
673cf209 466 my $conf = PVE::LXC::load_config($vmid);
f76a2828 467
611fe3aa
DM
468 my $storage_cfg = cfs_read_file("storage.cfg");
469
e22af68f 470 PVE::LXC::check_protection($conf, "can't remove CT $vmid");
7e806596 471
9d87e069 472 die "unable to remove CT $vmid - used in HA resources\n"
b6e0b774
AG
473 if PVE::HA::Config::vm_is_ha_managed($vmid);
474
611fe3aa 475 my $code = sub {
673cf209
DM
476 # reload config after lock
477 $conf = PVE::LXC::load_config($vmid);
478 PVE::LXC::check_lock($conf);
479
27916659 480 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf);
be5fc936 481 PVE::AccessControl::remove_vm_access($vmid);
2f9b5ead 482 PVE::Firewall::remove_vmfw_conf($vmid);
f76a2828
DM
483 };
484
611fe3aa
DM
485 my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
486
f76a2828
DM
487 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
488 }});
489
fff3a342
DM
490my $sslcert;
491
492__PACKAGE__->register_method ({
5b4657d0
DM
493 name => 'vncproxy',
494 path => '{vmid}/vncproxy',
fff3a342
DM
495 method => 'POST',
496 protected => 1,
497 permissions => {
498 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
499 },
500 description => "Creates a TCP VNC proxy connections.",
501 parameters => {
502 additionalProperties => 0,
503 properties => {
504 node => get_standard_option('pve-node'),
505 vmid => get_standard_option('pve-vmid'),
506 websocket => {
507 optional => 1,
508 type => 'boolean',
509 description => "use websocket instead of standard VNC.",
510 },
511 },
512 },
5b4657d0 513 returns => {
fff3a342
DM
514 additionalProperties => 0,
515 properties => {
516 user => { type => 'string' },
517 ticket => { type => 'string' },
518 cert => { type => 'string' },
519 port => { type => 'integer' },
520 upid => { type => 'string' },
521 },
522 },
523 code => sub {
524 my ($param) = @_;
525
526 my $rpcenv = PVE::RPCEnvironment::get();
527
528 my $authuser = $rpcenv->get_user();
529
530 my $vmid = $param->{vmid};
531 my $node = $param->{node};
532
533 my $authpath = "/vms/$vmid";
534
535 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
536
537 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
538 if !$sslcert;
539
ec2c57eb 540 my ($remip, $family);
5b4657d0 541
fff3a342 542 if ($node ne PVE::INotify::nodename()) {
85ae6211 543 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
ec2c57eb
WB
544 } else {
545 $family = PVE::Tools::get_host_address_family($node);
fff3a342
DM
546 }
547
ec2c57eb
WB
548 my $port = PVE::Tools::next_vnc_port($family);
549
fff3a342
DM
550 # NOTE: vncterm VNC traffic is already TLS encrypted,
551 # so we select the fastest chipher here (or 'none'?)
5b4657d0 552 my $remcmd = $remip ?
fff3a342
DM
553 ['/usr/bin/ssh', '-t', $remip] : [];
554
d18499cf 555 my $conf = PVE::LXC::load_config($vmid, $node);
aca816ad
DM
556 my $concmd = PVE::LXC::get_console_command($vmid, $conf);
557
5b4657d0
DM
558 my $shcmd = [ '/usr/bin/dtach', '-A',
559 "/var/run/dtach/vzctlconsole$vmid",
aca816ad 560 '-r', 'winch', '-z', @$concmd];
fff3a342
DM
561
562 my $realcmd = sub {
563 my $upid = shift;
564
5b4657d0 565 syslog ('info', "starting lxc vnc proxy $upid\n");
fff3a342 566
5b4657d0 567 my $timeout = 10;
fff3a342
DM
568
569 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
5b4657d0 570 '-timeout', $timeout, '-authpath', $authpath,
fff3a342
DM
571 '-perm', 'VM.Console'];
572
573 if ($param->{websocket}) {
5b4657d0 574 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
fff3a342
DM
575 push @$cmd, '-notls', '-listen', 'localhost';
576 }
577
578 push @$cmd, '-c', @$remcmd, @$shcmd;
579
580 run_command($cmd);
581
582 return;
583 };
584
585 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
586
587 PVE::Tools::wait_for_vnc_port($port);
588
589 return {
590 user => $authuser,
591 ticket => $ticket,
5b4657d0
DM
592 port => $port,
593 upid => $upid,
594 cert => $sslcert,
fff3a342
DM
595 };
596 }});
597
598__PACKAGE__->register_method({
599 name => 'vncwebsocket',
600 path => '{vmid}/vncwebsocket',
601 method => 'GET',
5b4657d0 602 permissions => {
fff3a342
DM
603 description => "You also need to pass a valid ticket (vncticket).",
604 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
605 },
606 description => "Opens a weksocket for VNC traffic.",
607 parameters => {
608 additionalProperties => 0,
609 properties => {
610 node => get_standard_option('pve-node'),
611 vmid => get_standard_option('pve-vmid'),
612 vncticket => {
613 description => "Ticket from previous call to vncproxy.",
614 type => 'string',
615 maxLength => 512,
616 },
617 port => {
618 description => "Port number returned by previous vncproxy call.",
619 type => 'integer',
620 minimum => 5900,
621 maximum => 5999,
622 },
623 },
624 },
625 returns => {
626 type => "object",
627 properties => {
628 port => { type => 'string' },
629 },
630 },
631 code => sub {
632 my ($param) = @_;
633
634 my $rpcenv = PVE::RPCEnvironment::get();
635
636 my $authuser = $rpcenv->get_user();
637
638 my $authpath = "/vms/$param->{vmid}";
639
640 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
641
642 my $port = $param->{port};
5b4657d0 643
fff3a342
DM
644 return { port => $port };
645 }});
646
647__PACKAGE__->register_method ({
5b4657d0
DM
648 name => 'spiceproxy',
649 path => '{vmid}/spiceproxy',
fff3a342
DM
650 method => 'POST',
651 protected => 1,
652 proxyto => 'node',
653 permissions => {
654 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
655 },
656 description => "Returns a SPICE configuration to connect to the CT.",
657 parameters => {
658 additionalProperties => 0,
659 properties => {
660 node => get_standard_option('pve-node'),
661 vmid => get_standard_option('pve-vmid'),
662 proxy => get_standard_option('spice-proxy', { optional => 1 }),
663 },
664 },
665 returns => get_standard_option('remote-viewer-config'),
666 code => sub {
667 my ($param) = @_;
668
669 my $vmid = $param->{vmid};
670 my $node = $param->{node};
671 my $proxy = $param->{proxy};
672
673 my $authpath = "/vms/$vmid";
674 my $permissions = 'VM.Console';
675
aca816ad 676 my $conf = PVE::LXC::load_config($vmid);
da4db334
TL
677
678 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
679
aca816ad
DM
680 my $concmd = PVE::LXC::get_console_command($vmid, $conf);
681
5b4657d0
DM
682 my $shcmd = ['/usr/bin/dtach', '-A',
683 "/var/run/dtach/vzctlconsole$vmid",
aca816ad 684 '-r', 'winch', '-z', @$concmd];
fff3a342
DM
685
686 my $title = "CT $vmid";
687
688 return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
689 }});
5c752bbf 690
5c752bbf
DM
691
692__PACKAGE__->register_method({
52389a07
DM
693 name => 'migrate_vm',
694 path => '{vmid}/migrate',
5c752bbf
DM
695 method => 'POST',
696 protected => 1,
697 proxyto => 'node',
52389a07 698 description => "Migrate the container to another node. Creates a new migration task.",
5c752bbf 699 permissions => {
52389a07 700 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
5c752bbf
DM
701 },
702 parameters => {
703 additionalProperties => 0,
704 properties => {
705 node => get_standard_option('pve-node'),
68e8f3c5
DM
706 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
707 target => get_standard_option('pve-node', {
708 description => "Target node.",
39bb88df 709 completion => \&PVE::Cluster::complete_migration_target,
68e8f3c5 710 }),
52389a07
DM
711 online => {
712 type => 'boolean',
713 description => "Use online/live migration.",
714 optional => 1,
715 },
5c752bbf
DM
716 },
717 },
718 returns => {
719 type => 'string',
52389a07 720 description => "the task ID.",
5c752bbf
DM
721 },
722 code => sub {
723 my ($param) = @_;
724
725 my $rpcenv = PVE::RPCEnvironment::get();
726
727 my $authuser = $rpcenv->get_user();
728
52389a07 729 my $target = extract_param($param, 'target');
bb1ac2de 730
52389a07
DM
731 my $localnode = PVE::INotify::nodename();
732 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
bb1ac2de 733
52389a07 734 PVE::Cluster::check_cfs_quorum();
5c752bbf 735
52389a07 736 PVE::Cluster::check_node_exists($target);
27916659 737
52389a07 738 my $targetip = PVE::Cluster::remote_node_ip($target);
5c752bbf 739
52389a07 740 my $vmid = extract_param($param, 'vmid');
5c752bbf 741
52389a07
DM
742 # test if VM exists
743 PVE::LXC::load_config($vmid);
5c752bbf 744
52389a07
DM
745 # try to detect errors early
746 if (PVE::LXC::check_running($vmid)) {
5c45496e 747 die "can't migrate running container without --online\n"
52389a07 748 if !$param->{online};
5c752bbf 749 }
5c752bbf
DM
750
751 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
752
753 my $hacmd = sub {
754 my $upid = shift;
755
756 my $service = "ct:$vmid";
757
52389a07 758 my $cmd = ['ha-manager', 'migrate', $service, $target];
5c752bbf 759
52389a07 760 print "Executing HA migrate for CT $vmid to node $target\n";
5c752bbf
DM
761
762 PVE::Tools::run_command($cmd);
763
764 return;
765 };
766
52389a07 767 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
5c752bbf
DM
768
769 } else {
770
771 my $realcmd = sub {
772 my $upid = shift;
773
6f42807e 774 PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param);
5c752bbf
DM
775
776 return;
777 };
778
52389a07 779 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
5c752bbf
DM
780 }
781 }});
782
cc5392c8
WL
783__PACKAGE__->register_method({
784 name => 'vm_feature',
785 path => '{vmid}/feature',
786 method => 'GET',
787 proxyto => 'node',
788 protected => 1,
789 description => "Check if feature for virtual machine is available.",
790 permissions => {
791 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
792 },
793 parameters => {
794 additionalProperties => 0,
795 properties => {
796 node => get_standard_option('pve-node'),
797 vmid => get_standard_option('pve-vmid'),
798 feature => {
799 description => "Feature to check.",
800 type => 'string',
801 enum => [ 'snapshot' ],
802 },
803 snapname => get_standard_option('pve-lxc-snapshot-name', {
804 optional => 1,
805 }),
806 },
807 },
808 returns => {
809 type => "object",
810 properties => {
811 hasFeature => { type => 'boolean' },
812 #nodes => {
813 #type => 'array',
814 #items => { type => 'string' },
815 #}
816 },
817 },
818 code => sub {
819 my ($param) = @_;
820
821 my $node = extract_param($param, 'node');
822
823 my $vmid = extract_param($param, 'vmid');
824
825 my $snapname = extract_param($param, 'snapname');
826
827 my $feature = extract_param($param, 'feature');
828
829 my $conf = PVE::LXC::load_config($vmid);
830
831 if($snapname){
832 my $snap = $conf->{snapshots}->{$snapname};
833 die "snapshot '$snapname' does not exist\n" if !defined($snap);
834 $conf = $snap;
835 }
ef241384 836 my $storage_cfg = PVE::Storage::config();
cc5392c8 837 #Maybe include later
ef241384
DM
838 #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
839 my $hasFeature = PVE::LXC::has_feature($feature, $conf, $storage_cfg, $snapname);
cc5392c8
WL
840
841 return {
842 hasFeature => $hasFeature,
843 #nodes => [ keys %$nodelist ],
844 };
845 }});
bb1ac2de
DM
846
847__PACKAGE__->register_method({
848 name => 'template',
849 path => '{vmid}/template',
850 method => 'POST',
851 protected => 1,
852 proxyto => 'node',
853 description => "Create a Template.",
854 permissions => {
855 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
856 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
857 },
858 parameters => {
859 additionalProperties => 0,
860 properties => {
861 node => get_standard_option('pve-node'),
68e8f3c5 862 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
bb1ac2de
DM
863 },
864 },
865 returns => { type => 'null'},
866 code => sub {
867 my ($param) = @_;
868
869 my $rpcenv = PVE::RPCEnvironment::get();
870
871 my $authuser = $rpcenv->get_user();
872
873 my $node = extract_param($param, 'node');
874
875 my $vmid = extract_param($param, 'vmid');
876
877 my $updatefn = sub {
878
879 my $conf = PVE::LXC::load_config($vmid);
880 PVE::LXC::check_lock($conf);
881
882 die "unable to create template, because CT contains snapshots\n"
883 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
884
885 die "you can't convert a template to a template\n"
886 if PVE::LXC::is_template($conf);
887
888 die "you can't convert a CT to template if the CT is running\n"
889 if PVE::LXC::check_running($vmid);
890
891 my $realcmd = sub {
892 PVE::LXC::template_create($vmid, $conf);
893 };
894
895 $conf->{template} = 1;
896
897 PVE::LXC::write_config($vmid, $conf);
898 # and remove lxc config
899 PVE::LXC::update_lxc_config(undef, $vmid, $conf);
900
901 return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
902 };
903
904 PVE::LXC::lock_container($vmid, undef, $updatefn);
905
906 return undef;
907 }});
908
f76a2828 9091;