Skip to content

Commit 079f726

Browse files
committed
Implement system and user provisioning scripts
Signed-off-by: Jan Dubois <[email protected]>
1 parent aeffb15 commit 079f726

File tree

6 files changed

+78
-31
lines changed

6 files changed

+78
-31
lines changed

pkg/cidata/cidata.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
2929
return err
3030
}
3131
args := TemplateArgs{
32-
Name: name,
33-
User: u.Username,
34-
UID: uid,
32+
Name: name,
33+
User: u.Username,
34+
UID: uid,
35+
Provision: y.Provision,
3536
}
3637
for _, f := range sshutil.DefaultPubKeys() {
3738
args.SSHPubKeys = append(args.SSHPubKeys, f.Content)

pkg/cidata/template.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
_ "embed"
55
"path/filepath"
66

7+
"github.com/AkihiroSuda/lima/pkg/limayaml"
8+
79
"github.com/AkihiroSuda/lima/pkg/templateutil"
810
"github.com/containerd/containerd/identifiers"
911
"github.com/pkg/errors"
@@ -22,6 +24,7 @@ type TemplateArgs struct {
2224
UID int
2325
SSHPubKeys []string
2426
Mounts []string // abs path, accessible by the User
27+
Provision []limayaml.Provision
2528
}
2629

2730
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/cidata/user-data.TEMPLATE

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ users:
1313
sudo: ALL=(ALL) NOPASSWD:ALL
1414
lock_passwd: true
1515
ssh-authorized-keys:
16-
{{range $val := .SSHPubKeys}}
16+
{{- range $val := .SSHPubKeys}}
1717
- {{$val}}
18-
{{end}}
18+
{{- end}}
1919

2020
write_files:
2121
- content: |
@@ -49,10 +49,10 @@ write_files:
4949
loginctl enable-linger "{{.User}}"
5050
5151
# Create mount points
52-
{{range $val := .Mounts}}
52+
{{- range $val := .Mounts}}
5353
mkdir -p "{{$val}}"
5454
chown "{{$.User}}" "{{$val}}" || true
55-
{{end}}
55+
{{- end}}
5656
5757
# Install or update the guestagent binary
5858
mkdir -p -m 600 /mnt/lima-cidata
@@ -110,3 +110,26 @@ write_files:
110110
owner: root:root
111111
path: /var/lib/cloud/scripts/per-boot/20-install-containerd.boot.sh
112112
permissions: '0755'
113+
{{- if .Provision}}
114+
- content: |
115+
#!/bin/bash
116+
set -eu -o pipefail
117+
{{- range $i, $val := .Provision}}
118+
{{- $script := printf "/var/lib/lima-guestagent/provision-%02d-%s" $i $val.Mode}}
119+
{{- if eq $val.Mode "system"}}
120+
{{$script}}
121+
{{- else}}
122+
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
123+
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" {{$script}}
124+
{{- end}}
125+
{{- end}}
126+
owner: root:root
127+
path: /var/lib/cloud/scripts/per-boot/30-execute-provision-scripts.boot.sh
128+
permissions: '0755'
129+
{{- end}}
130+
{{- range $i, $val := .Provision}}
131+
- content: {{printf "%q" $val.Script}}
132+
owner: root:root
133+
path: {{printf "/var/lib/lima-guestagent/provision-%02d-%s" $i $val.Mode}}
134+
permissions: '0755'
135+
{{- end}}

pkg/limayaml/default.TEMPLATE.yaml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ video:
5858
# Default: "none"
5959
display: "none"
6060

61-
#UNIMPLEMENTED| provision:
62-
#UNIMPLEMENTED| # `system` is executed with the root privilege
63-
#UNIMPLEMENTED| system: |
64-
#UNIMPLEMENTED| #!/bin/bash
65-
#UNIMPLEMENTED| set -eux -o pipefail
66-
#UNIMPLEMENTED| export DEBIAN_FRONTEND=noninteractive
67-
#UNIMPLEMENTED| apt-get install -y vim
68-
#UNIMPLEMENTED| # `user` is executed without the root privilege
69-
#UNIMPLEMENTED| user: |
70-
#UNIMPLEMENTED| #!/bin/bash
71-
#UNIMPLEMENTED| set -eux -o pipefail
72-
#UNIMPLEMENTED| cat <<EOF > ~/.vimrc
73-
#UNIMPLEMENTED| set number
74-
#UNIMPLEMENTED| EOF
61+
# provision:
62+
# # `system` is executed with the root privilege
63+
# - mode: system
64+
# script: |
65+
# #!/bin/bash
66+
# set -eux -o pipefail
67+
# export DEBIAN_FRONTEND=noninteractive
68+
# apt-get install -y vim
69+
# # `user` is executed without the root privilege
70+
# - mode: user
71+
# script: |
72+
# #!/bin/bash
73+
# set -eux -o pipefail
74+
# cat <<EOF > ~/.vimrc
75+
# set number
76+
# EOF

pkg/limayaml/defaults.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ func FillDefault(y *LimaYAML) {
1919
if y.Disk == "" {
2020
y.Disk = "100GiB"
2121
}
22-
2322
if y.Video.Display == "" {
2423
y.Video.Display = "none"
2524
}
25+
for i := range y.Provision {
26+
provision := &y.Provision[i]
27+
if provision.Mode == "" {
28+
provision.Mode = ProvisionModeSystem
29+
}
30+
}
2631
}
2732

2833
func resolveArch(s string) Arch {

pkg/limayaml/limayaml.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package limayaml
22

33
type LimaYAML struct {
4-
Arch Arch `yaml:"arch,omitempty"`
5-
Images []Image `yaml:"images"` // REQUIRED
6-
CPUs int `yaml:"cpus,omitempty"`
7-
Memory string `yaml:"memory,omitempty"` // go-units.RAMInBytes
8-
Disk string `yaml:"disk,omitempty"` // go-units.RAMInBytes
9-
Mounts []Mount `yaml:"mounts,omitempty"`
10-
SSH SSH `yaml:"ssh,omitempty"` // REQUIRED (FIXME)
11-
Firmware Firmware `yaml:"firmware,omitempty"`
12-
Video Video `yaml:"video,omitempty"`
4+
Arch Arch `yaml:"arch,omitempty"`
5+
Images []Image `yaml:"images"` // REQUIRED
6+
CPUs int `yaml:"cpus,omitempty"`
7+
Memory string `yaml:"memory,omitempty"` // go-units.RAMInBytes
8+
Disk string `yaml:"disk,omitempty"` // go-units.RAMInBytes
9+
Mounts []Mount `yaml:"mounts,omitempty"`
10+
SSH SSH `yaml:"ssh,omitempty"` // REQUIRED (FIXME)
11+
Firmware Firmware `yaml:"firmware,omitempty"`
12+
Video Video `yaml:"video,omitempty"`
13+
Provision []Provision `yaml:"provision,omitempty"`
1314
}
1415

1516
type Arch = string
@@ -43,3 +44,15 @@ type Video struct {
4344
// Display is a QEMU display string
4445
Display string `yaml:"display,omitempty"`
4546
}
47+
48+
type ProvisionMode = string
49+
50+
const (
51+
ProvisionModeSystem ProvisionMode = "system"
52+
ProvisionModeUser ProvisionMode = "user"
53+
)
54+
55+
type Provision struct {
56+
Mode ProvisionMode `yaml:"mode"` // default: "system"
57+
Script string `yaml:"script"`
58+
}

0 commit comments

Comments
 (0)