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