]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC.pm
a913982361f19faac8715a6d152dc20f3eae0a94
[pve-container.git] / src / PVE / API2 / LXC.pm
1 package PVE::API2::LXC;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param run_command);
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Firewall;
13 use PVE::Storage;
14 use PVE::RESTHandler;
15 use PVE::RPCEnvironment;
16 use PVE::LXC;
17 use PVE::LXC::Create;
18 use PVE::LXC::Migrate;
19 use PVE::API2::LXC::Config;
20 use PVE::API2::LXC::Status;
21 use PVE::API2::LXC::Snapshot;
22 use PVE::ReplicationTools;
23 use PVE::JSONSchema qw(get_standard_option);
24 use base qw(PVE::RESTHandler);
25
26 BEGIN {
27 if (!$ENV{PVE_GENERATING_DOCS}) {
28 require PVE::HA::Env::PVE2;
29 import PVE::HA::Env::PVE2;
30 require PVE::HA::Config;
31 import PVE::HA::Config;
32 }
33 }
34
35 __PACKAGE__->register_method ({
36 subclass => "PVE::API2::LXC::Config",
37 path => '{vmid}/config',
38 });
39
40 __PACKAGE__->register_method ({
41 subclass => "PVE::API2::LXC::Status",
42 path => '{vmid}/status',
43 });
44
45 __PACKAGE__->register_method ({
46 subclass => "PVE::API2::LXC::Snapshot",
47 path => '{vmid}/snapshot',
48 });
49
50 __PACKAGE__->register_method ({
51 subclass => "PVE::API2::Firewall::CT",
52 path => '{vmid}/firewall',
53 });
54
55 __PACKAGE__->register_method({
56 name => 'vmlist',
57 path => '',
58 method => 'GET',
59 description => "LXC container index (per node).",
60 permissions => {
61 description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
62 user => 'all',
63 },
64 proxyto => 'node',
65 protected => 1, # /proc files are only readable by root
66 parameters => {
67 additionalProperties => 0,
68 properties => {
69 node => get_standard_option('pve-node'),
70 },
71 },
72 returns => {
73 type => 'array',
74 items => {
75 type => "object",
76 properties => {},
77 },
78 links => [ { rel => 'child', href => "{vmid}" } ],
79 },
80 code => sub {
81 my ($param) = @_;
82
83 my $rpcenv = PVE::RPCEnvironment::get();
84 my $authuser = $rpcenv->get_user();
85
86 my $vmstatus = PVE::LXC::vmstatus();
87
88 my $res = [];
89 foreach my $vmid (keys %$vmstatus) {
90 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
91
92 my $data = $vmstatus->{$vmid};
93 $data->{vmid} = $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 pool => {
148 optional => 1,
149 type => 'string', format => 'pve-poolid',
150 description => "Add the VM to the specified pool.",
151 },
152 'ignore-unpack-errors' => {
153 optional => 1,
154 type => 'boolean',
155 description => "Ignore errors when extracting the template.",
156 },
157 'ssh-public-keys' => {
158 optional => 1,
159 type => 'string',
160 description => "Setup public SSH keys (one key per line, " .
161 "OpenSSH format).",
162 },
163 }),
164 },
165 returns => {
166 type => 'string',
167 },
168 code => sub {
169 my ($param) = @_;
170
171 my $rpcenv = PVE::RPCEnvironment::get();
172
173 my $authuser = $rpcenv->get_user();
174
175 my $node = extract_param($param, 'node');
176
177 my $vmid = extract_param($param, 'vmid');
178
179 my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors');
180
181 my $basecfg_fn = PVE::LXC::Config->config_file($vmid);
182
183 my $same_container_exists = -f $basecfg_fn;
184
185 # 'unprivileged' is read-only, so we can't pass it to update_pct_config
186 my $unprivileged = extract_param($param, 'unprivileged');
187
188 my $restore = extract_param($param, 'restore');
189
190 if ($restore) {
191 # fixme: limit allowed parameters
192
193 }
194
195 my $force = extract_param($param, 'force');
196
197 if (!($same_container_exists && $restore && $force)) {
198 PVE::Cluster::check_vmid_unused($vmid);
199 } else {
200 my $conf = PVE::LXC::Config->load_config($vmid);
201 PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid");
202 }
203
204 my $password = extract_param($param, 'password');
205
206 my $ssh_keys = extract_param($param, 'ssh-public-keys');
207 PVE::Tools::validate_ssh_public_keys($ssh_keys) if defined($ssh_keys);
208
209 my $pool = extract_param($param, 'pool');
210
211 if (defined($pool)) {
212 $rpcenv->check_pool_exist($pool);
213 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
214 }
215
216 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
217 # OK
218 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
219 # OK
220 } elsif ($restore && $force && $same_container_exists &&
221 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
222 # OK: user has VM.Backup permissions, and want to restore an existing VM
223 } else {
224 raise_perm_exc();
225 }
226
227 my $ostemplate = extract_param($param, 'ostemplate');
228 my $storage = extract_param($param, 'storage') // 'local';
229
230 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []);
231
232 my $storage_cfg = cfs_read_file("storage.cfg");
233
234
235 my $archive;
236
237 if ($ostemplate eq '-') {
238 die "pipe requires cli environment\n"
239 if $rpcenv->{type} ne 'cli';
240 die "pipe can only be used with restore tasks\n"
241 if !$restore;
242 $archive = '-';
243 die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs});
244 } else {
245 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, $vmid, $ostemplate);
246 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
247 }
248
249 my $check_and_activate_storage = sub {
250 my ($sid) = @_;
251
252 my $scfg = PVE::Storage::storage_check_node($storage_cfg, $sid, $node);
253
254 raise_param_exc({ storage => "storage '$sid' does not support container directories"})
255 if !$scfg->{content}->{rootdir};
256
257 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
258
259 PVE::Storage::activate_storage($storage_cfg, $sid);
260 };
261
262 my $conf = {};
263
264 my $no_disk_param = {};
265 my $mp_param = {};
266 my $storage_only_mode = 1;
267 foreach my $opt (keys %$param) {
268 my $value = $param->{$opt};
269 if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) {
270 # allow to use simple numbers (add default storage in that case)
271 if ($value =~ m/^\d+(\.\d+)?$/) {
272 $mp_param->{$opt} = "$storage:$value";
273 } else {
274 $mp_param->{$opt} = $value;
275 }
276 $storage_only_mode = 0;
277 } elsif ($opt =~ m/^unused\d+$/) {
278 warn "ignoring '$opt', cannot create/restore with unused volume\n";
279 delete $param->{$opt};
280 } else {
281 $no_disk_param->{$opt} = $value;
282 }
283 }
284
285 die "mount points configured, but 'rootfs' not set - aborting\n"
286 if !$storage_only_mode && !defined($mp_param->{rootfs});
287
288 # check storage access, activate storage
289 my $delayed_mp_param = {};
290 PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
291 my ($ms, $mountpoint) = @_;
292
293 my $volid = $mountpoint->{volume};
294 my $mp = $mountpoint->{mp};
295
296 if ($mountpoint->{type} ne 'volume') { # bind or device
297 die "Only root can pass arbitrary filesystem paths.\n"
298 if $authuser ne 'root@pam';
299 } else {
300 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
301 &$check_and_activate_storage($sid);
302 }
303 });
304
305 # check/activate default storage
306 &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs});
307
308 PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param);
309
310 $conf->{unprivileged} = 1 if $unprivileged;
311
312 my $check_vmid_usage = sub {
313 if ($force) {
314 die "can't overwrite running container\n"
315 if PVE::LXC::check_running($vmid);
316 } else {
317 PVE::Cluster::check_vmid_unused($vmid);
318 }
319 };
320
321 my $code = sub {
322 &$check_vmid_usage(); # final check after locking
323 my $old_conf;
324
325 my $config_fn = PVE::LXC::Config->config_file($vmid);
326 if (-f $config_fn) {
327 die "container exists" if !$restore; # just to be sure
328 $old_conf = PVE::LXC::Config->load_config($vmid);
329 } else {
330 eval {
331 # try to create empty config on local node, we have an flock
332 PVE::LXC::Config->write_config($vmid, {});
333 };
334
335 # another node was faster, abort
336 die "Could not reserve ID $vmid, already taken\n" if $@;
337 }
338
339 PVE::Cluster::check_cfs_quorum();
340 my $vollist = [];
341
342 eval {
343 if ($storage_only_mode) {
344 if ($restore) {
345 (undef, $mp_param) = PVE::LXC::Create::recover_config($archive);
346 die "rootfs configuration could not be recovered, please check and specify manually!\n"
347 if !defined($mp_param->{rootfs});
348 PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
349 my ($ms, $mountpoint) = @_;
350 my $type = $mountpoint->{type};
351 if ($type eq 'volume') {
352 die "unable to detect disk size - please specify $ms (size)\n"
353 if !defined($mountpoint->{size});
354 my $disksize = $mountpoint->{size} / (1024 * 1024 * 1024); # create_disks expects GB as unit size
355 delete $mountpoint->{size};
356 $mountpoint->{volume} = "$storage:$disksize";
357 $mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
358 } else {
359 my $type = $mountpoint->{type};
360 die "restoring rootfs to $type mount is only possible by specifying -rootfs manually!\n"
361 if ($ms eq 'rootfs');
362 die "restoring '$ms' to $type mount is only possible for root\n"
363 if $authuser ne 'root@pam';
364
365 if ($mountpoint->{backup}) {
366 warn "WARNING - unsupported configuration!\n";
367 warn "backup was enabled for $type mount point $ms ('$mountpoint->{mp}')\n";
368 warn "mount point configuration will be restored after archive extraction!\n";
369 warn "contained files will be restored to wrong directory!\n";
370 }
371 delete $mp_param->{$ms}; # actually delay bind/dev mps
372 $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
373 }
374 });
375 } else {
376 $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB
377 }
378 }
379
380 $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
381
382 if (defined($old_conf)) {
383 # destroy old container volumes
384 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {});
385 }
386
387 eval {
388 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
389 PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors);
390
391 if ($restore) {
392 PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, $authuser ne 'root@pam');
393 } else {
394 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
395 PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
396 $lxc_setup->post_create_hook($password, $ssh_keys);
397 }
398 };
399 my $err = $@;
400 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
401 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
402 die $err if $err;
403 # set some defaults
404 $conf->{hostname} ||= "CT$vmid";
405 $conf->{memory} ||= 512;
406 $conf->{swap} //= 512;
407 foreach my $mp (keys %$delayed_mp_param) {
408 $conf->{$mp} = $delayed_mp_param->{$mp};
409 }
410 PVE::LXC::Config->write_config($vmid, $conf);
411 };
412 if (my $err = $@) {
413 PVE::LXC::destroy_disks($storage_cfg, $vollist);
414 PVE::LXC::destroy_config($vmid);
415 die $err;
416 }
417 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
418 };
419
420 my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
421
422 &$check_vmid_usage(); # first check before locking
423
424 return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
425 $vmid, $authuser, $realcmd);
426
427 }});
428
429 __PACKAGE__->register_method({
430 name => 'vmdiridx',
431 path => '{vmid}',
432 method => 'GET',
433 proxyto => 'node',
434 description => "Directory index",
435 permissions => {
436 user => 'all',
437 },
438 parameters => {
439 additionalProperties => 0,
440 properties => {
441 node => get_standard_option('pve-node'),
442 vmid => get_standard_option('pve-vmid'),
443 },
444 },
445 returns => {
446 type => 'array',
447 items => {
448 type => "object",
449 properties => {
450 subdir => { type => 'string' },
451 },
452 },
453 links => [ { rel => 'child', href => "{subdir}" } ],
454 },
455 code => sub {
456 my ($param) = @_;
457
458 # test if VM exists
459 my $conf = PVE::LXC::Config->load_config($param->{vmid});
460
461 my $res = [
462 { subdir => 'config' },
463 { subdir => 'status' },
464 { subdir => 'vncproxy' },
465 { subdir => 'vncwebsocket' },
466 { subdir => 'spiceproxy' },
467 { subdir => 'migrate' },
468 { subdir => 'clone' },
469 # { subdir => 'initlog' },
470 { subdir => 'rrd' },
471 { subdir => 'rrddata' },
472 { subdir => 'firewall' },
473 { subdir => 'snapshot' },
474 { subdir => 'resize' },
475 ];
476
477 return $res;
478 }});
479
480
481 __PACKAGE__->register_method({
482 name => 'rrd',
483 path => '{vmid}/rrd',
484 method => 'GET',
485 protected => 1, # fixme: can we avoid that?
486 permissions => {
487 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
488 },
489 description => "Read VM RRD statistics (returns PNG)",
490 parameters => {
491 additionalProperties => 0,
492 properties => {
493 node => get_standard_option('pve-node'),
494 vmid => get_standard_option('pve-vmid'),
495 timeframe => {
496 description => "Specify the time frame you are interested in.",
497 type => 'string',
498 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
499 },
500 ds => {
501 description => "The list of datasources you want to display.",
502 type => 'string', format => 'pve-configid-list',
503 },
504 cf => {
505 description => "The RRD consolidation function",
506 type => 'string',
507 enum => [ 'AVERAGE', 'MAX' ],
508 optional => 1,
509 },
510 },
511 },
512 returns => {
513 type => "object",
514 properties => {
515 filename => { type => 'string' },
516 },
517 },
518 code => sub {
519 my ($param) = @_;
520
521 return PVE::Cluster::create_rrd_graph(
522 "pve2-vm/$param->{vmid}", $param->{timeframe},
523 $param->{ds}, $param->{cf});
524
525 }});
526
527 __PACKAGE__->register_method({
528 name => 'rrddata',
529 path => '{vmid}/rrddata',
530 method => 'GET',
531 protected => 1, # fixme: can we avoid that?
532 permissions => {
533 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
534 },
535 description => "Read VM RRD statistics",
536 parameters => {
537 additionalProperties => 0,
538 properties => {
539 node => get_standard_option('pve-node'),
540 vmid => get_standard_option('pve-vmid'),
541 timeframe => {
542 description => "Specify the time frame you are interested in.",
543 type => 'string',
544 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
545 },
546 cf => {
547 description => "The RRD consolidation function",
548 type => 'string',
549 enum => [ 'AVERAGE', 'MAX' ],
550 optional => 1,
551 },
552 },
553 },
554 returns => {
555 type => "array",
556 items => {
557 type => "object",
558 properties => {},
559 },
560 },
561 code => sub {
562 my ($param) = @_;
563
564 return PVE::Cluster::create_rrd_data(
565 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
566 }});
567
568 __PACKAGE__->register_method({
569 name => 'destroy_vm',
570 path => '{vmid}',
571 method => 'DELETE',
572 protected => 1,
573 proxyto => 'node',
574 description => "Destroy the container (also delete all uses files).",
575 permissions => {
576 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
577 },
578 parameters => {
579 additionalProperties => 0,
580 properties => {
581 node => get_standard_option('pve-node'),
582 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
583 },
584 },
585 returns => {
586 type => 'string',
587 },
588 code => sub {
589 my ($param) = @_;
590
591 my $rpcenv = PVE::RPCEnvironment::get();
592
593 my $authuser = $rpcenv->get_user();
594
595 my $vmid = $param->{vmid};
596
597 # test if container exists
598 my $conf = PVE::LXC::Config->load_config($vmid);
599
600 my $storage_cfg = cfs_read_file("storage.cfg");
601
602 PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid");
603
604 die "unable to remove CT $vmid - used in HA resources\n"
605 if PVE::HA::Config::vm_is_ha_managed($vmid);
606
607 my $running_error_msg = "unable to destroy CT $vmid - container is running\n";
608
609 die $running_error_msg if PVE::LXC::check_running($vmid); # check early
610
611 my $code = sub {
612 # reload config after lock
613 $conf = PVE::LXC::Config->load_config($vmid);
614 PVE::LXC::Config->check_lock($conf);
615
616 die $running_error_msg if PVE::LXC::check_running($vmid);
617
618 # return without error if vm has no replica job
619 PVE::ReplicationTools::destroy_replica($vmid);
620
621 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf);
622 PVE::AccessControl::remove_vm_access($vmid);
623 PVE::Firewall::remove_vmfw_conf($vmid);
624 };
625
626 my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
627
628 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
629 }});
630
631 my $sslcert;
632
633 __PACKAGE__->register_method ({
634 name => 'vncproxy',
635 path => '{vmid}/vncproxy',
636 method => 'POST',
637 protected => 1,
638 permissions => {
639 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
640 },
641 description => "Creates a TCP VNC proxy connections.",
642 parameters => {
643 additionalProperties => 0,
644 properties => {
645 node => get_standard_option('pve-node'),
646 vmid => get_standard_option('pve-vmid'),
647 websocket => {
648 optional => 1,
649 type => 'boolean',
650 description => "use websocket instead of standard VNC.",
651 },
652 },
653 },
654 returns => {
655 additionalProperties => 0,
656 properties => {
657 user => { type => 'string' },
658 ticket => { type => 'string' },
659 cert => { type => 'string' },
660 port => { type => 'integer' },
661 upid => { type => 'string' },
662 },
663 },
664 code => sub {
665 my ($param) = @_;
666
667 my $rpcenv = PVE::RPCEnvironment::get();
668
669 my $authuser = $rpcenv->get_user();
670
671 my $vmid = $param->{vmid};
672 my $node = $param->{node};
673
674 my $authpath = "/vms/$vmid";
675
676 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
677
678 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
679 if !$sslcert;
680
681 my ($remip, $family);
682
683 if ($node ne PVE::INotify::nodename()) {
684 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
685 } else {
686 $family = PVE::Tools::get_host_address_family($node);
687 }
688
689 my $port = PVE::Tools::next_vnc_port($family);
690
691 # NOTE: vncterm VNC traffic is already TLS encrypted,
692 # so we select the fastest chipher here (or 'none'?)
693 my $remcmd = $remip ?
694 ['/usr/bin/ssh', '-t', $remip] : [];
695
696 my $conf = PVE::LXC::Config->load_config($vmid, $node);
697 my $concmd = PVE::LXC::get_console_command($vmid, $conf);
698
699 my $shcmd = [ '/usr/bin/dtach', '-A',
700 "/var/run/dtach/vzctlconsole$vmid",
701 '-r', 'winch', '-z', @$concmd];
702
703 my $realcmd = sub {
704 my $upid = shift;
705
706 syslog ('info', "starting lxc vnc proxy $upid\n");
707
708 my $timeout = 10;
709
710 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
711 '-timeout', $timeout, '-authpath', $authpath,
712 '-perm', 'VM.Console'];
713
714 if ($param->{websocket}) {
715 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
716 push @$cmd, '-notls', '-listen', 'localhost';
717 }
718
719 push @$cmd, '-c', @$remcmd, @$shcmd;
720
721 run_command($cmd, keeplocale => 1);
722
723 return;
724 };
725
726 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
727
728 PVE::Tools::wait_for_vnc_port($port);
729
730 return {
731 user => $authuser,
732 ticket => $ticket,
733 port => $port,
734 upid => $upid,
735 cert => $sslcert,
736 };
737 }});
738
739 __PACKAGE__->register_method({
740 name => 'vncwebsocket',
741 path => '{vmid}/vncwebsocket',
742 method => 'GET',
743 permissions => {
744 description => "You also need to pass a valid ticket (vncticket).",
745 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
746 },
747 description => "Opens a weksocket for VNC traffic.",
748 parameters => {
749 additionalProperties => 0,
750 properties => {
751 node => get_standard_option('pve-node'),
752 vmid => get_standard_option('pve-vmid'),
753 vncticket => {
754 description => "Ticket from previous call to vncproxy.",
755 type => 'string',
756 maxLength => 512,
757 },
758 port => {
759 description => "Port number returned by previous vncproxy call.",
760 type => 'integer',
761 minimum => 5900,
762 maximum => 5999,
763 },
764 },
765 },
766 returns => {
767 type => "object",
768 properties => {
769 port => { type => 'string' },
770 },
771 },
772 code => sub {
773 my ($param) = @_;
774
775 my $rpcenv = PVE::RPCEnvironment::get();
776
777 my $authuser = $rpcenv->get_user();
778
779 my $authpath = "/vms/$param->{vmid}";
780
781 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
782
783 my $port = $param->{port};
784
785 return { port => $port };
786 }});
787
788 __PACKAGE__->register_method ({
789 name => 'spiceproxy',
790 path => '{vmid}/spiceproxy',
791 method => 'POST',
792 protected => 1,
793 proxyto => 'node',
794 permissions => {
795 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
796 },
797 description => "Returns a SPICE configuration to connect to the CT.",
798 parameters => {
799 additionalProperties => 0,
800 properties => {
801 node => get_standard_option('pve-node'),
802 vmid => get_standard_option('pve-vmid'),
803 proxy => get_standard_option('spice-proxy', { optional => 1 }),
804 },
805 },
806 returns => get_standard_option('remote-viewer-config'),
807 code => sub {
808 my ($param) = @_;
809
810 my $vmid = $param->{vmid};
811 my $node = $param->{node};
812 my $proxy = $param->{proxy};
813
814 my $authpath = "/vms/$vmid";
815 my $permissions = 'VM.Console';
816
817 my $conf = PVE::LXC::Config->load_config($vmid);
818
819 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
820
821 my $concmd = PVE::LXC::get_console_command($vmid, $conf);
822
823 my $shcmd = ['/usr/bin/dtach', '-A',
824 "/var/run/dtach/vzctlconsole$vmid",
825 '-r', 'winch', '-z', @$concmd];
826
827 my $title = "CT $vmid";
828
829 return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
830 }});
831
832
833 __PACKAGE__->register_method({
834 name => 'migrate_vm',
835 path => '{vmid}/migrate',
836 method => 'POST',
837 protected => 1,
838 proxyto => 'node',
839 description => "Migrate the container to another node. Creates a new migration task.",
840 permissions => {
841 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
842 },
843 parameters => {
844 additionalProperties => 0,
845 properties => {
846 node => get_standard_option('pve-node'),
847 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
848 target => get_standard_option('pve-node', {
849 description => "Target node.",
850 completion => \&PVE::Cluster::complete_migration_target,
851 }),
852 online => {
853 type => 'boolean',
854 description => "Use online/live migration.",
855 optional => 1,
856 },
857 restart => {
858 type => 'boolean',
859 description => "Use restart migration",
860 optional => 1,
861 },
862 timeout => {
863 type => 'integer',
864 description => "Timeout in seconds for shutdown for restart migration",
865 optional => 1,
866 default => 180,
867 },
868 force => {
869 type => 'boolean',
870 description => "Force migration despite local bind / device" .
871 " mounts. NOTE: deprecated, use 'shared' property of mount point instead.",
872 optional => 1,
873 },
874 },
875 },
876 returns => {
877 type => 'string',
878 description => "the task ID.",
879 },
880 code => sub {
881 my ($param) = @_;
882
883 my $rpcenv = PVE::RPCEnvironment::get();
884
885 my $authuser = $rpcenv->get_user();
886
887 my $target = extract_param($param, 'target');
888
889 my $localnode = PVE::INotify::nodename();
890 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
891
892 PVE::Cluster::check_cfs_quorum();
893
894 PVE::Cluster::check_node_exists($target);
895
896 my $targetip = PVE::Cluster::remote_node_ip($target);
897
898 my $vmid = extract_param($param, 'vmid');
899
900 # test if VM exists
901 PVE::LXC::Config->load_config($vmid);
902
903 # try to detect errors early
904 if (PVE::LXC::check_running($vmid)) {
905 die "can't migrate running container without --online or --restart\n"
906 if !$param->{online} && !$param->{restart};
907 }
908
909 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
910
911 my $hacmd = sub {
912 my $upid = shift;
913
914 my $service = "ct:$vmid";
915
916 my $cmd = ['ha-manager', 'migrate', $service, $target];
917
918 print "Executing HA migrate for CT $vmid to node $target\n";
919
920 PVE::Tools::run_command($cmd);
921
922 return;
923 };
924
925 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
926
927 } else {
928
929 my $realcmd = sub {
930 my $upid = shift;
931
932 PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param);
933
934 return;
935 };
936
937 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
938 }
939 }});
940
941 __PACKAGE__->register_method({
942 name => 'vm_feature',
943 path => '{vmid}/feature',
944 method => 'GET',
945 proxyto => 'node',
946 protected => 1,
947 description => "Check if feature for virtual machine is available.",
948 permissions => {
949 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
950 },
951 parameters => {
952 additionalProperties => 0,
953 properties => {
954 node => get_standard_option('pve-node'),
955 vmid => get_standard_option('pve-vmid'),
956 feature => {
957 description => "Feature to check.",
958 type => 'string',
959 enum => [ 'snapshot' ],
960 },
961 snapname => get_standard_option('pve-lxc-snapshot-name', {
962 optional => 1,
963 }),
964 },
965 },
966 returns => {
967 type => "object",
968 properties => {
969 hasFeature => { type => 'boolean' },
970 #nodes => {
971 #type => 'array',
972 #items => { type => 'string' },
973 #}
974 },
975 },
976 code => sub {
977 my ($param) = @_;
978
979 my $node = extract_param($param, 'node');
980
981 my $vmid = extract_param($param, 'vmid');
982
983 my $snapname = extract_param($param, 'snapname');
984
985 my $feature = extract_param($param, 'feature');
986
987 my $conf = PVE::LXC::Config->load_config($vmid);
988
989 if($snapname){
990 my $snap = $conf->{snapshots}->{$snapname};
991 die "snapshot '$snapname' does not exist\n" if !defined($snap);
992 $conf = $snap;
993 }
994 my $storage_cfg = PVE::Storage::config();
995 #Maybe include later
996 #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
997 my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname);
998
999 return {
1000 hasFeature => $hasFeature,
1001 #nodes => [ keys %$nodelist ],
1002 };
1003 }});
1004
1005 __PACKAGE__->register_method({
1006 name => 'template',
1007 path => '{vmid}/template',
1008 method => 'POST',
1009 protected => 1,
1010 proxyto => 'node',
1011 description => "Create a Template.",
1012 permissions => {
1013 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
1014 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
1015 },
1016 parameters => {
1017 additionalProperties => 0,
1018 properties => {
1019 node => get_standard_option('pve-node'),
1020 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
1021 experimental => {
1022 type => 'boolean',
1023 description => "The template feature is experimental, set this " .
1024 "flag if you know what you are doing.",
1025 default => 0,
1026 },
1027 },
1028 },
1029 returns => { type => 'null'},
1030 code => sub {
1031 my ($param) = @_;
1032
1033 my $rpcenv = PVE::RPCEnvironment::get();
1034
1035 my $authuser = $rpcenv->get_user();
1036
1037 my $node = extract_param($param, 'node');
1038
1039 my $vmid = extract_param($param, 'vmid');
1040
1041 my $updatefn = sub {
1042
1043 my $conf = PVE::LXC::Config->load_config($vmid);
1044 PVE::LXC::Config->check_lock($conf);
1045
1046 die "unable to create template, because CT contains snapshots\n"
1047 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
1048
1049 die "you can't convert a template to a template\n"
1050 if PVE::LXC::Config->is_template($conf);
1051
1052 die "you can't convert a CT to template if the CT is running\n"
1053 if PVE::LXC::check_running($vmid);
1054
1055 my $realcmd = sub {
1056 PVE::LXC::template_create($vmid, $conf);
1057 };
1058
1059 $conf->{template} = 1;
1060
1061 PVE::LXC::Config->write_config($vmid, $conf);
1062 # and remove lxc config
1063 PVE::LXC::update_lxc_config($vmid, $conf);
1064
1065 return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
1066 };
1067
1068 PVE::LXC::Config->lock_config($vmid, $updatefn);
1069
1070 return undef;
1071 }});
1072
1073 __PACKAGE__->register_method({
1074 name => 'clone_vm',
1075 path => '{vmid}/clone',
1076 method => 'POST',
1077 protected => 1,
1078 proxyto => 'node',
1079 description => "Create a container clone/copy",
1080 permissions => {
1081 description => "You need 'VM.Clone' permissions on /vms/{vmid}, " .
1082 "and 'VM.Allocate' permissions " .
1083 "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
1084 "'Datastore.AllocateSpace' on any used storage.",
1085 check =>
1086 [ 'and',
1087 ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
1088 [ 'or',
1089 [ 'perm', '/vms/{newid}', ['VM.Allocate']],
1090 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
1091 ],
1092 ]
1093 },
1094 parameters => {
1095 additionalProperties => 0,
1096 properties => {
1097 node => get_standard_option('pve-node'),
1098 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1099 newid => get_standard_option('pve-vmid', {
1100 completion => \&PVE::Cluster::complete_next_vmid,
1101 description => 'VMID for the clone.' }),
1102 hostname => {
1103 optional => 1,
1104 type => 'string', format => 'dns-name',
1105 description => "Set a hostname for the new CT.",
1106 },
1107 description => {
1108 optional => 1,
1109 type => 'string',
1110 description => "Description for the new CT.",
1111 },
1112 pool => {
1113 optional => 1,
1114 type => 'string', format => 'pve-poolid',
1115 description => "Add the new CT to the specified pool.",
1116 },
1117 snapname => get_standard_option('pve-lxc-snapshot-name', {
1118 optional => 1,
1119 }),
1120 storage => get_standard_option('pve-storage-id', {
1121 description => "Target storage for full clone.",
1122 requires => 'full',
1123 optional => 1,
1124 }),
1125 full => {
1126 optional => 1,
1127 type => 'boolean',
1128 description => "Create a full copy of all disk. This is always done when " .
1129 "you clone a normal CT. For CT templates, we try to create a linked clone by default.",
1130 default => 0,
1131 },
1132 experimental => {
1133 type => 'boolean',
1134 description => "The clone feature is experimental, set this " .
1135 "flag if you know what you are doing.",
1136 default => 0,
1137 },
1138 # target => get_standard_option('pve-node', {
1139 # description => "Target node. Only allowed if the original VM is on shared storage.",
1140 # optional => 1,
1141 # }),
1142 },
1143 },
1144 returns => {
1145 type => 'string',
1146 },
1147 code => sub {
1148 my ($param) = @_;
1149
1150 my $rpcenv = PVE::RPCEnvironment::get();
1151
1152 my $authuser = $rpcenv->get_user();
1153
1154 my $node = extract_param($param, 'node');
1155
1156 my $vmid = extract_param($param, 'vmid');
1157
1158 my $newid = extract_param($param, 'newid');
1159
1160 my $pool = extract_param($param, 'pool');
1161
1162 if (defined($pool)) {
1163 $rpcenv->check_pool_exist($pool);
1164 }
1165
1166 my $snapname = extract_param($param, 'snapname');
1167
1168 my $storage = extract_param($param, 'storage');
1169
1170 my $localnode = PVE::INotify::nodename();
1171
1172 my $storecfg = PVE::Storage::config();
1173
1174 if ($storage) {
1175 # check if storage is enabled on local node
1176 PVE::Storage::storage_check_enabled($storecfg, $storage);
1177 }
1178
1179 PVE::Cluster::check_cfs_quorum();
1180
1181 my $running = PVE::LXC::check_running($vmid) || 0;
1182
1183 my $clonefn = sub {
1184
1185 # do all tests after lock
1186 # we also try to do all tests before we fork the worker
1187 my $conf = PVE::LXC::Config->load_config($vmid);
1188
1189 PVE::LXC::Config->check_lock($conf);
1190
1191 my $verify_running = PVE::LXC::check_running($vmid) || 0;
1192
1193 die "unexpected state change\n" if $verify_running != $running;
1194
1195 die "snapshot '$snapname' does not exist\n"
1196 if $snapname && !defined( $conf->{snapshots}->{$snapname});
1197
1198 my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
1199
1200 my $conffile = PVE::LXC::Config->config_file($newid);
1201 die "unable to create CT $newid: config file already exists\n"
1202 if -f $conffile;
1203
1204 my $newconf = { lock => 'clone' };
1205 my $mountpoints = {};
1206 my $fullclone = {};
1207 my $vollist = [];
1208
1209 foreach my $opt (keys %$oldconf) {
1210 my $value = $oldconf->{$opt};
1211
1212 # no need to copy unused images, because VMID(owner) changes anyways
1213 next if $opt =~ m/^unused\d+$/;
1214
1215 if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) {
1216 my $mp = $opt eq 'rootfs' ?
1217 PVE::LXC::Config->parse_ct_rootfs($value) :
1218 PVE::LXC::Config->parse_ct_mountpoint($value);
1219
1220 if ($mp->{type} eq 'volume') {
1221 my $volid = $mp->{volume};
1222 if ($param->{full}) {
1223 die "fixme: full clone not implemented";
1224
1225 die "Full clone feature for '$volid' is not available\n"
1226 if !PVE::Storage::volume_has_feature($storecfg, 'copy', $volid, $snapname, $running);
1227 $fullclone->{$opt} = 1;
1228 } else {
1229 # not full means clone instead of copy
1230 die "Linked clone feature for '$volid' is not available\n"
1231 if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running);
1232 }
1233
1234 $mountpoints->{$opt} = $mp;
1235 push @$vollist, $volid;
1236
1237 } else {
1238 # TODO: allow bind mounts?
1239 die "unable to clone mountpint '$opt' (type $mp->{type})\n";
1240 }
1241
1242 } else {
1243 # copy everything else
1244 $newconf->{$opt} = $value;
1245 }
1246 }
1247
1248 delete $newconf->{template};
1249 if ($param->{hostname}) {
1250 $newconf->{hostname} = $param->{hostname};
1251 }
1252
1253 if ($param->{description}) {
1254 $newconf->{description} = $param->{description};
1255 }
1256
1257 # create empty/temp config - this fails if CT already exists on other node
1258 PVE::Tools::file_set_contents($conffile, "# ctclone temporary file\nlock: clone\n");
1259
1260 my $realcmd = sub {
1261 my $upid = shift;
1262
1263 my $newvollist = [];
1264
1265 eval {
1266 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
1267
1268 PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
1269
1270 foreach my $opt (keys %$mountpoints) {
1271 my $mp = $mountpoints->{$opt};
1272 my $volid = $mp->{volume};
1273
1274 if ($fullclone->{$opt}) {
1275 die "fixme: full clone not implemented\n";
1276 } else {
1277 print "create linked clone of mount point $opt ($volid)\n";
1278 my $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
1279 push @$newvollist, $newvolid;
1280 $mp->{volume} = $newvolid;
1281
1282 $newconf->{$opt} = PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs');
1283 PVE::LXC::Config->write_config($newid, $newconf);
1284 }
1285 }
1286
1287 delete $newconf->{lock};
1288 PVE::LXC::Config->write_config($newid, $newconf);
1289
1290 PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
1291 };
1292 if (my $err = $@) {
1293 unlink $conffile;
1294
1295 sleep 1; # some storage like rbd need to wait before release volume - really?
1296
1297 foreach my $volid (@$newvollist) {
1298 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
1299 warn $@ if $@;
1300 }
1301 die "clone failed: $err";
1302 }
1303
1304 return;
1305 };
1306
1307 PVE::Firewall::clone_vmfw_conf($vmid, $newid);
1308
1309 return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
1310
1311 };
1312
1313 return PVE::LXC::Config->lock_config($vmid, $clonefn);
1314 }});
1315
1316
1317 __PACKAGE__->register_method({
1318 name => 'resize_vm',
1319 path => '{vmid}/resize',
1320 method => 'PUT',
1321 protected => 1,
1322 proxyto => 'node',
1323 description => "Resize a container mount point.",
1324 permissions => {
1325 check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1],
1326 },
1327 parameters => {
1328 additionalProperties => 0,
1329 properties => {
1330 node => get_standard_option('pve-node'),
1331 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
1332 disk => {
1333 type => 'string',
1334 description => "The disk you want to resize.",
1335 enum => [PVE::LXC::Config->mountpoint_names()],
1336 },
1337 size => {
1338 type => 'string',
1339 pattern => '\+?\d+(\.\d+)?[KMGT]?',
1340 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.",
1341 },
1342 digest => {
1343 type => 'string',
1344 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1345 maxLength => 40,
1346 optional => 1,
1347 }
1348 },
1349 },
1350 returns => {
1351 type => 'string',
1352 description => "the task ID.",
1353 },
1354 code => sub {
1355 my ($param) = @_;
1356
1357 my $rpcenv = PVE::RPCEnvironment::get();
1358
1359 my $authuser = $rpcenv->get_user();
1360
1361 my $node = extract_param($param, 'node');
1362
1363 my $vmid = extract_param($param, 'vmid');
1364
1365 my $digest = extract_param($param, 'digest');
1366
1367 my $sizestr = extract_param($param, 'size');
1368 my $ext = ($sizestr =~ s/^\+//);
1369 my $newsize = PVE::JSONSchema::parse_size($sizestr);
1370 die "invalid size string" if !defined($newsize);
1371
1372 die "no options specified\n" if !scalar(keys %$param);
1373
1374 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []);
1375
1376 my $storage_cfg = cfs_read_file("storage.cfg");
1377
1378 my $code = sub {
1379
1380 my $conf = PVE::LXC::Config->load_config($vmid);
1381 PVE::LXC::Config->check_lock($conf);
1382
1383 PVE::Tools::assert_if_modified($digest, $conf->{digest});
1384
1385 my $running = PVE::LXC::check_running($vmid);
1386
1387 my $disk = $param->{disk};
1388 my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) :
1389 PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk});
1390
1391 my $volid = $mp->{volume};
1392
1393 my (undef, undef, $owner, undef, undef, undef, $format) =
1394 PVE::Storage::parse_volname($storage_cfg, $volid);
1395
1396 die "can't resize mount point owned by another container ($owner)"
1397 if $vmid != $owner;
1398
1399 die "can't resize volume: $disk if snapshot exists\n"
1400 if %{$conf->{snapshots}} && $format eq 'qcow2';
1401
1402 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1403
1404 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1405
1406 PVE::Storage::activate_volumes($storage_cfg, [$volid]);
1407
1408 my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5);
1409 $newsize += $size if $ext;
1410 $newsize = int($newsize);
1411
1412 die "unable to shrink disk size\n" if $newsize < $size;
1413
1414 return if $size == $newsize;
1415
1416 PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
1417 my $realcmd = sub {
1418 # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so
1419 # we pass 0 here (parameter only makes sense for qemu)
1420 PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
1421
1422 $mp->{size} = $newsize;
1423 $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs');
1424
1425 PVE::LXC::Config->write_config($vmid, $conf);
1426
1427 if ($format eq 'raw') {
1428 my $path = PVE::Storage::path($storage_cfg, $volid, undef);
1429 if ($running) {
1430
1431 $mp->{mp} = '/';
1432 my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1];
1433 $path = PVE::LXC::query_loopdev($path) if $use_loopdev;
1434 die "internal error: CT running but mount point not attached to a loop device"
1435 if !$path;
1436 PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev;
1437
1438 # In order for resize2fs to know that we need online-resizing a mountpoint needs
1439 # to be visible to it in its namespace.
1440 # To not interfere with the rest of the system we unshare the current mount namespace,
1441 # mount over /tmp and then run resize2fs.
1442
1443 # interestingly we don't need to e2fsck on mounted systems...
1444 my $quoted = PVE::Tools::shellquote($path);
1445 my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
1446 eval {
1447 PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
1448 };
1449 warn "Failed to update the container's filesystem: $@\n" if $@;
1450 } else {
1451 eval {
1452 PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
1453 PVE::Tools::run_command(['resize2fs', $path]);
1454 };
1455 warn "Failed to update the container's filesystem: $@\n" if $@;
1456 }
1457 }
1458 };
1459
1460 return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd);
1461 };
1462
1463 return PVE::LXC::Config->lock_config($vmid, $code);;
1464 }});
1465
1466 1;