]> git.proxmox.com Git - pve-container.git/commitdiff
new rewrite_ssh_host_keys hook
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 29 Jun 2015 08:48:32 +0000 (10:48 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 29 Jun 2015 08:48:32 +0000 (10:48 +0200)
I want to reuse that in future for clone.

src/PVE/LXCSetup.pm
src/PVE/LXCSetup/Base.pm

index 197c224870972e66cea7014b727ada20ee4c56e1..e57ed1a262257448e0e655efc2f0ea7a321ad016 100644 (file)
@@ -87,6 +87,12 @@ sub set_user_password {
     $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
 }
 
+sub rewrite_ssh_host_keys {
+    my ($self) = @_;
+
+    $self->{plugin}->rewrite_ssh_host_keys($self->{conf});
+}    
+
 sub pre_start_hook {
     my ($self) = @_;
 
index 5141b9e4255c21003203a583b558b420cd87fd93..2ee9fa783afa7b88f2ee7b497bbfde615c3645cc 100644 (file)
@@ -285,6 +285,37 @@ my $randomize_crontab = sub {
    }
 };
 
+sub rewrite_ssh_host_keys {
+    my ($self, $conf) = @_;
+
+    my $rootdir = $self->{rootdir};
+
+    my $etc_ssh_dir = "$rootdir/etc/ssh";
+
+    return if ! -d $etc_ssh_dir;
+    
+    my $keynames = {
+       rsa1 => 'ssh_host_key',
+       rsa => 'ssh_host_rsa_key',
+       dsa => 'ssh_host_dsa_key',
+       ecdsa => 'ssh_host_ecdsa_key', 
+       ed25519 => 'ssh_host_ed25519_key',
+    };
+
+    my $hostname = $conf->{'lxc.utsname'} || 'localhost';
+    $hostname =~ s/\..*$//;
+
+    foreach my $keytype (keys %$keynames) {
+       my $basename = $keynames->{$keytype};
+       unlink "${etc_ssh_dir}/$basename";
+       unlink "${etc_ssh_dir}/$basename.pub";
+       print "Creating SSH host key '$basename' - this may take some time ...\n";
+       my $cmd = ['ssh-keygen', '-q', '-f', "${etc_ssh_dir}/$basename", '-t', $keytype,
+                  '-N', '', '-C', "root\@$hostname"];
+       PVE::Tools::run_command($cmd);
+    }
+}
+
 sub pre_start_hook {
     my ($self, $conf) = @_;
 
@@ -308,6 +339,7 @@ sub post_create_hook {
     $self->setup_network($conf);
     $self->set_hostname($conf);
     $self->set_dns($conf);
+    $self->rewrite_ssh_host_keys($conf);
     
     # fixme: what else ?
 }