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