]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC.pm
restore: correctly handle fw config from archive
[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 raise_perm_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Firewall;
13 use PVE::Storage;
14 use PVE::RESTHandler;
15 use PVE::RPCEnvironment;
16 use PVE::ReplicationConfig;
17 use PVE::LXC;
18 use PVE::LXC::Create;
19 use PVE::LXC::Migrate;
20 use PVE::GuestHelpers;
21 use PVE::API2::LXC::Config;
22 use PVE::API2::LXC::Status;
23 use PVE::API2::LXC::Snapshot;
24 use PVE::JSONSchema qw(get_standard_option);
25 use base qw(PVE::RESTHandler);
26
27 BEGIN {
28 if (!$ENV{PVE_GENERATING_DOCS}) {
29 require PVE::HA::Env::PVE2;
30 import PVE::HA::Env::PVE2;
31 require PVE::HA::Config;
32 import PVE::HA::Config;
33 }
34 }
35
36 __PACKAGE__->register_method ({
37 subclass => "PVE::API2::LXC::Config",
38 path => '{vmid}/config',
39 });
40
41 __PACKAGE__->register_method ({
42 subclass => "PVE::API2::LXC::Status",
43 path => '{vmid}/status',
44 });
45
46 __PACKAGE__->register_method ({
47 subclass => "PVE::API2::LXC::Snapshot",
48 path => '{vmid}/snapshot',
49 });
50
51 __PACKAGE__->register_method ({
52 subclass => "PVE::API2::Firewall::CT",
53 path => '{vmid}/firewall',
54 });
55
56 __PACKAGE__->register_method({
57 name => 'vmlist',
58 path => '',
59 method => 'GET',
60 description => "LXC container index (per node).",
61 permissions => {
62 description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
63 user => 'all',
64 },
65 proxyto => 'node',
66 protected => 1, # /proc files are only readable by root
67 parameters => {
68 additionalProperties => 0,
69 properties => {
70 node => get_standard_option('pve-node'),
71 },
72 },
73 returns => {
74 type => 'array',
75 items => {
76 type => "object",
77 properties => $PVE::LXC::vmstatus_return_properties,
78 },
79 links => [ { rel => 'child', href => "{vmid}" } ],
80 },
81 code => sub {
82 my ($param) = @_;
83
84 my $rpcenv = PVE::RPCEnvironment::get();
85 my $authuser = $rpcenv->get_user();
86
87 my $vmstatus = PVE::LXC::vmstatus();
88
89 my $res = [];
90 foreach my $vmid (keys %$vmstatus) {
91 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
92
93 my $data = $vmstatus->{$vmid};
94 push @$res, $data;
95 }
96
97 return $res;
98
99 }});
100
101 __PACKAGE__->register_method({
102 name => 'create_vm',
103 path => '',
104 method => 'POST',
105 description => "Create or restore a container.",
106 permissions => {
107 user => 'all', # check inside
108 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
109 "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
110 "You also need 'Datastore.AllocateSpace' permissions on the storage.",
111 },
112 protected => 1,
113 proxyto => 'node',
114 parameters => {
115 additionalProperties => 0,
116 properties => PVE::LXC::Config->json_config_properties({
117 node => get_standard_option('pve-node'),
118 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
119 ostemplate => {
120 description => "The OS template or backup file.",
121 type => 'string',
122 maxLength => 255,
123 completion => \&PVE::LXC::complete_os_templates,
124 },
125 password => {
126 optional => 1,
127 type => 'string',
128 description => "Sets root password inside container.",
129 minLength => 5,
130 },
131 storage => get_standard_option('pve-storage-id', {
132 description => "Default Storage.",
133 default => 'local',
134 optional => 1,
135 completion => \&PVE::Storage::complete_storage_enabled,
136 }),
137 force => {
138 optional => 1,
139 type => 'boolean',
140 description => "Allow to overwrite existing container.",
141 },
142 restore => {
143 optional => 1,
144 type => 'boolean',
145 description => "Mark this as restore task.",
146 },
147 unique => {
148 optional => 1,
149 type => 'boolean',
150 description => "Assign a unique random ethernet address.",
151 requires => 'restore',
152 },
153 pool => {
154 optional => 1,
155 type => 'string', format => 'pve-poolid',
156 description => "Add the VM to the specified pool.",
157 },
158 'ignore-unpack-errors' => {
159 optional => 1,
160 type => 'boolean',
161 description => "Ignore errors when extracting the template.",
162 },
163 'ssh-public-keys' => {
164 optional => 1,
165 type => 'string',
166 description => "Setup public SSH keys (one key per line, " .
167 "OpenSSH format).",
168 },
169 bwlimit => {
170 description => "Override I/O bandwidth limit (in KiB/s).",
171 optional => 1,
172 type => 'number',
173 minimum => '0',
174 default => 'restore limit from datacenter or storage config',
175 },
176 start => {
177 optional => 1,
178 type => 'boolean',
179 default => 0,
180 description => "Start the CT after its creation finished successfully.",
181 },
182 }),
183 },
184 returns => {
185 type => 'string',
186 },
187 code => sub {
188 my ($param) = @_;
189
190 PVE::Cluster::check_cfs_quorum();
191
192 my $rpcenv = PVE::RPCEnvironment::get();
193 my $authuser = $rpcenv->get_user();
194
195 my $node = extract_param($param, 'node');
196 my $vmid = extract_param($param, 'vmid');
197 my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors');
198 my $bwlimit = extract_param($param, 'bwlimit');
199 my $start_after_create = extract_param($param, 'start');
200
201 my $basecfg_fn = PVE::LXC::Config->config_file($vmid);
202 my $same_container_exists = -f $basecfg_fn;
203
204 # 'unprivileged' is read-only, so we can't pass it to update_pct_config
205 my $unprivileged = extract_param($param, 'unprivileged');
206 my $restore = extract_param($param, 'restore');
207 my $unique = extract_param($param, 'unique');
208
209 # used to skip firewall config restore if user lacks permission
210 my $skip_fw_config_restore = 0;
211
212 if ($restore) {
213 # fixme: limit allowed parameters
214 }
215
216 my $force = extract_param($param, 'force');
217
218 if (!($same_container_exists && $restore && $force)) {
219 PVE::Cluster::check_vmid_unused($vmid);
220 } else {
221 die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
222 my $conf = PVE::LXC::Config->load_config($vmid);
223 PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid");
224 }
225
226 my $password = extract_param($param, 'password');
227 my $ssh_keys = extract_param($param, 'ssh-public-keys');
228 PVE::Tools::validate_ssh_public_keys($ssh_keys) if defined($ssh_keys);
229
230 my $pool = extract_param($param, 'pool');
231 if (defined($pool)) {
232 $rpcenv->check_pool_exist($pool);
233 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
234 }
235
236 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
237 # OK
238 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
239 # OK
240 } elsif ($restore && $force && $same_container_exists &&
241 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
242 # OK: user has VM.Backup permissions, and want to restore an existing VM
243
244 # we don't want to restore a container-provided FW conf in this case
245 # since the user is lacking permission to configure the container's FW
246 $skip_fw_config_restore = 1;
247 } else {
248 raise_perm_exc();
249 }
250
251 my $ostemplate = extract_param($param, 'ostemplate');
252 my $storage = extract_param($param, 'storage') // 'local';
253
254 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []);
255
256 my $storage_cfg = cfs_read_file("storage.cfg");
257
258 my $archive;
259 if ($ostemplate eq '-') {
260 die "pipe requires cli environment\n"
261 if $rpcenv->{type} ne 'cli';
262 die "pipe can only be used with restore tasks\n"
263 if !$restore;
264 $archive = '-';
265 die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs});
266 } else {
267 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, $vmid, $ostemplate);
268 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
269 }
270
271 my %used_storages;
272 my $check_and_activate_storage = sub {
273 my ($sid) = @_;
274
275 my $scfg = PVE::Storage::storage_check_node($storage_cfg, $sid, $node);
276
277 raise_param_exc({ storage => "storage '$sid' does not support container directories"})
278 if !$scfg->{content}->{rootdir};
279
280 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
281
282 PVE::Storage::activate_storage($storage_cfg, $sid);
283 $used_storages{$sid} = 1;
284 };
285
286 my $conf = {};
287
288 my $is_root = $authuser eq 'root@pam';
289
290 my $no_disk_param = {};
291 my $mp_param = {};
292 my $storage_only_mode = 1;
293 foreach my $opt (keys %$param) {
294 my $value = $param->{$opt};
295 if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) {
296 # allow to use simple numbers (add default storage in that case)
297 if ($value =~ m/^\d+(\.\d+)?$/) {
298 $mp_param->{$opt} = "$storage:$value";
299 } else {
300 $mp_param->{$opt} = $value;
301 }
302 $storage_only_mode = 0;
303 } elsif ($opt =~ m/^unused\d+$/) {
304 warn "ignoring '$opt', cannot create/restore with unused volume\n";
305 delete $param->{$opt};
306 } else {
307 $no_disk_param->{$opt} = $value;
308 }
309 }
310
311 die "mount points configured, but 'rootfs' not set - aborting\n"
312 if !$storage_only_mode && !defined($mp_param->{rootfs});
313
314 # check storage access, activate storage
315 my $delayed_mp_param = {};
316 PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
317 my ($ms, $mountpoint) = @_;
318
319 my $volid = $mountpoint->{volume};
320 my $mp = $mountpoint->{mp};
321
322 if ($mountpoint->{type} ne 'volume') { # bind or device
323 die "Only root can pass arbitrary filesystem paths.\n"
324 if !$is_root;
325 } else {
326 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
327 &$check_and_activate_storage($sid);
328 }
329 });
330
331 # check/activate default storage
332 &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs});
333
334 PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param);
335
336 $conf->{unprivileged} = 1 if $unprivileged;
337
338 my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -";
339
340 eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) };
341 die "$emsg $@" if $@;
342
343 my $code = sub {
344 my $old_conf = PVE::LXC::Config->load_config($vmid);
345 my $was_template;
346
347 my $vollist = [];
348 eval {
349 my $orig_mp_param; # only used if $restore
350 if ($restore) {
351 die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
352 if ($is_root && $archive ne '-') {
353 my $orig_conf;
354 ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
355 $was_template = delete $orig_conf->{template};
356 # When we're root call 'restore_configuration' with ristricted=0,
357 # causing it to restore the raw lxc entries, among which there may be
358 # 'lxc.idmap' entries. We need to make sure that the extracted contents
359 # of the container match up with the restored configuration afterwards:
360 $conf->{lxc} = [grep { $_->[0] eq 'lxc.idmap' } @{$orig_conf->{lxc}}];
361 }
362 }
363 if ($storage_only_mode) {
364 if ($restore) {
365 if (!defined($orig_mp_param)) {
366 (undef, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
367 }
368 $mp_param = $orig_mp_param;
369 die "rootfs configuration could not be recovered, please check and specify manually!\n"
370 if !defined($mp_param->{rootfs});
371 PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
372 my ($ms, $mountpoint) = @_;
373 my $type = $mountpoint->{type};
374 if ($type eq 'volume') {
375 die "unable to detect disk size - please specify $ms (size)\n"
376 if !defined($mountpoint->{size});
377 my $disksize = $mountpoint->{size} / (1024 * 1024 * 1024); # create_disks expects GB as unit size
378 delete $mountpoint->{size};
379 $mountpoint->{volume} = "$storage:$disksize";
380 $mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
381 } else {
382 my $type = $mountpoint->{type};
383 die "restoring rootfs to $type mount is only possible by specifying -rootfs manually!\n"
384 if ($ms eq 'rootfs');
385 die "restoring '$ms' to $type mount is only possible for root\n"
386 if !$is_root;
387
388 if ($mountpoint->{backup}) {
389 warn "WARNING - unsupported configuration!\n";
390 warn "backup was enabled for $type mount point $ms ('$mountpoint->{mp}')\n";
391 warn "mount point configuration will be restored after archive extraction!\n";
392 warn "contained files will be restored to wrong directory!\n";
393 }
394 delete $mp_param->{$ms}; # actually delay bind/dev mps
395 $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
396 }
397 });
398 } else {
399 $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB
400 }
401 }
402
403 $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
404
405 # we always have the 'create' lock so check for more than 1 entry
406 if (scalar(keys %$old_conf) > 1) {
407 # destroy old container volumes
408 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, { lock => 'create' });
409 }
410
411 eval {
412 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
413 $bwlimit = PVE::Storage::get_bandwidth_limit('restore', [keys %used_storages], $bwlimit);
414 PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit);
415
416 if ($restore) {
417 PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root, $unique, $skip_fw_config_restore);
418 } else {
419 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
420 PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
421 $lxc_setup->post_create_hook($password, $ssh_keys);
422 }
423 };
424 my $err = $@;
425 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
426 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
427 die $err if $err;
428 # set some defaults
429 $conf->{hostname} ||= "CT$vmid";
430 $conf->{memory} ||= 512;
431 $conf->{swap} //= 512;
432 foreach my $mp (keys %$delayed_mp_param) {
433 $conf->{$mp} = $delayed_mp_param->{$mp};
434 }
435 # If the template flag was set, we try to convert again to template after restore
436 if ($was_template) {
437 print STDERR "Convert restored container to template...\n";
438 if (my $err = check_storage_supports_templates($conf)) {
439 warn $err;
440 warn "Leave restored backup as container instead of converting to template.\n"
441 } else {
442 PVE::LXC::template_create($vmid, $conf);
443 $conf->{template} = 1;
444 }
445 }
446 PVE::LXC::Config->write_config($vmid, $conf);
447 };
448 if (my $err = $@) {
449 PVE::LXC::destroy_disks($storage_cfg, $vollist);
450 eval { PVE::LXC::destroy_config($vmid) };
451 warn $@ if $@;
452 die "$emsg $err";
453 }
454 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
455
456 PVE::API2::LXC::Status->vm_start({ vmid => $vmid, node => $node })
457 if $start_after_create;
458 };
459
460 my $workername = $restore ? 'vzrestore' : 'vzcreate';
461 my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
462
463 return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd);
464 }});
465
466 sub check_storage_supports_templates {
467 my ($conf) = @_;
468
469 my $scfg = PVE::Storage::config();
470 eval {
471 PVE::LXC::Config->foreach_mountpoint($conf, sub {
472 my ($ms, $mp) = @_;
473
474 my ($sid) = PVE::Storage::parse_volume_id($mp->{volume}, 0);
475 die "Warning: Directory storage '$sid' does not support container templates!\n"
476 if $scfg->{ids}->{$sid}->{path};
477 });
478 };
479 return $@
480 }
481
482 __PACKAGE__->register_method({
483 name => 'vmdiridx',
484 path => '{vmid}',
485 method => 'GET',
486 proxyto => 'node',
487 description => "Directory index",
488 permissions => {
489 user => 'all',
490 },
491 parameters => {
492 additionalProperties => 0,
493 properties => {
494 node => get_standard_option('pve-node'),
495 vmid => get_standard_option('pve-vmid'),
496 },
497 },
498 returns => {
499 type => 'array',
500 items => {
501 type => "object",
502 properties => {
503 subdir => { type => 'string' },
504 },
505 },
506 links => [ { rel => 'child', href => "{subdir}" } ],
507 },
508 code => sub {
509 my ($param) = @_;
510
511 # test if VM exists
512 my $conf = PVE::LXC::Config->load_config($param->{vmid});
513
514 my $res = [
515 { subdir => 'config' },
516 { subdir => 'status' },
517 { subdir => 'vncproxy' },
518 { subdir => 'termproxy' },
519 { subdir => 'vncwebsocket' },
520 { subdir => 'spiceproxy' },
521 { subdir => 'migrate' },
522 { subdir => 'clone' },
523 # { subdir => 'initlog' },
524 { subdir => 'rrd' },
525 { subdir => 'rrddata' },
526 { subdir => 'firewall' },
527 { subdir => 'snapshot' },
528 { subdir => 'resize' },
529 ];
530
531 return $res;
532 }});
533
534
535 __PACKAGE__->register_method({
536 name => 'rrd',
537 path => '{vmid}/rrd',
538 method => 'GET',
539 protected => 1, # fixme: can we avoid that?
540 permissions => {
541 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
542 },
543 description => "Read VM RRD statistics (returns PNG)",
544 parameters => {
545 additionalProperties => 0,
546 properties => {
547 node => get_standard_option('pve-node'),
548 vmid => get_standard_option('pve-vmid'),
549 timeframe => {
550 description => "Specify the time frame you are interested in.",
551 type => 'string',
552 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
553 },
554 ds => {
555 description => "The list of datasources you want to display.",
556 type => 'string', format => 'pve-configid-list',
557 },
558 cf => {
559 description => "The RRD consolidation function",
560 type => 'string',
561 enum => [ 'AVERAGE', 'MAX' ],
562 optional => 1,
563 },
564 },
565 },
566 returns => {
567 type => "object",
568 properties => {
569 filename => { type => 'string' },
570 },
571 },
572 code => sub {
573 my ($param) = @_;
574
575 return PVE::Cluster::create_rrd_graph(
576 "pve2-vm/$param->{vmid}", $param->{timeframe},
577 $param->{ds}, $param->{cf});
578
579 }});
580
581 __PACKAGE__->register_method({
582 name => 'rrddata',
583 path => '{vmid}/rrddata',
584 method => 'GET',
585 protected => 1, # fixme: can we avoid that?
586 permissions => {
587 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
588 },
589 description => "Read VM RRD statistics",
590 parameters => {
591 additionalProperties => 0,
592 properties => {
593 node => get_standard_option('pve-node'),
594 vmid => get_standard_option('pve-vmid'),
595 timeframe => {
596 description => "Specify the time frame you are interested in.",
597 type => 'string',
598 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
599 },
600 cf => {
601 description => "The RRD consolidation function",
602 type => 'string',
603 enum => [ 'AVERAGE', 'MAX' ],
604 optional => 1,
605 },
606 },
607 },
608 returns => {
609 type => "array",
610 items => {
611 type => "object",
612 properties => {},
613 },
614 },
615 code => sub {
616 my ($param) = @_;
617
618 return PVE::Cluster::create_rrd_data(
619 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
620 }});
621
622 __PACKAGE__->register_method({
623 name => 'destroy_vm',
624 path => '{vmid}',
625 method => 'DELETE',
626 protected => 1,
627 proxyto => 'node',
628 description => "Destroy the container (also delete all uses files).",
629 permissions => {
630 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
631 },
632 parameters => {
633 additionalProperties => 0,
634 properties => {
635 node => get_standard_option('pve-node'),
636 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
637 },
638 },
639 returns => {
640 type => 'string',
641 },
642 code => sub {
643 my ($param) = @_;
644
645 my $rpcenv = PVE::RPCEnvironment::get();
646
647 my $authuser = $rpcenv->get_user();
648
649 my $vmid = $param->{vmid};
650
651 # test if container exists
652 my $conf = PVE::LXC::Config->load_config($vmid);
653
654 my $storage_cfg = cfs_read_file("storage.cfg");
655
656 PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid");
657
658 die "unable to remove CT $vmid - used in HA resources\n"
659 if PVE::HA::Config::vm_is_ha_managed($vmid);
660
661 # do not allow destroy if there are replication jobs
662 my $repl_conf = PVE::ReplicationConfig->new();
663 $repl_conf->check_for_existing_jobs($vmid);
664
665 my $running_error_msg = "unable to destroy CT $vmid - container is running\n";
666
667 die $running_error_msg if PVE::LXC::check_running($vmid); # check early
668
669 my $code = sub {
670 # reload config after lock
671 $conf = PVE::LXC::Config->load_config($vmid);
672 PVE::LXC::Config->check_lock($conf);
673
674 die $running_error_msg if PVE::LXC::check_running($vmid);
675
676 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf);
677 PVE::AccessControl::remove_vm_access($vmid);
678 PVE::Firewall::remove_vmfw_conf($vmid);
679 };
680
681 my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
682
683 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
684 }});
685
686 my $sslcert;
687
688 __PACKAGE__->register_method ({
689 name => 'vncproxy',
690 path => '{vmid}/vncproxy',
691 method => 'POST',
692 protected => 1,
693 permissions => {
694 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
695 },
696 description => "Creates a TCP VNC proxy connections.",
697 parameters => {
698 additionalProperties => 0,
699 properties => {
700 node => get_standard_option('pve-node'),
701 vmid => get_standard_option('pve-vmid'),
702 websocket => {
703 optional => 1,
704 type => 'boolean',
705 description => "use websocket instead of standard VNC.",
706 },
707 width => {
708 optional => 1,
709 description => "sets the width of the console in pixels.",
710 type => 'integer',
711 minimum => 16,
712 maximum => 4096,
713 },
714 height => {
715 optional => 1,
716 description => "sets the height of the console in pixels.",
717 type => 'integer',
718 minimum => 16,
719 maximum => 2160,
720 },
721 },
722 },
723 returns => {
724 additionalProperties => 0,
725 properties => {
726 user => { type => 'string' },
727 ticket => { type => 'string' },
728 cert => { type => 'string' },
729 port => { type => 'integer' },
730 upid => { type => 'string' },
731 },
732 },
733 code => sub {
734 my ($param) = @_;
735
736 my $rpcenv = PVE::RPCEnvironment::get();
737
738 my $authuser = $rpcenv->get_user();
739
740 my $vmid = $param->{vmid};
741 my $node = $param->{node};
742
743 my $authpath = "/vms/$vmid";
744
745 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
746
747 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
748 if !$sslcert;
749
750 my ($remip, $family);
751
752 if ($node ne PVE::INotify::nodename()) {
753 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
754 } else {
755 $family = PVE::Tools::get_host_address_family($node);
756 }
757
758 my $port = PVE::Tools::next_vnc_port($family);
759
760 # NOTE: vncterm VNC traffic is already TLS encrypted,
761 # so we select the fastest chipher here (or 'none'?)
762 my $remcmd = $remip ?
763 ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : [];
764
765 my $conf = PVE::LXC::Config->load_config($vmid, $node);
766 my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
767
768 my $shcmd = [ '/usr/bin/dtach', '-A',
769 "/var/run/dtach/vzctlconsole$vmid",
770 '-r', 'winch', '-z', @$concmd];
771
772 my $realcmd = sub {
773 my $upid = shift;
774
775 syslog ('info', "starting lxc vnc proxy $upid\n");
776
777 my $timeout = 10;
778
779 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
780 '-timeout', $timeout, '-authpath', $authpath,
781 '-perm', 'VM.Console'];
782
783 if ($param->{width}) {
784 push @$cmd, '-width', $param->{width};
785 }
786
787 if ($param->{height}) {
788 push @$cmd, '-height', $param->{height};
789 }
790
791 if ($param->{websocket}) {
792 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
793 push @$cmd, '-notls', '-listen', 'localhost';
794 }
795
796 push @$cmd, '-c', @$remcmd, @$shcmd;
797
798 run_command($cmd, keeplocale => 1);
799
800 return;
801 };
802
803 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
804
805 PVE::Tools::wait_for_vnc_port($port);
806
807 return {
808 user => $authuser,
809 ticket => $ticket,
810 port => $port,
811 upid => $upid,
812 cert => $sslcert,
813 };
814 }});
815
816 __PACKAGE__->register_method ({
817 name => 'termproxy',
818 path => '{vmid}/termproxy',
819 method => 'POST',
820 protected => 1,
821 permissions => {
822 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
823 },
824 description => "Creates a TCP proxy connection.",
825 parameters => {
826 additionalProperties => 0,
827 properties => {
828 node => get_standard_option('pve-node'),
829 vmid => get_standard_option('pve-vmid'),
830 },
831 },
832 returns => {
833 additionalProperties => 0,
834 properties => {
835 user => { type => 'string' },
836 ticket => { type => 'string' },
837 port => { type => 'integer' },
838 upid => { type => 'string' },
839 },
840 },
841 code => sub {
842 my ($param) = @_;
843
844 my $rpcenv = PVE::RPCEnvironment::get();
845
846 my $authuser = $rpcenv->get_user();
847
848 my $vmid = $param->{vmid};
849 my $node = $param->{node};
850
851 my $authpath = "/vms/$vmid";
852
853 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
854
855 my ($remip, $family);
856
857 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
858 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
859 } else {
860 $family = PVE::Tools::get_host_address_family($node);
861 }
862
863 my $port = PVE::Tools::next_vnc_port($family);
864
865 my $remcmd = $remip ?
866 ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : [];
867
868 my $conf = PVE::LXC::Config->load_config($vmid, $node);
869 my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
870
871 my $shcmd = [ '/usr/bin/dtach', '-A',
872 "/var/run/dtach/vzctlconsole$vmid",
873 '-r', 'winch', '-z', @$concmd];
874
875 my $realcmd = sub {
876 my $upid = shift;
877
878 syslog ('info', "starting lxc termproxy $upid\n");
879
880 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
881 '--perm', 'VM.Console', '--'];
882 push @$cmd, @$remcmd, @$shcmd;
883
884 PVE::Tools::run_command($cmd);
885 };
886
887 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
888
889 PVE::Tools::wait_for_vnc_port($port);
890
891 return {
892 user => $authuser,
893 ticket => $ticket,
894 port => $port,
895 upid => $upid,
896 };
897 }});
898
899 __PACKAGE__->register_method({
900 name => 'vncwebsocket',
901 path => '{vmid}/vncwebsocket',
902 method => 'GET',
903 permissions => {
904 description => "You also need to pass a valid ticket (vncticket).",
905 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
906 },
907 description => "Opens a weksocket for VNC traffic.",
908 parameters => {
909 additionalProperties => 0,
910 properties => {
911 node => get_standard_option('pve-node'),
912 vmid => get_standard_option('pve-vmid'),
913 vncticket => {
914 description => "Ticket from previous call to vncproxy.",
915 type => 'string',
916 maxLength => 512,
917 },
918 port => {
919 description => "Port number returned by previous vncproxy call.",
920 type => 'integer',
921 minimum => 5900,
922 maximum => 5999,
923 },
924 },
925 },
926 returns => {
927 type => "object",
928 properties => {
929 port => { type => 'string' },
930 },
931 },
932 code => sub {
933 my ($param) = @_;
934
935 my $rpcenv = PVE::RPCEnvironment::get();
936
937 my $authuser = $rpcenv->get_user();
938
939 my $authpath = "/vms/$param->{vmid}";
940
941 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
942
943 my $port = $param->{port};
944
945 return { port => $port };
946 }});
947
948 __PACKAGE__->register_method ({
949 name => 'spiceproxy',
950 path => '{vmid}/spiceproxy',
951 method => 'POST',
952 protected => 1,
953 proxyto => 'node',
954 permissions => {
955 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
956 },
957 description => "Returns a SPICE configuration to connect to the CT.",
958 parameters => {
959 additionalProperties => 0,
960 properties => {
961 node => get_standard_option('pve-node'),
962 vmid => get_standard_option('pve-vmid'),
963 proxy => get_standard_option('spice-proxy', { optional => 1 }),
964 },
965 },
966 returns => get_standard_option('remote-viewer-config'),
967 code => sub {
968 my ($param) = @_;
969
970 my $vmid = $param->{vmid};
971 my $node = $param->{node};
972 my $proxy = $param->{proxy};
973
974 my $authpath = "/vms/$vmid";
975 my $permissions = 'VM.Console';
976
977 my $conf = PVE::LXC::Config->load_config($vmid);
978
979 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
980
981 my $concmd = PVE::LXC::get_console_command($vmid, $conf);
982
983 my $shcmd = ['/usr/bin/dtach', '-A',
984 "/var/run/dtach/vzctlconsole$vmid",
985 '-r', 'winch', '-z', @$concmd];
986
987 my $title = "CT $vmid";
988
989 return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
990 }});
991
992
993 __PACKAGE__->register_method({
994 name => 'migrate_vm',
995 path => '{vmid}/migrate',
996 method => 'POST',
997 protected => 1,
998 proxyto => 'node',
999 description => "Migrate the container to another node. Creates a new migration task.",
1000 permissions => {
1001 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1002 },
1003 parameters => {
1004 additionalProperties => 0,
1005 properties => {
1006 node => get_standard_option('pve-node'),
1007 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1008 target => get_standard_option('pve-node', {
1009 description => "Target node.",
1010 completion => \&PVE::Cluster::complete_migration_target,
1011 }),
1012 online => {
1013 type => 'boolean',
1014 description => "Use online/live migration.",
1015 optional => 1,
1016 },
1017 restart => {
1018 type => 'boolean',
1019 description => "Use restart migration",
1020 optional => 1,
1021 },
1022 timeout => {
1023 type => 'integer',
1024 description => "Timeout in seconds for shutdown for restart migration",
1025 optional => 1,
1026 default => 180,
1027 },
1028 force => {
1029 type => 'boolean',
1030 description => "Force migration despite local bind / device" .
1031 " mounts. NOTE: deprecated, use 'shared' property of mount point instead.",
1032 optional => 1,
1033 },
1034 bwlimit => {
1035 description => "Override I/O bandwidth limit (in KiB/s).",
1036 optional => 1,
1037 type => 'number',
1038 minimum => '0',
1039 default => 'migrate limit from datacenter or storage config',
1040 },
1041 },
1042 },
1043 returns => {
1044 type => 'string',
1045 description => "the task ID.",
1046 },
1047 code => sub {
1048 my ($param) = @_;
1049
1050 my $rpcenv = PVE::RPCEnvironment::get();
1051
1052 my $authuser = $rpcenv->get_user();
1053
1054 my $target = extract_param($param, 'target');
1055
1056 my $localnode = PVE::INotify::nodename();
1057 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1058
1059 PVE::Cluster::check_cfs_quorum();
1060
1061 PVE::Cluster::check_node_exists($target);
1062
1063 my $targetip = PVE::Cluster::remote_node_ip($target);
1064
1065 my $vmid = extract_param($param, 'vmid');
1066
1067 # test if VM exists
1068 PVE::LXC::Config->load_config($vmid);
1069
1070 # try to detect errors early
1071 if (PVE::LXC::check_running($vmid)) {
1072 die "can't migrate running container without --online or --restart\n"
1073 if !$param->{online} && !$param->{restart};
1074 }
1075
1076 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1077
1078 my $hacmd = sub {
1079 my $upid = shift;
1080
1081 my $service = "ct:$vmid";
1082
1083 my $cmd = ['ha-manager', 'migrate', $service, $target];
1084
1085 print "Requesting HA migration for CT $vmid to node $target\n";
1086
1087 PVE::Tools::run_command($cmd);
1088
1089 return;
1090 };
1091
1092 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1093
1094 } else {
1095
1096 my $realcmd = sub {
1097 PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param);
1098 };
1099
1100 my $worker = sub {
1101 return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
1102 };
1103
1104 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker);
1105 }
1106 }});
1107
1108 __PACKAGE__->register_method({
1109 name => 'vm_feature',
1110 path => '{vmid}/feature',
1111 method => 'GET',
1112 proxyto => 'node',
1113 protected => 1,
1114 description => "Check if feature for virtual machine is available.",
1115 permissions => {
1116 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1117 },
1118 parameters => {
1119 additionalProperties => 0,
1120 properties => {
1121 node => get_standard_option('pve-node'),
1122 vmid => get_standard_option('pve-vmid'),
1123 feature => {
1124 description => "Feature to check.",
1125 type => 'string',
1126 enum => [ 'snapshot', 'clone', 'copy' ],
1127 },
1128 snapname => get_standard_option('pve-snapshot-name', {
1129 optional => 1,
1130 }),
1131 },
1132 },
1133 returns => {
1134 type => "object",
1135 properties => {
1136 hasFeature => { type => 'boolean' },
1137 #nodes => {
1138 #type => 'array',
1139 #items => { type => 'string' },
1140 #}
1141 },
1142 },
1143 code => sub {
1144 my ($param) = @_;
1145
1146 my $node = extract_param($param, 'node');
1147
1148 my $vmid = extract_param($param, 'vmid');
1149
1150 my $snapname = extract_param($param, 'snapname');
1151
1152 my $feature = extract_param($param, 'feature');
1153
1154 my $conf = PVE::LXC::Config->load_config($vmid);
1155
1156 if($snapname){
1157 my $snap = $conf->{snapshots}->{$snapname};
1158 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1159 $conf = $snap;
1160 }
1161 my $storage_cfg = PVE::Storage::config();
1162 #Maybe include later
1163 #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
1164 my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname);
1165
1166 return {
1167 hasFeature => $hasFeature,
1168 #nodes => [ keys %$nodelist ],
1169 };
1170 }});
1171
1172 __PACKAGE__->register_method({
1173 name => 'template',
1174 path => '{vmid}/template',
1175 method => 'POST',
1176 protected => 1,
1177 proxyto => 'node',
1178 description => "Create a Template.",
1179 permissions => {
1180 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
1181 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
1182 },
1183 parameters => {
1184 additionalProperties => 0,
1185 properties => {
1186 node => get_standard_option('pve-node'),
1187 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
1188 },
1189 },
1190 returns => { type => 'null'},
1191 code => sub {
1192 my ($param) = @_;
1193
1194 my $rpcenv = PVE::RPCEnvironment::get();
1195
1196 my $authuser = $rpcenv->get_user();
1197
1198 my $node = extract_param($param, 'node');
1199
1200 my $vmid = extract_param($param, 'vmid');
1201
1202 my $updatefn = sub {
1203
1204 my $conf = PVE::LXC::Config->load_config($vmid);
1205 PVE::LXC::Config->check_lock($conf);
1206
1207 die "unable to create template, because CT contains snapshots\n"
1208 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
1209
1210 die "you can't convert a template to a template\n"
1211 if PVE::LXC::Config->is_template($conf);
1212
1213 die "you can't convert a CT to template if the CT is running\n"
1214 if PVE::LXC::check_running($vmid);
1215
1216 if (my $err = check_storage_supports_templates($conf)) {
1217 die $err;
1218 }
1219
1220 my $realcmd = sub {
1221 PVE::LXC::template_create($vmid, $conf);
1222
1223 $conf->{template} = 1;
1224
1225 PVE::LXC::Config->write_config($vmid, $conf);
1226 # and remove lxc config
1227 PVE::LXC::update_lxc_config($vmid, $conf);
1228 };
1229
1230 return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
1231 };
1232
1233 PVE::LXC::Config->lock_config($vmid, $updatefn);
1234
1235 return undef;
1236 }});
1237
1238 __PACKAGE__->register_method({
1239 name => 'clone_vm',
1240 path => '{vmid}/clone',
1241 method => 'POST',
1242 protected => 1,
1243 proxyto => 'node',
1244 description => "Create a container clone/copy",
1245 permissions => {
1246 description => "You need 'VM.Clone' permissions on /vms/{vmid}, " .
1247 "and 'VM.Allocate' permissions " .
1248 "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
1249 "'Datastore.AllocateSpace' on any used storage.",
1250 check =>
1251 [ 'and',
1252 ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
1253 [ 'or',
1254 [ 'perm', '/vms/{newid}', ['VM.Allocate']],
1255 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
1256 ],
1257 ]
1258 },
1259 parameters => {
1260 additionalProperties => 0,
1261 properties => {
1262 node => get_standard_option('pve-node'),
1263 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1264 newid => get_standard_option('pve-vmid', {
1265 completion => \&PVE::Cluster::complete_next_vmid,
1266 description => 'VMID for the clone.' }),
1267 hostname => {
1268 optional => 1,
1269 type => 'string', format => 'dns-name',
1270 description => "Set a hostname for the new CT.",
1271 },
1272 description => {
1273 optional => 1,
1274 type => 'string',
1275 description => "Description for the new CT.",
1276 },
1277 pool => {
1278 optional => 1,
1279 type => 'string', format => 'pve-poolid',
1280 description => "Add the new CT to the specified pool.",
1281 },
1282 snapname => get_standard_option('pve-snapshot-name', {
1283 optional => 1,
1284 }),
1285 storage => get_standard_option('pve-storage-id', {
1286 description => "Target storage for full clone.",
1287 optional => 1,
1288 }),
1289 full => {
1290 optional => 1,
1291 type => 'boolean',
1292 description => "Create a full copy of all disks. This is always done when " .
1293 "you clone a normal CT. For CT templates, we try to create a linked clone by default.",
1294 },
1295 target => get_standard_option('pve-node', {
1296 description => "Target node. Only allowed if the original VM is on shared storage.",
1297 optional => 1,
1298 }),
1299 bwlimit => {
1300 description => "Override I/O bandwidth limit (in KiB/s).",
1301 optional => 1,
1302 type => 'number',
1303 minimum => '0',
1304 default => 'clone limit from datacenter or storage config',
1305 },
1306 },
1307 },
1308 returns => {
1309 type => 'string',
1310 },
1311 code => sub {
1312 my ($param) = @_;
1313
1314 my $rpcenv = PVE::RPCEnvironment::get();
1315
1316 my $authuser = $rpcenv->get_user();
1317
1318 my $node = extract_param($param, 'node');
1319
1320 my $vmid = extract_param($param, 'vmid');
1321
1322 my $newid = extract_param($param, 'newid');
1323
1324 my $pool = extract_param($param, 'pool');
1325
1326 if (defined($pool)) {
1327 $rpcenv->check_pool_exist($pool);
1328 }
1329
1330 my $snapname = extract_param($param, 'snapname');
1331
1332 my $storage = extract_param($param, 'storage');
1333
1334 my $target = extract_param($param, 'target');
1335
1336 my $localnode = PVE::INotify::nodename();
1337
1338 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
1339
1340 PVE::Cluster::check_node_exists($target) if $target;
1341
1342 my $storecfg = PVE::Storage::config();
1343
1344 if ($storage) {
1345 # check if storage is enabled on local node
1346 PVE::Storage::storage_check_enabled($storecfg, $storage);
1347 if ($target) {
1348 # check if storage is available on target node
1349 PVE::Storage::storage_check_node($storecfg, $storage, $target);
1350 # clone only works if target storage is shared
1351 my $scfg = PVE::Storage::storage_config($storecfg, $storage);
1352 die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared};
1353 }
1354 }
1355
1356 PVE::Cluster::check_cfs_quorum();
1357
1358 my $conffile;
1359 my $newconf = {};
1360 my $mountpoints = {};
1361 my $fullclone = {};
1362 my $vollist = [];
1363 my $running;
1364
1365 PVE::LXC::Config->lock_config($vmid, sub {
1366 my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk');
1367
1368 $running = PVE::LXC::check_running($vmid) || 0;
1369
1370 my $full = extract_param($param, 'full');
1371 if (!defined($full)) {
1372 $full = !PVE::LXC::Config->is_template($src_conf);
1373 }
1374 die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full;
1375
1376 eval {
1377 die "snapshot '$snapname' does not exist\n"
1378 if $snapname && !defined($src_conf->{snapshots}->{$snapname});
1379
1380
1381 my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf;
1382
1383 $conffile = PVE::LXC::Config->config_file($newid);
1384 die "unable to create CT $newid: config file already exists\n"
1385 if -f $conffile;
1386
1387 my $sharedvm = 1;
1388 foreach my $opt (keys %$src_conf) {
1389 next if $opt =~ m/^unused\d+$/;
1390
1391 my $value = $src_conf->{$opt};
1392
1393 if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) {
1394 my $mp = $opt eq 'rootfs' ?
1395 PVE::LXC::Config->parse_ct_rootfs($value) :
1396 PVE::LXC::Config->parse_ct_mountpoint($value);
1397
1398 if ($mp->{type} eq 'volume') {
1399 my $volid = $mp->{volume};
1400
1401 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
1402 $sid = $storage if defined($storage);
1403 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
1404 if (!$scfg->{shared}) {
1405 $sharedvm = 0;
1406 warn "found non-shared volume: $volid\n" if $target;
1407 }
1408
1409 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
1410
1411 if ($full) {
1412 die "Cannot do full clones on a running container without snapshots\n"
1413 if $running && !defined($snapname);
1414 $fullclone->{$opt} = 1;
1415 } else {
1416 # not full means clone instead of copy
1417 die "Linked clone feature for '$volid' is not available\n"
1418 if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running);
1419 }
1420
1421 $mountpoints->{$opt} = $mp;
1422 push @$vollist, $volid;
1423
1424 } else {
1425 # TODO: allow bind mounts?
1426 die "unable to clone mountpint '$opt' (type $mp->{type})\n";
1427 }
1428 } elsif ($opt =~ m/^net(\d+)$/) {
1429 # always change MAC! address
1430 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
1431 my $net = PVE::LXC::Config->parse_lxc_network($value);
1432 $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
1433 $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net);
1434 } else {
1435 # copy everything else
1436 $newconf->{$opt} = $value;
1437 }
1438 }
1439 die "can't clone CT to node '$target' (CT uses local storage)\n"
1440 if $target && !$sharedvm;
1441
1442 # Replace the 'disk' lock with a 'create' lock.
1443 $newconf->{lock} = 'create';
1444
1445 delete $newconf->{template};
1446 if ($param->{hostname}) {
1447 $newconf->{hostname} = $param->{hostname};
1448 }
1449
1450 if ($param->{description}) {
1451 $newconf->{description} = $param->{description};
1452 }
1453
1454 # create empty/temp config - this fails if CT already exists on other node
1455 PVE::LXC::Config->write_config($newid, $newconf);
1456 };
1457 if (my $err = $@) {
1458 eval { PVE::LXC::Config->remove_lock($vmid, 'disk') };
1459 warn $@ if $@;
1460 die $err;
1461 }
1462 });
1463
1464 my $update_conf = sub {
1465 my ($key, $value) = @_;
1466 return PVE::LXC::Config->lock_config($newid, sub {
1467 my $conf = PVE::LXC::Config->load_config($newid);
1468 die "Lost 'create' config lock, aborting.\n"
1469 if !PVE::LXC::Config->has_lock($conf, 'create');
1470 $conf->{$key} = $value;
1471 PVE::LXC::Config->write_config($newid, $conf);
1472 });
1473 };
1474
1475 my $realcmd = sub {
1476 my ($upid) = @_;
1477
1478 my $newvollist = [];
1479
1480 my $verify_running = PVE::LXC::check_running($vmid) || 0;
1481 die "unexpected state change\n" if $verify_running != $running;
1482
1483 eval {
1484 local $SIG{INT} =
1485 local $SIG{TERM} =
1486 local $SIG{QUIT} =
1487 local $SIG{HUP} = sub { die "interrupted by signal\n"; };
1488
1489 PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
1490 my $bwlimit = extract_param($param, 'bwlimit');
1491
1492 foreach my $opt (keys %$mountpoints) {
1493 my $mp = $mountpoints->{$opt};
1494 my $volid = $mp->{volume};
1495
1496 my $newvolid;
1497 if ($fullclone->{$opt}) {
1498 print "create full clone of mountpoint $opt ($volid)\n";
1499 my $source_storage = PVE::Storage::parse_volume_id($volid);
1500 my $target_storage = $storage // $source_storage;
1501 my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', [$source_storage, $target_storage], $bwlimit);
1502 $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname, $clonelimit);
1503 } else {
1504 print "create linked clone of mount point $opt ($volid)\n";
1505 $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
1506 }
1507
1508 push @$newvollist, $newvolid;
1509 $mp->{volume} = $newvolid;
1510
1511 $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs'));
1512 }
1513
1514 PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
1515 PVE::LXC::Config->remove_lock($newid, 'create');
1516
1517 if ($target) {
1518 # always deactivate volumes - avoid lvm LVs to be active on several nodes
1519 PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
1520 PVE::Storage::deactivate_volumes($storecfg, $newvollist);
1521
1522 my $newconffile = PVE::LXC::Config->config_file($newid, $target);
1523 die "Failed to move config to node '$target' - rename failed: $!\n"
1524 if !rename($conffile, $newconffile);
1525 }
1526 };
1527 my $err = $@;
1528
1529 # Unlock the source config in any case:
1530 eval { PVE::LXC::Config->remove_lock($vmid, 'disk') };
1531 warn $@ if $@;
1532
1533 if ($err) {
1534 # Now cleanup the config & disks:
1535 unlink $conffile;
1536
1537 sleep 1; # some storages like rbd need to wait before release volume - really?
1538
1539 foreach my $volid (@$newvollist) {
1540 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
1541 warn $@ if $@;
1542 }
1543 die "clone failed: $err";
1544 }
1545
1546 return;
1547 };
1548
1549 PVE::Firewall::clone_vmfw_conf($vmid, $newid);
1550 return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
1551 }});
1552
1553
1554 __PACKAGE__->register_method({
1555 name => 'resize_vm',
1556 path => '{vmid}/resize',
1557 method => 'PUT',
1558 protected => 1,
1559 proxyto => 'node',
1560 description => "Resize a container mount point.",
1561 permissions => {
1562 check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1],
1563 },
1564 parameters => {
1565 additionalProperties => 0,
1566 properties => {
1567 node => get_standard_option('pve-node'),
1568 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1569 disk => {
1570 type => 'string',
1571 description => "The disk you want to resize.",
1572 enum => [PVE::LXC::Config->mountpoint_names()],
1573 },
1574 size => {
1575 type => 'string',
1576 pattern => '\+?\d+(\.\d+)?[KMGT]?',
1577 description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
1578 },
1579 digest => {
1580 type => 'string',
1581 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1582 maxLength => 40,
1583 optional => 1,
1584 }
1585 },
1586 },
1587 returns => {
1588 type => 'string',
1589 description => "the task ID.",
1590 },
1591 code => sub {
1592 my ($param) = @_;
1593
1594 my $rpcenv = PVE::RPCEnvironment::get();
1595
1596 my $authuser = $rpcenv->get_user();
1597
1598 my $node = extract_param($param, 'node');
1599
1600 my $vmid = extract_param($param, 'vmid');
1601
1602 my $digest = extract_param($param, 'digest');
1603
1604 my $sizestr = extract_param($param, 'size');
1605 my $ext = ($sizestr =~ s/^\+//);
1606 my $newsize = PVE::JSONSchema::parse_size($sizestr);
1607 die "invalid size string" if !defined($newsize);
1608
1609 die "no options specified\n" if !scalar(keys %$param);
1610
1611 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []);
1612
1613 my $storage_cfg = cfs_read_file("storage.cfg");
1614
1615 my $code = sub {
1616
1617 my $conf = PVE::LXC::Config->load_config($vmid);
1618 PVE::LXC::Config->check_lock($conf);
1619
1620 PVE::Tools::assert_if_modified($digest, $conf->{digest});
1621
1622 my $running = PVE::LXC::check_running($vmid);
1623
1624 my $disk = $param->{disk};
1625 my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) :
1626 PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk});
1627
1628 my $volid = $mp->{volume};
1629
1630 my (undef, undef, $owner, undef, undef, undef, $format) =
1631 PVE::Storage::parse_volname($storage_cfg, $volid);
1632
1633 die "can't resize mount point owned by another container ($owner)"
1634 if $vmid != $owner;
1635
1636 die "can't resize volume: $disk if snapshot exists\n"
1637 if %{$conf->{snapshots}} && $format eq 'qcow2';
1638
1639 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1640
1641 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1642
1643 PVE::Storage::activate_volumes($storage_cfg, [$volid]);
1644
1645 my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5);
1646 $newsize += $size if $ext;
1647 $newsize = int($newsize);
1648
1649 die "unable to shrink disk size\n" if $newsize < $size;
1650
1651 return if $size == $newsize;
1652
1653 PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
1654 my $realcmd = sub {
1655 # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so
1656 # we pass 0 here (parameter only makes sense for qemu)
1657 PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
1658
1659 $mp->{size} = $newsize;
1660 $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs');
1661
1662 PVE::LXC::Config->write_config($vmid, $conf);
1663
1664 if ($format eq 'raw') {
1665 # we need to ensure that the volume is mapped, if not needed this is a NOP
1666 my $path = PVE::Storage::map_volume($storage_cfg, $volid);
1667 $path = PVE::Storage::path($storage_cfg, $volid) if !defined($path);
1668 if ($running) {
1669
1670 $mp->{mp} = '/';
1671 my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1];
1672 $path = PVE::LXC::query_loopdev($path) if $use_loopdev;
1673 die "internal error: CT running but mount point not attached to a loop device"
1674 if !$path;
1675 PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev;
1676
1677 # In order for resize2fs to know that we need online-resizing a mountpoint needs
1678 # to be visible to it in its namespace.
1679 # To not interfere with the rest of the system we unshare the current mount namespace,
1680 # mount over /tmp and then run resize2fs.
1681
1682 # interestingly we don't need to e2fsck on mounted systems...
1683 my $quoted = PVE::Tools::shellquote($path);
1684 my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
1685 eval {
1686 PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
1687 };
1688 warn "Failed to update the container's filesystem: $@\n" if $@;
1689 } else {
1690 eval {
1691 PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
1692 PVE::Tools::run_command(['resize2fs', $path]);
1693 };
1694 warn "Failed to update the container's filesystem: $@\n" if $@;
1695
1696 # always un-map if not running, this is a NOP if not needed
1697 PVE::Storage::unmap_volume($storage_cfg, $volid);
1698 }
1699 }
1700 };
1701
1702 return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd);
1703 };
1704
1705 return PVE::LXC::Config->lock_config($vmid, $code);;
1706 }});
1707
1708 __PACKAGE__->register_method({
1709 name => 'move_volume',
1710 path => '{vmid}/move_volume',
1711 method => 'POST',
1712 protected => 1,
1713 proxyto => 'node',
1714 description => "Move a rootfs-/mp-volume to a different storage",
1715 permissions => {
1716 description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " .
1717 "and 'Datastore.AllocateSpace' permissions on the storage.",
1718 check =>
1719 [ 'and',
1720 ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
1721 ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]],
1722 ],
1723 },
1724 parameters => {
1725 additionalProperties => 0,
1726 properties => {
1727 node => get_standard_option('pve-node'),
1728 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1729 volume => {
1730 type => 'string',
1731 enum => [ PVE::LXC::Config->mountpoint_names() ],
1732 description => "Volume which will be moved.",
1733 },
1734 storage => get_standard_option('pve-storage-id', {
1735 description => "Target Storage.",
1736 completion => \&PVE::Storage::complete_storage_enabled,
1737 }),
1738 delete => {
1739 type => 'boolean',
1740 description => "Delete the original volume after successful copy. By default the original is kept as an unused volume entry.",
1741 optional => 1,
1742 default => 0,
1743 },
1744 digest => {
1745 type => 'string',
1746 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1747 maxLength => 40,
1748 optional => 1,
1749 },
1750 bwlimit => {
1751 description => "Override I/O bandwidth limit (in KiB/s).",
1752 optional => 1,
1753 type => 'number',
1754 minimum => '0',
1755 default => 'clone limit from datacenter or storage config',
1756 },
1757 },
1758 },
1759 returns => {
1760 type => 'string',
1761 },
1762 code => sub {
1763 my ($param) = @_;
1764
1765 my $rpcenv = PVE::RPCEnvironment::get();
1766
1767 my $authuser = $rpcenv->get_user();
1768
1769 my $vmid = extract_param($param, 'vmid');
1770
1771 my $storage = extract_param($param, 'storage');
1772
1773 my $mpkey = extract_param($param, 'volume');
1774
1775 my $lockname = 'disk';
1776
1777 my ($mpdata, $old_volid);
1778
1779 PVE::LXC::Config->lock_config($vmid, sub {
1780 my $conf = PVE::LXC::Config->load_config($vmid);
1781 PVE::LXC::Config->check_lock($conf);
1782
1783 die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid);
1784
1785 if ($mpkey eq 'rootfs') {
1786 $mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey});
1787 } elsif ($mpkey =~ m/mp\d+/) {
1788 $mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey});
1789 } else {
1790 die "Can't parse $mpkey\n";
1791 }
1792 $old_volid = $mpdata->{volume};
1793
1794 die "you can't move a volume with snapshots and delete the source\n"
1795 if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid);
1796
1797 PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest});
1798
1799 PVE::LXC::Config->set_lock($vmid, $lockname);
1800 });
1801
1802 my $realcmd = sub {
1803 eval {
1804 PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage");
1805
1806 my $conf = PVE::LXC::Config->load_config($vmid);
1807 my $storage_cfg = PVE::Storage::config();
1808
1809 my $new_volid;
1810
1811 eval {
1812 PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]);
1813 my $bwlimit = extract_param($param, 'bwlimit');
1814 my $source_storage = PVE::Storage::parse_volume_id($old_volid);
1815 my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$source_storage, $storage], $bwlimit);
1816 $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf, undef, $movelimit);
1817 $mpdata->{volume} = $new_volid;
1818
1819 PVE::LXC::Config->lock_config($vmid, sub {
1820 my $digest = $conf->{digest};
1821 $conf = PVE::LXC::Config->load_config($vmid);
1822 PVE::Tools::assert_if_modified($digest, $conf->{digest});
1823
1824 $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs');
1825
1826 PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete};
1827
1828 PVE::LXC::Config->write_config($vmid, $conf);
1829 });
1830
1831 eval {
1832 # try to deactivate volumes - avoid lvm LVs to be active on several nodes
1833 PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ])
1834 };
1835 warn $@ if $@;
1836 };
1837 if (my $err = $@) {
1838 eval {
1839 PVE::Storage::vdisk_free($storage_cfg, $new_volid)
1840 if defined($new_volid);
1841 };
1842 warn $@ if $@;
1843 die $err;
1844 }
1845
1846 if ($param->{delete}) {
1847 eval {
1848 PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]);
1849 PVE::Storage::vdisk_free($storage_cfg, $old_volid);
1850 };
1851 warn $@ if $@;
1852 }
1853 };
1854 my $err = $@;
1855 eval { PVE::LXC::Config->remove_lock($vmid, $lockname) };
1856 warn $@ if $@;
1857 die $err if $err;
1858 };
1859 my $task = eval {
1860 $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd);
1861 };
1862 if (my $err = $@) {
1863 eval { PVE::LXC::Config->remove_lock($vmid, $lockname) };
1864 warn $@ if $@;
1865 die $err;
1866 }
1867 return $task;
1868 }});
1869
1870 1;