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