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