]>
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) = @_; | |
95 | join ",", map { ( $delete_hash->{$_}->{force} ? '!' : '' ) . $_ } keys %$delete_hash; | |
96 | } | |
97 | ||
98 | sub add_to_pending_delete { | |
99 | my ($class, $conf, $key, $force) = @_; | |
100 | ||
101 | delete $conf->{pending}->{$key}; | |
102 | my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete}); | |
103 | $pending_delete_hash->{$key}->{force} = $force; | |
104 | $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash); | |
105 | } | |
106 | ||
107 | sub remove_from_pending_delete { | |
108 | my ($class, $conf, $key) = @_; | |
109 | ||
110 | my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete}); | |
111 | delete $pending_delete_hash->{$key}; | |
112 | ||
113 | if (%$pending_delete_hash) { | |
114 | $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash); | |
115 | } else { | |
116 | delete $conf->{pending}->{delete}; | |
117 | } | |
118 | } | |
119 | ||
120 | sub cleanup_pending { | |
121 | my ($class, $conf) = @_; | |
122 | ||
123 | # remove pending changes when nothing changed | |
124 | my $changes; | |
125 | foreach my $opt (keys %{$conf->{pending}}) { | |
126 | next if $opt eq 'delete'; # just to be sure | |
127 | if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq $conf->{$opt})) { | |
128 | $changes = 1; | |
129 | delete $conf->{pending}->{$opt}; | |
130 | } | |
131 | } | |
132 | ||
133 | my $current_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete}); | |
134 | my $pending_delete_hash = {}; | |
135 | while (my ($opt, $force) = each %$current_delete_hash) { | |
136 | if (defined($conf->{$opt})) { | |
137 | $pending_delete_hash->{$opt} = $force; | |
138 | } else { | |
139 | $changes = 1; | |
140 | } | |
141 | } | |
142 | ||
143 | if (%$pending_delete_hash) { | |
144 | $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash); | |
145 | } else { | |
146 | delete $conf->{pending}->{delete}; | |
147 | } | |
148 | ||
149 | return $changes; | |
150 | } | |
151 | ||
61264e82 OB |
152 | sub load_snapshot_config { |
153 | my ($class, $vmid, $snapname) = @_; | |
154 | ||
155 | my $conf = $class->load_config($vmid); | |
156 | ||
157 | my $snapshot = $conf->{snapshots}->{$snapname}; | |
158 | die "snapshot '$snapname' does not exist\n" if !defined($snapshot); | |
159 | ||
160 | $snapshot->{digest} = $conf->{digest}; | |
161 | ||
162 | return $snapshot; | |
163 | ||
164 | } | |
165 | ||
166 | sub load_current_config { | |
167 | my ($class, $vmid, $current) = @_; | |
168 | ||
169 | my $conf = $class->load_config($vmid); | |
170 | ||
171 | # take pending changes in | |
172 | if (!$current) { | |
173 | foreach my $opt (keys %{$conf->{pending}}) { | |
174 | next if $opt eq 'delete'; | |
175 | my $value = $conf->{pending}->{$opt}; | |
176 | next if ref($value); # just to be sure | |
177 | $conf->{$opt} = $value; | |
178 | } | |
179 | my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete}); | |
180 | foreach my $opt (keys %$pending_delete_hash) { | |
181 | delete $conf->{$opt} if $conf->{$opt}; | |
182 | } | |
183 | } | |
184 | ||
185 | delete $conf->{snapshots}; | |
186 | delete $conf->{pending}; | |
187 | ||
188 | return $conf; | |
189 | } | |
190 | ||
191 | ||
58a3c91c FG |
192 | # Lock config file using flock, run $code with @param, unlock config file. |
193 | # $timeout is the maximum time to aquire the flock | |
194 | sub lock_config_full { | |
195 | my ($class, $vmid, $timeout, $code, @param) = @_; | |
196 | ||
197 | my $filename = $class->config_file_lock($vmid); | |
198 | ||
199 | my $res = lock_file($filename, $timeout, $code, @param); | |
200 | ||
201 | die $@ if $@; | |
202 | ||
203 | return $res; | |
204 | } | |
205 | ||
cbbb06a5 TL |
206 | sub create_and_lock_config { |
207 | my ($class, $vmid, $allow_existing) = @_; | |
208 | ||
209 | $class->lock_config_full($vmid, 5, sub { | |
210 | PVE::Cluster::check_vmid_unused($vmid, $allow_existing); | |
211 | ||
212 | my $conf = eval { $class->load_config($vmid) } || {}; | |
213 | $class->check_lock($conf); | |
214 | $conf->{lock} = 'create'; | |
215 | $class->write_config($vmid, $conf); | |
216 | }); | |
217 | } | |
218 | ||
1aa76f2f TL |
219 | # destroys configuration, only applyable for configs owned by the callers node. |
220 | # dies if removal fails, e.g., when inquorate. | |
221 | sub destroy_config { | |
222 | my ($class, $vmid) = @_; | |
223 | ||
224 | my $config_fn = $class->config_file($vmid, $nodename); | |
225 | unlink $config_fn or die "failed to remove config file: $!\n"; | |
226 | } | |
227 | ||
58a3c91c FG |
228 | # Lock config file using flock, run $code with @param, unlock config file. |
229 | # $timeout is the maximum time to aquire the flock | |
230 | # $shared eq 1 creates a non-exclusive ("read") flock | |
231 | sub lock_config_mode { | |
232 | my ($class, $vmid, $timeout, $shared, $code, @param) = @_; | |
233 | ||
234 | my $filename = $class->config_file_lock($vmid); | |
235 | ||
236 | my $res = lock_file_full($filename, $timeout, $shared, $code, @param); | |
237 | ||
238 | die $@ if $@; | |
239 | ||
240 | return $res; | |
241 | } | |
242 | ||
243 | # Lock config file using flock, run $code with @param, unlock config file. | |
244 | sub lock_config { | |
245 | my ($class, $vmid, $code, @param) = @_; | |
246 | ||
247 | return $class->lock_config_full($vmid, 10, $code, @param); | |
248 | } | |
249 | ||
250 | # Checks whether the config is locked with the lock parameter | |
251 | sub check_lock { | |
252 | my ($class, $conf) = @_; | |
253 | ||
254 | die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock}; | |
255 | } | |
256 | ||
257 | # Returns whether the config is locked with the lock parameter, also checks | |
258 | # whether the lock value is correct if the optional $lock is set. | |
259 | sub has_lock { | |
260 | my ($class, $conf, $lock) = @_; | |
261 | ||
262 | return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock}); | |
263 | } | |
264 | ||
265 | # Sets the lock parameter for this VM/CT's config to $lock. | |
266 | sub set_lock { | |
267 | my ($class, $vmid, $lock) = @_; | |
268 | ||
269 | my $conf; | |
270 | $class->lock_config($vmid, sub { | |
271 | $conf = $class->load_config($vmid); | |
272 | $class->check_lock($conf); | |
273 | $conf->{lock} = $lock; | |
274 | $class->write_config($vmid, $conf); | |
275 | }); | |
276 | return $conf; | |
277 | } | |
278 | ||
279 | # Removes the lock parameter for this VM/CT's config, also checks whether | |
280 | # the lock value is correct if the optional $lock is set. | |
281 | sub remove_lock { | |
282 | my ($class, $vmid, $lock) = @_; | |
283 | ||
284 | $class->lock_config($vmid, sub { | |
285 | my $conf = $class->load_config($vmid); | |
286 | if (!$conf->{lock}) { | |
287 | my $lockstring = defined($lock) ? "'$lock' " : "any"; | |
288 | die "no lock found trying to remove $lockstring lock\n"; | |
289 | } elsif (defined($lock) && $conf->{lock} ne $lock) { | |
290 | die "found lock '$conf->{lock}' trying to remove '$lock' lock\n"; | |
291 | } | |
292 | delete $conf->{lock}; | |
293 | $class->write_config($vmid, $conf); | |
294 | }); | |
295 | } | |
296 | ||
297 | # Checks whether protection mode is enabled for this VM/CT. | |
298 | sub check_protection { | |
299 | my ($class, $conf, $err_msg) = @_; | |
300 | ||
301 | if ($conf->{protection}) { | |
302 | die "$err_msg - protection mode enabled\n"; | |
303 | } | |
304 | } | |
305 | ||
306 | # Adds an unused volume to $config, if possible. | |
307 | sub add_unused_volume { | |
308 | my ($class, $config, $volid) = @_; | |
309 | ||
310 | my $key; | |
311 | for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) { | |
312 | my $test = "unused$ind"; | |
313 | if (my $vid = $config->{$test}) { | |
314 | return if $vid eq $volid; # do not add duplicates | |
315 | } else { | |
316 | $key = $test; | |
317 | } | |
318 | } | |
319 | ||
320 | die "Too many unused volumes - please delete them first.\n" if !$key; | |
321 | ||
322 | $config->{$key} = $volid; | |
323 | ||
324 | return $key; | |
325 | } | |
326 | ||
327 | # Returns whether the template parameter is set in $conf. | |
328 | sub is_template { | |
329 | my ($class, $conf) = @_; | |
330 | ||
331 | return 1 if defined $conf->{template} && $conf->{template} == 1; | |
332 | } | |
333 | ||
334 | # Checks whether $feature is availabe for the referenced volumes in $conf. | |
335 | # Note: depending on the parameters, some volumes may be skipped! | |
336 | sub has_feature { | |
337 | my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_; | |
338 | die "implement me - abstract method\n"; | |
339 | } | |
340 | ||
3f85b14d DM |
341 | # get all replicatable volume (hash $res->{$volid} = 1) |
342 | # $cleanup: for cleanup - simply ignores volumes without replicate feature | |
343 | # $norerr: never raise exceptions - return undef instead | |
344 | sub get_replicatable_volumes { | |
54b79ff5 | 345 | my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_; |
3f85b14d DM |
346 | |
347 | die "implement me - abstract method\n"; | |
348 | } | |
349 | ||
58a3c91c FG |
350 | # Internal snapshots |
351 | ||
352 | # NOTE: Snapshot create/delete involves several non-atomic | |
353 | # actions, and can take a long time. | |
354 | # So we try to avoid locking the file and use the 'lock' variable | |
355 | # inside the config file instead. | |
356 | ||
357 | # Save the vmstate (RAM). | |
358 | sub __snapshot_save_vmstate { | |
359 | my ($class, $vmid, $conf, $snapname, $storecfg) = @_; | |
360 | die "implement me - abstract method\n"; | |
361 | } | |
362 | ||
363 | # Check whether the VM/CT is running. | |
364 | sub __snapshot_check_running { | |
365 | my ($class, $vmid) = @_; | |
366 | die "implement me - abstract method\n"; | |
367 | } | |
368 | ||
369 | # Check whether we need to freeze the VM/CT | |
370 | sub __snapshot_check_freeze_needed { | |
371 | my ($sself, $vmid, $config, $save_vmstate) = @_; | |
372 | die "implement me - abstract method\n"; | |
373 | } | |
374 | ||
375 | # Freeze or unfreeze the VM/CT. | |
376 | sub __snapshot_freeze { | |
377 | my ($class, $vmid, $unfreeze) = @_; | |
378 | ||
379 | die "abstract method - implement me\n"; | |
380 | } | |
381 | ||
382 | # Code run before and after creating all the volume snapshots | |
383 | # base: noop | |
384 | sub __snapshot_create_vol_snapshots_hook { | |
385 | my ($class, $vmid, $snap, $running, $hook) = @_; | |
386 | ||
387 | return; | |
388 | } | |
389 | ||
390 | # Create the volume snapshots for the VM/CT. | |
391 | sub __snapshot_create_vol_snapshot { | |
392 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
393 | ||
394 | die "abstract method - implement me\n"; | |
395 | } | |
396 | ||
397 | # Remove a drive from the snapshot config. | |
398 | sub __snapshot_delete_remove_drive { | |
399 | my ($class, $snap, $drive) = @_; | |
400 | ||
401 | die "abstract method - implement me\n"; | |
402 | } | |
403 | ||
404 | # Delete the vmstate file/drive | |
405 | sub __snapshot_delete_vmstate_file { | |
406 | my ($class, $snap, $force) = @_; | |
407 | ||
408 | die "abstract method - implement me\n"; | |
409 | } | |
410 | ||
411 | # Delete a volume snapshot | |
412 | sub __snapshot_delete_vol_snapshot { | |
413 | my ($class, $vmid, $vs, $volume, $snapname) = @_; | |
414 | ||
415 | die "abstract method - implement me\n"; | |
416 | } | |
417 | ||
3fa4ce45 DC |
418 | # called during rollback prepare, and between config rollback and starting guest |
419 | # can change config, e.g. for vmgenid | |
420 | # $data is shared across calls and passed to vm_start | |
421 | sub __snapshot_rollback_hook { | |
422 | my ($class, $vmid, $conf, $snap, $prepare, $data) = @_; | |
423 | ||
424 | return; | |
425 | } | |
426 | ||
58a3c91c FG |
427 | # Checks whether a volume snapshot is possible for this volume. |
428 | sub __snapshot_rollback_vol_possible { | |
429 | my ($class, $volume, $snapname) = @_; | |
430 | ||
431 | die "abstract method - implement me\n"; | |
432 | } | |
433 | ||
434 | # Rolls back this volume. | |
435 | sub __snapshot_rollback_vol_rollback { | |
436 | my ($class, $volume, $snapname) = @_; | |
437 | ||
438 | die "abstract method - implement me\n"; | |
439 | } | |
440 | ||
441 | # Stops the VM/CT for a rollback. | |
442 | sub __snapshot_rollback_vm_stop { | |
443 | my ($class, $vmid) = @_; | |
444 | ||
445 | die "abstract method - implement me\n"; | |
446 | } | |
447 | ||
448 | # Start the VM/CT after a rollback with restored vmstate. | |
449 | sub __snapshot_rollback_vm_start { | |
3fa4ce45 | 450 | my ($class, $vmid, $vmstate, $data); |
58a3c91c FG |
451 | |
452 | die "abstract method - implement me\n"; | |
453 | } | |
454 | ||
455 | # Get list of volume IDs which are referenced in $conf, but not in $snap. | |
456 | sub __snapshot_rollback_get_unused { | |
457 | my ($class, $conf, $snap) = @_; | |
458 | ||
459 | die "abstract method - implement me\n"; | |
460 | } | |
461 | ||
462 | # Iterate over all configured volumes, calling $func for each key/value pair. | |
463 | sub __snapshot_foreach_volume { | |
464 | my ($class, $conf, $func) = @_; | |
465 | ||
466 | die "abstract method - implement me\n"; | |
467 | } | |
468 | ||
469 | # Copy the current config $source to the snapshot config $dest | |
470 | sub __snapshot_copy_config { | |
471 | my ($class, $source, $dest) = @_; | |
472 | ||
473 | foreach my $k (keys %$source) { | |
474 | next if $k eq 'snapshots'; | |
475 | next if $k eq 'snapstate'; | |
476 | next if $k eq 'snaptime'; | |
477 | next if $k eq 'vmstate'; | |
478 | next if $k eq 'lock'; | |
479 | next if $k eq 'digest'; | |
480 | next if $k eq 'description'; | |
481 | next if $k =~ m/^unused\d+$/; | |
482 | ||
483 | $dest->{$k} = $source->{$k}; | |
484 | } | |
485 | }; | |
486 | ||
487 | # Apply the snapshot config $snap to the config $conf (rollback) | |
488 | sub __snapshot_apply_config { | |
489 | my ($class, $conf, $snap) = @_; | |
490 | ||
491 | # copy snapshot list | |
492 | my $newconf = { | |
493 | snapshots => $conf->{snapshots}, | |
494 | }; | |
495 | ||
eb74d483 | 496 | # keep description and list of unused disks |
58a3c91c | 497 | foreach my $k (keys %$conf) { |
eb74d483 | 498 | next if !($k =~ m/^unused\d+$/ || $k eq 'description'); |
38532f70 | 499 | |
58a3c91c FG |
500 | $newconf->{$k} = $conf->{$k}; |
501 | } | |
502 | ||
503 | $class->__snapshot_copy_config($snap, $newconf); | |
504 | ||
505 | return $newconf; | |
506 | } | |
507 | ||
508 | # Prepares the configuration for snapshotting. | |
509 | sub __snapshot_prepare { | |
510 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
511 | ||
512 | my $snap; | |
513 | ||
514 | my $updatefn = sub { | |
515 | ||
516 | my $conf = $class->load_config($vmid); | |
517 | ||
518 | die "you can't take a snapshot if it's a template\n" | |
519 | if $class->is_template($conf); | |
520 | ||
521 | $class->check_lock($conf); | |
522 | ||
523 | $conf->{lock} = 'snapshot'; | |
524 | ||
525 | die "snapshot name '$snapname' already used\n" | |
526 | if defined($conf->{snapshots}->{$snapname}); | |
527 | ||
528 | my $storecfg = PVE::Storage::config(); | |
529 | die "snapshot feature is not available\n" | |
530 | if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump'); | |
531 | ||
532 | $snap = $conf->{snapshots}->{$snapname} = {}; | |
533 | ||
534 | if ($save_vmstate && $class->__snapshot_check_running($vmid)) { | |
535 | $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg); | |
536 | } | |
537 | ||
538 | $class->__snapshot_copy_config($conf, $snap); | |
539 | ||
540 | $snap->{snapstate} = "prepare"; | |
541 | $snap->{snaptime} = time(); | |
542 | $snap->{description} = $comment if $comment; | |
543 | ||
544 | $class->write_config($vmid, $conf); | |
545 | }; | |
546 | ||
547 | $class->lock_config($vmid, $updatefn); | |
548 | ||
549 | return $snap; | |
550 | } | |
551 | ||
552 | # Commits the configuration after snapshotting. | |
553 | sub __snapshot_commit { | |
554 | my ($class, $vmid, $snapname) = @_; | |
555 | ||
556 | my $updatefn = sub { | |
557 | ||
558 | my $conf = $class->load_config($vmid); | |
559 | ||
560 | die "missing snapshot lock\n" | |
561 | if !($conf->{lock} && $conf->{lock} eq 'snapshot'); | |
562 | ||
563 | my $snap = $conf->{snapshots}->{$snapname}; | |
564 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
565 | ||
566 | die "wrong snapshot state\n" | |
567 | if !($snap->{snapstate} && $snap->{snapstate} eq "prepare"); | |
568 | ||
569 | delete $snap->{snapstate}; | |
570 | delete $conf->{lock}; | |
571 | ||
572 | $conf->{parent} = $snapname; | |
573 | ||
574 | $class->write_config($vmid, $conf); | |
575 | }; | |
576 | ||
577 | $class->lock_config($vmid, $updatefn); | |
578 | } | |
579 | ||
580 | # Creates a snapshot for the VM/CT. | |
581 | sub snapshot_create { | |
582 | my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; | |
583 | ||
584 | my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment); | |
585 | ||
586 | $save_vmstate = 0 if !$snap->{vmstate}; | |
587 | ||
588 | my $conf = $class->load_config($vmid); | |
589 | ||
590 | my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate}); | |
591 | ||
592 | my $drivehash = {}; | |
593 | ||
594 | eval { | |
595 | if ($freezefs) { | |
596 | $class->__snapshot_freeze($vmid, 0); | |
597 | } | |
598 | ||
599 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before"); | |
600 | ||
601 | $class->__snapshot_foreach_volume($snap, sub { | |
602 | my ($vs, $volume) = @_; | |
603 | ||
604 | $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname); | |
605 | $drivehash->{$vs} = 1; | |
606 | }); | |
607 | }; | |
608 | my $err = $@; | |
609 | ||
610 | if ($running) { | |
611 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after"); | |
612 | if ($freezefs) { | |
613 | $class->__snapshot_freeze($vmid, 1); | |
614 | } | |
615 | $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze"); | |
616 | } | |
617 | ||
618 | if ($err) { | |
619 | warn "snapshot create failed: starting cleanup\n"; | |
620 | eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); }; | |
621 | warn "$@" if $@; | |
622 | die "$err\n"; | |
623 | } | |
624 | ||
625 | $class->__snapshot_commit($vmid, $snapname); | |
626 | } | |
627 | ||
628 | # Deletes a snapshot. | |
629 | # Note: $drivehash is only set when called from snapshot_create. | |
630 | sub snapshot_delete { | |
631 | my ($class, $vmid, $snapname, $force, $drivehash) = @_; | |
632 | ||
633 | my $prepare = 1; | |
634 | ||
58a3c91c FG |
635 | my $unused = []; |
636 | ||
4c3144ea AA |
637 | my $conf = $class->load_config($vmid); |
638 | my $snap = $conf->{snapshots}->{$snapname}; | |
639 | ||
640 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
641 | ||
58a3c91c FG |
642 | $class->set_lock($vmid, 'snapshot-delete') |
643 | if (!$drivehash); # doesn't already have a 'snapshot' lock | |
644 | ||
645 | my $unlink_parent = sub { | |
646 | my ($confref, $new_parent) = @_; | |
647 | ||
648 | if ($confref->{parent} && $confref->{parent} eq $snapname) { | |
649 | if ($new_parent) { | |
650 | $confref->{parent} = $new_parent; | |
651 | } else { | |
652 | delete $confref->{parent}; | |
653 | } | |
654 | } | |
655 | }; | |
656 | ||
657 | my $remove_drive = sub { | |
658 | my ($drive) = @_; | |
659 | ||
660 | my $conf = $class->load_config($vmid); | |
661 | $snap = $conf->{snapshots}->{$snapname}; | |
662 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
663 | ||
664 | $class->__snapshot_delete_remove_drive($snap, $drive); | |
665 | ||
666 | $class->write_config($vmid, $conf); | |
667 | }; | |
668 | ||
669 | #prepare | |
670 | $class->lock_config($vmid, sub { | |
671 | my $conf = $class->load_config($vmid); | |
672 | ||
673 | die "you can't delete a snapshot if vm is a template\n" | |
674 | if $class->is_template($conf); | |
675 | ||
676 | $snap = $conf->{snapshots}->{$snapname}; | |
677 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
678 | ||
679 | $snap->{snapstate} = 'delete'; | |
680 | ||
681 | $class->write_config($vmid, $conf); | |
682 | }); | |
683 | ||
684 | # now remove vmstate file | |
685 | if ($snap->{vmstate}) { | |
686 | $class->__snapshot_delete_vmstate_file($snap, $force); | |
687 | ||
688 | # save changes (remove vmstate from snapshot) | |
689 | $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force; | |
690 | }; | |
691 | ||
692 | # now remove all volume snapshots | |
693 | $class->__snapshot_foreach_volume($snap, sub { | |
694 | my ($vs, $volume) = @_; | |
695 | ||
696 | return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup}; | |
697 | if (!$drivehash || $drivehash->{$vs}) { | |
698 | eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); }; | |
699 | if (my $err = $@) { | |
700 | die $err if !$force; | |
701 | warn $err; | |
702 | } | |
703 | } | |
704 | ||
705 | # save changes (remove drive from snapshot) | |
706 | $class->lock_config($vmid, $remove_drive, $vs) if !$force; | |
707 | }); | |
708 | ||
709 | # now cleanup config | |
710 | $class->lock_config($vmid, sub { | |
711 | my $conf = $class->load_config($vmid); | |
712 | $snap = $conf->{snapshots}->{$snapname}; | |
713 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
714 | ||
715 | # remove parent refs | |
716 | &$unlink_parent($conf, $snap->{parent}); | |
717 | foreach my $sn (keys %{$conf->{snapshots}}) { | |
718 | next if $sn eq $snapname; | |
719 | &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent}); | |
720 | } | |
721 | ||
722 | ||
723 | delete $conf->{snapshots}->{$snapname}; | |
724 | delete $conf->{lock}; | |
725 | foreach my $volid (@$unused) { | |
726 | $class->add_unused_volume($conf, $volid); | |
727 | } | |
728 | ||
729 | $class->write_config($vmid, $conf); | |
730 | }); | |
731 | } | |
732 | ||
733 | # Rolls back to a given snapshot. | |
734 | sub snapshot_rollback { | |
735 | my ($class, $vmid, $snapname) = @_; | |
736 | ||
737 | my $prepare = 1; | |
738 | ||
739 | my $storecfg = PVE::Storage::config(); | |
740 | ||
741 | my $conf = $class->load_config($vmid); | |
742 | ||
743 | my $get_snapshot_config = sub { | |
744 | ||
745 | die "you can't rollback if vm is a template\n" if $class->is_template($conf); | |
746 | ||
747 | my $res = $conf->{snapshots}->{$snapname}; | |
748 | ||
749 | die "snapshot '$snapname' does not exist\n" if !defined($res); | |
750 | ||
751 | return $res; | |
752 | }; | |
753 | ||
683a30e0 WL |
754 | my $repl_conf = PVE::ReplicationConfig->new(); |
755 | if ($repl_conf->check_for_existing_jobs($vmid, 1)) { | |
756 | # remove all replication snapshots | |
c324e907 | 757 | my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1); |
683a30e0 WL |
758 | my $sorted_volids = [ sort keys %$volumes ]; |
759 | ||
760 | # remove all local replication snapshots (jobid => undef) | |
761 | my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; }; | |
762 | PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc); | |
763 | } | |
764 | ||
58a3c91c FG |
765 | my $snap = &$get_snapshot_config(); |
766 | ||
767 | $class->__snapshot_foreach_volume($snap, sub { | |
768 | my ($vs, $volume) = @_; | |
769 | ||
770 | $class->__snapshot_rollback_vol_possible($volume, $snapname); | |
771 | }); | |
772 | ||
3fa4ce45 DC |
773 | my $data = {}; |
774 | ||
58a3c91c FG |
775 | my $updatefn = sub { |
776 | ||
777 | $conf = $class->load_config($vmid); | |
778 | ||
779 | $snap = &$get_snapshot_config(); | |
780 | ||
781 | die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" | |
782 | if $snap->{snapstate}; | |
783 | ||
784 | if ($prepare) { | |
785 | $class->check_lock($conf); | |
786 | $class->__snapshot_rollback_vm_stop($vmid); | |
787 | } | |
788 | ||
789 | die "unable to rollback vm $vmid: vm is running\n" | |
790 | if $class->__snapshot_check_running($vmid); | |
791 | ||
792 | if ($prepare) { | |
793 | $conf->{lock} = 'rollback'; | |
794 | } else { | |
795 | die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback'); | |
796 | delete $conf->{lock}; | |
58a3c91c | 797 | |
58a3c91c FG |
798 | my $unused = $class->__snapshot_rollback_get_unused($conf, $snap); |
799 | ||
800 | foreach my $volid (@$unused) { | |
801 | $class->add_unused_volume($conf, $volid); | |
802 | } | |
803 | ||
58a3c91c FG |
804 | # copy snapshot config to current config |
805 | $conf = $class->__snapshot_apply_config($conf, $snap); | |
806 | $conf->{parent} = $snapname; | |
58a3c91c FG |
807 | } |
808 | ||
3fa4ce45 DC |
809 | $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data); |
810 | ||
58a3c91c FG |
811 | $class->write_config($vmid, $conf); |
812 | ||
813 | if (!$prepare && $snap->{vmstate}) { | |
3fa4ce45 | 814 | $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data); |
58a3c91c FG |
815 | } |
816 | }; | |
817 | ||
818 | $class->lock_config($vmid, $updatefn); | |
819 | ||
820 | $class->__snapshot_foreach_volume($snap, sub { | |
821 | my ($vs, $volume) = @_; | |
822 | ||
823 | $class->__snapshot_rollback_vol_rollback($volume, $snapname); | |
824 | }); | |
825 | ||
826 | $prepare = 0; | |
827 | $class->lock_config($vmid, $updatefn); | |
828 | } | |
829 | ||
eb50bb61 RV |
830 | # bash completion helper |
831 | ||
90fba9cd WB |
832 | sub snapshot_list { |
833 | my ($class, $vmid) = @_; | |
eb50bb61 | 834 | |
90fba9cd WB |
835 | my $snapshot = eval { |
836 | my $conf = $class->load_config($vmid); | |
837 | my $snapshots = $conf->{snapshots} || []; | |
838 | [ sort keys %$snapshots ] | |
839 | } || []; | |
eb50bb61 RV |
840 | |
841 | return $snapshot; | |
842 | } | |
843 | ||
58a3c91c | 844 | 1; |