Skip to content

Compatibility with Ubuntu 18.04 LTS (bionic) as a host OS #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cheggaaa/pb/v3 v3.0.8
github.com/containerd/containerd v1.5.7
github.com/containerd/continuity v0.2.0
github.com/coreos/go-semver v0.3.0
github.com/digitalocean/go-qemu v0.0.0-20210326154740-ac9e0b687001
github.com/diskfs/go-diskfs v1.2.0
github.com/docker/go-units v0.4.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ github.com/coreos/go-iptables v0.4.5/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmeka
github.com/coreos/go-iptables v0.5.0/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmekav8Dbxlm1MU=
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20161114122254-48702e0da86b/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down
12 changes: 10 additions & 2 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,22 @@ func inspectFeatures(exe string) (*features, error) {
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
}
f.AccelHelp = stdout.Bytes()
// on older versions qemu will write "help" output to stderr
if len(f.AccelHelp) == 0 {
f.AccelHelp = stderr.Bytes()
}

cmd = exec.Command(exe, "-M", "none", "-netdev", "help")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
logrus.Warnf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
} else {
f.NetdevHelp = stdout.Bytes()
if len(f.NetdevHelp) == 0 {
f.NetdevHelp = stderr.Bytes()
}
}
f.NetdevHelp = stdout.Bytes()
return &f, nil
}

Expand Down
67 changes: 53 additions & 14 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package sshutil

import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/lockutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/store/dirnames"
Expand Down Expand Up @@ -102,6 +106,15 @@ func DefaultPubKeys(loadDotSSH bool) ([]PubKey, error) {
return res, nil
}

var sshInfo struct {
sync.Once
// aesAccelerated is set to true when AES acceleration is available.
// Available on almost all modern Intel/AMD processors.
aesAccelerated bool
// openSSHVersion is set to the version of OpenSSH, or semver.New("0.0.0") if the version cannot be determined.
openSSHVersion semver.Version
}

func CommonArgs(useDotSSH bool) ([]string, error) {
configDir, err := dirnames.LimaConfigDir()
if err != nil {
Expand Down Expand Up @@ -159,16 +172,24 @@ func CommonArgs(useDotSSH bool) ([]string, error) {
"-F", "/dev/null",
)

// By default, `ssh` choose chacha20-poly1305@openssh.com, even when AES accelerator is available.
// (OpenSSH_8.1p1, macOS 11.6, MacBookPro 2020, Core i7-1068NG7)
//
// We prioritize AES algorithms when AES accelerator is available.
if aesAccelerated {
logrus.Debugf("AES accelerator seems available, prioritizing aes128-gcm@openssh.com and aes256-gcm@openssh.com")
args = append(args, "-o", "Ciphers=^aes128-gcm@openssh.com,aes256-gcm@openssh.com")
} else {
logrus.Debugf("AES accelerator does not seem available, prioritizing chacha20-poly1305@openssh.com")
args = append(args, "-o", "Ciphers=^chacha20-poly1305@openssh.com")
sshInfo.Do(func() {
sshInfo.aesAccelerated = detectAESAcceleration()
sshInfo.openSSHVersion = detectOpenSSHVersion()
})

// Only OpenSSH version 8.0 and later support adding ciphers to the front of the default set
if !sshInfo.openSSHVersion.LessThan(*semver.New("8.0.0")) {
// By default, `ssh` choose chacha20-poly1305@openssh.com, even when AES accelerator is available.
// (OpenSSH_8.1p1, macOS 11.6, MacBookPro 2020, Core i7-1068NG7)
//
// We prioritize AES algorithms when AES accelerator is available.
if sshInfo.aesAccelerated {
logrus.Debugf("AES accelerator seems available, prioritizing aes128-gcm@openssh.com and aes256-gcm@openssh.com")
args = append(args, "-o", "Ciphers=^aes128-gcm@openssh.com,aes256-gcm@openssh.com")
} else {
logrus.Debugf("AES accelerator does not seem available, prioritizing chacha20-poly1305@openssh.com")
args = append(args, "-o", "Ciphers=^chacha20-poly1305@openssh.com")
}
}
return args, nil
}
Expand All @@ -195,7 +216,25 @@ func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
return args, nil
}

// aesAccelerated is set to true when AES acceleration is available.
//
// Available on almost all modern Intel/AMD processors.
var aesAccelerated = detectAESAcceleration()
func detectOpenSSHVersion() semver.Version {
var (
v semver.Version
stderr bytes.Buffer
)
cmd := exec.Command("ssh", "-V")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Warnf("failed to run %v: stderr=%q", cmd.Args, stderr.String())
} else {
regex := regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have unit tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the regexp? Then yes, we can move it to a separate function that can be tested without calling ssh -V.

matches := regex.FindSubmatch(stderr.Bytes())
if len(matches) == 3 {
if len(matches[2]) == 0 {
matches[2] = []byte("0")
}
v = *semver.New(fmt.Sprintf("%s.%s", matches[1], matches[2]))
}
}
logrus.Debugf("OpenSSH version %s detected", v)
return v
}