Skip to content

Commit

Permalink
Make it possible to configure user shell
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Feb 8, 2025
1 parent 75221d6 commit 0680266
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 9 deletions.
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

0 comments on commit 0680266

Please sign in to comment.