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