diff --git a/pkg/hostman/guestfs/fsdriver/interface.go b/pkg/hostman/guestfs/fsdriver/interface.go index 6d4d8b6be2..08b73bf9da 100644 --- a/pkg/hostman/guestfs/fsdriver/interface.go +++ b/pkg/hostman/guestfs/fsdriver/interface.go @@ -56,6 +56,8 @@ type IDiskPartition interface { IsReadonly() bool GetPhysicalPartitionType() string Zerofree() + + GenerateSshHostKeys() error } type IRootFsDriver interface { diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index c8b50444ea..80c045947c 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -337,6 +337,14 @@ func (l *sLinuxRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*ty return errors.Wrapf(err, "write %s fail", dockerDaemonConfPath) } } + // deploy ssh host key + { + err := rootFs.GenerateSshHostKeys() + if err != nil { + // ignore error + log.Errorf("rootFs.GenerateSshHostKeys fail %s", err) + } + } return nil } diff --git a/pkg/hostman/guestfs/kvmpart/localfs.go b/pkg/hostman/guestfs/kvmpart/localfs.go index d5cfb073d1..b79e8a6321 100644 --- a/pkg/hostman/guestfs/kvmpart/localfs.go +++ b/pkg/hostman/guestfs/kvmpart/localfs.go @@ -310,3 +310,13 @@ func (f *SLocalGuestFS) FilePutContents(sPath, content string, modAppend, caseIn return fmt.Errorf("Cann't put content") } } + +func (f *SLocalGuestFS) GenerateSshHostKeys() error { + cmd := []string{"chroot", f.mountPath, "ssh-keygen", "-A"} + output, err := procutils.NewCommand(cmd[0], cmd[1:]...).Output() + if err != nil { + log.Errorf("ssh-keygen host keys fail: %s, %s", err, output) + return fmt.Errorf("%s", output) + } + return nil +} diff --git a/pkg/hostman/guestfs/sshpart/sshpart.go b/pkg/hostman/guestfs/sshpart/sshpart.go index fb5f7a19d9..f661f2c593 100644 --- a/pkg/hostman/guestfs/sshpart/sshpart.go +++ b/pkg/hostman/guestfs/sshpart/sshpart.go @@ -610,3 +610,9 @@ func MountSSHRootfs(tool *disktool.SSHPartitionTool, term *ssh.Client, layouts [ } return nil, nil, fmt.Errorf("Fail to find rootfs") } + +func (p *SSHPartition) GenerateSshHostKeys() error { + cmd := fmt.Sprintf("/usr/sbin/chroot %s /usr/bin/ssh-keygen -A", p.mountPath) + _, err := p.term.Run(cmd) + return errors.Wrap(err, "GenerateSshHostKeys") +}