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