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