Skip to content

Commit

Permalink
Merge pull request #386 from davidcassany/fix_scp_command_for_old_ope…
Browse files Browse the repository at this point in the history
…nssh

Adapt scp source and target arguments for old openssh
  • Loading branch information
jandubois authored Nov 15, 2021
2 parents fc86229 + a3a7d44 commit 2d0572d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
Expand Down Expand Up @@ -51,18 +52,22 @@ func copyAction(cmd *cobra.Command, args []string) error {
return err
}
instDirs := make(map[string]string)
scpFlags := []string{}
scpArgs := []string{}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if debug {
scpArgs = append(scpArgs, "-v")
scpFlags = append(scpFlags, "-v")
}
if recursive {
scpArgs = append(scpArgs, "-r")
scpFlags = append(scpFlags, "-r")
}
legacySSH := false
if sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.0.0")) {
legacySSH = true
}
scpArgs = append(scpArgs, "-3", "--")
for _, arg := range args {
path := strings.Split(arg, ":")
switch len(path) {
Expand All @@ -80,12 +85,22 @@ func copyAction(cmd *cobra.Command, args []string) error {
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
if legacySSH {
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
scpArgs = append(scpArgs, fmt.Sprintf("%[email protected]:%s", u.Username, path[1]))
} else {
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
}
instDirs[instName] = inst.Dir
default:
return fmt.Errorf("path %q contains multiple colons", arg)
}
}
if legacySSH && len(instDirs) > 1 {
return fmt.Errorf("More than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
}
scpFlags = append(scpFlags, "-3", "--")
scpArgs = append(scpFlags, scpArgs...)

var sshOpts []string
if len(instDirs) == 1 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func CommonOpts(useDotSSH bool) ([]string, error) {

sshInfo.Do(func() {
sshInfo.aesAccelerated = detectAESAcceleration()
sshInfo.openSSHVersion = detectOpenSSHVersion()
sshInfo.openSSHVersion = DetectOpenSSHVersion()
})

// Only OpenSSH version 8.1 and later support adding ciphers to the front of the default set
Expand Down Expand Up @@ -248,7 +248,7 @@ func ParseOpenSSHVersion(version []byte) *semver.Version {
return &semver.Version{}
}

func detectOpenSSHVersion() semver.Version {
func DetectOpenSSHVersion() semver.Version {
var (
v semver.Version
stderr bytes.Buffer
Expand Down

0 comments on commit 2d0572d

Please sign in to comment.