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