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