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