]>
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 | ||
14f17b49 DM |
11 | use PVE::ReplicationConfig; |
12 | use PVE::Replication; | |
13 | ||
58a3c91c FG |
14 | my $nodename = PVE::INotify::nodename(); |
15 | ||
16 | # Printable string, currently either "VM" or "CT" | |
17 | sub guest_type { | |
18 | my ($class) = @_; | |
19 | die "abstract method - implement me "; | |
20 | } | |
21 | ||
22 | sub __config_max_unused_disks { | |
23 | my ($class) = @_; | |
24 | ||
25 | die "implement me - abstract method\n"; | |
26 | } | |
27 | ||
28 | # Path to the flock file for this VM/CT | |
29 | sub config_file_lock { | |
30 | my ($class, $vmid) = @_; | |
31 | die "abstract method - implement me"; | |
32 | } | |
33 | ||
34 | # Relative config file path for this VM/CT in CFS | |
35 | sub cfs_config_path { | |
36 | my ($class, $vmid, $node) = @_; | |
37 | die "abstract method - implement me"; | |
38 | } | |
39 | ||
40 | # Absolute config file path for this VM/CT | |
41 | sub config_file { | |
42 | my ($class, $vmid, $node) = @_; | |
43 | ||
44 | my $cfspath = $class->cfs_config_path($vmid, $node); | |
45 | return "/etc/pve/$cfspath"; | |
46 | } | |
47 | ||
48 | # Read and parse config file for this VM/CT | |
49 | sub load_config { | |
50 | my ($class, $vmid, $node) = @_; | |
51 | ||
52 | $node = $nodename if !$node; | |
53 | my $cfspath = $class->cfs_config_path($vmid, $node); | |
54 | ||
55 | my $conf = PVE::Cluster::cfs_read_file($cfspath); | |
56 | die "Configuration file '$cfspath' does not exist\n" | |
57 | if !defined($conf); | |
58 | ||
59 | return $conf; | |
60 | } | |
61 | ||
62 | # Generate and write config file for this VM/CT | |
63 | sub write_config { | |
64 | my ($class, $vmid, $conf) = @_; | |
65 | ||
66 | my $cfspath = $class->cfs_config_path($vmid); | |
67 | ||
68 | PVE::Cluster::cfs_write_file($cfspath, $conf); | |
69 | } | |
70 | ||
810ee088 OB |
71 | # Pending changes related |
72 | ||
73 | sub parse_pending_delete { | |
74 | my ($class, $data) = @_; | |
7b9d1ade TL |
75 | |
76 | return {} if !$data; | |
77 | ||
810ee088 OB |
78 | $data =~ s/[,;]/ /g; |
79 | $data =~ s/^\s+//; | |
7b9d1ade TL |
80 | |
81 | my $pending_deletions = {}; | |
82 | for my $entry (split(/\s+/, $data)) { | |
83 | my ($force, $key) = $entry =~ /^(!?)(.*)$/; | |
84 | ||
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}; | |
199 | if (PVE::GuestHelpers::typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) { | |
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 | ||
255 | ||
58a3c91c | 256 | # Lock config file using flock, run $code with @param, unlock config file. |
ab44df53 | 257 | # $timeout is the maximum time to acquire the flock |
58a3c91c FG |
258 | sub lock_config_full { |
259 | my ($class, $vmid, $timeout, $code, @param) = @_; | |
260 | ||
261 | my $filename = $class->config_file_lock($vmid); | |
262 | ||
263 | my $res = lock_file($filename, $timeout, $code, @param); | |
264 | ||
265 | die $@ if $@; | |
266 | ||
267 | return $res; | |
268 | } | |
269 | ||
cbbb06a5 | 270 | sub create_and_lock_config { |
fde98d5e | 271 | my ($class, $vmid, $allow_existing, $lock) = @_; |
cbbb06a5 TL |
272 | |
273 | $class->lock_config_full($vmid, 5, sub { | |
274 | PVE::Cluster::check_vmid_unused($vmid, $allow_existing); | |
275 | ||
276 | my $conf = eval { $class->load_config($vmid) } || {}; | |
277 | $class->check_lock($conf); | |
fde98d5e | 278 | $conf->{lock} = $lock // 'create'; |
cbbb06a5 TL |
279 | $class->write_config($vmid, $conf); |
280 | }); | |
281 | } | |
282 | ||
ab44df53 | 283 | # destroys configuration, only applicable for configs owned by the callers node. |
1aa76f2f TL |
284 | # dies if removal fails, e.g., when inquorate. |
285 | sub destroy_config { | |
286 | my ($class, $vmid) = @_; | |
287 | ||
288 | my $config_fn = $class->config_file($vmid, $nodename); | |
289 | unlink $config_fn or die "failed to remove config file: $!\n"; | |
290 | } | |
291 | ||
58a3c91c | 292 | # Lock config file using flock, run $code with @param, unlock config file. |
ab44df53 | 293 | # $timeout is the maximum time to acquire the flock |
58a3c91c FG |
294 | # $shared eq 1 creates a non-exclusive ("read") flock |
295 | sub lock_config_mode { | |
296 | my ($class, $vmid, $timeout, $shared, $code, @param) = @_; | |
297 | ||
298 | my $filename = $class->config_file_lock($vmid); | |
299 | ||
300 | my $res = lock_file_full($filename, $timeout, $shared, $code, @param); | |
301 | ||
302 | die $@ if $@; | |
303 | ||
304 | return $res; | |
305 | } | |
306 | ||
307 | # Lock config file using flock, run $code with @param, unlock config file. | |
308 | sub lock_config { | |
309 | my ($class, $vmid, $code, @param) = @_; | |
310 | ||
311 | return $class->lock_config_full($vmid, 10, $code, @param); | |
312 | } | |
313 | ||
314 | # Checks whether the config is locked with the lock parameter | |
315 | sub check_lock { | |
316 | my ($class, $conf) = @_; | |
317 | ||
318 | die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock}; | |
319 | } | |
320 | ||
321 | # Returns whether the config is locked with the lock parameter, also checks | |
322 | # whether the lock value is correct if the optional $lock is set. | |
323 | sub has_lock { | |
324 | my ($class, $conf, $lock) = @_; | |
325 | ||
326 | return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock}); | |
327 | } | |
328 | ||
329 | # Sets the lock parameter for this VM/CT's config to $lock. | |
330 | sub set_lock { | |
331 | my ($class, $vmid, $lock) = @_; | |
332 | ||
333 | my $conf; | |
334 | $class->lock_config($vmid, sub { | |
335 | $conf = $class->load_config($vmid); | |
336 | $class->check_lock($conf); | |
337 | $conf->{lock} = $lock; | |
338 | $class->write_config($vmid, $conf); | |
339 | }); | |
340 | return $conf; | |
341 | } | |
342 | ||
343 | # Removes the lock parameter for this VM/CT's config, also checks whether | |
344 | # the lock value is correct if the optional $lock is set. | |
345 | sub remove_lock { | |
346 | my ($class, $vmid, $lock) = @_; | |
347 | ||
348 | $class->lock_config($vmid, sub { | |
349 | my $conf = $class->load_config($vmid); | |
350 | if (!$conf->{lock}) { | |
351 | my $lockstring = defined($lock) ? "'$lock' " : "any"; | |
352 | die "no lock found trying to remove $lockstring lock\n"; | |
353 | } elsif (defined($lock) && $conf->{lock} ne $lock) { | |
354 | die "found lock '$conf->{lock}' trying to remove '$lock' lock\n"; | |
355 | } | |
356 | delete $conf->{lock}; | |
357 | $class->write_config($vmid, $conf); | |
358 | }); | |
359 | } | |
360 | ||
361 | # Checks whether protection mode is enabled for this VM/CT. | |
362 | sub check_protection { | |
363 | my ($class, $conf, $err_msg) = @_; | |
364 | ||
365 | if ($conf->{protection}) { | |
366 | die "$err_msg - protection mode enabled\n"; | |
367 | } | |
368 | } | |
369 | ||
370 | # Adds an unused volume to $config, if possible. | |
371 | sub add_unused_volume { | |
372 | my ($class, $config, $volid) = @_; | |
373 | ||
374 | my $key; | |
375 | for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) { | |
376 | my $test = "unused$ind"; | |
377 | if (my $vid = $config->{$test}) { | |
378 | return if $vid eq $volid; # do not add duplicates | |
379 | } else { | |
380 | $key = $test; | |
381 | } | |
382 | } | |
383 | ||
384 | die "Too many unused volumes - please delete them first.\n" if !$key; | |
385 | ||
386 | $config->{$key} = $volid; | |
387 | ||
388 | return $key; | |
389 | } | |
390 | ||
391 | # Returns whether the template parameter is set in $conf. | |
392 | sub is_template { | |
393 | my ($class, $conf) = @_; | |
394 | ||
395 | return 1 if defined $conf->{template} && $conf->{template} == 1; | |
396 | } | |
397 | ||
ab44df53 | 398 | # Checks whether $feature is available for the referenced volumes in $conf. |
58a3c91c FG |
399 | # Note: depending on the parameters, some volumes may be skipped! |
400 | sub has_feature { | |
401 | my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_; | |
402 | die "implement me - abstract method\n"; | |
403 | } | |
404 | ||
3f85b14d DM |
405 | # get all replicatable volume (hash $res->{$volid} = 1) |
406 | # $cleanup: for cleanup - simply ignores volumes without replicate feature | |
407 | # $norerr: never raise exceptions - return undef instead | |
408 | sub get_replicatable_volumes { | |
54b79ff5 | 409 | my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_; |
3f85b14d DM |
410 | |
411 | die "implement me - abstract method\n"; | |
412 | } | |
413 | ||
58a3c91c FG |
414 | # Internal snapshots |
415 | ||
416 | # NOTE: Snapshot create/delete involves several non-atomic | |
417 | # actions, and can take a long time. | |
418 | # So we try to avoid locking the file and use the 'lock' variable | |
419 | # inside the config file instead. | |
420 | ||
421 | # Save the vmstate (RAM). | |
422 | sub __snapshot_save_vmstate { | |
423 | my ($class, $vmid, $conf, $snapname, $storecfg) = @_; | |
424 | die "implement me - abstract method\n"; | |
425 | } | |
426 | ||
427 | # Check whether the VM/CT is running. | |
428 | sub __snapshot_check_running { | |
429 | my ($class, $vmid) = @_; | |
430 | die "implement me - abstract method\n"; | |
431 | } | |
432 | ||
433 | # Check whether we need to freeze the VM/CT | |
434 | sub __snapshot_check_freeze_needed { | |
435 | my ($sself, $vmid, $config, $save_vmstate) = @_; | |
436 | die "implement me - abstract method\n"; | |
437 | } | |
438 | ||
439 | # Freeze or unfreeze the VM/CT. | |
440 | sub __snapshot_freeze { | |
441 | my ($class, $vmid, $unfreeze) = @_; | |
442 | ||
443 | die "abstract method - implement me\n"; | |
444 | } | |
445 | ||
446 | # Code run before and after creating all the volume snapshots | |
447 | # base: noop | |
448 | sub __snapshot_create_vol_snapshots_hook { | |
449 | my ($class, $vmid, $snap, $running, $hook) = @_; | |
450 | ||
451 | return; | |
452 | } | |
453 | ||
454 | # Create the volume snapshots for the VM/CT. | |
455 | sub __snapshot_create_vol_snapshot { | |
456 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
457 | ||
458 | die "abstract method - implement me\n"; | |
459 | } | |
460 | ||
461 | # Remove a drive from the snapshot config. | |
462 | sub __snapshot_delete_remove_drive { | |
463 | my ($class, $snap, $drive) = @_; | |
464 | ||
465 | die "abstract method - implement me\n"; | |
466 | } | |
467 | ||
468 | # Delete the vmstate file/drive | |
469 | sub __snapshot_delete_vmstate_file { | |
470 | my ($class, $snap, $force) = @_; | |
471 | ||
472 | die "abstract method - implement me\n"; | |
473 | } | |
474 | ||
475 | # Delete a volume snapshot | |
476 | sub __snapshot_delete_vol_snapshot { | |
477 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
478 | ||
479 | die "abstract method - implement me\n"; | |
480 | } | |
481 | ||
3fa4ce45 DC |
482 | # called during rollback prepare, and between config rollback and starting guest |
483 | # can change config, e.g. for vmgenid | |
484 | # $data is shared across calls and passed to vm_start | |
485 | sub __snapshot_rollback_hook { | |
486 | my ($class, $vmid, $conf, $snap, $prepare, $data) = @_; | |
487 | ||
488 | return; | |
489 | } | |
490 | ||
58a3c91c FG |
491 | # Checks whether a volume snapshot is possible for this volume. |
492 | sub __snapshot_rollback_vol_possible { | |
493 | my ($class, $volume, $snapname) = @_; | |
494 | ||
495 | die "abstract method - implement me\n"; | |
496 | } | |
497 | ||
498 | # Rolls back this volume. | |
499 | sub __snapshot_rollback_vol_rollback { | |
500 | my ($class, $volume, $snapname) = @_; | |
501 | ||
502 | die "abstract method - implement me\n"; | |
503 | } | |
504 | ||
505 | # Stops the VM/CT for a rollback. | |
506 | sub __snapshot_rollback_vm_stop { | |
507 | my ($class, $vmid) = @_; | |
508 | ||
509 | die "abstract method - implement me\n"; | |
510 | } | |
511 | ||
512 | # Start the VM/CT after a rollback with restored vmstate. | |
513 | sub __snapshot_rollback_vm_start { | |
3fa4ce45 | 514 | my ($class, $vmid, $vmstate, $data); |
58a3c91c FG |
515 | |
516 | die "abstract method - implement me\n"; | |
517 | } | |
518 | ||
519 | # Get list of volume IDs which are referenced in $conf, but not in $snap. | |
520 | sub __snapshot_rollback_get_unused { | |
521 | my ($class, $conf, $snap) = @_; | |
522 | ||
523 | die "abstract method - implement me\n"; | |
524 | } | |
525 | ||
526 | # Iterate over all configured volumes, calling $func for each key/value pair. | |
527 | sub __snapshot_foreach_volume { | |
528 | my ($class, $conf, $func) = @_; | |
529 | ||
530 | die "abstract method - implement me\n"; | |
531 | } | |
532 | ||
533 | # Copy the current config $source to the snapshot config $dest | |
534 | sub __snapshot_copy_config { | |
535 | my ($class, $source, $dest) = @_; | |
536 | ||
537 | foreach my $k (keys %$source) { | |
538 | next if $k eq 'snapshots'; | |
539 | next if $k eq 'snapstate'; | |
540 | next if $k eq 'snaptime'; | |
541 | next if $k eq 'vmstate'; | |
542 | next if $k eq 'lock'; | |
543 | next if $k eq 'digest'; | |
544 | next if $k eq 'description'; | |
545 | next if $k =~ m/^unused\d+$/; | |
546 | ||
547 | $dest->{$k} = $source->{$k}; | |
548 | } | |
549 | }; | |
550 | ||
551 | # Apply the snapshot config $snap to the config $conf (rollback) | |
552 | sub __snapshot_apply_config { | |
553 | my ($class, $conf, $snap) = @_; | |
554 | ||
555 | # copy snapshot list | |
556 | my $newconf = { | |
557 | snapshots => $conf->{snapshots}, | |
558 | }; | |
559 | ||
eb74d483 | 560 | # keep description and list of unused disks |
58a3c91c | 561 | foreach my $k (keys %$conf) { |
eb74d483 | 562 | next if !($k =~ m/^unused\d+$/ || $k eq 'description'); |
38532f70 | 563 | |
58a3c91c FG |
564 | $newconf->{$k} = $conf->{$k}; |
565 | } | |
566 | ||
567 | $class->__snapshot_copy_config($snap, $newconf); | |
568 | ||
569 | return $newconf; | |
570 | } | |
571 | ||
572 | # Prepares the configuration for snapshotting. | |
573 | sub __snapshot_prepare { | |
574 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
575 | ||
576 | my $snap; | |
577 | ||
578 | my $updatefn = sub { | |
579 | ||
580 | my $conf = $class->load_config($vmid); | |
581 | ||
582 | die "you can't take a snapshot if it's a template\n" | |
583 | if $class->is_template($conf); | |
584 | ||
585 | $class->check_lock($conf); | |
586 | ||
587 | $conf->{lock} = 'snapshot'; | |
588 | ||
589 | die "snapshot name '$snapname' already used\n" | |
590 | if defined($conf->{snapshots}->{$snapname}); | |
591 | ||
592 | my $storecfg = PVE::Storage::config(); | |
593 | die "snapshot feature is not available\n" | |
594 | if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump'); | |
595 | ||
596 | $snap = $conf->{snapshots}->{$snapname} = {}; | |
597 | ||
598 | if ($save_vmstate && $class->__snapshot_check_running($vmid)) { | |
599 | $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg); | |
600 | } | |
601 | ||
602 | $class->__snapshot_copy_config($conf, $snap); | |
603 | ||
604 | $snap->{snapstate} = "prepare"; | |
605 | $snap->{snaptime} = time(); | |
606 | $snap->{description} = $comment if $comment; | |
607 | ||
608 | $class->write_config($vmid, $conf); | |
609 | }; | |
610 | ||
611 | $class->lock_config($vmid, $updatefn); | |
612 | ||
613 | return $snap; | |
614 | } | |
615 | ||
616 | # Commits the configuration after snapshotting. | |
617 | sub __snapshot_commit { | |
618 | my ($class, $vmid, $snapname) = @_; | |
619 | ||
620 | my $updatefn = sub { | |
621 | ||
622 | my $conf = $class->load_config($vmid); | |
623 | ||
624 | die "missing snapshot lock\n" | |
625 | if !($conf->{lock} && $conf->{lock} eq 'snapshot'); | |
626 | ||
627 | my $snap = $conf->{snapshots}->{$snapname}; | |
628 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
629 | ||
630 | die "wrong snapshot state\n" | |
631 | if !($snap->{snapstate} && $snap->{snapstate} eq "prepare"); | |
632 | ||
633 | delete $snap->{snapstate}; | |
634 | delete $conf->{lock}; | |
635 | ||
636 | $conf->{parent} = $snapname; | |
637 | ||
638 | $class->write_config($vmid, $conf); | |
639 | }; | |
640 | ||
641 | $class->lock_config($vmid, $updatefn); | |
642 | } | |
643 | ||
644 | # Creates a snapshot for the VM/CT. | |
645 | sub snapshot_create { | |
646 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
647 | ||
648 | my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment); | |
649 | ||
650 | $save_vmstate = 0 if !$snap->{vmstate}; | |
651 | ||
652 | my $conf = $class->load_config($vmid); | |
653 | ||
654 | my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate}); | |
655 | ||
656 | my $drivehash = {}; | |
657 | ||
658 | eval { | |
659 | if ($freezefs) { | |
660 | $class->__snapshot_freeze($vmid, 0); | |
661 | } | |
662 | ||
663 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before"); | |
664 | ||
665 | $class->__snapshot_foreach_volume($snap, sub { | |
666 | my ($vs, $volume) = @_; | |
667 | ||
668 | $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname); | |
669 | $drivehash->{$vs} = 1; | |
670 | }); | |
671 | }; | |
672 | my $err = $@; | |
673 | ||
674 | if ($running) { | |
675 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after"); | |
676 | if ($freezefs) { | |
677 | $class->__snapshot_freeze($vmid, 1); | |
678 | } | |
679 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze"); | |
680 | } | |
681 | ||
682 | if ($err) { | |
683 | warn "snapshot create failed: starting cleanup\n"; | |
684 | eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); }; | |
685 | warn "$@" if $@; | |
686 | die "$err\n"; | |
687 | } | |
688 | ||
689 | $class->__snapshot_commit($vmid, $snapname); | |
690 | } | |
691 | ||
692 | # Deletes a snapshot. | |
693 | # Note: $drivehash is only set when called from snapshot_create. | |
694 | sub snapshot_delete { | |
695 | my ($class, $vmid, $snapname, $force, $drivehash) = @_; | |
696 | ||
697 | my $prepare = 1; | |
698 | ||
58a3c91c FG |
699 | my $unused = []; |
700 | ||
4c3144ea AA |
701 | my $conf = $class->load_config($vmid); |
702 | my $snap = $conf->{snapshots}->{$snapname}; | |
703 | ||
704 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
705 | ||
58a3c91c FG |
706 | $class->set_lock($vmid, 'snapshot-delete') |
707 | if (!$drivehash); # doesn't already have a 'snapshot' lock | |
708 | ||
709 | my $unlink_parent = sub { | |
710 | my ($confref, $new_parent) = @_; | |
711 | ||
712 | if ($confref->{parent} && $confref->{parent} eq $snapname) { | |
713 | if ($new_parent) { | |
714 | $confref->{parent} = $new_parent; | |
715 | } else { | |
716 | delete $confref->{parent}; | |
717 | } | |
718 | } | |
719 | }; | |
720 | ||
721 | my $remove_drive = sub { | |
722 | my ($drive) = @_; | |
723 | ||
724 | my $conf = $class->load_config($vmid); | |
725 | $snap = $conf->{snapshots}->{$snapname}; | |
726 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
727 | ||
728 | $class->__snapshot_delete_remove_drive($snap, $drive); | |
729 | ||
730 | $class->write_config($vmid, $conf); | |
731 | }; | |
732 | ||
733 | #prepare | |
734 | $class->lock_config($vmid, sub { | |
735 | my $conf = $class->load_config($vmid); | |
736 | ||
737 | die "you can't delete a snapshot if vm is a template\n" | |
738 | if $class->is_template($conf); | |
739 | ||
740 | $snap = $conf->{snapshots}->{$snapname}; | |
741 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
742 | ||
743 | $snap->{snapstate} = 'delete'; | |
744 | ||
745 | $class->write_config($vmid, $conf); | |
746 | }); | |
747 | ||
748 | # now remove vmstate file | |
749 | if ($snap->{vmstate}) { | |
750 | $class->__snapshot_delete_vmstate_file($snap, $force); | |
751 | ||
752 | # save changes (remove vmstate from snapshot) | |
753 | $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force; | |
754 | }; | |
755 | ||
756 | # now remove all volume snapshots | |
757 | $class->__snapshot_foreach_volume($snap, sub { | |
758 | my ($vs, $volume) = @_; | |
759 | ||
760 | return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup}; | |
761 | if (!$drivehash || $drivehash->{$vs}) { | |
762 | eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); }; | |
763 | if (my $err = $@) { | |
764 | die $err if !$force; | |
765 | warn $err; | |
766 | } | |
767 | } | |
768 | ||
769 | # save changes (remove drive from snapshot) | |
770 | $class->lock_config($vmid, $remove_drive, $vs) if !$force; | |
771 | }); | |
772 | ||
773 | # now cleanup config | |
774 | $class->lock_config($vmid, sub { | |
775 | my $conf = $class->load_config($vmid); | |
776 | $snap = $conf->{snapshots}->{$snapname}; | |
777 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
778 | ||
779 | # remove parent refs | |
780 | &$unlink_parent($conf, $snap->{parent}); | |
781 | foreach my $sn (keys %{$conf->{snapshots}}) { | |
782 | next if $sn eq $snapname; | |
783 | &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent}); | |
784 | } | |
785 | ||
786 | ||
787 | delete $conf->{snapshots}->{$snapname}; | |
788 | delete $conf->{lock}; | |
789 | foreach my $volid (@$unused) { | |
790 | $class->add_unused_volume($conf, $volid); | |
791 | } | |
792 | ||
793 | $class->write_config($vmid, $conf); | |
794 | }); | |
795 | } | |
796 | ||
797 | # Rolls back to a given snapshot. | |
798 | sub snapshot_rollback { | |
799 | my ($class, $vmid, $snapname) = @_; | |
800 | ||
801 | my $prepare = 1; | |
802 | ||
803 | my $storecfg = PVE::Storage::config(); | |
804 | ||
805 | my $conf = $class->load_config($vmid); | |
806 | ||
807 | my $get_snapshot_config = sub { | |
808 | ||
809 | die "you can't rollback if vm is a template\n" if $class->is_template($conf); | |
810 | ||
811 | my $res = $conf->{snapshots}->{$snapname}; | |
812 | ||
813 | die "snapshot '$snapname' does not exist\n" if !defined($res); | |
814 | ||
815 | return $res; | |
816 | }; | |
817 | ||
683a30e0 WL |
818 | my $repl_conf = PVE::ReplicationConfig->new(); |
819 | if ($repl_conf->check_for_existing_jobs($vmid, 1)) { | |
820 | # remove all replication snapshots | |
c324e907 | 821 | my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1); |
683a30e0 WL |
822 | my $sorted_volids = [ sort keys %$volumes ]; |
823 | ||
824 | # remove all local replication snapshots (jobid => undef) | |
825 | my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; }; | |
826 | PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc); | |
827 | } | |
828 | ||
58a3c91c FG |
829 | my $snap = &$get_snapshot_config(); |
830 | ||
831 | $class->__snapshot_foreach_volume($snap, sub { | |
832 | my ($vs, $volume) = @_; | |
833 | ||
834 | $class->__snapshot_rollback_vol_possible($volume, $snapname); | |
835 | }); | |
836 | ||
3fa4ce45 DC |
837 | my $data = {}; |
838 | ||
58a3c91c FG |
839 | my $updatefn = sub { |
840 | ||
841 | $conf = $class->load_config($vmid); | |
842 | ||
843 | $snap = &$get_snapshot_config(); | |
844 | ||
845 | die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" | |
846 | if $snap->{snapstate}; | |
847 | ||
848 | if ($prepare) { | |
849 | $class->check_lock($conf); | |
850 | $class->__snapshot_rollback_vm_stop($vmid); | |
851 | } | |
852 | ||
853 | die "unable to rollback vm $vmid: vm is running\n" | |
854 | if $class->__snapshot_check_running($vmid); | |
855 | ||
856 | if ($prepare) { | |
857 | $conf->{lock} = 'rollback'; | |
858 | } else { | |
859 | die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback'); | |
860 | delete $conf->{lock}; | |
58a3c91c | 861 | |
58a3c91c FG |
862 | my $unused = $class->__snapshot_rollback_get_unused($conf, $snap); |
863 | ||
864 | foreach my $volid (@$unused) { | |
865 | $class->add_unused_volume($conf, $volid); | |
866 | } | |
867 | ||
58a3c91c FG |
868 | # copy snapshot config to current config |
869 | $conf = $class->__snapshot_apply_config($conf, $snap); | |
870 | $conf->{parent} = $snapname; | |
58a3c91c FG |
871 | } |
872 | ||
3fa4ce45 DC |
873 | $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data); |
874 | ||
58a3c91c FG |
875 | $class->write_config($vmid, $conf); |
876 | ||
877 | if (!$prepare && $snap->{vmstate}) { | |
3fa4ce45 | 878 | $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data); |
58a3c91c FG |
879 | } |
880 | }; | |
881 | ||
882 | $class->lock_config($vmid, $updatefn); | |
883 | ||
884 | $class->__snapshot_foreach_volume($snap, sub { | |
885 | my ($vs, $volume) = @_; | |
886 | ||
887 | $class->__snapshot_rollback_vol_rollback($volume, $snapname); | |
888 | }); | |
889 | ||
890 | $prepare = 0; | |
891 | $class->lock_config($vmid, $updatefn); | |
892 | } | |
893 | ||
eb50bb61 RV |
894 | # bash completion helper |
895 | ||
90fba9cd WB |
896 | sub snapshot_list { |
897 | my ($class, $vmid) = @_; | |
eb50bb61 | 898 | |
90fba9cd WB |
899 | my $snapshot = eval { |
900 | my $conf = $class->load_config($vmid); | |
901 | my $snapshots = $conf->{snapshots} || []; | |
902 | [ sort keys %$snapshots ] | |
903 | } || []; | |
eb50bb61 RV |
904 | |
905 | return $snapshot; | |
906 | } | |
907 | ||
58a3c91c | 908 | 1; |