Skip to content

Commit 9c21e15

Browse files
authored
Merge pull request #2827 from jandubois/username
Make guest username, uid, home directory configurable
2 parents 5329fd4 + c77bfa2 commit 9c21e15

File tree

27 files changed

+347
-199
lines changed

27 files changed

+347
-199
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ linters-settings:
121121
- name: context-keys-type
122122
- name: deep-exit
123123
- name: dot-imports
124+
arguments:
125+
- allowedPackages:
126+
- github.com/lima-vm/lima/pkg/must
124127
- name: empty-block
125128
- name: error-naming
126129
- name: error-return

cmd/limactl/copy.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/coreos/go-semver/semver"
11-
"github.com/lima-vm/lima/pkg/osutil"
1211
"github.com/lima-vm/lima/pkg/sshutil"
1312
"github.com/lima-vm/lima/pkg/store"
1413
"github.com/sirupsen/logrus"
@@ -48,11 +47,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
4847
if err != nil {
4948
return err
5049
}
51-
u, err := osutil.LimaUser(false)
52-
if err != nil {
53-
return err
54-
}
55-
instDirs := make(map[string]string)
50+
instances := make(map[string]*store.Instance)
5651
scpFlags := []string{}
5752
scpArgs := []string{}
5853
debug, err := cmd.Flags().GetBool("debug")
@@ -85,28 +80,28 @@ func copyAction(cmd *cobra.Command, args []string) error {
8580
}
8681
if legacySSH {
8782
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
88-
scpArgs = append(scpArgs, fmt.Sprintf("%[email protected]:%s", u.Username, path[1]))
83+
scpArgs = append(scpArgs, fmt.Sprintf("%[email protected]:%s", *inst.Config.User.Name, path[1]))
8984
} else {
90-
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
85+
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", *inst.Config.User.Name, inst.SSHLocalPort, path[1]))
9186
}
92-
instDirs[instName] = inst.Dir
87+
instances[instName] = inst
9388
default:
9489
return fmt.Errorf("path %q contains multiple colons", arg)
9590
}
9691
}
97-
if legacySSH && len(instDirs) > 1 {
92+
if legacySSH && len(instances) > 1 {
9893
return fmt.Errorf("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
9994
}
10095
scpFlags = append(scpFlags, "-3", "--")
10196
scpArgs = append(scpFlags, scpArgs...)
10297

10398
var sshOpts []string
104-
if len(instDirs) == 1 {
99+
if len(instances) == 1 {
105100
// Only one (instance) host is involved; we can use the instance-specific
106101
// arguments such as ControlPath. This is preferred as we can multiplex
107102
// sessions without re-authenticating (MaxSessions permitting).
108-
for _, instDir := range instDirs {
109-
sshOpts, err = sshutil.SSHOpts(instDir, false, false, false, false)
103+
for _, inst := range instances {
104+
sshOpts, err = sshutil.SSHOpts(inst.Dir, *inst.Config.User.Name, false, false, false, false)
110105
if err != nil {
111106
return err
112107
}

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func editAction(cmd *cobra.Command, args []string) error {
115115
logrus.Info("Aborting, no changes made to the instance")
116116
return nil
117117
}
118-
y, err := limayaml.Load(yBytes, filePath)
118+
y, err := limayaml.LoadWithWarnings(yBytes, filePath)
119119
if err != nil {
120120
return err
121121
}

cmd/limactl/shell.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
167167

168168
sshOpts, err := sshutil.SSHOpts(
169169
inst.Dir,
170+
*inst.Config.User.Name,
170171
*inst.Config.SSH.LoadDotSSHPubKeys,
171172
*inst.Config.SSH.ForwardAgent,
172173
*inst.Config.SSH.ForwardX11,

cmd/limactl/show-ssh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9090
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
9191
opts, err := sshutil.SSHOpts(
9292
inst.Dir,
93+
*inst.Config.User.Name,
9394
*inst.Config.SSH.LoadDotSSHPubKeys,
9495
*inst.Config.SSH.ForwardAgent,
9596
*inst.Config.SSH.ForwardX11,

cmd/limactl/tunnel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
107107

108108
sshOpts, err := sshutil.SSHOpts(
109109
inst.Dir,
110+
*inst.Config.User.Name,
110111
*inst.Config.SSH.LoadDotSSHPubKeys,
111112
*inst.Config.SSH.ForwardAgent,
112113
*inst.Config.SSH.ForwardX11,

hack/test-templates.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ declare -A CHECKS=(
3737
["mount-path-with-spaces"]=""
3838
["provision-ansible"]=""
3939
["param-env-variables"]=""
40+
["set-user"]=""
4041
)
4142

4243
case "$NAME" in
@@ -63,6 +64,7 @@ case "$NAME" in
6364
CHECKS["mount-path-with-spaces"]="1"
6465
CHECKS["provision-ansible"]="1"
6566
CHECKS["param-env-variables"]="1"
67+
CHECKS["set-user"]="1"
6668
;;
6769
"docker")
6870
CONTAINER_ENGINE="docker"
@@ -172,6 +174,11 @@ if [[ -n ${CHECKS["param-env-variables"]} ]]; then
172174
limactl shell "$NAME" test -e /tmp/param-user
173175
fi
174176

177+
if [[ -n ${CHECKS["set-user"]} ]]; then
178+
INFO 'Testing that user settings can be provided by lima.yaml'
179+
limactl shell "$NAME" grep "^john:x:4711:4711:John Doe:/home/john-john" /etc/passwd
180+
fi
181+
175182
INFO "Testing proxy settings are imported"
176183
got=$(limactl shell "$NAME" env | grep FTP_PROXY)
177184
# Expected: FTP_PROXY is set in addition to ftp_proxy, localhost is replaced

hack/test-templates/test-misc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ probes:
5858
# $ limactl disk create data --size 10G
5959
additionalDisks:
6060
- "data"
61+
62+
user:
63+
name: john
64+
comment: John Doe
65+
home: "/home/{{.User}}-{{.User}}"
66+
uid: 4711

pkg/cidata/cidata.TEMPLATE.d/boot/02-wsl2-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[ "$LIMA_CIDATA_VMTYPE" = "wsl2" ] || exit 0
55

66
# create user
7-
sudo useradd -u "${LIMA_CIDATA_UID}" "${LIMA_CIDATA_USER}" -c "${LIMA_CIDATA_GECOS}" -d "${LIMA_CIDATA_HOME}"
7+
sudo useradd -u "${LIMA_CIDATA_UID}" "${LIMA_CIDATA_USER}" -c "${LIMA_CIDATA_COMMENT}" -d "${LIMA_CIDATA_HOME}"
88
sudo mkdir "${LIMA_CIDATA_HOME}"/.ssh/
99
sudo cp "${LIMA_CIDATA_MNT}"/ssh_authorized_keys "${LIMA_CIDATA_HOME}"/.ssh/authorized_keys
1010
sudo chown "${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}"/.ssh/authorized_keys

pkg/cidata/cidata.TEMPLATE.d/lima.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ LIMA_CIDATA_DEBUG={{ .Debug }}
22
LIMA_CIDATA_NAME={{ .Name }}
33
LIMA_CIDATA_USER={{ .User }}
44
LIMA_CIDATA_UID={{ .UID }}
5-
LIMA_CIDATA_GECOS={{ .GECOS }}
5+
LIMA_CIDATA_COMMENT={{ .Comment }}
66
LIMA_CIDATA_HOME={{ .Home}}
77
LIMA_CIDATA_HOSTHOME_MOUNTPOINT={{ .HostHomeMountPoint }}
88
LIMA_CIDATA_MOUNTS={{ len .Mounts }}

0 commit comments

Comments
 (0)