[pve-devel] [PATCH container v2 2/2] pct: add keep-env option

Folke Gleumes f.gleumes at proxmox.com
Mon Jan 29 16:43:18 CET 2024


The keep-env option allows the user to define if the current environment
should be kept when running 'pct enter/exec'. pct will now always set
'--keep-env' or '--discard-env' when calling lxc-attach to anticipate
the upcoming change in default behavior.

Signed-off-by: Folke Gleumes <f.gleumes at proxmox.com>
---
This wasn't present in v1

 src/PVE/CLI/pct.pm | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index 091ac8e..7ce0de0 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -162,12 +162,21 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => {
 	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
+	    # TODO: set keep-env to default false with PVE 9
+	    'keep-env' => {
+		type => 'boolean',
+		description => "Keep the current environment. This option will disabled by default with PVE 9. "
+		    ."If you rely on a preserved environment, please use this option to be future-proof.",
+		optional => 1,
+		default => 1,
+	    },
 	},
     },
     returns => { type => 'null' },
 
     code => sub {
 	my ($param) = @_;
+	my $keep_env = $param->{'keep-env'} // 1; # TODO: toggle with pve 9
 
 	my $vmid = $param->{vmid};
 
@@ -175,7 +184,7 @@ __PACKAGE__->register_method ({
 	die "container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
 
 	clean_environment();
-	exec('lxc-attach', '-n',  $vmid);
+	exec('lxc-attach', $keep_env ? '--keep-env' : '--clear-env', '-n', $vmid);
     }});
 
 __PACKAGE__->register_method ({
@@ -187,12 +196,21 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => {
 	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
+	    # TODO: set keep-env to default false with PVE 9
+	    'keep-env' => {
+		type => 'boolean',
+		description => "Keep the current environment. This option will disabled by default with PVE 9. "
+		    ."If you rely on a preserved environment, please use this option to be future-proof.",
+		optional => 1,
+		default => 1,
+	    },
 	    'extra-args' => get_standard_option('extra-args'),
 	},
     },
     returns => { type => 'null' },
     code => sub {
 	my ($param) = @_;
+	my $keep_env = $param->{'keep-env'} // 1; # TODO: toggle with pve 9
 
 	my $vmid = $param->{vmid};
 	PVE::LXC::Config->load_config($vmid); # test if container exists on this node
@@ -201,7 +219,7 @@ __PACKAGE__->register_method ({
 	die "missing command" if !@{$param->{'extra-args'}};
 
 	clean_environment();
-	exec('lxc-attach', '-n', $vmid, '--', @{$param->{'extra-args'}});
+	exec('lxc-attach', $keep_env ? '--keep-env' : '--clear-env', '-n', $vmid, '--', @{$param->{'extra-args'}});
     }});
 
 __PACKAGE__->register_method ({
-- 
2.39.2





More information about the pve-devel mailing list