On Wed, Feb 12, 2025 at 12:04:26PM +0100, Fiona Ebner wrote:
> 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.gruenbich...@proxmox.com>
> Suggested-by: Thomas Lamprecht <t.lampre...@proxmox.com>
> Signed-off-by: Fiona Ebner <f.eb...@proxmox.com>
> ---
> 
> Changes in v3:
> * use dedicated class
> 
>  PVE/Makefile              |  1 +
>  PVE/QemuConfig.pm         | 15 +++++++++++++++
>  PVE/QemuConfig/Makefile   |  5 +++++
>  PVE/QemuConfig/NoWrite.pm | 25 +++++++++++++++++++++++++
>  4 files changed, 46 insertions(+)
>  create mode 100644 PVE/QemuConfig/Makefile
>  create mode 100644 PVE/QemuConfig/NoWrite.pm
> 
> diff --git a/PVE/Makefile b/PVE/Makefile
> index dc173681..440fbe77 100644
> --- a/PVE/Makefile
> +++ b/PVE/Makefile
> @@ -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
> diff --git a/PVE/QemuConfig.pm b/PVE/QemuConfig.pm
> index ffdf9f03..b60cc398 100644
> --- a/PVE/QemuConfig.pm
> +++ b/PVE/QemuConfig.pm
> @@ -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 {
> diff --git a/PVE/QemuConfig/Makefile b/PVE/QemuConfig/Makefile
> new file mode 100644
> index 00000000..c3a0d277
> --- /dev/null
> +++ b/PVE/QemuConfig/Makefile
> @@ -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
> diff --git a/PVE/QemuConfig/NoWrite.pm b/PVE/QemuConfig/NoWrite.pm
> new file mode 100644
> index 00000000..f697506f
> --- /dev/null
> +++ b/PVE/QemuConfig/NoWrite.pm
> @@ -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);

^ This blesses the original reference.
In the next patch it is used as:

    $newconf = NoWrite->new($newconf);

which *suggests* that the original stays untouched - which is not the
case.

Generally, I'd recommend such methods to do a shallow copy for the sake
of clarity (simply start with `$conf = {%$conf};`).

Otherwise the following code counter-intuitively prints
'PVE::QemuConfig::NoWrite'.

    my $original = { ... };
    my $blessed = NoWrite->new($original);
    print(ref($original));

> +
> +    return $conf;
> +}
> +
> +sub write_config {
> +    my ($class, $vmid, $conf) = @_;
> +
> +    log_warn("refusing to write temporary configuration");
> +    return;
> +}
> +
> +1;
> -- 
> 2.39.5


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to