fix: generate ssh host keys for linux guest (#15303)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2022-11-07 02:16:25 +08:00
committed by GitHub
co-authored by Qiu Jian
parent fea06ca2d2
commit f472e7aa57
4 changed files with 26 additions and 0 deletions
@@ -56,6 +56,8 @@ type IDiskPartition interface {
IsReadonly() bool
GetPhysicalPartitionType() string
Zerofree()
GenerateSshHostKeys() error
}
type IRootFsDriver interface {
+8
View File
@@ -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
}
+10
View File
@@ -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
}
+6
View File
@@ -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")
}