config: add special class that prevents writing the configuration

To be used in the context of template backup, where a minimized
temporary configuration is created to start the VM in 'prelaunch'
mode. Issue a warning, so that code paths where this happens will be
noted and can be evaluated and adapted.

Since the code currently doesn't use blessed config objects, special
dispatching is needed to potentially defer to the new child class in
the write_config() method.

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Suggested-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fiona Ebner 2025-02-12 12:04:26 +01:00 committed by Fabian Grünbichler
parent 4db7e8612a
commit 1afbefb2d0
4 changed files with 46 additions and 0 deletions

View File

@ -11,4 +11,5 @@ install:
$(MAKE) -C VZDump install
$(MAKE) -C API2 install
$(MAKE) -C CLI install
$(MAKE) -C QemuConfig install
$(MAKE) -C QemuServer install

View File

@ -3,6 +3,8 @@ package PVE::QemuConfig;
use strict;
use warnings;
use Scalar::Util qw(blessed);
use PVE::AbstractConfig;
use PVE::INotify;
use PVE::JSONSchema;
@ -561,6 +563,19 @@ sub get_derived_property {
}
}
sub write_config {
my ($class, $vmid, $conf) = @_;
# Dispatch to class the object was blessed with if caller invoked the method via the
# 'PVE::QemuConfig' class name explicitly. This is hack, but the code currently doesn't
# generally use blessed config objects. Safeguard against infinite recursion.
if (blessed($conf) && !blessed($class)) {
return $conf->write_config($vmid, $conf);
}
return $class->SUPER::write_config($vmid, $conf);
}
# END implemented abstract methods from PVE::AbstractConfig
sub has_cloudinit {

5
PVE/QemuConfig/Makefile Normal file
View File

@ -0,0 +1,5 @@
SOURCES=NoWrite.pm
.PHONY: install
install: ${SOURCES}
for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/QemuConfig/$$i; done

25
PVE/QemuConfig/NoWrite.pm Normal file
View File

@ -0,0 +1,25 @@
package PVE::QemuConfig::NoWrite;
use strict;
use warnings;
use PVE::RESTEnvironment qw(log_warn);
use base qw(PVE::QemuConfig);
sub new {
my ($class, $conf) = @_;
bless($conf, $class);
return $conf;
}
sub write_config {
my ($class, $vmid, $conf) = @_;
log_warn("refusing to write temporary configuration");
return;
}
1;