From 7ee31468b6f0a942fa39acf1bb6f769acdb2685c Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 29 Jun 2015 10:48:32 +0200 Subject: [PATCH] new rewrite_ssh_host_keys hook I want to reuse that in future for clone. --- src/PVE/LXCSetup.pm | 6 ++++++ src/PVE/LXCSetup/Base.pm | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/PVE/LXCSetup.pm b/src/PVE/LXCSetup.pm index 197c224..e57ed1a 100644 --- a/src/PVE/LXCSetup.pm +++ b/src/PVE/LXCSetup.pm @@ -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) = @_; diff --git a/src/PVE/LXCSetup/Base.pm b/src/PVE/LXCSetup/Base.pm index 5141b9e..2ee9fa7 100644 --- a/src/PVE/LXCSetup/Base.pm +++ b/src/PVE/LXCSetup/Base.pm @@ -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 ? } -- 2.39.5