[pve-devel] [PATCH container v2] implement 'edit' command for pct

Dominik Csapak d.csapak at proxmox.com
Mon Nov 6 15:15:08 CET 2017


this opens the container config file via PVE::Tools::edit_file, minus
the snapshot section

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes since v1:
* open temp file instead of real file
* warn and die when digest is different on write than on read
* use PVE::Tools::edit_file
 src/PVE/CLI/pct.pm | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index 3253906..8e81cf1 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -715,6 +715,64 @@ __PACKAGE__->register_method ({
 	return undef;
     }});
 
+__PACKAGE__->register_method ({
+    name => 'edit',
+    path => 'edit',
+    method => 'POST',
+    description => "Opens the CT config file in the editor set via update-alternatives.".
+		   "This does not actually change the CT without turning it off and on.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+	my $digest;
+
+	my $readfn = sub {
+	    # load config of vm
+	    my $oldconf = PVE::LXC::Config->load_config($vmid);
+
+	    # for later check
+	    $digest = $oldconf->{digest};
+
+	    # we do not want to edit snapshots
+	    delete $oldconf->{snapshots};
+
+	    # write_vm_config returns the raw content
+	    return PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $oldconf);
+	};
+
+	my $writefn = sub {
+	    my ($content) = @_;
+	    PVE::LXC::Config->lock_config($vmid, sub {
+
+		# update cluster file system to avoid config cache
+		PVE::Cluster::cfs_update();
+		my $conf = PVE::LXC::Config->load_config($vmid);
+
+		PVE::Tools::assert_if_modified($digest, $conf->{digest});
+
+		my $newconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $content);
+
+		# use the snapshots from the original config
+		$newconf->{snapshots} = $conf->{snapshots};
+
+		# write config back
+		PVE::LXC::Config->write_config($vmid, $newconf);
+	    });
+	};
+
+	PVE::Tools::edit_file($readfn, $writefn);
+
+	return undef;
+    }});
+
 our $cmddef = {
     list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
 	my $res = shift;
@@ -799,6 +857,8 @@ our $cmddef = {
 
     cpusets => [ __PACKAGE__, 'cpusets', []],
 
+    edit => [ __PACKAGE__, 'edit', ['vmid']],
+
 };
 
 
-- 
2.11.0





More information about the pve-devel mailing list