Skip to content

Commit 8c0e316

Browse files
committed
Use "lima" username if the local user is not a valid Linux name
At least some companies set local users via directory services and choose the email address as the user name. The '@' character is not valid in Linux usernames. Signed-off-by: Jan Dubois <[email protected]>
1 parent be68278 commit 8c0e316

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

cmd/limactl/copy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8-
"os/user"
98
"strings"
109

10+
"github.com/lima-vm/lima/pkg/osutil"
1111
"github.com/lima-vm/lima/pkg/sshutil"
1212
"github.com/lima-vm/lima/pkg/store"
1313
"github.com/sirupsen/logrus"
@@ -31,7 +31,7 @@ func copyAction(clicontext *cli.Context) error {
3131
if err != nil {
3232
return err
3333
}
34-
u, err := user.Current()
34+
u, err := osutil.LimaUser(false)
3535
if err != nil {
3636
return err
3737
}

pkg/cidata/cidata.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io/fs"
88
"io/ioutil"
99
"os"
10-
"os/user"
1110
"path/filepath"
1211
"strconv"
1312
"strings"
@@ -39,7 +38,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
3938
if err := limayaml.Validate(*y); err != nil {
4039
return err
4140
}
42-
u, err := user.Current()
41+
u, err := osutil.LimaUser(true)
4342
if err != nil {
4443
return err
4544
}

pkg/limayaml/validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net"
66
"os"
7-
"os/user"
87
"path/filepath"
98
"runtime"
109
"strings"
@@ -13,6 +12,7 @@ import (
1312

1413
"github.com/docker/go-units"
1514
"github.com/lima-vm/lima/pkg/localpathutil"
15+
"github.com/lima-vm/lima/pkg/osutil"
1616
"github.com/lima-vm/lima/pkg/qemu/qemuconst"
1717
"github.com/sirupsen/logrus"
1818
)
@@ -61,7 +61,7 @@ func Validate(y LimaYAML) error {
6161
return fmt.Errorf("field `memory` has an invalid value: %w", err)
6262
}
6363

64-
u, err := user.Current()
64+
u, err := osutil.LimaUser(false)
6565
if err != nil {
6666
return fmt.Errorf("internal error (not an error of YAML): %w", err)
6767
}

pkg/osutil/user.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package osutil
2+
3+
import (
4+
"fmt"
5+
"github.com/sirupsen/logrus"
6+
"os/user"
7+
"sync"
8+
9+
"github.com/containerd/containerd/identifiers"
10+
)
11+
12+
const (
13+
fallbackUser = "lima"
14+
)
15+
16+
var cache struct {
17+
sync.Once
18+
u *user.User
19+
err error
20+
warning string
21+
}
22+
23+
func LimaUser(warn bool) (*user.User, error) {
24+
cache.Do(func() {
25+
cache.u, cache.err = user.Current()
26+
if cache.err == nil {
27+
if err := identifiers.Validate(cache.u.Username); err != nil {
28+
cache.warning = fmt.Sprintf("local user %q is not a valid Linux username: %v; using %q username instead",
29+
cache.u.Username, err, fallbackUser)
30+
}
31+
cache.u.Username = fallbackUser
32+
}
33+
})
34+
if warn && cache.warning != "" {
35+
logrus.Warn(cache.warning)
36+
}
37+
return cache.u, cache.err
38+
}

pkg/sshutil/sshutil.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/fs"
77
"os"
88
"os/exec"
9-
"os/user"
109
"path/filepath"
1110
"strings"
1211

@@ -163,7 +162,7 @@ func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
163162
if len(controlSock) >= osutil.UnixPathMax {
164163
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
165164
}
166-
u, err := user.Current()
165+
u, err := osutil.LimaUser(false)
167166
if err != nil {
168167
return nil, err
169168
}

0 commit comments

Comments
 (0)