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