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