]> git.proxmox.com Git - pve-guest-common.git/blame - PVE/AbstractConfig.pm
Make rollback compatible with storage replica.
[pve-guest-common.git] / PVE / AbstractConfig.pm
CommitLineData
58a3c91c
FG
1package PVE::AbstractConfig;
2
3use strict;
4use warnings;
5
6use PVE::Tools qw(lock_file lock_file_full);
7use PVE::INotify;
8use PVE::Cluster;
9use PVE::Storage;
10
11my $nodename = PVE::INotify::nodename();
12
13# Printable string, currently either "VM" or "CT"
14sub guest_type {
15 my ($class) = @_;
16 die "abstract method - implement me ";
17}
18
19sub __config_max_unused_disks {
20 my ($class) = @_;
21
22 die "implement me - abstract method\n";
23}
24
25# Path to the flock file for this VM/CT
26sub config_file_lock {
27 my ($class, $vmid) = @_;
28 die "abstract method - implement me";
29}
30
31# Relative config file path for this VM/CT in CFS
32sub cfs_config_path {
33 my ($class, $vmid, $node) = @_;
34 die "abstract method - implement me";
35}
36
37# Absolute config file path for this VM/CT
38sub config_file {
39 my ($class, $vmid, $node) = @_;
40
41 my $cfspath = $class->cfs_config_path($vmid, $node);
42 return "/etc/pve/$cfspath";
43}
44
45# Read and parse config file for this VM/CT
46sub load_config {
47 my ($class, $vmid, $node) = @_;
48
49 $node = $nodename if !$node;
50 my $cfspath = $class->cfs_config_path($vmid, $node);
51
52 my $conf = PVE::Cluster::cfs_read_file($cfspath);
53 die "Configuration file '$cfspath' does not exist\n"
54 if !defined($conf);
55
56 return $conf;
57}
58
59# Generate and write config file for this VM/CT
60sub write_config {
61 my ($class, $vmid, $conf) = @_;
62
63 my $cfspath = $class->cfs_config_path($vmid);
64
65 PVE::Cluster::cfs_write_file($cfspath, $conf);
66}
67
68# Lock config file using flock, run $code with @param, unlock config file.
69# $timeout is the maximum time to aquire the flock
70sub lock_config_full {
71 my ($class, $vmid, $timeout, $code, @param) = @_;
72
73 my $filename = $class->config_file_lock($vmid);
74
75 my $res = lock_file($filename, $timeout, $code, @param);
76
77 die $@ if $@;
78
79 return $res;
80}
81
82# Lock config file using flock, run $code with @param, unlock config file.
83# $timeout is the maximum time to aquire the flock
84# $shared eq 1 creates a non-exclusive ("read") flock
85sub lock_config_mode {
86 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
87
88 my $filename = $class->config_file_lock($vmid);
89
90 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
91
92 die $@ if $@;
93
94 return $res;
95}
96
97# Lock config file using flock, run $code with @param, unlock config file.
98sub lock_config {
99 my ($class, $vmid, $code, @param) = @_;
100
101 return $class->lock_config_full($vmid, 10, $code, @param);
102}
103
104# Checks whether the config is locked with the lock parameter
105sub check_lock {
106 my ($class, $conf) = @_;
107
108 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
109}
110
111# Returns whether the config is locked with the lock parameter, also checks
112# whether the lock value is correct if the optional $lock is set.
113sub has_lock {
114 my ($class, $conf, $lock) = @_;
115
116 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
117}
118
119# Sets the lock parameter for this VM/CT's config to $lock.
120sub set_lock {
121 my ($class, $vmid, $lock) = @_;
122
123 my $conf;
124 $class->lock_config($vmid, sub {
125 $conf = $class->load_config($vmid);
126 $class->check_lock($conf);
127 $conf->{lock} = $lock;
128 $class->write_config($vmid, $conf);
129 });
130 return $conf;
131}
132
133# Removes the lock parameter for this VM/CT's config, also checks whether
134# the lock value is correct if the optional $lock is set.
135sub remove_lock {
136 my ($class, $vmid, $lock) = @_;
137
138 $class->lock_config($vmid, sub {
139 my $conf = $class->load_config($vmid);
140 if (!$conf->{lock}) {
141 my $lockstring = defined($lock) ? "'$lock' " : "any";
142 die "no lock found trying to remove $lockstring lock\n";
143 } elsif (defined($lock) && $conf->{lock} ne $lock) {
144 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
145 }
146 delete $conf->{lock};
147 $class->write_config($vmid, $conf);
148 });
149}
150
151# Checks whether protection mode is enabled for this VM/CT.
152sub check_protection {
153 my ($class, $conf, $err_msg) = @_;
154
155 if ($conf->{protection}) {
156 die "$err_msg - protection mode enabled\n";
157 }
158}
159
160# Adds an unused volume to $config, if possible.
161sub add_unused_volume {
162 my ($class, $config, $volid) = @_;
163
164 my $key;
165 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
166 my $test = "unused$ind";
167 if (my $vid = $config->{$test}) {
168 return if $vid eq $volid; # do not add duplicates
169 } else {
170 $key = $test;
171 }
172 }
173
174 die "Too many unused volumes - please delete them first.\n" if !$key;
175
176 $config->{$key} = $volid;
177
178 return $key;
179}
180
181# Returns whether the template parameter is set in $conf.
182sub is_template {
183 my ($class, $conf) = @_;
184
185 return 1 if defined $conf->{template} && $conf->{template} == 1;
186}
187
188# Checks whether $feature is availabe for the referenced volumes in $conf.
189# Note: depending on the parameters, some volumes may be skipped!
190sub has_feature {
191 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
192 die "implement me - abstract method\n";
193}
194
3f85b14d
DM
195# get all replicatable volume (hash $res->{$volid} = 1)
196# $cleanup: for cleanup - simply ignores volumes without replicate feature
197# $norerr: never raise exceptions - return undef instead
198sub get_replicatable_volumes {
199 my ($class, $storecfg, $conf, $cleanup, $noerr) = @_;
200
201 die "implement me - abstract method\n";
202}
203
58a3c91c
FG
204# Internal snapshots
205
206# NOTE: Snapshot create/delete involves several non-atomic
207# actions, and can take a long time.
208# So we try to avoid locking the file and use the 'lock' variable
209# inside the config file instead.
210
211# Save the vmstate (RAM).
212sub __snapshot_save_vmstate {
213 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
214 die "implement me - abstract method\n";
215}
216
217# Check whether the VM/CT is running.
218sub __snapshot_check_running {
219 my ($class, $vmid) = @_;
220 die "implement me - abstract method\n";
221}
222
223# Check whether we need to freeze the VM/CT
224sub __snapshot_check_freeze_needed {
225 my ($sself, $vmid, $config, $save_vmstate) = @_;
226 die "implement me - abstract method\n";
227}
228
229# Freeze or unfreeze the VM/CT.
230sub __snapshot_freeze {
231 my ($class, $vmid, $unfreeze) = @_;
232
233 die "abstract method - implement me\n";
234}
235
236# Code run before and after creating all the volume snapshots
237# base: noop
238sub __snapshot_create_vol_snapshots_hook {
239 my ($class, $vmid, $snap, $running, $hook) = @_;
240
241 return;
242}
243
244# Create the volume snapshots for the VM/CT.
245sub __snapshot_create_vol_snapshot {
246 my ($class, $vmid, $vs, $volume, $snapname) = @_;
247
248 die "abstract method - implement me\n";
249}
250
251# Remove a drive from the snapshot config.
252sub __snapshot_delete_remove_drive {
253 my ($class, $snap, $drive) = @_;
254
255 die "abstract method - implement me\n";
256}
257
258# Delete the vmstate file/drive
259sub __snapshot_delete_vmstate_file {
260 my ($class, $snap, $force) = @_;
261
262 die "abstract method - implement me\n";
263}
264
265# Delete a volume snapshot
266sub __snapshot_delete_vol_snapshot {
267 my ($class, $vmid, $vs, $volume, $snapname) = @_;
268
269 die "abstract method - implement me\n";
270}
271
272# Checks whether a volume snapshot is possible for this volume.
273sub __snapshot_rollback_vol_possible {
274 my ($class, $volume, $snapname) = @_;
275
276 die "abstract method - implement me\n";
277}
278
279# Rolls back this volume.
280sub __snapshot_rollback_vol_rollback {
281 my ($class, $volume, $snapname) = @_;
282
283 die "abstract method - implement me\n";
284}
285
286# Stops the VM/CT for a rollback.
287sub __snapshot_rollback_vm_stop {
288 my ($class, $vmid) = @_;
289
290 die "abstract method - implement me\n";
291}
292
293# Start the VM/CT after a rollback with restored vmstate.
294sub __snapshot_rollback_vm_start {
295 my ($class, $vmid, $vmstate, $forcemachine);
296
297 die "abstract method - implement me\n";
298}
299
300# Get list of volume IDs which are referenced in $conf, but not in $snap.
301sub __snapshot_rollback_get_unused {
302 my ($class, $conf, $snap) = @_;
303
304 die "abstract method - implement me\n";
305}
306
307# Iterate over all configured volumes, calling $func for each key/value pair.
308sub __snapshot_foreach_volume {
309 my ($class, $conf, $func) = @_;
310
311 die "abstract method - implement me\n";
312}
313
314# Copy the current config $source to the snapshot config $dest
315sub __snapshot_copy_config {
316 my ($class, $source, $dest) = @_;
317
318 foreach my $k (keys %$source) {
319 next if $k eq 'snapshots';
320 next if $k eq 'snapstate';
321 next if $k eq 'snaptime';
322 next if $k eq 'vmstate';
323 next if $k eq 'lock';
324 next if $k eq 'digest';
325 next if $k eq 'description';
326 next if $k =~ m/^unused\d+$/;
327
328 $dest->{$k} = $source->{$k};
329 }
330};
331
332# Apply the snapshot config $snap to the config $conf (rollback)
333sub __snapshot_apply_config {
334 my ($class, $conf, $snap) = @_;
335
336 # copy snapshot list
337 my $newconf = {
338 snapshots => $conf->{snapshots},
339 };
340
eb74d483 341 # keep description and list of unused disks
58a3c91c 342 foreach my $k (keys %$conf) {
eb74d483 343 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
38532f70 344
58a3c91c
FG
345 $newconf->{$k} = $conf->{$k};
346 }
347
348 $class->__snapshot_copy_config($snap, $newconf);
349
350 return $newconf;
351}
352
353# Prepares the configuration for snapshotting.
354sub __snapshot_prepare {
355 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
356
357 my $snap;
358
359 my $updatefn = sub {
360
361 my $conf = $class->load_config($vmid);
362
363 die "you can't take a snapshot if it's a template\n"
364 if $class->is_template($conf);
365
366 $class->check_lock($conf);
367
368 $conf->{lock} = 'snapshot';
369
370 die "snapshot name '$snapname' already used\n"
371 if defined($conf->{snapshots}->{$snapname});
372
373 my $storecfg = PVE::Storage::config();
374 die "snapshot feature is not available\n"
375 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
376
377 $snap = $conf->{snapshots}->{$snapname} = {};
378
379 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
380 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
381 }
382
383 $class->__snapshot_copy_config($conf, $snap);
384
385 $snap->{snapstate} = "prepare";
386 $snap->{snaptime} = time();
387 $snap->{description} = $comment if $comment;
388
389 $class->write_config($vmid, $conf);
390 };
391
392 $class->lock_config($vmid, $updatefn);
393
394 return $snap;
395}
396
397# Commits the configuration after snapshotting.
398sub __snapshot_commit {
399 my ($class, $vmid, $snapname) = @_;
400
401 my $updatefn = sub {
402
403 my $conf = $class->load_config($vmid);
404
405 die "missing snapshot lock\n"
406 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
407
408 my $snap = $conf->{snapshots}->{$snapname};
409 die "snapshot '$snapname' does not exist\n" if !defined($snap);
410
411 die "wrong snapshot state\n"
412 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
413
414 delete $snap->{snapstate};
415 delete $conf->{lock};
416
417 $conf->{parent} = $snapname;
418
419 $class->write_config($vmid, $conf);
420 };
421
422 $class->lock_config($vmid, $updatefn);
423}
424
425# Creates a snapshot for the VM/CT.
426sub snapshot_create {
427 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
428
429 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
430
431 $save_vmstate = 0 if !$snap->{vmstate};
432
433 my $conf = $class->load_config($vmid);
434
435 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
436
437 my $drivehash = {};
438
439 eval {
440 if ($freezefs) {
441 $class->__snapshot_freeze($vmid, 0);
442 }
443
444 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
445
446 $class->__snapshot_foreach_volume($snap, sub {
447 my ($vs, $volume) = @_;
448
449 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
450 $drivehash->{$vs} = 1;
451 });
452 };
453 my $err = $@;
454
455 if ($running) {
456 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
457 if ($freezefs) {
458 $class->__snapshot_freeze($vmid, 1);
459 }
460 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
461 }
462
463 if ($err) {
464 warn "snapshot create failed: starting cleanup\n";
465 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
466 warn "$@" if $@;
467 die "$err\n";
468 }
469
470 $class->__snapshot_commit($vmid, $snapname);
471}
472
473# Deletes a snapshot.
474# Note: $drivehash is only set when called from snapshot_create.
475sub snapshot_delete {
476 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
477
478 my $prepare = 1;
479
480 my $snap;
481 my $unused = [];
482
483 $class->set_lock($vmid, 'snapshot-delete')
484 if (!$drivehash); # doesn't already have a 'snapshot' lock
485
486 my $unlink_parent = sub {
487 my ($confref, $new_parent) = @_;
488
489 if ($confref->{parent} && $confref->{parent} eq $snapname) {
490 if ($new_parent) {
491 $confref->{parent} = $new_parent;
492 } else {
493 delete $confref->{parent};
494 }
495 }
496 };
497
498 my $remove_drive = sub {
499 my ($drive) = @_;
500
501 my $conf = $class->load_config($vmid);
502 $snap = $conf->{snapshots}->{$snapname};
503 die "snapshot '$snapname' does not exist\n" if !defined($snap);
504
505 $class->__snapshot_delete_remove_drive($snap, $drive);
506
507 $class->write_config($vmid, $conf);
508 };
509
510 #prepare
511 $class->lock_config($vmid, sub {
512 my $conf = $class->load_config($vmid);
513
514 die "you can't delete a snapshot if vm is a template\n"
515 if $class->is_template($conf);
516
517 $snap = $conf->{snapshots}->{$snapname};
518 die "snapshot '$snapname' does not exist\n" if !defined($snap);
519
520 $snap->{snapstate} = 'delete';
521
522 $class->write_config($vmid, $conf);
523 });
524
525 # now remove vmstate file
526 if ($snap->{vmstate}) {
527 $class->__snapshot_delete_vmstate_file($snap, $force);
528
529 # save changes (remove vmstate from snapshot)
530 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
531 };
532
533 # now remove all volume snapshots
534 $class->__snapshot_foreach_volume($snap, sub {
535 my ($vs, $volume) = @_;
536
537 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
538 if (!$drivehash || $drivehash->{$vs}) {
539 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
540 if (my $err = $@) {
541 die $err if !$force;
542 warn $err;
543 }
544 }
545
546 # save changes (remove drive from snapshot)
547 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
548 });
549
550 # now cleanup config
551 $class->lock_config($vmid, sub {
552 my $conf = $class->load_config($vmid);
553 $snap = $conf->{snapshots}->{$snapname};
554 die "snapshot '$snapname' does not exist\n" if !defined($snap);
555
556 # remove parent refs
557 &$unlink_parent($conf, $snap->{parent});
558 foreach my $sn (keys %{$conf->{snapshots}}) {
559 next if $sn eq $snapname;
560 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
561 }
562
563
564 delete $conf->{snapshots}->{$snapname};
565 delete $conf->{lock};
566 foreach my $volid (@$unused) {
567 $class->add_unused_volume($conf, $volid);
568 }
569
570 $class->write_config($vmid, $conf);
571 });
572}
573
574# Rolls back to a given snapshot.
575sub snapshot_rollback {
576 my ($class, $vmid, $snapname) = @_;
577
578 my $prepare = 1;
579
580 my $storecfg = PVE::Storage::config();
581
582 my $conf = $class->load_config($vmid);
583
584 my $get_snapshot_config = sub {
585
586 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
587
588 my $res = $conf->{snapshots}->{$snapname};
589
590 die "snapshot '$snapname' does not exist\n" if !defined($res);
591
592 return $res;
593 };
594
683a30e0
WL
595 my $repl_conf = PVE::ReplicationConfig->new();
596 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
597 # remove all replication snapshots
598 my $volumes = $class->get_replicatable_volumes($storecfg, $conf, 1);
599 my $sorted_volids = [ sort keys %$volumes ];
600
601 # remove all local replication snapshots (jobid => undef)
602 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
603 PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
604 }
605
58a3c91c
FG
606 my $snap = &$get_snapshot_config();
607
608 $class->__snapshot_foreach_volume($snap, sub {
609 my ($vs, $volume) = @_;
610
611 $class->__snapshot_rollback_vol_possible($volume, $snapname);
612 });
613
614 my $updatefn = sub {
615
616 $conf = $class->load_config($vmid);
617
618 $snap = &$get_snapshot_config();
619
620 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
621 if $snap->{snapstate};
622
623 if ($prepare) {
624 $class->check_lock($conf);
625 $class->__snapshot_rollback_vm_stop($vmid);
626 }
627
628 die "unable to rollback vm $vmid: vm is running\n"
629 if $class->__snapshot_check_running($vmid);
630
631 if ($prepare) {
632 $conf->{lock} = 'rollback';
633 } else {
634 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
635 delete $conf->{lock};
636 }
637
638 # machine only relevant for Qemu
639 my $forcemachine;
640
641 if (!$prepare) {
642 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
643
644 foreach my $volid (@$unused) {
645 $class->add_unused_volume($conf, $volid);
646 }
647
648 my $has_machine_config = defined($conf->{machine});
649
650 # copy snapshot config to current config
651 $conf = $class->__snapshot_apply_config($conf, $snap);
652 $conf->{parent} = $snapname;
653
654 # Note: old code did not store 'machine', so we try to be smart
655 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
656 $forcemachine = $conf->{machine} || 'pc-i440fx-1.4';
657 # we remove the 'machine' configuration if not explicitly specified
658 # in the original config.
659 delete $conf->{machine} if $snap->{vmstate} && !$has_machine_config;
660 }
661
662 $class->write_config($vmid, $conf);
663
664 if (!$prepare && $snap->{vmstate}) {
665 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $forcemachine);
666 }
667 };
668
669 $class->lock_config($vmid, $updatefn);
670
671 $class->__snapshot_foreach_volume($snap, sub {
672 my ($vs, $volume) = @_;
673
674 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
675 });
676
677 $prepare = 0;
678 $class->lock_config($vmid, $updatefn);
679}
680
6811;