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