[pve-devel] [PATCH container 4/4] Expose ssh_keys feature over API

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Apr 5 13:17:03 CEST 2016


this adds a new optional parameter to the create_vm API
method, using which public ssh keys can be setup for the
container at creation.

the ssh-public-keys parameter is mapped to a filepath for
pct using the new string->filepath mapping mechanism.
---
 src/PVE/API2/LXC.pm | 13 ++++++++++++-
 src/PVE/CLI/pct.pm  | 10 ++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 25a912f..34ebf4e 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -148,6 +148,12 @@ __PACKAGE__->register_method({
 		type => 'boolean',
 		description => "Ignore errors when extracting the template.",
 	    },
+	    'ssh-public-keys' => {
+		optional => 1,
+		type => 'string',
+		description => "Setup public SSH keys (one key per line, " .
+				"OpenSSH format).",
+	    },
 	}),
     },
     returns => {
@@ -191,6 +197,9 @@ __PACKAGE__->register_method({
 
 	my $password = extract_param($param, 'password');
 
+	my $ssh_keys = extract_param($param, 'ssh-public-keys');
+	PVE::Tools::validate_ssh_public_keys($ssh_keys);
+
 	my $pool = extract_param($param, 'pool');
 
 	if (defined($pool)) {
@@ -310,7 +319,9 @@ __PACKAGE__->register_method({
 
 		$vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $param, $conf);
 
-		PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf, $archive, $password, $restore, $ignore_unpack_errors);
+		PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf,
+						$archive, $password, $restore,
+						$ignore_unpack_errors, $ssh_keys);
 		# set some defaults
 		$conf->{hostname} ||= "CT$vmid";
 		$conf->{memory} ||= 512;
diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index c3cea6a..1c04329 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -42,6 +42,16 @@ sub read_password {
     return $input;
 }
 
+sub string_param_file_mapping {
+    my ($name) = @_;
+
+    my $mapping = {
+	'create_vm' => ['ssh-public-keys'],
+    };
+
+    return defined($mapping->{$name}) ? $mapping->{$name} : [];
+}
+
 __PACKAGE__->register_method ({
     name => 'unlock',
     path => 'unlock',
-- 
2.1.4




More information about the pve-devel mailing list