]>
Commit | Line | Data |
---|---|---|
58a3c91c FG |
1 | package PVE::AbstractConfig; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Tools qw(lock_file lock_file_full); | |
7 | use PVE::INotify; | |
8 | use PVE::Cluster; | |
9 | use PVE::Storage; | |
10 | ||
67b3581e | 11 | use PVE::GuestHelpers qw(typesafe_ne); |
14f17b49 DM |
12 | use PVE::ReplicationConfig; |
13 | use PVE::Replication; | |
14 | ||
58a3c91c FG |
15 | my $nodename = PVE::INotify::nodename(); |
16 | ||
17 | # Printable string, currently either "VM" or "CT" | |
18 | sub guest_type { | |
19 | my ($class) = @_; | |
20 | die "abstract method - implement me "; | |
21 | } | |
22 | ||
23 | sub __config_max_unused_disks { | |
24 | my ($class) = @_; | |
25 | ||
26 | die "implement me - abstract method\n"; | |
27 | } | |
28 | ||
29 | # Path to the flock file for this VM/CT | |
30 | sub config_file_lock { | |
31 | my ($class, $vmid) = @_; | |
32 | die "abstract method - implement me"; | |
33 | } | |
34 | ||
35 | # Relative config file path for this VM/CT in CFS | |
36 | sub cfs_config_path { | |
37 | my ($class, $vmid, $node) = @_; | |
38 | die "abstract method - implement me"; | |
39 | } | |
40 | ||
41 | # Absolute config file path for this VM/CT | |
42 | sub config_file { | |
43 | my ($class, $vmid, $node) = @_; | |
44 | ||
45 | my $cfspath = $class->cfs_config_path($vmid, $node); | |
46 | return "/etc/pve/$cfspath"; | |
47 | } | |
48 | ||
49 | # Read and parse config file for this VM/CT | |
50 | sub load_config { | |
51 | my ($class, $vmid, $node) = @_; | |
52 | ||
53 | $node = $nodename if !$node; | |
54 | my $cfspath = $class->cfs_config_path($vmid, $node); | |
55 | ||
56 | my $conf = PVE::Cluster::cfs_read_file($cfspath); | |
57 | die "Configuration file '$cfspath' does not exist\n" | |
58 | if !defined($conf); | |
59 | ||
60 | return $conf; | |
61 | } | |
62 | ||
63 | # Generate and write config file for this VM/CT | |
64 | sub write_config { | |
65 | my ($class, $vmid, $conf) = @_; | |
66 | ||
67 | my $cfspath = $class->cfs_config_path($vmid); | |
68 | ||
69 | PVE::Cluster::cfs_write_file($cfspath, $conf); | |
70 | } | |
71 | ||
810ee088 OB |
72 | # Pending changes related |
73 | ||
74 | sub parse_pending_delete { | |
75 | my ($class, $data) = @_; | |
7b9d1ade TL |
76 | |
77 | return {} if !$data; | |
78 | ||
810ee088 OB |
79 | $data =~ s/[,;]/ /g; |
80 | $data =~ s/^\s+//; | |
7b9d1ade TL |
81 | |
82 | my $pending_deletions = {}; | |
83 | for my $entry (split(/\s+/, $data)) { | |
84 | my ($force, $key) = $entry =~ /^(!?)(.*)$/; | |
7b9d1ade TL |
85 | $pending_deletions->{$key} = { |
86 | force => $force ? 1 : 0, | |
87 | }; | |
88 | } | |
89 | ||
90 | return $pending_deletions; | |
810ee088 OB |
91 | } |
92 | ||
93 | sub print_pending_delete { | |
94 | my ($class, $delete_hash) = @_; | |
51088c75 TL |
95 | |
96 | my $render_key = sub { | |
97 | my $key = shift; | |
98 | $key = "!$key" if $delete_hash->{$key}->{force}; | |
99 | return $key; | |
100 | }; | |
101 | ||
6a6ad855 | 102 | join (',', map { $render_key->($_) } sort keys %$delete_hash); |
810ee088 OB |
103 | } |
104 | ||
105 | sub add_to_pending_delete { | |
106 | my ($class, $conf, $key, $force) = @_; | |
107 | ||
51e827b8 TL |
108 | $conf->{pending} //= {}; |
109 | my $pending = $conf->{pending}; | |
110 | my $pending_delete_hash = $class->parse_pending_delete($pending->{delete}); | |
111 | ||
112 | $pending_delete_hash->{$key} = { force => $force }; | |
113 | ||
114 | $pending->{delete} = $class->print_pending_delete($pending_delete_hash); | |
115 | ||
116 | return $conf; | |
810ee088 OB |
117 | } |
118 | ||
119 | sub remove_from_pending_delete { | |
120 | my ($class, $conf, $key) = @_; | |
121 | ||
464a42c4 TL |
122 | my $pending = $conf->{pending}; |
123 | my $pending_delete_hash = $class->parse_pending_delete($pending->{delete}); | |
124 | ||
125 | return $conf if ! exists $pending_delete_hash->{$key}; | |
126 | ||
810ee088 OB |
127 | delete $pending_delete_hash->{$key}; |
128 | ||
129 | if (%$pending_delete_hash) { | |
464a42c4 | 130 | $pending->{delete} = $class->print_pending_delete($pending_delete_hash); |
810ee088 | 131 | } else { |
464a42c4 | 132 | delete $pending->{delete}; |
810ee088 | 133 | } |
464a42c4 TL |
134 | |
135 | return $conf; | |
810ee088 OB |
136 | } |
137 | ||
138 | sub cleanup_pending { | |
139 | my ($class, $conf) = @_; | |
140 | ||
c24919a9 | 141 | my $pending = $conf->{pending}; |
810ee088 OB |
142 | # remove pending changes when nothing changed |
143 | my $changes; | |
144 | foreach my $opt (keys %{$conf->{pending}}) { | |
145 | next if $opt eq 'delete'; # just to be sure | |
9420142c | 146 | if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) { |
810ee088 | 147 | $changes = 1; |
c24919a9 | 148 | delete $pending->{$opt}; |
810ee088 OB |
149 | } |
150 | } | |
151 | ||
c24919a9 | 152 | my $current_delete_hash = $class->parse_pending_delete($pending->{delete}); |
810ee088 | 153 | my $pending_delete_hash = {}; |
c24919a9 | 154 | for my $opt (keys %$current_delete_hash) { |
810ee088 | 155 | if (defined($conf->{$opt})) { |
c24919a9 | 156 | $pending_delete_hash->{$opt} = $current_delete_hash->{$opt}; |
810ee088 OB |
157 | } else { |
158 | $changes = 1; | |
159 | } | |
160 | } | |
161 | ||
162 | if (%$pending_delete_hash) { | |
c24919a9 | 163 | $pending->{delete} = $class->print_pending_delete($pending_delete_hash); |
810ee088 | 164 | } else { |
c24919a9 | 165 | delete $pending->{delete}; |
810ee088 OB |
166 | } |
167 | ||
168 | return $changes; | |
169 | } | |
170 | ||
866f5834 OB |
171 | sub get_partial_fast_plug_option { |
172 | my ($class) = @_; | |
173 | ||
174 | die "abstract method - implement me "; | |
175 | } | |
176 | ||
177 | sub partial_fast_plug { | |
178 | my ($class, $conf, $opt) = @_; | |
179 | ||
180 | my $partial_fast_plug_option = $class->get_partial_fast_plug_option(); | |
47d7954c TL |
181 | return 0 if !$partial_fast_plug_option->{$opt}; |
182 | ||
866f5834 OB |
183 | my $format = $partial_fast_plug_option->{$opt}->{fmt}; |
184 | my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties}; | |
185 | ||
186 | my $configured = {}; | |
187 | if (exists($conf->{$opt})) { | |
188 | $configured = PVE::JSONSchema::parse_property_string($format, $conf->{$opt}); | |
189 | } | |
190 | my $pending = PVE::JSONSchema::parse_property_string($format, $conf->{pending}->{$opt}); | |
191 | ||
192 | my $changes = 0; | |
193 | ||
194 | # merge configured and pending opts to iterate | |
195 | my @all_keys = keys %{{ %$pending, %$configured }}; | |
196 | ||
197 | foreach my $subopt (@all_keys) { | |
198 | my $type = $format->{$subopt}->{type}; | |
67b3581e | 199 | if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) { |
866f5834 OB |
200 | if ($fast_pluggable->{$subopt}) { |
201 | $configured->{$subopt} = $pending->{$subopt}; | |
202 | $changes = 1 | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | # if there're no keys in $configured (after merge) there shouldn't be anything to change | |
208 | if (keys %$configured) { | |
209 | $conf->{$opt} = PVE::JSONSchema::print_property_string($configured, $format); | |
210 | } | |
211 | ||
212 | return $changes; | |
213 | } | |
214 | ||
215 | ||
61264e82 OB |
216 | sub load_snapshot_config { |
217 | my ($class, $vmid, $snapname) = @_; | |
218 | ||
219 | my $conf = $class->load_config($vmid); | |
220 | ||
221 | my $snapshot = $conf->{snapshots}->{$snapname}; | |
222 | die "snapshot '$snapname' does not exist\n" if !defined($snapshot); | |
223 | ||
224 | $snapshot->{digest} = $conf->{digest}; | |
225 | ||
226 | return $snapshot; | |
227 | ||
228 | } | |
229 | ||
230 | sub load_current_config { | |
231 | my ($class, $vmid, $current) = @_; | |
232 | ||
233 | my $conf = $class->load_config($vmid); | |
234 | ||
235 | # take pending changes in | |
236 | if (!$current) { | |
237 | foreach my $opt (keys %{$conf->{pending}}) { | |
238 | next if $opt eq 'delete'; | |
239 | my $value = $conf->{pending}->{$opt}; | |
240 | next if ref($value); # just to be sure | |
241 | $conf->{$opt} = $value; | |
242 | } | |
243 | my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete}); | |
244 | foreach my $opt (keys %$pending_delete_hash) { | |
245 | delete $conf->{$opt} if $conf->{$opt}; | |
246 | } | |
247 | } | |
248 | ||
249 | delete $conf->{snapshots}; | |
250 | delete $conf->{pending}; | |
251 | ||
252 | return $conf; | |
253 | } | |
254 | ||
cbbb06a5 | 255 | sub create_and_lock_config { |
fde98d5e | 256 | my ($class, $vmid, $allow_existing, $lock) = @_; |
cbbb06a5 TL |
257 | |
258 | $class->lock_config_full($vmid, 5, sub { | |
259 | PVE::Cluster::check_vmid_unused($vmid, $allow_existing); | |
260 | ||
261 | my $conf = eval { $class->load_config($vmid) } || {}; | |
262 | $class->check_lock($conf); | |
fde98d5e | 263 | $conf->{lock} = $lock // 'create'; |
cbbb06a5 TL |
264 | $class->write_config($vmid, $conf); |
265 | }); | |
266 | } | |
267 | ||
ab44df53 | 268 | # destroys configuration, only applicable for configs owned by the callers node. |
1aa76f2f TL |
269 | # dies if removal fails, e.g., when inquorate. |
270 | sub destroy_config { | |
271 | my ($class, $vmid) = @_; | |
272 | ||
273 | my $config_fn = $class->config_file($vmid, $nodename); | |
274 | unlink $config_fn or die "failed to remove config file: $!\n"; | |
275 | } | |
276 | ||
65906123 FE |
277 | # moves configuration owned by calling node to the target node. |
278 | # dies if renaming fails. | |
279 | sub move_config_to_node { | |
280 | my ($class, $vmid, $target_node) = @_; | |
281 | ||
282 | my $config_fn = $class->config_file($vmid); | |
283 | my $new_config_fn = $class->config_file($vmid, $target_node); | |
284 | ||
285 | rename($config_fn, $new_config_fn) | |
286 | or die "failed to move config file to node '$target_node': $!\n"; | |
287 | } | |
288 | ||
9cdd4400 FG |
289 | my $lock_file_full_wrapper = sub { |
290 | my ($class, $vmid, $timeout, $shared, $realcode, @param) = @_; | |
58a3c91c FG |
291 | |
292 | my $filename = $class->config_file_lock($vmid); | |
293 | ||
97b9ccec | 294 | # make sure configuration file is up-to-date |
9cdd4400 | 295 | my $code = sub { |
97b9ccec | 296 | PVE::Cluster::cfs_update(); |
9cdd4400 | 297 | $realcode->(@_); |
97b9ccec FE |
298 | }; |
299 | ||
9cdd4400 | 300 | my $res = lock_file_full($filename, $timeout, $shared, $code, @param); |
58a3c91c FG |
301 | |
302 | die $@ if $@; | |
303 | ||
304 | return $res; | |
9cdd4400 FG |
305 | }; |
306 | ||
307 | # Lock config file using non-exclusive ("read") flock, run $code with @param, unlock config file. | |
308 | # $timeout is the maximum time to acquire the flock | |
309 | sub lock_config_shared { | |
310 | my ($class, $vmid, $timeout, $code, @param) = @_; | |
311 | ||
312 | return $lock_file_full_wrapper->($class, $vmid, $timeout, 1, $code, @param); | |
58a3c91c FG |
313 | } |
314 | ||
9cdd4400 FG |
315 | # Lock config file using flock, run $code with @param, unlock config file. |
316 | # $timeout is the maximum time to acquire the flock | |
317 | sub lock_config_full { | |
318 | my ($class, $vmid, $timeout, $code, @param) = @_; | |
319 | ||
320 | return $lock_file_full_wrapper->($class, $vmid, $timeout, 0, $code, @param); | |
321 | } | |
322 | ||
323 | ||
58a3c91c FG |
324 | # Lock config file using flock, run $code with @param, unlock config file. |
325 | sub lock_config { | |
326 | my ($class, $vmid, $code, @param) = @_; | |
327 | ||
328 | return $class->lock_config_full($vmid, 10, $code, @param); | |
329 | } | |
330 | ||
331 | # Checks whether the config is locked with the lock parameter | |
332 | sub check_lock { | |
333 | my ($class, $conf) = @_; | |
334 | ||
335 | die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock}; | |
336 | } | |
337 | ||
338 | # Returns whether the config is locked with the lock parameter, also checks | |
339 | # whether the lock value is correct if the optional $lock is set. | |
340 | sub has_lock { | |
341 | my ($class, $conf, $lock) = @_; | |
342 | ||
343 | return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock}); | |
344 | } | |
345 | ||
346 | # Sets the lock parameter for this VM/CT's config to $lock. | |
347 | sub set_lock { | |
348 | my ($class, $vmid, $lock) = @_; | |
349 | ||
350 | my $conf; | |
351 | $class->lock_config($vmid, sub { | |
352 | $conf = $class->load_config($vmid); | |
353 | $class->check_lock($conf); | |
354 | $conf->{lock} = $lock; | |
355 | $class->write_config($vmid, $conf); | |
356 | }); | |
357 | return $conf; | |
358 | } | |
359 | ||
360 | # Removes the lock parameter for this VM/CT's config, also checks whether | |
361 | # the lock value is correct if the optional $lock is set. | |
362 | sub remove_lock { | |
363 | my ($class, $vmid, $lock) = @_; | |
364 | ||
365 | $class->lock_config($vmid, sub { | |
366 | my $conf = $class->load_config($vmid); | |
367 | if (!$conf->{lock}) { | |
368 | my $lockstring = defined($lock) ? "'$lock' " : "any"; | |
369 | die "no lock found trying to remove $lockstring lock\n"; | |
370 | } elsif (defined($lock) && $conf->{lock} ne $lock) { | |
371 | die "found lock '$conf->{lock}' trying to remove '$lock' lock\n"; | |
372 | } | |
373 | delete $conf->{lock}; | |
374 | $class->write_config($vmid, $conf); | |
375 | }); | |
376 | } | |
377 | ||
378 | # Checks whether protection mode is enabled for this VM/CT. | |
379 | sub check_protection { | |
380 | my ($class, $conf, $err_msg) = @_; | |
381 | ||
382 | if ($conf->{protection}) { | |
383 | die "$err_msg - protection mode enabled\n"; | |
384 | } | |
385 | } | |
386 | ||
4e6382ab FE |
387 | # Returns a list of keys where used volumes can be located |
388 | sub valid_volume_keys { | |
389 | my ($class, $reverse) = @_; | |
390 | ||
391 | die "implement me - abstract method\n"; | |
392 | } | |
393 | ||
394 | # Returns a hash with the information from $volume_string | |
395 | # $key is used to determine the format of the string | |
396 | sub parse_volume { | |
397 | my ($class, $key, $volume_string, $noerr) = @_; | |
398 | ||
399 | die "implement me - abstract method\n"; | |
400 | } | |
401 | ||
402 | # Returns a string with the information from $volume | |
403 | # $key is used to determine the format of the string | |
404 | sub print_volume { | |
405 | my ($class, $key, $volume) = @_; | |
406 | ||
407 | die "implement me - abstract method\n"; | |
408 | } | |
409 | ||
410 | # The key under which the volume ID is located in volume hashes | |
411 | sub volid_key { | |
412 | my ($class) = @_; | |
413 | ||
414 | die "implement me - abstract method\n"; | |
415 | } | |
416 | ||
58a3c91c FG |
417 | # Adds an unused volume to $config, if possible. |
418 | sub add_unused_volume { | |
419 | my ($class, $config, $volid) = @_; | |
420 | ||
421 | my $key; | |
422 | for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) { | |
423 | my $test = "unused$ind"; | |
424 | if (my $vid = $config->{$test}) { | |
425 | return if $vid eq $volid; # do not add duplicates | |
426 | } else { | |
427 | $key = $test; | |
428 | } | |
429 | } | |
430 | ||
431 | die "Too many unused volumes - please delete them first.\n" if !$key; | |
432 | ||
433 | $config->{$key} = $volid; | |
434 | ||
435 | return $key; | |
436 | } | |
437 | ||
8db01440 FE |
438 | # Iterate over all unused volumes, calling $func for each key/value pair |
439 | # with additional parameters @param. | |
440 | sub foreach_unused_volume { | |
441 | my ($class, $conf, $func, @param) = @_; | |
442 | ||
443 | foreach my $key (keys %{$conf}) { | |
444 | if ($key =~ m/^unused\d+$/) { | |
445 | my $volume = $class->parse_volume($key, $conf->{$key}); | |
446 | $func->($key, $volume, @param); | |
447 | } | |
448 | } | |
449 | } | |
450 | ||
261d47a4 FE |
451 | # Iterate over all configured volumes, calling $func for each key/value pair |
452 | # with additional parameters @param. | |
453 | # By default, unused volumes and specials like vmstate are excluded. | |
454 | # Options: reverse - reverses the order for the iteration | |
455 | # include_unused - also iterate over unused volumes | |
456 | # extra_keys - an array of extra keys to use for the iteration | |
457 | sub foreach_volume_full { | |
458 | my ($class, $conf, $opts, $func, @param) = @_; | |
459 | ||
460 | die "'reverse' iteration only supported for default keys\n" | |
461 | if $opts->{reverse} && ($opts->{extra_keys} || $opts->{include_unused}); | |
462 | ||
463 | my @keys = $class->valid_volume_keys($opts->{reverse}); | |
464 | push @keys, @{$opts->{extra_keys}} if $opts->{extra_keys}; | |
465 | ||
466 | foreach my $key (@keys) { | |
467 | my $volume_string = $conf->{$key}; | |
468 | next if !defined($volume_string); | |
469 | ||
470 | my $volume = $class->parse_volume($key, $volume_string, 1); | |
471 | next if !defined($volume); | |
472 | ||
473 | $func->($key, $volume, @param); | |
474 | } | |
475 | ||
476 | $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused}; | |
477 | } | |
478 | ||
479 | sub foreach_volume { | |
480 | my ($class, $conf, $func, @param) = @_; | |
481 | ||
482 | return $class->foreach_volume_full($conf, undef, $func, @param); | |
483 | } | |
484 | ||
95a9508e FE |
485 | # $volume_map is a hash of 'old_volid' => 'new_volid' pairs. |
486 | # This method replaces 'old_volid' by 'new_volid' throughout | |
487 | # the config including snapshots and unused and vmstate volumes | |
488 | sub update_volume_ids { | |
489 | my ($class, $conf, $volume_map) = @_; | |
490 | ||
491 | my $volid_key = $class->volid_key(); | |
492 | ||
493 | my $do_replace = sub { | |
494 | my ($key, $volume, $current_section) = @_; | |
495 | ||
496 | my $old_volid = $volume->{$volid_key}; | |
497 | if (my $new_volid = $volume_map->{$old_volid}) { | |
498 | $volume->{$volid_key} = $new_volid; | |
499 | $current_section->{$key} = $class->print_volume($key, $volume); | |
500 | } | |
501 | }; | |
502 | ||
503 | my $opts = { | |
504 | 'include_unused' => 1, | |
505 | 'extra_keys' => ['vmstate'], | |
506 | }; | |
507 | ||
508 | $class->foreach_volume_full($conf, $opts, $do_replace, $conf); | |
509 | foreach my $snap (keys %{$conf->{snapshots}}) { | |
510 | my $snap_conf = $conf->{snapshots}->{$snap}; | |
511 | $class->foreach_volume_full($snap_conf, $opts, $do_replace, $snap_conf); | |
512 | } | |
513 | } | |
514 | ||
58a3c91c FG |
515 | # Returns whether the template parameter is set in $conf. |
516 | sub is_template { | |
517 | my ($class, $conf) = @_; | |
518 | ||
519 | return 1 if defined $conf->{template} && $conf->{template} == 1; | |
520 | } | |
521 | ||
ab44df53 | 522 | # Checks whether $feature is available for the referenced volumes in $conf. |
58a3c91c FG |
523 | # Note: depending on the parameters, some volumes may be skipped! |
524 | sub has_feature { | |
525 | my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_; | |
526 | die "implement me - abstract method\n"; | |
527 | } | |
528 | ||
3f85b14d DM |
529 | # get all replicatable volume (hash $res->{$volid} = 1) |
530 | # $cleanup: for cleanup - simply ignores volumes without replicate feature | |
531 | # $norerr: never raise exceptions - return undef instead | |
532 | sub get_replicatable_volumes { | |
54b79ff5 | 533 | my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_; |
3f85b14d DM |
534 | |
535 | die "implement me - abstract method\n"; | |
536 | } | |
537 | ||
1245ad85 TL |
538 | # Returns all the guests volumes which would be included in a vzdump job |
539 | # Return Format (array-ref with hash-refs as elements): | |
b2a2affa AL |
540 | # [ |
541 | # { | |
a8878e5e AL |
542 | # key, key in the config, e.g. mp0, scsi0,... |
543 | # included, boolean | |
544 | # reason, string | |
545 | # volume_config volume object as returned from foreach_volume() | |
1245ad85 | 546 | # }, |
b2a2affa AL |
547 | # ] |
548 | sub get_backup_volumes { | |
549 | my ($class, $conf) = @_; | |
550 | ||
551 | die "implement me - abstract method\n"; | |
552 | } | |
553 | ||
58a3c91c FG |
554 | # Internal snapshots |
555 | ||
556 | # NOTE: Snapshot create/delete involves several non-atomic | |
557 | # actions, and can take a long time. | |
558 | # So we try to avoid locking the file and use the 'lock' variable | |
559 | # inside the config file instead. | |
560 | ||
561 | # Save the vmstate (RAM). | |
562 | sub __snapshot_save_vmstate { | |
563 | my ($class, $vmid, $conf, $snapname, $storecfg) = @_; | |
564 | die "implement me - abstract method\n"; | |
565 | } | |
566 | ||
567 | # Check whether the VM/CT is running. | |
568 | sub __snapshot_check_running { | |
569 | my ($class, $vmid) = @_; | |
570 | die "implement me - abstract method\n"; | |
571 | } | |
572 | ||
573 | # Check whether we need to freeze the VM/CT | |
574 | sub __snapshot_check_freeze_needed { | |
575 | my ($sself, $vmid, $config, $save_vmstate) = @_; | |
576 | die "implement me - abstract method\n"; | |
577 | } | |
578 | ||
579 | # Freeze or unfreeze the VM/CT. | |
580 | sub __snapshot_freeze { | |
581 | my ($class, $vmid, $unfreeze) = @_; | |
582 | ||
583 | die "abstract method - implement me\n"; | |
584 | } | |
585 | ||
586 | # Code run before and after creating all the volume snapshots | |
587 | # base: noop | |
588 | sub __snapshot_create_vol_snapshots_hook { | |
589 | my ($class, $vmid, $snap, $running, $hook) = @_; | |
590 | ||
591 | return; | |
592 | } | |
593 | ||
594 | # Create the volume snapshots for the VM/CT. | |
595 | sub __snapshot_create_vol_snapshot { | |
596 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
597 | ||
598 | die "abstract method - implement me\n"; | |
599 | } | |
600 | ||
601 | # Remove a drive from the snapshot config. | |
602 | sub __snapshot_delete_remove_drive { | |
603 | my ($class, $snap, $drive) = @_; | |
604 | ||
605 | die "abstract method - implement me\n"; | |
606 | } | |
607 | ||
608 | # Delete the vmstate file/drive | |
609 | sub __snapshot_delete_vmstate_file { | |
610 | my ($class, $snap, $force) = @_; | |
611 | ||
612 | die "abstract method - implement me\n"; | |
613 | } | |
614 | ||
615 | # Delete a volume snapshot | |
616 | sub __snapshot_delete_vol_snapshot { | |
617 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
618 | ||
619 | die "abstract method - implement me\n"; | |
620 | } | |
621 | ||
3fa4ce45 DC |
622 | # called during rollback prepare, and between config rollback and starting guest |
623 | # can change config, e.g. for vmgenid | |
624 | # $data is shared across calls and passed to vm_start | |
625 | sub __snapshot_rollback_hook { | |
626 | my ($class, $vmid, $conf, $snap, $prepare, $data) = @_; | |
627 | ||
628 | return; | |
629 | } | |
630 | ||
58a3c91c FG |
631 | # Checks whether a volume snapshot is possible for this volume. |
632 | sub __snapshot_rollback_vol_possible { | |
633 | my ($class, $volume, $snapname) = @_; | |
634 | ||
635 | die "abstract method - implement me\n"; | |
636 | } | |
637 | ||
638 | # Rolls back this volume. | |
639 | sub __snapshot_rollback_vol_rollback { | |
640 | my ($class, $volume, $snapname) = @_; | |
641 | ||
642 | die "abstract method - implement me\n"; | |
643 | } | |
644 | ||
645 | # Stops the VM/CT for a rollback. | |
646 | sub __snapshot_rollback_vm_stop { | |
647 | my ($class, $vmid) = @_; | |
648 | ||
649 | die "abstract method - implement me\n"; | |
650 | } | |
651 | ||
652 | # Start the VM/CT after a rollback with restored vmstate. | |
653 | sub __snapshot_rollback_vm_start { | |
3fa4ce45 | 654 | my ($class, $vmid, $vmstate, $data); |
58a3c91c FG |
655 | |
656 | die "abstract method - implement me\n"; | |
657 | } | |
658 | ||
659 | # Get list of volume IDs which are referenced in $conf, but not in $snap. | |
660 | sub __snapshot_rollback_get_unused { | |
661 | my ($class, $conf, $snap) = @_; | |
662 | ||
663 | die "abstract method - implement me\n"; | |
664 | } | |
665 | ||
58a3c91c FG |
666 | # Copy the current config $source to the snapshot config $dest |
667 | sub __snapshot_copy_config { | |
668 | my ($class, $source, $dest) = @_; | |
669 | ||
670 | foreach my $k (keys %$source) { | |
671 | next if $k eq 'snapshots'; | |
672 | next if $k eq 'snapstate'; | |
673 | next if $k eq 'snaptime'; | |
674 | next if $k eq 'vmstate'; | |
675 | next if $k eq 'lock'; | |
676 | next if $k eq 'digest'; | |
677 | next if $k eq 'description'; | |
678 | next if $k =~ m/^unused\d+$/; | |
679 | ||
680 | $dest->{$k} = $source->{$k}; | |
681 | } | |
682 | }; | |
683 | ||
684 | # Apply the snapshot config $snap to the config $conf (rollback) | |
685 | sub __snapshot_apply_config { | |
686 | my ($class, $conf, $snap) = @_; | |
687 | ||
688 | # copy snapshot list | |
689 | my $newconf = { | |
690 | snapshots => $conf->{snapshots}, | |
691 | }; | |
692 | ||
eb74d483 | 693 | # keep description and list of unused disks |
58a3c91c | 694 | foreach my $k (keys %$conf) { |
eb74d483 | 695 | next if !($k =~ m/^unused\d+$/ || $k eq 'description'); |
38532f70 | 696 | |
58a3c91c FG |
697 | $newconf->{$k} = $conf->{$k}; |
698 | } | |
699 | ||
700 | $class->__snapshot_copy_config($snap, $newconf); | |
701 | ||
702 | return $newconf; | |
703 | } | |
704 | ||
705 | # Prepares the configuration for snapshotting. | |
706 | sub __snapshot_prepare { | |
707 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
708 | ||
709 | my $snap; | |
710 | ||
711 | my $updatefn = sub { | |
712 | ||
713 | my $conf = $class->load_config($vmid); | |
714 | ||
715 | die "you can't take a snapshot if it's a template\n" | |
716 | if $class->is_template($conf); | |
717 | ||
718 | $class->check_lock($conf); | |
719 | ||
720 | $conf->{lock} = 'snapshot'; | |
721 | ||
722 | die "snapshot name '$snapname' already used\n" | |
723 | if defined($conf->{snapshots}->{$snapname}); | |
724 | ||
725 | my $storecfg = PVE::Storage::config(); | |
726 | die "snapshot feature is not available\n" | |
727 | if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump'); | |
728 | ||
729 | $snap = $conf->{snapshots}->{$snapname} = {}; | |
730 | ||
731 | if ($save_vmstate && $class->__snapshot_check_running($vmid)) { | |
732 | $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg); | |
733 | } | |
734 | ||
735 | $class->__snapshot_copy_config($conf, $snap); | |
736 | ||
737 | $snap->{snapstate} = "prepare"; | |
738 | $snap->{snaptime} = time(); | |
739 | $snap->{description} = $comment if $comment; | |
740 | ||
741 | $class->write_config($vmid, $conf); | |
742 | }; | |
743 | ||
744 | $class->lock_config($vmid, $updatefn); | |
745 | ||
746 | return $snap; | |
747 | } | |
748 | ||
749 | # Commits the configuration after snapshotting. | |
750 | sub __snapshot_commit { | |
751 | my ($class, $vmid, $snapname) = @_; | |
752 | ||
753 | my $updatefn = sub { | |
754 | ||
755 | my $conf = $class->load_config($vmid); | |
756 | ||
757 | die "missing snapshot lock\n" | |
758 | if !($conf->{lock} && $conf->{lock} eq 'snapshot'); | |
759 | ||
760 | my $snap = $conf->{snapshots}->{$snapname}; | |
761 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
762 | ||
763 | die "wrong snapshot state\n" | |
764 | if !($snap->{snapstate} && $snap->{snapstate} eq "prepare"); | |
765 | ||
766 | delete $snap->{snapstate}; | |
767 | delete $conf->{lock}; | |
768 | ||
769 | $conf->{parent} = $snapname; | |
770 | ||
771 | $class->write_config($vmid, $conf); | |
772 | }; | |
773 | ||
774 | $class->lock_config($vmid, $updatefn); | |
775 | } | |
776 | ||
777 | # Creates a snapshot for the VM/CT. | |
778 | sub snapshot_create { | |
779 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
780 | ||
781 | my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment); | |
782 | ||
783 | $save_vmstate = 0 if !$snap->{vmstate}; | |
784 | ||
785 | my $conf = $class->load_config($vmid); | |
786 | ||
787 | my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate}); | |
788 | ||
789 | my $drivehash = {}; | |
790 | ||
791 | eval { | |
792 | if ($freezefs) { | |
793 | $class->__snapshot_freeze($vmid, 0); | |
794 | } | |
795 | ||
796 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before"); | |
797 | ||
261d47a4 | 798 | $class->foreach_volume($snap, sub { |
58a3c91c FG |
799 | my ($vs, $volume) = @_; |
800 | ||
801 | $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname); | |
802 | $drivehash->{$vs} = 1; | |
803 | }); | |
804 | }; | |
805 | my $err = $@; | |
806 | ||
807 | if ($running) { | |
808 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after"); | |
809 | if ($freezefs) { | |
810 | $class->__snapshot_freeze($vmid, 1); | |
811 | } | |
812 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze"); | |
813 | } | |
814 | ||
815 | if ($err) { | |
816 | warn "snapshot create failed: starting cleanup\n"; | |
817 | eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); }; | |
818 | warn "$@" if $@; | |
819 | die "$err\n"; | |
820 | } | |
821 | ||
822 | $class->__snapshot_commit($vmid, $snapname); | |
823 | } | |
824 | ||
825 | # Deletes a snapshot. | |
826 | # Note: $drivehash is only set when called from snapshot_create. | |
827 | sub snapshot_delete { | |
828 | my ($class, $vmid, $snapname, $force, $drivehash) = @_; | |
829 | ||
830 | my $prepare = 1; | |
831 | ||
58a3c91c FG |
832 | my $unused = []; |
833 | ||
4c3144ea AA |
834 | my $conf = $class->load_config($vmid); |
835 | my $snap = $conf->{snapshots}->{$snapname}; | |
836 | ||
837 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
838 | ||
58a3c91c FG |
839 | $class->set_lock($vmid, 'snapshot-delete') |
840 | if (!$drivehash); # doesn't already have a 'snapshot' lock | |
841 | ||
990d9675 FG |
842 | my $expected_lock = $drivehash ? 'snapshot' : 'snapshot-delete'; |
843 | ||
844 | my $ensure_correct_lock = sub { | |
845 | my ($conf) = @_; | |
846 | ||
847 | die "encountered invalid lock, expected '$expected_lock'\n" | |
848 | if !$class->has_lock($conf, $expected_lock); | |
849 | }; | |
850 | ||
58a3c91c FG |
851 | my $unlink_parent = sub { |
852 | my ($confref, $new_parent) = @_; | |
853 | ||
854 | if ($confref->{parent} && $confref->{parent} eq $snapname) { | |
855 | if ($new_parent) { | |
856 | $confref->{parent} = $new_parent; | |
857 | } else { | |
858 | delete $confref->{parent}; | |
859 | } | |
860 | } | |
861 | }; | |
862 | ||
863 | my $remove_drive = sub { | |
864 | my ($drive) = @_; | |
865 | ||
866 | my $conf = $class->load_config($vmid); | |
990d9675 FG |
867 | $ensure_correct_lock->($conf); |
868 | ||
58a3c91c FG |
869 | $snap = $conf->{snapshots}->{$snapname}; |
870 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
871 | ||
872 | $class->__snapshot_delete_remove_drive($snap, $drive); | |
873 | ||
874 | $class->write_config($vmid, $conf); | |
875 | }; | |
876 | ||
877 | #prepare | |
878 | $class->lock_config($vmid, sub { | |
879 | my $conf = $class->load_config($vmid); | |
990d9675 | 880 | $ensure_correct_lock->($conf); |
58a3c91c FG |
881 | |
882 | die "you can't delete a snapshot if vm is a template\n" | |
883 | if $class->is_template($conf); | |
884 | ||
885 | $snap = $conf->{snapshots}->{$snapname}; | |
886 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
887 | ||
888 | $snap->{snapstate} = 'delete'; | |
889 | ||
890 | $class->write_config($vmid, $conf); | |
891 | }); | |
892 | ||
893 | # now remove vmstate file | |
894 | if ($snap->{vmstate}) { | |
895 | $class->__snapshot_delete_vmstate_file($snap, $force); | |
896 | ||
897 | # save changes (remove vmstate from snapshot) | |
898 | $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force; | |
899 | }; | |
900 | ||
901 | # now remove all volume snapshots | |
261d47a4 | 902 | $class->foreach_volume($snap, sub { |
58a3c91c FG |
903 | my ($vs, $volume) = @_; |
904 | ||
905 | return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup}; | |
906 | if (!$drivehash || $drivehash->{$vs}) { | |
907 | eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); }; | |
908 | if (my $err = $@) { | |
909 | die $err if !$force; | |
910 | warn $err; | |
911 | } | |
912 | } | |
913 | ||
914 | # save changes (remove drive from snapshot) | |
915 | $class->lock_config($vmid, $remove_drive, $vs) if !$force; | |
916 | }); | |
917 | ||
918 | # now cleanup config | |
919 | $class->lock_config($vmid, sub { | |
920 | my $conf = $class->load_config($vmid); | |
990d9675 FG |
921 | $ensure_correct_lock->($conf); |
922 | ||
58a3c91c FG |
923 | $snap = $conf->{snapshots}->{$snapname}; |
924 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
925 | ||
926 | # remove parent refs | |
927 | &$unlink_parent($conf, $snap->{parent}); | |
928 | foreach my $sn (keys %{$conf->{snapshots}}) { | |
929 | next if $sn eq $snapname; | |
930 | &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent}); | |
931 | } | |
932 | ||
933 | ||
934 | delete $conf->{snapshots}->{$snapname}; | |
935 | delete $conf->{lock}; | |
936 | foreach my $volid (@$unused) { | |
937 | $class->add_unused_volume($conf, $volid); | |
938 | } | |
939 | ||
940 | $class->write_config($vmid, $conf); | |
941 | }); | |
942 | } | |
943 | ||
944 | # Rolls back to a given snapshot. | |
945 | sub snapshot_rollback { | |
946 | my ($class, $vmid, $snapname) = @_; | |
947 | ||
948 | my $prepare = 1; | |
949 | ||
950 | my $storecfg = PVE::Storage::config(); | |
951 | ||
82a04857 | 952 | my $data = {}; |
58a3c91c FG |
953 | |
954 | my $get_snapshot_config = sub { | |
82a04857 | 955 | my ($conf) = @_; |
58a3c91c FG |
956 | |
957 | die "you can't rollback if vm is a template\n" if $class->is_template($conf); | |
958 | ||
959 | my $res = $conf->{snapshots}->{$snapname}; | |
960 | ||
961 | die "snapshot '$snapname' does not exist\n" if !defined($res); | |
962 | ||
963 | return $res; | |
964 | }; | |
965 | ||
82a04857 | 966 | my $snap; |
3fa4ce45 | 967 | |
58a3c91c | 968 | my $updatefn = sub { |
82a04857 FG |
969 | my $conf = $class->load_config($vmid); |
970 | $snap = $get_snapshot_config->($conf); | |
58a3c91c | 971 | |
82a04857 FG |
972 | if ($prepare) { |
973 | my $repl_conf = PVE::ReplicationConfig->new(); | |
974 | if ($repl_conf->check_for_existing_jobs($vmid, 1)) { | |
975 | # remove all replication snapshots | |
976 | my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1); | |
977 | my $sorted_volids = [ sort keys %$volumes ]; | |
978 | ||
979 | # remove all local replication snapshots (jobid => undef) | |
980 | my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; }; | |
c261973c | 981 | PVE::Replication::prepare($storecfg, $sorted_volids, undef, 1, undef, $logfunc); |
82a04857 FG |
982 | } |
983 | ||
984 | $class->foreach_volume($snap, sub { | |
985 | my ($vs, $volume) = @_; | |
58a3c91c | 986 | |
82a04857 FG |
987 | $class->__snapshot_rollback_vol_possible($volume, $snapname); |
988 | }); | |
989 | } | |
58a3c91c FG |
990 | |
991 | die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" | |
992 | if $snap->{snapstate}; | |
993 | ||
994 | if ($prepare) { | |
995 | $class->check_lock($conf); | |
996 | $class->__snapshot_rollback_vm_stop($vmid); | |
997 | } | |
998 | ||
999 | die "unable to rollback vm $vmid: vm is running\n" | |
1000 | if $class->__snapshot_check_running($vmid); | |
1001 | ||
1002 | if ($prepare) { | |
1003 | $conf->{lock} = 'rollback'; | |
1004 | } else { | |
1005 | die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback'); | |
1006 | delete $conf->{lock}; | |
58a3c91c | 1007 | |
58a3c91c FG |
1008 | my $unused = $class->__snapshot_rollback_get_unused($conf, $snap); |
1009 | ||
1010 | foreach my $volid (@$unused) { | |
1011 | $class->add_unused_volume($conf, $volid); | |
1012 | } | |
1013 | ||
58a3c91c FG |
1014 | # copy snapshot config to current config |
1015 | $conf = $class->__snapshot_apply_config($conf, $snap); | |
1016 | $conf->{parent} = $snapname; | |
58a3c91c FG |
1017 | } |
1018 | ||
3fa4ce45 DC |
1019 | $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data); |
1020 | ||
58a3c91c FG |
1021 | $class->write_config($vmid, $conf); |
1022 | ||
1023 | if (!$prepare && $snap->{vmstate}) { | |
3fa4ce45 | 1024 | $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data); |
58a3c91c FG |
1025 | } |
1026 | }; | |
1027 | ||
1028 | $class->lock_config($vmid, $updatefn); | |
1029 | ||
261d47a4 | 1030 | $class->foreach_volume($snap, sub { |
58a3c91c FG |
1031 | my ($vs, $volume) = @_; |
1032 | ||
1033 | $class->__snapshot_rollback_vol_rollback($volume, $snapname); | |
1034 | }); | |
1035 | ||
1036 | $prepare = 0; | |
1037 | $class->lock_config($vmid, $updatefn); | |
1038 | } | |
1039 | ||
eb50bb61 RV |
1040 | # bash completion helper |
1041 | ||
90fba9cd WB |
1042 | sub snapshot_list { |
1043 | my ($class, $vmid) = @_; | |
eb50bb61 | 1044 | |
90fba9cd WB |
1045 | my $snapshot = eval { |
1046 | my $conf = $class->load_config($vmid); | |
1047 | my $snapshots = $conf->{snapshots} || []; | |
1048 | [ sort keys %$snapshots ] | |
1049 | } || []; | |
eb50bb61 RV |
1050 | |
1051 | return $snapshot; | |
1052 | } | |
1053 | ||
58a3c91c | 1054 | 1; |