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