Skip to content

Commit 67c9e41

Browse files
authored
Merge pull request #1094 from pendo324/bootCmds
add "boot" provisioning script mode
2 parents 6b17018 + 4d77414 commit 67c9e41

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

examples/default.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ containerd:
171171
# cat <<EOF > ~/.vimrc
172172
# set number
173173
# EOF
174+
# # `boot` is executed directly by /bin/sh as part of cloud-init-local.service's early boot process,
175+
# # which is why there is no hash-bang specified in the example
176+
# # See cloud-init docs for more info https://cloudinit.readthedocs.io/en/latest/topics/examples.html#run-commands-on-first-boot
177+
# - mode: boot
178+
# script: |
179+
# systemctl disable NetworkManager-wait-online.service
174180

175181
# Probe scripts to check readiness.
176182
# 🟢 Builtin default: null

pkg/cidata/cidata.TEMPLATE.d/user-data

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ ca-certs:
6464
{{- end }}
6565
{{- end }}
6666
{{- end }}
67+
68+
{{- if .BootCmds }}
69+
bootcmd:
70+
{{- range $cmd := $.BootCmds }}
71+
- |
72+
{{- range $line := $cmd.Lines }}
73+
{{ $line }}
74+
{{- end }}
75+
{{- end }}
76+
{{- end }}

pkg/cidata/cidata.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
219219
args.CACerts.Trusted = append(args.CACerts.Trusted, cert)
220220
}
221221

222+
args.BootCmds = getBootCmds(y.Provision)
223+
222224
if err := ValidateTemplateArgs(args); err != nil {
223225
return err
224226
}
@@ -235,6 +237,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
235237
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
236238
Reader: strings.NewReader(f.Script),
237239
})
240+
case limayaml.ProvisionModeBoot:
241+
continue
238242
default:
239243
return fmt.Errorf("unknown provision mode %q", f.Mode)
240244
}
@@ -289,3 +293,20 @@ func getCert(content string) Cert {
289293
// return lines
290294
return Cert{Lines: lines}
291295
}
296+
297+
func getBootCmds(p []limayaml.Provision) []BootCmds {
298+
var bootCmds []BootCmds
299+
for _, f := range p {
300+
if f.Mode == limayaml.ProvisionModeBoot {
301+
lines := []string{}
302+
for _, line := range strings.Split(f.Script, "\n") {
303+
if line == "" {
304+
continue
305+
}
306+
lines = append(lines, strings.TrimSpace(line))
307+
}
308+
bootCmds = append(bootCmds, BootCmds{Lines: lines})
309+
}
310+
}
311+
return bootCmds
312+
}

pkg/cidata/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Mount struct {
4242
Type string
4343
Options string
4444
}
45+
type BootCmds struct {
46+
Lines []string
47+
}
4548
type TemplateArgs struct {
4649
Name string // instance name
4750
IID string // instance id
@@ -62,6 +65,7 @@ type TemplateArgs struct {
6265
DNSAddresses []string
6366
CACerts CACerts
6467
HostHomeMountPoint string
68+
BootCmds []BootCmds
6569
}
6670

6771
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type ProvisionMode = string
116116
const (
117117
ProvisionModeSystem ProvisionMode = "system"
118118
ProvisionModeUser ProvisionMode = "user"
119+
ProvisionModeBoot ProvisionMode = "boot"
119120
)
120121

121122
type Provision struct {

pkg/limayaml/validate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ func Validate(y LimaYAML, warn bool) error {
153153

154154
for i, p := range y.Provision {
155155
switch p.Mode {
156-
case ProvisionModeSystem, ProvisionModeUser:
156+
case ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot:
157157
default:
158-
return fmt.Errorf("field `provision[%d].mode` must be either %q or %q",
159-
i, ProvisionModeSystem, ProvisionModeUser)
158+
return fmt.Errorf("field `provision[%d].mode` must be either %q, %q, or %q",
159+
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot)
160160
}
161161
}
162162
needsContainerdArchives := (y.Containerd.User != nil && *y.Containerd.User) || (y.Containerd.System != nil && *y.Containerd.System)

0 commit comments

Comments
 (0)