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