]>
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); | |
0620edda | 8 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); |
f76a2828 | 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; | |
c5a8e04f | 16 | use PVE::ReplicationConfig; |
f76a2828 | 17 | use PVE::LXC; |
7af97ad5 | 18 | use PVE::LXC::Create; |
6f42807e | 19 | use PVE::LXC::Migrate; |
56462053 | 20 | use PVE::GuestHelpers; |
52389a07 DM |
21 | use PVE::API2::LXC::Config; |
22 | use PVE::API2::LXC::Status; | |
23 | use PVE::API2::LXC::Snapshot; | |
f76a2828 DM |
24 | use PVE::JSONSchema qw(get_standard_option); |
25 | use base qw(PVE::RESTHandler); | |
26 | ||
6c2e9377 WB |
27 | BEGIN { |
28 | if (!$ENV{PVE_GENERATING_DOCS}) { | |
29 | require PVE::HA::Env::PVE2; | |
30 | import PVE::HA::Env::PVE2; | |
31 | require PVE::HA::Config; | |
32 | import PVE::HA::Config; | |
33 | } | |
34 | } | |
f76a2828 | 35 | |
52389a07 DM |
36 | __PACKAGE__->register_method ({ |
37 | subclass => "PVE::API2::LXC::Config", | |
38 | path => '{vmid}/config', | |
39 | }); | |
f76a2828 | 40 | |
52389a07 DM |
41 | __PACKAGE__->register_method ({ |
42 | subclass => "PVE::API2::LXC::Status", | |
43 | path => '{vmid}/status', | |
44 | }); | |
f76a2828 | 45 | |
52389a07 DM |
46 | __PACKAGE__->register_method ({ |
47 | subclass => "PVE::API2::LXC::Snapshot", | |
48 | path => '{vmid}/snapshot', | |
49 | }); | |
f76a2828 | 50 | |
52389a07 DM |
51 | __PACKAGE__->register_method ({ |
52 | subclass => "PVE::API2::Firewall::CT", | |
53 | path => '{vmid}/firewall', | |
54 | }); | |
1e6c8d5b | 55 | |
f76a2828 | 56 | __PACKAGE__->register_method({ |
5c752bbf DM |
57 | name => 'vmlist', |
58 | path => '', | |
f76a2828 DM |
59 | method => 'GET', |
60 | description => "LXC container index (per node).", | |
61 | permissions => { | |
62 | description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.", | |
63 | user => 'all', | |
64 | }, | |
65 | proxyto => 'node', | |
66 | protected => 1, # /proc files are only readable by root | |
67 | parameters => { | |
68 | additionalProperties => 0, | |
69 | properties => { | |
70 | node => get_standard_option('pve-node'), | |
71 | }, | |
72 | }, | |
73 | returns => { | |
74 | type => 'array', | |
75 | items => { | |
76 | type => "object", | |
7c3d228f DM |
77 | properties => { |
78 | vmid => get_standard_option('pve-vmid'), | |
79 | status => { | |
80 | description => "LXC Container status.", | |
81 | type => 'string', | |
82 | enum => ['stopped', 'running'], | |
83 | }, | |
84 | maxmem => { | |
85 | description => "Maximum memory in bytes.", | |
86 | type => 'integer', | |
87 | optional => 1, | |
88 | renderer => 'bytes', | |
89 | }, | |
90 | maxswap => { | |
91 | description => "Maximum SWAP memory in bytes.", | |
92 | type => 'integer', | |
93 | optional => 1, | |
94 | renderer => 'bytes', | |
95 | }, | |
96 | maxdisk => { | |
97 | description => "Root disk size in bytes.", | |
98 | type => 'integer', | |
99 | optional => 1, | |
100 | renderer => 'bytes', | |
101 | }, | |
102 | name => { | |
103 | description => "Container name.", | |
104 | type => 'string', | |
105 | optional => 1, | |
106 | }, | |
107 | uptime => { | |
108 | description => "Uptime.", | |
109 | type => 'integer', | |
110 | optional => 1, | |
111 | renderer => 'duration', | |
112 | }, | |
113 | cpus => { | |
114 | description => "Maximum usable CPUs.", | |
115 | type => 'number', | |
116 | optional => 1, | |
117 | }, | |
118 | }, | |
f76a2828 DM |
119 | }, |
120 | links => [ { rel => 'child', href => "{vmid}" } ], | |
121 | }, | |
122 | code => sub { | |
123 | my ($param) = @_; | |
124 | ||
125 | my $rpcenv = PVE::RPCEnvironment::get(); | |
126 | my $authuser = $rpcenv->get_user(); | |
127 | ||
128 | my $vmstatus = PVE::LXC::vmstatus(); | |
129 | ||
130 | my $res = []; | |
131 | foreach my $vmid (keys %$vmstatus) { | |
132 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
133 | ||
134 | my $data = $vmstatus->{$vmid}; | |
135 | $data->{vmid} = $vmid; | |
136 | push @$res, $data; | |
137 | } | |
138 | ||
139 | return $res; | |
5c752bbf | 140 | |
f76a2828 DM |
141 | }}); |
142 | ||
9c2d4ce9 | 143 | __PACKAGE__->register_method({ |
5c752bbf DM |
144 | name => 'create_vm', |
145 | path => '', | |
9c2d4ce9 DM |
146 | method => 'POST', |
147 | description => "Create or restore a container.", | |
148 | permissions => { | |
149 | user => 'all', # check inside | |
150 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . | |
151 | "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
152 | "You also need 'Datastore.AllocateSpace' permissions on the storage.", | |
153 | }, | |
154 | protected => 1, | |
155 | proxyto => 'node', | |
156 | parameters => { | |
157 | additionalProperties => 0, | |
1b4cf758 | 158 | properties => PVE::LXC::Config->json_config_properties({ |
9c2d4ce9 | 159 | node => get_standard_option('pve-node'), |
781e26b2 | 160 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), |
9c2d4ce9 DM |
161 | ostemplate => { |
162 | description => "The OS template or backup file.", | |
5c752bbf | 163 | type => 'string', |
9c2d4ce9 | 164 | maxLength => 255, |
68e8f3c5 | 165 | completion => \&PVE::LXC::complete_os_templates, |
9c2d4ce9 | 166 | }, |
5c752bbf DM |
167 | password => { |
168 | optional => 1, | |
9c2d4ce9 DM |
169 | type => 'string', |
170 | description => "Sets root password inside container.", | |
168d6b07 | 171 | minLength => 5, |
9c2d4ce9 DM |
172 | }, |
173 | storage => get_standard_option('pve-storage-id', { | |
eb35f9c0 | 174 | description => "Default Storage.", |
9c2d4ce9 DM |
175 | default => 'local', |
176 | optional => 1, | |
c5362cda | 177 | completion => \&PVE::Storage::complete_storage_enabled, |
9c2d4ce9 DM |
178 | }), |
179 | force => { | |
5c752bbf | 180 | optional => 1, |
9c2d4ce9 DM |
181 | type => 'boolean', |
182 | description => "Allow to overwrite existing container.", | |
183 | }, | |
184 | restore => { | |
5c752bbf | 185 | optional => 1, |
9c2d4ce9 DM |
186 | type => 'boolean', |
187 | description => "Mark this as restore task.", | |
188 | }, | |
5c752bbf | 189 | pool => { |
9c2d4ce9 DM |
190 | optional => 1, |
191 | type => 'string', format => 'pve-poolid', | |
192 | description => "Add the VM to the specified pool.", | |
193 | }, | |
7c78b6cc WB |
194 | 'ignore-unpack-errors' => { |
195 | optional => 1, | |
196 | type => 'boolean', | |
197 | description => "Ignore errors when extracting the template.", | |
198 | }, | |
34ddbf08 FG |
199 | 'ssh-public-keys' => { |
200 | optional => 1, | |
201 | type => 'string', | |
202 | description => "Setup public SSH keys (one key per line, " . | |
203 | "OpenSSH format).", | |
204 | }, | |
f9b4407e WB |
205 | bwlimit => { |
206 | description => "Override i/o bandwidth limit (in KiB/s).", | |
207 | optional => 1, | |
208 | type => 'number', | |
209 | minimum => '0', | |
210 | }, | |
9d0225fb TL |
211 | start => { |
212 | optional => 1, | |
213 | type => 'boolean', | |
214 | default => 0, | |
215 | description => "Start the CT after its creation finished successfully.", | |
216 | }, | |
9c2d4ce9 DM |
217 | }), |
218 | }, | |
5c752bbf | 219 | returns => { |
9c2d4ce9 DM |
220 | type => 'string', |
221 | }, | |
222 | code => sub { | |
223 | my ($param) = @_; | |
224 | ||
225 | my $rpcenv = PVE::RPCEnvironment::get(); | |
226 | ||
227 | my $authuser = $rpcenv->get_user(); | |
228 | ||
229 | my $node = extract_param($param, 'node'); | |
230 | ||
231 | my $vmid = extract_param($param, 'vmid'); | |
232 | ||
7c78b6cc WB |
233 | my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors'); |
234 | ||
f9b4407e WB |
235 | my $bwlimit = extract_param($param, 'bwlimit'); |
236 | ||
9d0225fb TL |
237 | my $start_after_create = extract_param($param, 'start'); |
238 | ||
67afe46e | 239 | my $basecfg_fn = PVE::LXC::Config->config_file($vmid); |
9c2d4ce9 DM |
240 | |
241 | my $same_container_exists = -f $basecfg_fn; | |
242 | ||
425b62cb WB |
243 | # 'unprivileged' is read-only, so we can't pass it to update_pct_config |
244 | my $unprivileged = extract_param($param, 'unprivileged'); | |
245 | ||
9c2d4ce9 DM |
246 | my $restore = extract_param($param, 'restore'); |
247 | ||
148d1cb4 DM |
248 | if ($restore) { |
249 | # fixme: limit allowed parameters | |
250 | ||
251 | } | |
252 | ||
9c2d4ce9 DM |
253 | my $force = extract_param($param, 'force'); |
254 | ||
255 | if (!($same_container_exists && $restore && $force)) { | |
256 | PVE::Cluster::check_vmid_unused($vmid); | |
e22af68f | 257 | } else { |
67afe46e FG |
258 | my $conf = PVE::LXC::Config->load_config($vmid); |
259 | PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid"); | |
9c2d4ce9 | 260 | } |
5c752bbf | 261 | |
9c2d4ce9 DM |
262 | my $password = extract_param($param, 'password'); |
263 | ||
34ddbf08 | 264 | my $ssh_keys = extract_param($param, 'ssh-public-keys'); |
2130286f | 265 | PVE::Tools::validate_ssh_public_keys($ssh_keys) if defined($ssh_keys); |
34ddbf08 | 266 | |
27916659 DM |
267 | my $pool = extract_param($param, 'pool'); |
268 | ||
9c2d4ce9 DM |
269 | if (defined($pool)) { |
270 | $rpcenv->check_pool_exist($pool); | |
271 | $rpcenv->check_perm_modify($authuser, "/pool/$pool"); | |
5c752bbf | 272 | } |
9c2d4ce9 DM |
273 | |
274 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { | |
275 | # OK | |
276 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
277 | # OK | |
278 | } elsif ($restore && $force && $same_container_exists && | |
279 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
280 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
281 | } else { | |
282 | raise_perm_exc(); | |
283 | } | |
284 | ||
9eb69152 | 285 | my $ostemplate = extract_param($param, 'ostemplate'); |
bb6afcb0 DM |
286 | my $storage = extract_param($param, 'storage') // 'local'; |
287 | ||
9eb69152 FG |
288 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []); |
289 | ||
bb6afcb0 | 290 | my $storage_cfg = cfs_read_file("storage.cfg"); |
9c2d4ce9 | 291 | |
5c752bbf | 292 | |
9c2d4ce9 DM |
293 | my $archive; |
294 | ||
295 | if ($ostemplate eq '-') { | |
148d1cb4 DM |
296 | die "pipe requires cli environment\n" |
297 | if $rpcenv->{type} ne 'cli'; | |
298 | die "pipe can only be used with restore tasks\n" | |
299 | if !$restore; | |
300 | $archive = '-'; | |
b51a98d4 | 301 | die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs}); |
9c2d4ce9 | 302 | } else { |
f26c66e0 | 303 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, $vmid, $ostemplate); |
9c2d4ce9 DM |
304 | $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate); |
305 | } | |
306 | ||
f9b4407e | 307 | my %used_storages; |
bb6afcb0 DM |
308 | my $check_and_activate_storage = sub { |
309 | my ($sid) = @_; | |
310 | ||
311 | my $scfg = PVE::Storage::storage_check_node($storage_cfg, $sid, $node); | |
312 | ||
313 | raise_param_exc({ storage => "storage '$sid' does not support container directories"}) | |
314 | if !$scfg->{content}->{rootdir}; | |
315 | ||
316 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
317 | ||
318 | PVE::Storage::activate_storage($storage_cfg, $sid); | |
f9b4407e WB |
319 | |
320 | $used_storages{$sid} = 1; | |
bb6afcb0 DM |
321 | }; |
322 | ||
9c2d4ce9 | 323 | my $conf = {}; |
5b4657d0 | 324 | |
b51a98d4 | 325 | my $no_disk_param = {}; |
db18c1e4 FG |
326 | my $mp_param = {}; |
327 | my $storage_only_mode = 1; | |
b51a98d4 | 328 | foreach my $opt (keys %$param) { |
78ccc99b DM |
329 | my $value = $param->{$opt}; |
330 | if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) { | |
331 | # allow to use simple numbers (add default storage in that case) | |
db18c1e4 FG |
332 | if ($value =~ m/^\d+(\.\d+)?$/) { |
333 | $mp_param->{$opt} = "$storage:$value"; | |
334 | } else { | |
335 | $mp_param->{$opt} = $value; | |
336 | } | |
337 | $storage_only_mode = 0; | |
a9dd8015 FG |
338 | } elsif ($opt =~ m/^unused\d+$/) { |
339 | warn "ignoring '$opt', cannot create/restore with unused volume\n"; | |
340 | delete $param->{$opt}; | |
78ccc99b DM |
341 | } else { |
342 | $no_disk_param->{$opt} = $value; | |
343 | } | |
b51a98d4 | 344 | } |
bb6afcb0 | 345 | |
235dbdf3 | 346 | die "mount points configured, but 'rootfs' not set - aborting\n" |
db18c1e4 FG |
347 | if !$storage_only_mode && !defined($mp_param->{rootfs}); |
348 | ||
bb6afcb0 | 349 | # check storage access, activate storage |
db18c1e4 FG |
350 | my $delayed_mp_param = {}; |
351 | PVE::LXC::Config->foreach_mountpoint($mp_param, sub { | |
bb6afcb0 DM |
352 | my ($ms, $mountpoint) = @_; |
353 | ||
354 | my $volid = $mountpoint->{volume}; | |
355 | my $mp = $mountpoint->{mp}; | |
356 | ||
e2007ac2 DM |
357 | if ($mountpoint->{type} ne 'volume') { # bind or device |
358 | die "Only root can pass arbitrary filesystem paths.\n" | |
359 | if $authuser ne 'root@pam'; | |
360 | } else { | |
361 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
362 | &$check_and_activate_storage($sid); | |
363 | } | |
bb6afcb0 DM |
364 | }); |
365 | ||
366 | # check/activate default storage | |
db18c1e4 | 367 | &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs}); |
bb6afcb0 | 368 | |
1b4cf758 | 369 | PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param); |
9c2d4ce9 | 370 | |
425b62cb WB |
371 | $conf->{unprivileged} = 1 if $unprivileged; |
372 | ||
148d1cb4 DM |
373 | my $check_vmid_usage = sub { |
374 | if ($force) { | |
5c45496e | 375 | die "can't overwrite running container\n" |
148d1cb4 DM |
376 | if PVE::LXC::check_running($vmid); |
377 | } else { | |
378 | PVE::Cluster::check_vmid_unused($vmid); | |
379 | } | |
380 | }; | |
f507c3a7 | 381 | |
5b4657d0 | 382 | my $code = sub { |
148d1cb4 | 383 | &$check_vmid_usage(); # final check after locking |
bccaa371 FG |
384 | my $old_conf; |
385 | ||
386 | my $config_fn = PVE::LXC::Config->config_file($vmid); | |
387 | if (-f $config_fn) { | |
388 | die "container exists" if !$restore; # just to be sure | |
389 | $old_conf = PVE::LXC::Config->load_config($vmid); | |
390 | } else { | |
391 | eval { | |
392 | # try to create empty config on local node, we have an flock | |
393 | PVE::LXC::Config->write_config($vmid, {}); | |
394 | }; | |
395 | ||
396 | # another node was faster, abort | |
397 | die "Could not reserve ID $vmid, already taken\n" if $@; | |
398 | } | |
399 | ||
148d1cb4 | 400 | PVE::Cluster::check_cfs_quorum(); |
eb35f9c0 | 401 | my $vollist = []; |
27916659 DM |
402 | |
403 | eval { | |
db18c1e4 | 404 | if ($storage_only_mode) { |
b51a98d4 | 405 | if ($restore) { |
db18c1e4 FG |
406 | (undef, $mp_param) = PVE::LXC::Create::recover_config($archive); |
407 | die "rootfs configuration could not be recovered, please check and specify manually!\n" | |
408 | if !defined($mp_param->{rootfs}); | |
409 | PVE::LXC::Config->foreach_mountpoint($mp_param, sub { | |
410 | my ($ms, $mountpoint) = @_; | |
411 | my $type = $mountpoint->{type}; | |
412 | if ($type eq 'volume') { | |
413 | die "unable to detect disk size - please specify $ms (size)\n" | |
414 | if !defined($mountpoint->{size}); | |
415 | my $disksize = $mountpoint->{size} / (1024 * 1024 * 1024); # create_disks expects GB as unit size | |
416 | delete $mountpoint->{size}; | |
417 | $mountpoint->{volume} = "$storage:$disksize"; | |
418 | $mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); | |
419 | } else { | |
15e4d443 | 420 | my $type = $mountpoint->{type}; |
7e0e7f38 FG |
421 | die "restoring rootfs to $type mount is only possible by specifying -rootfs manually!\n" |
422 | if ($ms eq 'rootfs'); | |
20f9339d FG |
423 | die "restoring '$ms' to $type mount is only possible for root\n" |
424 | if $authuser ne 'root@pam'; | |
7e0e7f38 | 425 | |
15e4d443 FG |
426 | if ($mountpoint->{backup}) { |
427 | warn "WARNING - unsupported configuration!\n"; | |
235dbdf3 FG |
428 | warn "backup was enabled for $type mount point $ms ('$mountpoint->{mp}')\n"; |
429 | warn "mount point configuration will be restored after archive extraction!\n"; | |
15e4d443 FG |
430 | warn "contained files will be restored to wrong directory!\n"; |
431 | } | |
136040f4 | 432 | delete $mp_param->{$ms}; # actually delay bind/dev mps |
db18c1e4 FG |
433 | $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); |
434 | } | |
435 | }); | |
b51a98d4 | 436 | } else { |
db18c1e4 | 437 | $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB |
b51a98d4 DM |
438 | } |
439 | } | |
440 | ||
db18c1e4 FG |
441 | $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf); |
442 | ||
bccaa371 | 443 | if (defined($old_conf)) { |
51665c2d | 444 | # destroy old container volumes |
bccaa371 | 445 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {}); |
51665c2d | 446 | } |
51665c2d FG |
447 | |
448 | eval { | |
449 | my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1); | |
f9b4407e WB |
450 | $bwlimit = PVE::Storage::get_bandwidth_limit('restore', [keys %used_storages], $bwlimit); |
451 | PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit); | |
51665c2d FG |
452 | |
453 | if ($restore) { | |
ba0e2930 | 454 | PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, $authuser ne 'root@pam'); |
51665c2d FG |
455 | } else { |
456 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS | |
457 | PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection) | |
458 | $lxc_setup->post_create_hook($password, $ssh_keys); | |
459 | } | |
460 | }; | |
461 | my $err = $@; | |
462 | PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0); | |
463 | PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf)); | |
464 | die $err if $err; | |
27916659 DM |
465 | # set some defaults |
466 | $conf->{hostname} ||= "CT$vmid"; | |
467 | $conf->{memory} ||= 512; | |
468 | $conf->{swap} //= 512; | |
db18c1e4 FG |
469 | foreach my $mp (keys %$delayed_mp_param) { |
470 | $conf->{$mp} = $delayed_mp_param->{$mp}; | |
471 | } | |
67afe46e | 472 | PVE::LXC::Config->write_config($vmid, $conf); |
27916659 DM |
473 | }; |
474 | if (my $err = $@) { | |
6c871c36 | 475 | PVE::LXC::destroy_disks($storage_cfg, $vollist); |
27916659 DM |
476 | PVE::LXC::destroy_config($vmid); |
477 | die $err; | |
6d098bf4 | 478 | } |
87273b2b | 479 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
9d0225fb TL |
480 | |
481 | PVE::API2::LXC::Status->vm_start({ vmid => $vmid, node => $node }) | |
482 | if $start_after_create; | |
9c2d4ce9 | 483 | }; |
5c752bbf | 484 | |
67afe46e | 485 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
9c2d4ce9 | 486 | |
148d1cb4 DM |
487 | &$check_vmid_usage(); # first check before locking |
488 | ||
489 | return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate', | |
9c2d4ce9 | 490 | $vmid, $authuser, $realcmd); |
5c752bbf | 491 | |
9c2d4ce9 DM |
492 | }}); |
493 | ||
f76a2828 DM |
494 | __PACKAGE__->register_method({ |
495 | name => 'vmdiridx', | |
5c752bbf | 496 | path => '{vmid}', |
f76a2828 DM |
497 | method => 'GET', |
498 | proxyto => 'node', | |
499 | description => "Directory index", | |
500 | permissions => { | |
501 | user => 'all', | |
502 | }, | |
503 | parameters => { | |
504 | additionalProperties => 0, | |
505 | properties => { | |
506 | node => get_standard_option('pve-node'), | |
507 | vmid => get_standard_option('pve-vmid'), | |
508 | }, | |
509 | }, | |
510 | returns => { | |
511 | type => 'array', | |
512 | items => { | |
513 | type => "object", | |
514 | properties => { | |
515 | subdir => { type => 'string' }, | |
516 | }, | |
517 | }, | |
518 | links => [ { rel => 'child', href => "{subdir}" } ], | |
519 | }, | |
520 | code => sub { | |
521 | my ($param) = @_; | |
522 | ||
523 | # test if VM exists | |
67afe46e | 524 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); |
f76a2828 DM |
525 | |
526 | my $res = [ | |
527 | { subdir => 'config' }, | |
fff3a342 DM |
528 | { subdir => 'status' }, |
529 | { subdir => 'vncproxy' }, | |
a0226a00 | 530 | { subdir => 'termproxy' }, |
fff3a342 DM |
531 | { subdir => 'vncwebsocket' }, |
532 | { subdir => 'spiceproxy' }, | |
533 | { subdir => 'migrate' }, | |
c4a33727 | 534 | { subdir => 'clone' }, |
f76a2828 DM |
535 | # { subdir => 'initlog' }, |
536 | { subdir => 'rrd' }, | |
537 | { subdir => 'rrddata' }, | |
538 | { subdir => 'firewall' }, | |
cc5392c8 | 539 | { subdir => 'snapshot' }, |
985b18ed | 540 | { subdir => 'resize' }, |
f76a2828 | 541 | ]; |
5c752bbf | 542 | |
f76a2828 DM |
543 | return $res; |
544 | }}); | |
545 | ||
c4a33727 | 546 | |
f76a2828 | 547 | __PACKAGE__->register_method({ |
5c752bbf DM |
548 | name => 'rrd', |
549 | path => '{vmid}/rrd', | |
f76a2828 DM |
550 | method => 'GET', |
551 | protected => 1, # fixme: can we avoid that? | |
552 | permissions => { | |
553 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
554 | }, | |
555 | description => "Read VM RRD statistics (returns PNG)", | |
556 | parameters => { | |
557 | additionalProperties => 0, | |
558 | properties => { | |
559 | node => get_standard_option('pve-node'), | |
560 | vmid => get_standard_option('pve-vmid'), | |
561 | timeframe => { | |
562 | description => "Specify the time frame you are interested in.", | |
563 | type => 'string', | |
564 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
565 | }, | |
566 | ds => { | |
567 | description => "The list of datasources you want to display.", | |
568 | type => 'string', format => 'pve-configid-list', | |
569 | }, | |
570 | cf => { | |
571 | description => "The RRD consolidation function", | |
572 | type => 'string', | |
573 | enum => [ 'AVERAGE', 'MAX' ], | |
574 | optional => 1, | |
575 | }, | |
576 | }, | |
577 | }, | |
578 | returns => { | |
579 | type => "object", | |
580 | properties => { | |
581 | filename => { type => 'string' }, | |
582 | }, | |
583 | }, | |
584 | code => sub { | |
585 | my ($param) = @_; | |
586 | ||
587 | return PVE::Cluster::create_rrd_graph( | |
5c752bbf | 588 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
f76a2828 | 589 | $param->{ds}, $param->{cf}); |
5c752bbf | 590 | |
f76a2828 DM |
591 | }}); |
592 | ||
593 | __PACKAGE__->register_method({ | |
5c752bbf DM |
594 | name => 'rrddata', |
595 | path => '{vmid}/rrddata', | |
f76a2828 DM |
596 | method => 'GET', |
597 | protected => 1, # fixme: can we avoid that? | |
598 | permissions => { | |
599 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
600 | }, | |
601 | description => "Read VM RRD statistics", | |
602 | parameters => { | |
603 | additionalProperties => 0, | |
604 | properties => { | |
605 | node => get_standard_option('pve-node'), | |
606 | vmid => get_standard_option('pve-vmid'), | |
607 | timeframe => { | |
608 | description => "Specify the time frame you are interested in.", | |
609 | type => 'string', | |
610 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
611 | }, | |
612 | cf => { | |
613 | description => "The RRD consolidation function", | |
614 | type => 'string', | |
615 | enum => [ 'AVERAGE', 'MAX' ], | |
616 | optional => 1, | |
617 | }, | |
618 | }, | |
619 | }, | |
620 | returns => { | |
621 | type => "array", | |
622 | items => { | |
623 | type => "object", | |
624 | properties => {}, | |
625 | }, | |
626 | }, | |
627 | code => sub { | |
628 | my ($param) = @_; | |
629 | ||
630 | return PVE::Cluster::create_rrd_data( | |
631 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
632 | }}); | |
633 | ||
f76a2828 | 634 | __PACKAGE__->register_method({ |
5c752bbf DM |
635 | name => 'destroy_vm', |
636 | path => '{vmid}', | |
f76a2828 DM |
637 | method => 'DELETE', |
638 | protected => 1, | |
639 | proxyto => 'node', | |
640 | description => "Destroy the container (also delete all uses files).", | |
641 | permissions => { | |
642 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
643 | }, | |
644 | parameters => { | |
645 | additionalProperties => 0, | |
646 | properties => { | |
647 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 648 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
f76a2828 DM |
649 | }, |
650 | }, | |
5c752bbf | 651 | returns => { |
f76a2828 DM |
652 | type => 'string', |
653 | }, | |
654 | code => sub { | |
655 | my ($param) = @_; | |
656 | ||
657 | my $rpcenv = PVE::RPCEnvironment::get(); | |
658 | ||
659 | my $authuser = $rpcenv->get_user(); | |
660 | ||
661 | my $vmid = $param->{vmid}; | |
662 | ||
663 | # test if container exists | |
67afe46e | 664 | my $conf = PVE::LXC::Config->load_config($vmid); |
f76a2828 | 665 | |
611fe3aa DM |
666 | my $storage_cfg = cfs_read_file("storage.cfg"); |
667 | ||
67afe46e | 668 | PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid"); |
7e806596 | 669 | |
9d87e069 | 670 | die "unable to remove CT $vmid - used in HA resources\n" |
b6e0b774 AG |
671 | if PVE::HA::Config::vm_is_ha_managed($vmid); |
672 | ||
c5a8e04f DM |
673 | # do not allow destroy if there are replication jobs |
674 | my $repl_conf = PVE::ReplicationConfig->new(); | |
675 | $repl_conf->check_for_existing_jobs($vmid); | |
676 | ||
d607c17d DM |
677 | my $running_error_msg = "unable to destroy CT $vmid - container is running\n"; |
678 | ||
679 | die $running_error_msg if PVE::LXC::check_running($vmid); # check early | |
680 | ||
611fe3aa | 681 | my $code = sub { |
673cf209 | 682 | # reload config after lock |
67afe46e FG |
683 | $conf = PVE::LXC::Config->load_config($vmid); |
684 | PVE::LXC::Config->check_lock($conf); | |
673cf209 | 685 | |
d607c17d DM |
686 | die $running_error_msg if PVE::LXC::check_running($vmid); |
687 | ||
27916659 | 688 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf); |
be5fc936 | 689 | PVE::AccessControl::remove_vm_access($vmid); |
2f9b5ead | 690 | PVE::Firewall::remove_vmfw_conf($vmid); |
f76a2828 DM |
691 | }; |
692 | ||
67afe46e | 693 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
611fe3aa | 694 | |
f76a2828 DM |
695 | return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd); |
696 | }}); | |
697 | ||
fff3a342 DM |
698 | my $sslcert; |
699 | ||
700 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
701 | name => 'vncproxy', |
702 | path => '{vmid}/vncproxy', | |
fff3a342 DM |
703 | method => 'POST', |
704 | protected => 1, | |
705 | permissions => { | |
706 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
707 | }, | |
708 | description => "Creates a TCP VNC proxy connections.", | |
709 | parameters => { | |
710 | additionalProperties => 0, | |
711 | properties => { | |
712 | node => get_standard_option('pve-node'), | |
713 | vmid => get_standard_option('pve-vmid'), | |
714 | websocket => { | |
715 | optional => 1, | |
716 | type => 'boolean', | |
717 | description => "use websocket instead of standard VNC.", | |
718 | }, | |
bd0f4d6d DC |
719 | width => { |
720 | optional => 1, | |
721 | description => "sets the width of the console in pixels.", | |
722 | type => 'integer', | |
723 | minimum => 16, | |
724 | maximum => 4096, | |
725 | }, | |
726 | height => { | |
727 | optional => 1, | |
728 | description => "sets the height of the console in pixels.", | |
729 | type => 'integer', | |
730 | minimum => 16, | |
731 | maximum => 2160, | |
732 | }, | |
fff3a342 DM |
733 | }, |
734 | }, | |
5b4657d0 | 735 | returns => { |
fff3a342 DM |
736 | additionalProperties => 0, |
737 | properties => { | |
738 | user => { type => 'string' }, | |
739 | ticket => { type => 'string' }, | |
740 | cert => { type => 'string' }, | |
741 | port => { type => 'integer' }, | |
742 | upid => { type => 'string' }, | |
743 | }, | |
744 | }, | |
745 | code => sub { | |
746 | my ($param) = @_; | |
747 | ||
748 | my $rpcenv = PVE::RPCEnvironment::get(); | |
749 | ||
750 | my $authuser = $rpcenv->get_user(); | |
751 | ||
752 | my $vmid = $param->{vmid}; | |
753 | my $node = $param->{node}; | |
754 | ||
755 | my $authpath = "/vms/$vmid"; | |
756 | ||
757 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
758 | ||
759 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
760 | if !$sslcert; | |
761 | ||
ec2c57eb | 762 | my ($remip, $family); |
5b4657d0 | 763 | |
fff3a342 | 764 | if ($node ne PVE::INotify::nodename()) { |
85ae6211 | 765 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); |
ec2c57eb WB |
766 | } else { |
767 | $family = PVE::Tools::get_host_address_family($node); | |
fff3a342 DM |
768 | } |
769 | ||
ec2c57eb WB |
770 | my $port = PVE::Tools::next_vnc_port($family); |
771 | ||
fff3a342 DM |
772 | # NOTE: vncterm VNC traffic is already TLS encrypted, |
773 | # so we select the fastest chipher here (or 'none'?) | |
5b4657d0 | 774 | my $remcmd = $remip ? |
806eae70 | 775 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : []; |
fff3a342 | 776 | |
67afe46e | 777 | my $conf = PVE::LXC::Config->load_config($vmid, $node); |
4d494664 | 778 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, 1); |
aca816ad | 779 | |
5b4657d0 DM |
780 | my $shcmd = [ '/usr/bin/dtach', '-A', |
781 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 782 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
783 | |
784 | my $realcmd = sub { | |
785 | my $upid = shift; | |
786 | ||
5b4657d0 | 787 | syslog ('info', "starting lxc vnc proxy $upid\n"); |
fff3a342 | 788 | |
5b4657d0 | 789 | my $timeout = 10; |
fff3a342 DM |
790 | |
791 | my $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
5b4657d0 | 792 | '-timeout', $timeout, '-authpath', $authpath, |
fff3a342 DM |
793 | '-perm', 'VM.Console']; |
794 | ||
bd0f4d6d DC |
795 | if ($param->{width}) { |
796 | push @$cmd, '-width', $param->{width}; | |
797 | } | |
798 | ||
799 | if ($param->{height}) { | |
800 | push @$cmd, '-height', $param->{height}; | |
801 | } | |
802 | ||
fff3a342 | 803 | if ($param->{websocket}) { |
5b4657d0 | 804 | $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm |
fff3a342 DM |
805 | push @$cmd, '-notls', '-listen', 'localhost'; |
806 | } | |
807 | ||
808 | push @$cmd, '-c', @$remcmd, @$shcmd; | |
809 | ||
37634d3d | 810 | run_command($cmd, keeplocale => 1); |
fff3a342 DM |
811 | |
812 | return; | |
813 | }; | |
814 | ||
815 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
816 | ||
817 | PVE::Tools::wait_for_vnc_port($port); | |
818 | ||
819 | return { | |
820 | user => $authuser, | |
821 | ticket => $ticket, | |
5b4657d0 DM |
822 | port => $port, |
823 | upid => $upid, | |
824 | cert => $sslcert, | |
fff3a342 DM |
825 | }; |
826 | }}); | |
827 | ||
a0226a00 DC |
828 | __PACKAGE__->register_method ({ |
829 | name => 'termproxy', | |
830 | path => '{vmid}/termproxy', | |
831 | method => 'POST', | |
832 | protected => 1, | |
833 | permissions => { | |
834 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
835 | }, | |
836 | description => "Creates a TCP proxy connection.", | |
837 | parameters => { | |
838 | additionalProperties => 0, | |
839 | properties => { | |
840 | node => get_standard_option('pve-node'), | |
841 | vmid => get_standard_option('pve-vmid'), | |
842 | }, | |
843 | }, | |
844 | returns => { | |
845 | additionalProperties => 0, | |
846 | properties => { | |
847 | user => { type => 'string' }, | |
848 | ticket => { type => 'string' }, | |
849 | port => { type => 'integer' }, | |
850 | upid => { type => 'string' }, | |
851 | }, | |
852 | }, | |
853 | code => sub { | |
854 | my ($param) = @_; | |
855 | ||
856 | my $rpcenv = PVE::RPCEnvironment::get(); | |
857 | ||
858 | my $authuser = $rpcenv->get_user(); | |
859 | ||
860 | my $vmid = $param->{vmid}; | |
861 | my $node = $param->{node}; | |
862 | ||
863 | my $authpath = "/vms/$vmid"; | |
864 | ||
865 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
866 | ||
867 | my ($remip, $family); | |
868 | ||
869 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
870 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
871 | } else { | |
872 | $family = PVE::Tools::get_host_address_family($node); | |
873 | } | |
874 | ||
875 | my $port = PVE::Tools::next_vnc_port($family); | |
876 | ||
877 | my $remcmd = $remip ? | |
878 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : []; | |
879 | ||
880 | my $conf = PVE::LXC::Config->load_config($vmid, $node); | |
881 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, 1); | |
882 | ||
883 | my $shcmd = [ '/usr/bin/dtach', '-A', | |
884 | "/var/run/dtach/vzctlconsole$vmid", | |
885 | '-r', 'winch', '-z', @$concmd]; | |
886 | ||
887 | my $realcmd = sub { | |
888 | my $upid = shift; | |
889 | ||
890 | syslog ('info', "starting lxc termproxy $upid\n"); | |
891 | ||
892 | my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, | |
893 | '--perm', 'VM.Console', '--']; | |
894 | push @$cmd, @$remcmd, @$shcmd; | |
895 | ||
896 | PVE::Tools::run_command($cmd); | |
897 | }; | |
898 | ||
899 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); | |
900 | ||
901 | PVE::Tools::wait_for_vnc_port($port); | |
902 | ||
903 | return { | |
904 | user => $authuser, | |
905 | ticket => $ticket, | |
906 | port => $port, | |
907 | upid => $upid, | |
908 | }; | |
909 | }}); | |
910 | ||
fff3a342 DM |
911 | __PACKAGE__->register_method({ |
912 | name => 'vncwebsocket', | |
913 | path => '{vmid}/vncwebsocket', | |
914 | method => 'GET', | |
5b4657d0 | 915 | permissions => { |
fff3a342 DM |
916 | description => "You also need to pass a valid ticket (vncticket).", |
917 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
918 | }, | |
919 | description => "Opens a weksocket for VNC traffic.", | |
920 | parameters => { | |
921 | additionalProperties => 0, | |
922 | properties => { | |
923 | node => get_standard_option('pve-node'), | |
924 | vmid => get_standard_option('pve-vmid'), | |
925 | vncticket => { | |
926 | description => "Ticket from previous call to vncproxy.", | |
927 | type => 'string', | |
928 | maxLength => 512, | |
929 | }, | |
930 | port => { | |
931 | description => "Port number returned by previous vncproxy call.", | |
932 | type => 'integer', | |
933 | minimum => 5900, | |
934 | maximum => 5999, | |
935 | }, | |
936 | }, | |
937 | }, | |
938 | returns => { | |
939 | type => "object", | |
940 | properties => { | |
941 | port => { type => 'string' }, | |
942 | }, | |
943 | }, | |
944 | code => sub { | |
945 | my ($param) = @_; | |
946 | ||
947 | my $rpcenv = PVE::RPCEnvironment::get(); | |
948 | ||
949 | my $authuser = $rpcenv->get_user(); | |
950 | ||
951 | my $authpath = "/vms/$param->{vmid}"; | |
952 | ||
953 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
954 | ||
955 | my $port = $param->{port}; | |
5b4657d0 | 956 | |
fff3a342 DM |
957 | return { port => $port }; |
958 | }}); | |
959 | ||
960 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
961 | name => 'spiceproxy', |
962 | path => '{vmid}/spiceproxy', | |
fff3a342 DM |
963 | method => 'POST', |
964 | protected => 1, | |
965 | proxyto => 'node', | |
966 | permissions => { | |
967 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
968 | }, | |
969 | description => "Returns a SPICE configuration to connect to the CT.", | |
970 | parameters => { | |
971 | additionalProperties => 0, | |
972 | properties => { | |
973 | node => get_standard_option('pve-node'), | |
974 | vmid => get_standard_option('pve-vmid'), | |
975 | proxy => get_standard_option('spice-proxy', { optional => 1 }), | |
976 | }, | |
977 | }, | |
978 | returns => get_standard_option('remote-viewer-config'), | |
979 | code => sub { | |
980 | my ($param) = @_; | |
981 | ||
982 | my $vmid = $param->{vmid}; | |
983 | my $node = $param->{node}; | |
984 | my $proxy = $param->{proxy}; | |
985 | ||
986 | my $authpath = "/vms/$vmid"; | |
987 | my $permissions = 'VM.Console'; | |
988 | ||
67afe46e | 989 | my $conf = PVE::LXC::Config->load_config($vmid); |
da4db334 TL |
990 | |
991 | die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid); | |
992 | ||
aca816ad DM |
993 | my $concmd = PVE::LXC::get_console_command($vmid, $conf); |
994 | ||
5b4657d0 DM |
995 | my $shcmd = ['/usr/bin/dtach', '-A', |
996 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 997 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
998 | |
999 | my $title = "CT $vmid"; | |
1000 | ||
1001 | return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd); | |
1002 | }}); | |
5c752bbf | 1003 | |
5c752bbf DM |
1004 | |
1005 | __PACKAGE__->register_method({ | |
52389a07 DM |
1006 | name => 'migrate_vm', |
1007 | path => '{vmid}/migrate', | |
5c752bbf DM |
1008 | method => 'POST', |
1009 | protected => 1, | |
1010 | proxyto => 'node', | |
52389a07 | 1011 | description => "Migrate the container to another node. Creates a new migration task.", |
5c752bbf | 1012 | permissions => { |
52389a07 | 1013 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], |
5c752bbf DM |
1014 | }, |
1015 | parameters => { | |
1016 | additionalProperties => 0, | |
1017 | properties => { | |
1018 | node => get_standard_option('pve-node'), | |
68e8f3c5 DM |
1019 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
1020 | target => get_standard_option('pve-node', { | |
1021 | description => "Target node.", | |
39bb88df | 1022 | completion => \&PVE::Cluster::complete_migration_target, |
68e8f3c5 | 1023 | }), |
52389a07 DM |
1024 | online => { |
1025 | type => 'boolean', | |
1026 | description => "Use online/live migration.", | |
1027 | optional => 1, | |
1028 | }, | |
00d8cdc0 DC |
1029 | restart => { |
1030 | type => 'boolean', | |
1031 | description => "Use restart migration", | |
1032 | optional => 1, | |
1033 | }, | |
1034 | timeout => { | |
1035 | type => 'integer', | |
1036 | description => "Timeout in seconds for shutdown for restart migration", | |
1037 | optional => 1, | |
1038 | default => 180, | |
1039 | }, | |
9746c095 FG |
1040 | force => { |
1041 | type => 'boolean', | |
1042 | description => "Force migration despite local bind / device" . | |
552e168f | 1043 | " mounts. NOTE: deprecated, use 'shared' property of mount point instead.", |
9746c095 FG |
1044 | optional => 1, |
1045 | }, | |
5c752bbf DM |
1046 | }, |
1047 | }, | |
1048 | returns => { | |
1049 | type => 'string', | |
52389a07 | 1050 | description => "the task ID.", |
5c752bbf DM |
1051 | }, |
1052 | code => sub { | |
1053 | my ($param) = @_; | |
1054 | ||
1055 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1056 | ||
1057 | my $authuser = $rpcenv->get_user(); | |
1058 | ||
52389a07 | 1059 | my $target = extract_param($param, 'target'); |
bb1ac2de | 1060 | |
52389a07 DM |
1061 | my $localnode = PVE::INotify::nodename(); |
1062 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
bb1ac2de | 1063 | |
52389a07 | 1064 | PVE::Cluster::check_cfs_quorum(); |
5c752bbf | 1065 | |
52389a07 | 1066 | PVE::Cluster::check_node_exists($target); |
27916659 | 1067 | |
52389a07 | 1068 | my $targetip = PVE::Cluster::remote_node_ip($target); |
5c752bbf | 1069 | |
52389a07 | 1070 | my $vmid = extract_param($param, 'vmid'); |
5c752bbf | 1071 | |
52389a07 | 1072 | # test if VM exists |
67afe46e | 1073 | PVE::LXC::Config->load_config($vmid); |
5c752bbf | 1074 | |
52389a07 DM |
1075 | # try to detect errors early |
1076 | if (PVE::LXC::check_running($vmid)) { | |
00d8cdc0 DC |
1077 | die "can't migrate running container without --online or --restart\n" |
1078 | if !$param->{online} && !$param->{restart}; | |
5c752bbf | 1079 | } |
5c752bbf DM |
1080 | |
1081 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
1082 | ||
1083 | my $hacmd = sub { | |
1084 | my $upid = shift; | |
1085 | ||
1086 | my $service = "ct:$vmid"; | |
1087 | ||
52389a07 | 1088 | my $cmd = ['ha-manager', 'migrate', $service, $target]; |
5c752bbf | 1089 | |
545dd4e1 | 1090 | print "Requesting HA migration for CT $vmid to node $target\n"; |
5c752bbf DM |
1091 | |
1092 | PVE::Tools::run_command($cmd); | |
1093 | ||
1094 | return; | |
1095 | }; | |
1096 | ||
52389a07 | 1097 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); |
5c752bbf DM |
1098 | |
1099 | } else { | |
1100 | ||
22557519 DM |
1101 | my $realcmd = sub { |
1102 | PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param); | |
1103 | }; | |
56462053 | 1104 | |
22557519 DM |
1105 | my $worker = sub { |
1106 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
5c752bbf DM |
1107 | }; |
1108 | ||
22557519 | 1109 | return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker); |
5c752bbf DM |
1110 | } |
1111 | }}); | |
1112 | ||
cc5392c8 WL |
1113 | __PACKAGE__->register_method({ |
1114 | name => 'vm_feature', | |
1115 | path => '{vmid}/feature', | |
1116 | method => 'GET', | |
1117 | proxyto => 'node', | |
1118 | protected => 1, | |
1119 | description => "Check if feature for virtual machine is available.", | |
1120 | permissions => { | |
1121 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1122 | }, | |
1123 | parameters => { | |
1124 | additionalProperties => 0, | |
1125 | properties => { | |
1126 | node => get_standard_option('pve-node'), | |
1127 | vmid => get_standard_option('pve-vmid'), | |
1128 | feature => { | |
1129 | description => "Feature to check.", | |
1130 | type => 'string', | |
d53e5c58 | 1131 | enum => [ 'snapshot', 'clone', 'copy' ], |
cc5392c8 WL |
1132 | }, |
1133 | snapname => get_standard_option('pve-lxc-snapshot-name', { | |
1134 | optional => 1, | |
1135 | }), | |
1136 | }, | |
1137 | }, | |
1138 | returns => { | |
1139 | type => "object", | |
1140 | properties => { | |
1141 | hasFeature => { type => 'boolean' }, | |
1142 | #nodes => { | |
1143 | #type => 'array', | |
1144 | #items => { type => 'string' }, | |
1145 | #} | |
1146 | }, | |
1147 | }, | |
1148 | code => sub { | |
1149 | my ($param) = @_; | |
1150 | ||
1151 | my $node = extract_param($param, 'node'); | |
1152 | ||
1153 | my $vmid = extract_param($param, 'vmid'); | |
1154 | ||
1155 | my $snapname = extract_param($param, 'snapname'); | |
1156 | ||
1157 | my $feature = extract_param($param, 'feature'); | |
1158 | ||
67afe46e | 1159 | my $conf = PVE::LXC::Config->load_config($vmid); |
cc5392c8 WL |
1160 | |
1161 | if($snapname){ | |
1162 | my $snap = $conf->{snapshots}->{$snapname}; | |
1163 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
1164 | $conf = $snap; | |
1165 | } | |
ef241384 | 1166 | my $storage_cfg = PVE::Storage::config(); |
cc5392c8 | 1167 | #Maybe include later |
ef241384 | 1168 | #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg); |
4518000b | 1169 | my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname); |
cc5392c8 WL |
1170 | |
1171 | return { | |
1172 | hasFeature => $hasFeature, | |
1173 | #nodes => [ keys %$nodelist ], | |
1174 | }; | |
1175 | }}); | |
bb1ac2de DM |
1176 | |
1177 | __PACKAGE__->register_method({ | |
1178 | name => 'template', | |
1179 | path => '{vmid}/template', | |
1180 | method => 'POST', | |
1181 | protected => 1, | |
1182 | proxyto => 'node', | |
1183 | description => "Create a Template.", | |
1184 | permissions => { | |
1185 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
1186 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1187 | }, | |
1188 | parameters => { | |
1189 | additionalProperties => 0, | |
1190 | properties => { | |
1191 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 1192 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
bb1ac2de DM |
1193 | }, |
1194 | }, | |
1195 | returns => { type => 'null'}, | |
1196 | code => sub { | |
1197 | my ($param) = @_; | |
1198 | ||
1199 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1200 | ||
1201 | my $authuser = $rpcenv->get_user(); | |
1202 | ||
1203 | my $node = extract_param($param, 'node'); | |
1204 | ||
1205 | my $vmid = extract_param($param, 'vmid'); | |
1206 | ||
1207 | my $updatefn = sub { | |
1208 | ||
67afe46e FG |
1209 | my $conf = PVE::LXC::Config->load_config($vmid); |
1210 | PVE::LXC::Config->check_lock($conf); | |
bb1ac2de DM |
1211 | |
1212 | die "unable to create template, because CT contains snapshots\n" | |
1213 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
1214 | ||
1215 | die "you can't convert a template to a template\n" | |
67afe46e | 1216 | if PVE::LXC::Config->is_template($conf); |
bb1ac2de DM |
1217 | |
1218 | die "you can't convert a CT to template if the CT is running\n" | |
1219 | if PVE::LXC::check_running($vmid); | |
1220 | ||
e1313b73 WL |
1221 | my $scfg = PVE::Storage::config(); |
1222 | PVE::LXC::Config->foreach_mountpoint($conf, sub { | |
1223 | my ($ms, $mp) = @_; | |
1224 | ||
1225 | my ($sid) =PVE::Storage::parse_volume_id($mp->{volume}, 0); | |
812a2bbc | 1226 | die "Directory storage '$sid' does not support container templates!\n" |
e1313b73 WL |
1227 | if $scfg->{ids}->{$sid}->{path}; |
1228 | }); | |
1229 | ||
bb1ac2de DM |
1230 | my $realcmd = sub { |
1231 | PVE::LXC::template_create($vmid, $conf); | |
bb1ac2de | 1232 | |
c2182c49 | 1233 | $conf->{template} = 1; |
bb1ac2de | 1234 | |
c2182c49 WL |
1235 | PVE::LXC::Config->write_config($vmid, $conf); |
1236 | # and remove lxc config | |
1237 | PVE::LXC::update_lxc_config($vmid, $conf); | |
1238 | }; | |
bb1ac2de DM |
1239 | |
1240 | return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd); | |
1241 | }; | |
1242 | ||
67afe46e | 1243 | PVE::LXC::Config->lock_config($vmid, $updatefn); |
bb1ac2de DM |
1244 | |
1245 | return undef; | |
1246 | }}); | |
1247 | ||
c4a33727 DM |
1248 | __PACKAGE__->register_method({ |
1249 | name => 'clone_vm', | |
1250 | path => '{vmid}/clone', | |
1251 | method => 'POST', | |
1252 | protected => 1, | |
1253 | proxyto => 'node', | |
1254 | description => "Create a container clone/copy", | |
1255 | permissions => { | |
1256 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, " . | |
1257 | "and 'VM.Allocate' permissions " . | |
1258 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
1259 | "'Datastore.AllocateSpace' on any used storage.", | |
1260 | check => | |
1261 | [ 'and', | |
1262 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
1263 | [ 'or', | |
1264 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
1265 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
1266 | ], | |
1267 | ] | |
1268 | }, | |
1269 | parameters => { | |
1270 | additionalProperties => 0, | |
1271 | properties => { | |
1272 | node => get_standard_option('pve-node'), | |
1273 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1274 | newid => get_standard_option('pve-vmid', { | |
1275 | completion => \&PVE::Cluster::complete_next_vmid, | |
1276 | description => 'VMID for the clone.' }), | |
1277 | hostname => { | |
1278 | optional => 1, | |
1279 | type => 'string', format => 'dns-name', | |
1280 | description => "Set a hostname for the new CT.", | |
1281 | }, | |
1282 | description => { | |
1283 | optional => 1, | |
1284 | type => 'string', | |
1285 | description => "Description for the new CT.", | |
1286 | }, | |
1287 | pool => { | |
1288 | optional => 1, | |
1289 | type => 'string', format => 'pve-poolid', | |
1290 | description => "Add the new CT to the specified pool.", | |
1291 | }, | |
1292 | snapname => get_standard_option('pve-lxc-snapshot-name', { | |
1293 | optional => 1, | |
1294 | }), | |
1295 | storage => get_standard_option('pve-storage-id', { | |
1296 | description => "Target storage for full clone.", | |
c4a33727 DM |
1297 | optional => 1, |
1298 | }), | |
1299 | full => { | |
1300 | optional => 1, | |
1301 | type => 'boolean', | |
c4b4cb83 | 1302 | description => "Create a full copy of all disks. This is always done when " . |
c4a33727 | 1303 | "you clone a normal CT. For CT templates, we try to create a linked clone by default.", |
c4a33727 | 1304 | }, |
411ae12c DM |
1305 | target => get_standard_option('pve-node', { |
1306 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
1307 | optional => 1, | |
1308 | }), | |
c4a33727 DM |
1309 | }, |
1310 | }, | |
1311 | returns => { | |
1312 | type => 'string', | |
1313 | }, | |
1314 | code => sub { | |
1315 | my ($param) = @_; | |
1316 | ||
1317 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1318 | ||
1319 | my $authuser = $rpcenv->get_user(); | |
1320 | ||
1321 | my $node = extract_param($param, 'node'); | |
1322 | ||
1323 | my $vmid = extract_param($param, 'vmid'); | |
1324 | ||
1325 | my $newid = extract_param($param, 'newid'); | |
1326 | ||
1327 | my $pool = extract_param($param, 'pool'); | |
1328 | ||
1329 | if (defined($pool)) { | |
1330 | $rpcenv->check_pool_exist($pool); | |
1331 | } | |
1332 | ||
1333 | my $snapname = extract_param($param, 'snapname'); | |
1334 | ||
1335 | my $storage = extract_param($param, 'storage'); | |
1336 | ||
411ae12c DM |
1337 | my $target = extract_param($param, 'target'); |
1338 | ||
c4a33727 DM |
1339 | my $localnode = PVE::INotify::nodename(); |
1340 | ||
411ae12c DM |
1341 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
1342 | ||
1343 | PVE::Cluster::check_node_exists($target) if $target; | |
1344 | ||
c4a33727 DM |
1345 | my $storecfg = PVE::Storage::config(); |
1346 | ||
1347 | if ($storage) { | |
1348 | # check if storage is enabled on local node | |
1349 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
411ae12c DM |
1350 | if ($target) { |
1351 | # check if storage is available on target node | |
1352 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
1353 | # clone only works if target storage is shared | |
1354 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1355 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
1356 | } | |
c4a33727 DM |
1357 | } |
1358 | ||
1359 | PVE::Cluster::check_cfs_quorum(); | |
1360 | ||
db01d674 WB |
1361 | my $conffile; |
1362 | my $newconf = {}; | |
1363 | my $mountpoints = {}; | |
1364 | my $fullclone = {}; | |
1365 | my $vollist = []; | |
411ae12c | 1366 | my $running; |
c4a33727 | 1367 | |
db01d674 WB |
1368 | PVE::LXC::Config->lock_config($vmid, sub { |
1369 | my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk'); | |
e7d553c7 | 1370 | |
411ae12c DM |
1371 | $running = PVE::LXC::check_running($vmid) || 0; |
1372 | ||
e7d553c7 DM |
1373 | my $full = extract_param($param, 'full'); |
1374 | if (!defined($full)) { | |
1375 | $full = !PVE::LXC::Config->is_template($src_conf); | |
1376 | } | |
1377 | die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full; | |
1378 | ||
db01d674 WB |
1379 | eval { |
1380 | die "snapshot '$snapname' does not exist\n" | |
1381 | if $snapname && !defined($src_conf->{snapshots}->{$snapname}); | |
c4a33727 | 1382 | |
c4a33727 | 1383 | |
db01d674 | 1384 | my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf; |
c4a33727 | 1385 | |
db01d674 WB |
1386 | $conffile = PVE::LXC::Config->config_file($newid); |
1387 | die "unable to create CT $newid: config file already exists\n" | |
1388 | if -f $conffile; | |
c4a33727 | 1389 | |
24f9d440 | 1390 | my $sharedvm = 1; |
db01d674 WB |
1391 | foreach my $opt (keys %$src_conf) { |
1392 | next if $opt =~ m/^unused\d+$/; | |
c4a33727 | 1393 | |
db01d674 | 1394 | my $value = $src_conf->{$opt}; |
c4a33727 | 1395 | |
db01d674 WB |
1396 | if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) { |
1397 | my $mp = $opt eq 'rootfs' ? | |
1398 | PVE::LXC::Config->parse_ct_rootfs($value) : | |
1399 | PVE::LXC::Config->parse_ct_mountpoint($value); | |
c4a33727 | 1400 | |
db01d674 WB |
1401 | if ($mp->{type} eq 'volume') { |
1402 | my $volid = $mp->{volume}; | |
09687674 DM |
1403 | |
1404 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1405 | $sid = $storage if defined($storage); | |
1406 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
24f9d440 WB |
1407 | if (!$scfg->{shared}) { |
1408 | $sharedvm = 0; | |
1409 | warn "found non-shared volume: $volid\n" if $target; | |
1410 | } | |
09687674 DM |
1411 | |
1412 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
1413 | ||
e7d553c7 | 1414 | if ($full) { |
db01d674 WB |
1415 | die "Cannot do full clones on a running container without snapshots\n" |
1416 | if $running && !defined($snapname); | |
1417 | $fullclone->{$opt} = 1; | |
1418 | } else { | |
1419 | # not full means clone instead of copy | |
1420 | die "Linked clone feature for '$volid' is not available\n" | |
1421 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running); | |
1422 | } | |
c4a33727 | 1423 | |
db01d674 WB |
1424 | $mountpoints->{$opt} = $mp; |
1425 | push @$vollist, $volid; | |
c4a33727 | 1426 | |
c4a33727 | 1427 | } else { |
db01d674 WB |
1428 | # TODO: allow bind mounts? |
1429 | die "unable to clone mountpint '$opt' (type $mp->{type})\n"; | |
c4a33727 | 1430 | } |
63231f52 TL |
1431 | } elsif ($opt =~ m/^net(\d+)$/) { |
1432 | # always change MAC! address | |
1433 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1434 | my $net = PVE::LXC::Config->parse_lxc_network($value); | |
1435 | $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
1436 | $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net); | |
c4a33727 | 1437 | } else { |
db01d674 WB |
1438 | # copy everything else |
1439 | $newconf->{$opt} = $value; | |
c4a33727 | 1440 | } |
db01d674 | 1441 | } |
24f9d440 WB |
1442 | die "can't clone CT to node '$target' (CT uses local storage)\n" |
1443 | if $target && !$sharedvm; | |
c4a33727 | 1444 | |
db01d674 WB |
1445 | # Replace the 'disk' lock with a 'create' lock. |
1446 | $newconf->{lock} = 'create'; | |
1447 | ||
1448 | delete $newconf->{template}; | |
1449 | if ($param->{hostname}) { | |
1450 | $newconf->{hostname} = $param->{hostname}; | |
c4a33727 | 1451 | } |
c4a33727 | 1452 | |
db01d674 WB |
1453 | if ($param->{description}) { |
1454 | $newconf->{description} = $param->{description}; | |
1455 | } | |
c4a33727 | 1456 | |
db01d674 WB |
1457 | # create empty/temp config - this fails if CT already exists on other node |
1458 | PVE::LXC::Config->write_config($newid, $newconf); | |
1459 | }; | |
1460 | if (my $err = $@) { | |
1461 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1462 | warn $@ if $@; | |
1463 | die $err; | |
c4a33727 | 1464 | } |
db01d674 | 1465 | }); |
c4a33727 | 1466 | |
db01d674 WB |
1467 | my $update_conf = sub { |
1468 | my ($key, $value) = @_; | |
1469 | return PVE::LXC::Config->lock_config($newid, sub { | |
1470 | my $conf = PVE::LXC::Config->load_config($newid); | |
1471 | die "Lost 'create' config lock, aborting.\n" | |
1472 | if !PVE::LXC::Config->has_lock($conf, 'create'); | |
1473 | $conf->{$key} = $value; | |
1474 | PVE::LXC::Config->write_config($newid, $conf); | |
1475 | }); | |
1476 | }; | |
c4a33727 | 1477 | |
db01d674 WB |
1478 | my $realcmd = sub { |
1479 | my ($upid) = @_; | |
c4a33727 | 1480 | |
db01d674 | 1481 | my $newvollist = []; |
c4a33727 | 1482 | |
411ae12c DM |
1483 | my $verify_running = PVE::LXC::check_running($vmid) || 0; |
1484 | die "unexpected state change\n" if $verify_running != $running; | |
1485 | ||
db01d674 WB |
1486 | eval { |
1487 | local $SIG{INT} = | |
1488 | local $SIG{TERM} = | |
1489 | local $SIG{QUIT} = | |
1490 | local $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
c4a33727 | 1491 | |
db01d674 | 1492 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); |
c4a33727 | 1493 | |
db01d674 WB |
1494 | foreach my $opt (keys %$mountpoints) { |
1495 | my $mp = $mountpoints->{$opt}; | |
1496 | my $volid = $mp->{volume}; | |
c4a33727 | 1497 | |
db01d674 WB |
1498 | my $newvolid; |
1499 | if ($fullclone->{$opt}) { | |
1500 | print "create full clone of mountpoint $opt ($volid)\n"; | |
d7b775db DM |
1501 | my $target_storage = $storage // PVE::Storage::parse_volume_id($volid); |
1502 | $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname); | |
db01d674 WB |
1503 | } else { |
1504 | print "create linked clone of mount point $opt ($volid)\n"; | |
1505 | $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname); | |
c4a33727 DM |
1506 | } |
1507 | ||
db01d674 WB |
1508 | push @$newvollist, $newvolid; |
1509 | $mp->{volume} = $newvolid; | |
c4a33727 | 1510 | |
db01d674 | 1511 | $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs')); |
c4a33727 DM |
1512 | } |
1513 | ||
db01d674 WB |
1514 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
1515 | PVE::LXC::Config->remove_lock($newid, 'create'); | |
411ae12c DM |
1516 | |
1517 | if ($target) { | |
1518 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
1519 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; | |
1520 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); | |
1521 | ||
1522 | my $newconffile = PVE::LXC::Config->config_file($newid, $target); | |
1523 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
1524 | if !rename($conffile, $newconffile); | |
1525 | } | |
c4a33727 | 1526 | }; |
db01d674 | 1527 | my $err = $@; |
c4a33727 | 1528 | |
db01d674 WB |
1529 | # Unlock the source config in any case: |
1530 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1531 | warn $@ if $@; | |
c4a33727 | 1532 | |
db01d674 WB |
1533 | if ($err) { |
1534 | # Now cleanup the config & disks: | |
1535 | unlink $conffile; | |
c4a33727 | 1536 | |
db01d674 WB |
1537 | sleep 1; # some storages like rbd need to wait before release volume - really? |
1538 | ||
1539 | foreach my $volid (@$newvollist) { | |
1540 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
1541 | warn $@ if $@; | |
1542 | } | |
1543 | die "clone failed: $err"; | |
1544 | } | |
1545 | ||
1546 | return; | |
c4a33727 DM |
1547 | }; |
1548 | ||
db01d674 WB |
1549 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); |
1550 | return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd); | |
c4a33727 DM |
1551 | }}); |
1552 | ||
1553 | ||
985b18ed WL |
1554 | __PACKAGE__->register_method({ |
1555 | name => 'resize_vm', | |
1556 | path => '{vmid}/resize', | |
1557 | method => 'PUT', | |
1558 | protected => 1, | |
1559 | proxyto => 'node', | |
235dbdf3 | 1560 | description => "Resize a container mount point.", |
985b18ed WL |
1561 | permissions => { |
1562 | check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1], | |
1563 | }, | |
1564 | parameters => { | |
1565 | additionalProperties => 0, | |
b8c5a95f WB |
1566 | properties => { |
1567 | node => get_standard_option('pve-node'), | |
1568 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1569 | disk => { | |
1570 | type => 'string', | |
1571 | description => "The disk you want to resize.", | |
d250604f | 1572 | enum => [PVE::LXC::Config->mountpoint_names()], |
b8c5a95f WB |
1573 | }, |
1574 | size => { | |
1575 | type => 'string', | |
1576 | pattern => '\+?\d+(\.\d+)?[KMGT]?', | |
1577 | 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.", | |
1578 | }, | |
1579 | digest => { | |
1580 | type => 'string', | |
1581 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1582 | maxLength => 40, | |
1583 | optional => 1, | |
1584 | } | |
1585 | }, | |
985b18ed | 1586 | }, |
9f3f7963 WL |
1587 | returns => { |
1588 | type => 'string', | |
1589 | description => "the task ID.", | |
1590 | }, | |
985b18ed WL |
1591 | code => sub { |
1592 | my ($param) = @_; | |
1593 | ||
1594 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1595 | ||
1596 | my $authuser = $rpcenv->get_user(); | |
1597 | ||
1598 | my $node = extract_param($param, 'node'); | |
1599 | ||
1600 | my $vmid = extract_param($param, 'vmid'); | |
1601 | ||
1602 | my $digest = extract_param($param, 'digest'); | |
1603 | ||
1604 | my $sizestr = extract_param($param, 'size'); | |
1605 | my $ext = ($sizestr =~ s/^\+//); | |
1606 | my $newsize = PVE::JSONSchema::parse_size($sizestr); | |
1607 | die "invalid size string" if !defined($newsize); | |
1608 | ||
1609 | die "no options specified\n" if !scalar(keys %$param); | |
1610 | ||
f1ba1a4b | 1611 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []); |
985b18ed WL |
1612 | |
1613 | my $storage_cfg = cfs_read_file("storage.cfg"); | |
1614 | ||
985b18ed WL |
1615 | my $code = sub { |
1616 | ||
67afe46e FG |
1617 | my $conf = PVE::LXC::Config->load_config($vmid); |
1618 | PVE::LXC::Config->check_lock($conf); | |
985b18ed WL |
1619 | |
1620 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1621 | ||
1622 | my $running = PVE::LXC::check_running($vmid); | |
1623 | ||
1624 | my $disk = $param->{disk}; | |
1b4cf758 FG |
1625 | my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) : |
1626 | PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk}); | |
44a9face | 1627 | |
985b18ed WL |
1628 | my $volid = $mp->{volume}; |
1629 | ||
1630 | my (undef, undef, $owner, undef, undef, undef, $format) = | |
1631 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
1632 | ||
235dbdf3 | 1633 | die "can't resize mount point owned by another container ($owner)" |
985b18ed WL |
1634 | if $vmid != $owner; |
1635 | ||
1636 | die "can't resize volume: $disk if snapshot exists\n" | |
1637 | if %{$conf->{snapshots}} && $format eq 'qcow2'; | |
1638 | ||
1639 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1640 | ||
1641 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
1642 | ||
4e1e1791 WB |
1643 | PVE::Storage::activate_volumes($storage_cfg, [$volid]); |
1644 | ||
985b18ed WL |
1645 | my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5); |
1646 | $newsize += $size if $ext; | |
1647 | $newsize = int($newsize); | |
1648 | ||
1649 | die "unable to shrink disk size\n" if $newsize < $size; | |
1650 | ||
1651 | return if $size == $newsize; | |
1652 | ||
1653 | PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr"); | |
9f3f7963 | 1654 | my $realcmd = sub { |
e72c8b0e DM |
1655 | # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so |
1656 | # we pass 0 here (parameter only makes sense for qemu) | |
9f3f7963 WL |
1657 | PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0); |
1658 | ||
1659 | $mp->{size} = $newsize; | |
1b4cf758 | 1660 | $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs'); |
9f3f7963 | 1661 | |
67afe46e | 1662 | PVE::LXC::Config->write_config($vmid, $conf); |
9f3f7963 WL |
1663 | |
1664 | if ($format eq 'raw') { | |
1665 | my $path = PVE::Storage::path($storage_cfg, $volid, undef); | |
1666 | if ($running) { | |
2a993004 WL |
1667 | |
1668 | $mp->{mp} = '/'; | |
1669 | my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1]; | |
21f292ff | 1670 | $path = PVE::LXC::query_loopdev($path) if $use_loopdev; |
235dbdf3 | 1671 | die "internal error: CT running but mount point not attached to a loop device" |
2a993004 WL |
1672 | if !$path; |
1673 | PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev; | |
9f3f7963 WL |
1674 | |
1675 | # In order for resize2fs to know that we need online-resizing a mountpoint needs | |
1676 | # to be visible to it in its namespace. | |
1677 | # To not interfere with the rest of the system we unshare the current mount namespace, | |
1678 | # mount over /tmp and then run resize2fs. | |
1679 | ||
1680 | # interestingly we don't need to e2fsck on mounted systems... | |
1681 | my $quoted = PVE::Tools::shellquote($path); | |
1682 | my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted"; | |
ce9e6458 WB |
1683 | eval { |
1684 | PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]); | |
1685 | }; | |
1686 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
9f3f7963 | 1687 | } else { |
ce9e6458 WB |
1688 | eval { |
1689 | PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]); | |
1690 | PVE::Tools::run_command(['resize2fs', $path]); | |
1691 | }; | |
1692 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
9f3f7963 | 1693 | } |
985b18ed | 1694 | } |
9f3f7963 | 1695 | }; |
985b18ed | 1696 | |
9f3f7963 WL |
1697 | return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd); |
1698 | }; | |
985b18ed | 1699 | |
67afe46e | 1700 | return PVE::LXC::Config->lock_config($vmid, $code);; |
985b18ed WL |
1701 | }}); |
1702 | ||
3c0f6806 WB |
1703 | __PACKAGE__->register_method({ |
1704 | name => 'move_volume', | |
1705 | path => '{vmid}/move_volume', | |
1706 | method => 'POST', | |
1707 | protected => 1, | |
1708 | proxyto => 'node', | |
1709 | description => "Move a rootfs-/mp-volume to a different storage", | |
1710 | permissions => { | |
1711 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . | |
1712 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
1713 | check => | |
1714 | [ 'and', | |
1715 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
1716 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
1717 | ], | |
1718 | }, | |
1719 | parameters => { | |
1720 | additionalProperties => 0, | |
1721 | properties => { | |
1722 | node => get_standard_option('pve-node'), | |
1723 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1724 | volume => { | |
1725 | type => 'string', | |
1726 | enum => [ PVE::LXC::Config->mountpoint_names() ], | |
1727 | description => "Volume which will be moved.", | |
1728 | }, | |
1729 | storage => get_standard_option('pve-storage-id', { | |
1730 | description => "Target Storage.", | |
1731 | completion => \&PVE::Storage::complete_storage_enabled, | |
1732 | }), | |
1733 | delete => { | |
1734 | type => 'boolean', | |
1735 | description => "Delete the original volume after successful copy. By default the original is kept as an unused volume entry.", | |
1736 | optional => 1, | |
1737 | default => 0, | |
1738 | }, | |
1739 | digest => { | |
1740 | type => 'string', | |
1741 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1742 | maxLength => 40, | |
1743 | optional => 1, | |
1744 | } | |
1745 | }, | |
1746 | }, | |
1747 | returns => { | |
1748 | type => 'string', | |
1749 | }, | |
1750 | code => sub { | |
1751 | my ($param) = @_; | |
1752 | ||
1753 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1754 | ||
1755 | my $authuser = $rpcenv->get_user(); | |
1756 | ||
1757 | my $vmid = extract_param($param, 'vmid'); | |
1758 | ||
1759 | my $storage = extract_param($param, 'storage'); | |
1760 | ||
1761 | my $mpkey = extract_param($param, 'volume'); | |
1762 | ||
1763 | my $lockname = 'disk'; | |
1764 | ||
1765 | my ($mpdata, $old_volid); | |
1766 | ||
1767 | PVE::LXC::Config->lock_config($vmid, sub { | |
1768 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1769 | PVE::LXC::Config->check_lock($conf); | |
1770 | ||
1771 | die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid); | |
1772 | ||
1773 | if ($mpkey eq 'rootfs') { | |
1774 | $mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey}); | |
1775 | } elsif ($mpkey =~ m/mp\d+/) { | |
1776 | $mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey}); | |
1777 | } else { | |
1778 | die "Can't parse $mpkey\n"; | |
1779 | } | |
1780 | $old_volid = $mpdata->{volume}; | |
1781 | ||
1782 | die "you can't move a volume with snapshots and delete the source\n" | |
1783 | if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid); | |
1784 | ||
1785 | PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest}); | |
1786 | ||
1787 | PVE::LXC::Config->set_lock($vmid, $lockname); | |
1788 | }); | |
1789 | ||
1790 | my $realcmd = sub { | |
1791 | eval { | |
1792 | PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage"); | |
1793 | ||
1794 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1795 | my $storage_cfg = PVE::Storage::config(); | |
1796 | ||
1797 | my $new_volid; | |
1798 | ||
1799 | eval { | |
1800 | PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]); | |
1801 | $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf); | |
1802 | $mpdata->{volume} = $new_volid; | |
1803 | ||
1804 | PVE::LXC::Config->lock_config($vmid, sub { | |
1805 | my $digest = $conf->{digest}; | |
1806 | $conf = PVE::LXC::Config->load_config($vmid); | |
1807 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1808 | ||
1809 | $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs'); | |
1810 | ||
1811 | PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete}; | |
1812 | ||
1813 | PVE::LXC::Config->write_config($vmid, $conf); | |
1814 | }); | |
1815 | ||
1816 | eval { | |
1817 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
1818 | PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ]) | |
1819 | }; | |
1820 | warn $@ if $@; | |
1821 | }; | |
1822 | if (my $err = $@) { | |
1823 | eval { | |
1824 | PVE::Storage::vdisk_free($storage_cfg, $new_volid) | |
1825 | if defined($new_volid); | |
1826 | }; | |
1827 | warn $@ if $@; | |
1828 | die $err; | |
1829 | } | |
1830 | ||
1831 | if ($param->{delete}) { | |
1832 | eval { | |
1833 | PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]); | |
1834 | PVE::Storage::vdisk_free($storage_cfg, $old_volid); | |
1835 | }; | |
1836 | warn $@ if $@; | |
1837 | } | |
1838 | }; | |
1839 | my $err = $@; | |
1840 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1841 | warn $@ if $@; | |
1842 | die $err if $err; | |
1843 | }; | |
1844 | my $task = eval { | |
1845 | $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd); | |
1846 | }; | |
1847 | if (my $err = $@) { | |
1848 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1849 | warn $@ if $@; | |
1850 | die $err; | |
1851 | } | |
1852 | return $task; | |
1853 | }}); | |
1854 | ||
f76a2828 | 1855 | 1; |