From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id D54391FF168
	for <inbox@lore.proxmox.com>; Tue, 18 Feb 2025 12:04:22 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 7CCA39856;
	Tue, 18 Feb 2025 12:04:16 +0100 (CET)
Date: Tue, 18 Feb 2025 12:03:42 +0100
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Fiona Ebner <f.ebner@proxmox.com>
Message-ID: <e3n6xn3zakvlzati7hfazdhuokjtirnaf4ri4pcdqj3anjye2c@vbkioxv4mvqc>
References: <20250212110427.33757-1-f.ebner@proxmox.com>
 <20250212110427.33757-3-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <20250212110427.33757-3-f.ebner@proxmox.com>
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.082 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: Re: [pve-devel] [PATCH qemu-server v3 2/3] config: add special
 class that prevents writing the configuration
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Cc: pve-devel@lists.proxmox.com
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

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=FCnbichler <f.gruenbichler@proxmox.com>
> Suggested-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
> Signed-off-by: Fiona Ebner <f.ebner@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) =3D @_;
> +
> +    # Dispatch to class the object was blessed with if caller invoked th=
e method via the
> +    # 'PVE::QemuConfig' class name explicitly. This is hack, but the cod=
e currently doesn't
> +    # generally use blessed config objects. Safeguard against infinite r=
ecursion.
> +    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=3DNoWrite.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) =3D @_;
> +
> +    bless($conf, $class);

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

    $newconf =3D 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 =3D {%$conf};`).

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

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

> +
> +    return $conf;
> +}
> +
> +sub write_config {
> +    my ($class, $vmid, $conf) =3D @_;
> +
> +    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