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