Skip to content
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

Make it possible to configure user shell #3207

Merged
merged 1 commit into from
Feb 9, 2025
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 pkg/cidata/cidata.TEMPLATE.d/lima.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ LIMA_CIDATA_USER={{ .User }}
LIMA_CIDATA_UID={{ .UID }}
LIMA_CIDATA_COMMENT={{ .Comment }}
LIMA_CIDATA_HOME={{ .Home}}
LIMA_CIDATA_SHELL={{ .Shell }}
LIMA_CIDATA_HOSTHOME_MOUNTPOINT={{ .HostHomeMountPoint }}
LIMA_CIDATA_MOUNTS={{ len .Mounts }}
{{- range $i, $val := .Mounts}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cidata/cidata.TEMPLATE.d/user-data
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ users:
gecos: {{ printf "%q" .Comment }}
{{- end }}
homedir: "{{.Home}}"
shell: /bin/bash
shell: {{.Shell}}
sudo: ALL=(ALL) NOPASSWD:ALL
lock_passwd: true
ssh-authorized-keys:
Expand Down
1 change: 1 addition & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
User: *instConfig.User.Name,
Comment: *instConfig.User.Comment,
Home: *instConfig.User.Home,
Shell: *instConfig.User.Shell,
UID: *instConfig.User.UID,
GuestInstallPrefix: *instConfig.GuestInstallPrefix,
UpgradePackages: *instConfig.UpgradePackages,
Expand Down
4 changes: 4 additions & 0 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type TemplateArgs struct {
User string // user name
Comment string // user information
Home string // home directory
Shell string // login shell
UID uint32
SSHPubKeys []string
Mounts []Mount
Expand Down Expand Up @@ -109,6 +110,9 @@ func ValidateTemplateArgs(args *TemplateArgs) error {
if args.Home == "" {
return errors.New("field Home must be set")
}
if args.Shell == "" {
return errors.New("field Shell must be set")
}
if len(args.SSHPubKeys) == 0 {
return errors.New("field SSHPubKeys must be set")
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/cidata/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestConfig(t *testing.T) {
UID: 501,
Comment: "Foo",
Home: "/home/foo.linux",
Shell: "/bin/bash",
SSHPubKeys: []string{
"ssh-rsa dummy [email protected]",
},
Expand All @@ -35,6 +36,7 @@ func TestConfigCACerts(t *testing.T) {
UID: 501,
Comment: "Foo",
Home: "/home/foo.linux",
Shell: "/bin/bash",
SSHPubKeys: []string{
"ssh-rsa dummy [email protected]",
},
Expand All @@ -51,10 +53,11 @@ func TestConfigCACerts(t *testing.T) {

func TestTemplate(t *testing.T) {
args := &TemplateArgs{
Name: "default",
User: "foo",
UID: 501,
Home: "/home/foo.linux",
Name: "default",
User: "foo",
UID: 501,
Home: "/home/foo.linux",
Shell: "/bin/bash",
SSHPubKeys: []string{
"ssh-rsa dummy [email protected]",
},
Expand Down Expand Up @@ -86,10 +89,11 @@ func TestTemplate(t *testing.T) {

func TestTemplate9p(t *testing.T) {
args := &TemplateArgs{
Name: "default",
User: "foo",
UID: 501,
Home: "/home/foo.linux",
Name: "default",
User: "foo",
UID: 501,
Home: "/home/foo.linux",
Shell: "/bin/bash",
SSHPubKeys: []string{
"ssh-rsa dummy [email protected]",
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
if y.User.Home == nil {
y.User.Home = d.User.Home
}
if y.User.Shell == nil {
y.User.Shell = d.User.Shell
}
if y.User.UID == nil {
y.User.UID = d.User.UID
}
Expand All @@ -221,6 +224,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
if o.User.Home != nil {
y.User.Home = o.User.Home
}
if o.User.Shell != nil {
y.User.Shell = o.User.Shell
}
if o.User.UID != nil {
y.User.UID = o.User.UID
}
Expand All @@ -236,6 +242,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
y.User.Home = ptr.Of(osutil.LimaUser(existingLimaVersion, warn).HomeDir)
warn = false
}
if y.User.Shell == nil {
y.User.Shell = ptr.Of("/bin/bash")
}
if y.User.UID == nil {
uidString := osutil.LimaUser(existingLimaVersion, warn).Uid
if uid, err := strconv.ParseUint(uidString, 10, 32); err == nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestFillDefault(t *testing.T) {
Name: ptr.Of(user.Username),
Comment: ptr.Of(user.Name),
Home: ptr.Of(user.HomeDir),
Shell: ptr.Of("/bin/bash"),
UID: ptr.Of(uint32(uid)),
},
}
Expand Down Expand Up @@ -441,6 +442,7 @@ func TestFillDefault(t *testing.T) {
Name: ptr.Of("xxx"),
Comment: ptr.Of("Foo Bar"),
Home: ptr.Of("/tmp"),
Shell: ptr.Of("/bin/tcsh"),
UID: ptr.Of(uint32(8080)),
},
}
Expand Down Expand Up @@ -664,6 +666,7 @@ func TestFillDefault(t *testing.T) {
Name: ptr.Of("foo"),
Comment: ptr.Of("foo bar baz"),
Home: ptr.Of("/override"),
Shell: ptr.Of("/bin/sh"),
UID: ptr.Of(uint32(1122)),
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type User struct {
Name *string `yaml:"name,omitempty" json:"name,omitempty" jsonschema:"nullable"`
Comment *string `yaml:"comment,omitempty" json:"comment,omitempty" jsonschema:"nullable"`
Home *string `yaml:"home,omitempty" json:"home,omitempty" jsonschema:"nullable"`
Shell *string `yaml:"shell,omitempty" json:"shell,omitempty" jsonschema:"nullable"`
UID *uint32 `yaml:"uid,omitempty" json:"uid,omitempty" jsonschema:"nullable"`
}

Expand Down
3 changes: 3 additions & 0 deletions templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ user:
# It can use the following template variables: {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
# 🟢 Builtin default: "/home/{{.User}}.linux"
home: null
# Shell. Needs to be an absolute path.
# 🟢 Builtin default: "/bin/bash"
shell: null

vmOpts:
qemu:
Expand Down
1 change: 1 addition & 0 deletions website/content/en/docs/dev/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ The volume label is "cidata", as defined by [cloud-init NoCloud](https://docs.cl
- `LIMA_CIDATA_UID`: the numeric UID
- `LIMA_CIDATA_COMMENT`: the full name or comment string
- `LIMA_CIDATA_HOME`: the guest home directory
- `LIMA_CIDATA_SHELL`: the guest login shell
- `LIMA_CIDATA_HOSTHOME_MOUNTPOINT`: the mount point of the host home directory, or empty if not mounted
- `LIMA_CIDATA_MOUNTS`: the number of the Lima mounts
- `LIMA_CIDATA_MOUNTS_%d_MOUNTPOINT`: the N-th mount point of Lima mounts (N=0, 1, ...)
Expand Down
Loading