Skip to content

Commit 287b070

Browse files
committed
Generate cloud-config outside of cidata.iso too
This does not include any mounts, networks, nor boot scripts. It is assumed that "reverse-sshfs" is being used, for mounts. It also does not include lima-guestagent, nerdctl-full.tgz, or any of the provisioning scripts that are in the cidata... Signed-off-by: Anders F Björklund <[email protected]>
1 parent b2f4873 commit 287b070

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

.yamllint

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
extends: default
44

5+
ignore: |
6+
# this is a yaml template, needs to be executed
7+
pkg/cidata/cloud-config.yaml
8+
59
rules:
610
indentation:
711
indent-sequences: false

cmd/limactl/start.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/containerd/containerd/identifiers"
1515
"github.com/lima-vm/lima/cmd/limactl/editflags"
1616
"github.com/lima-vm/lima/cmd/limactl/guessarg"
17+
"github.com/lima-vm/lima/pkg/cidata"
1718
"github.com/lima-vm/lima/pkg/editutil"
1819
"github.com/lima-vm/lima/pkg/ioutilx"
1920
"github.com/lima-vm/lima/pkg/limayaml"
@@ -343,6 +344,9 @@ func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffe
343344
if err := os.WriteFile(filePath, st.yBytes, 0o644); err != nil {
344345
return nil, err
345346
}
347+
if err := cidata.GenerateCloudConfig(instDir, st.instName, y); err != nil {
348+
return nil, err
349+
}
346350
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
347351
return nil, err
348352
}

pkg/cidata/cidata.go

+18
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,24 @@ func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, t
319319
return &args, nil
320320
}
321321

322+
func GenerateCloudConfig(instDir, name string, y *limayaml.LimaYAML) error {
323+
args, err := templateArgs(instDir, name, y, 0, 0, 0, "")
324+
if err != nil {
325+
return err
326+
}
327+
328+
if err := ValidateTemplateArgs(args); err != nil {
329+
return err
330+
}
331+
332+
config, err := ExpandTemplate(args)
333+
if err != nil {
334+
return err
335+
}
336+
337+
return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o644)
338+
}
339+
322340
func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
323341
args, err := templateArgs(instDir, name, y, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
324342
if err != nil {

pkg/cidata/cloud-config.yaml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#cloud-config
2+
# vim:syntax=yaml
3+
4+
growpart:
5+
mode: auto
6+
devices: ['/']
7+
8+
{{- if .UpgradePackages }}
9+
package_update: true
10+
package_upgrade: true
11+
package_reboot_if_required: true
12+
{{- end }}
13+
14+
{{- if or (eq .MountType "9p") (eq .MountType "virtiofs") }}
15+
{{- if .Mounts }}
16+
# mounts are not included here
17+
{{- end }}
18+
{{- end }}
19+
20+
{{- if .TimeZone }}
21+
timezone: {{.TimeZone}}
22+
{{- end }}
23+
24+
users:
25+
- name: "{{.User}}"
26+
uid: "{{.UID}}"
27+
homedir: "{{.Home}}"
28+
shell: /bin/bash
29+
sudo: ALL=(ALL) NOPASSWD:ALL
30+
lock_passwd: true
31+
ssh-authorized-keys:
32+
{{- range $val := .SSHPubKeys }}
33+
- {{ printf "%q" $val }}
34+
{{- end }}
35+
36+
{{- if .DNSAddresses }}
37+
# resolv_conf is not included here
38+
{{- end }}
39+
40+
{{ with .CACerts }}
41+
ca_certs:
42+
remove_defaults: {{ .RemoveDefaults }}
43+
{{- if .Trusted}}
44+
trusted:
45+
{{- range $cert := .Trusted }}
46+
- |
47+
{{- range $line := $cert.Lines }}
48+
{{ $line }}
49+
{{- end }}
50+
{{- end }}
51+
{{- end }}
52+
{{- end }}
53+
54+
{{- if .BootCmds }}
55+
bootcmd:
56+
{{- range $cmd := $.BootCmds }}
57+
- |
58+
{{- range $line := $cmd.Lines }}
59+
{{ $line }}
60+
{{- end }}
61+
{{- end }}
62+
{{- end }}

pkg/cidata/template.go

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var templateFS embed.FS
1919

2020
const templateFSRoot = "cidata.TEMPLATE.d"
2121

22+
//go:embed cloud-config.yaml
23+
var cloudConfigYaml string
24+
2225
type CACerts struct {
2326
RemoveDefaults *bool
2427
Trusted []Cert
@@ -118,6 +121,13 @@ func ValidateTemplateArgs(args *TemplateArgs) error {
118121
return nil
119122
}
120123

124+
func ExpandTemplate(args *TemplateArgs) ([]byte, error) {
125+
if err := ValidateTemplateArgs(args); err != nil {
126+
return nil, err
127+
}
128+
return textutil.ExecuteTemplate(cloudConfigYaml, args)
129+
}
130+
121131
func ExecuteTemplate(args *TemplateArgs) ([]iso9660util.Entry, error) {
122132
if err := ValidateTemplateArgs(args); err != nil {
123133
return nil, err

pkg/cidata/template_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ import (
1010

1111
var defaultRemoveDefaults = false
1212

13+
func TestConfig(t *testing.T) {
14+
args := &TemplateArgs{
15+
Name: "default",
16+
User: "foo",
17+
UID: 501,
18+
Home: "/home/foo.linux",
19+
SSHPubKeys: []string{
20+
"ssh-rsa dummy [email protected]",
21+
},
22+
MountType: "reverse-sshfs",
23+
CACerts: CACerts{
24+
RemoveDefaults: &defaultRemoveDefaults,
25+
},
26+
}
27+
config, err := ExpandTemplate(args)
28+
assert.NilError(t, err)
29+
t.Log(string(config))
30+
}
31+
1332
func TestTemplate(t *testing.T) {
1433
args := &TemplateArgs{
1534
Name: "default",

pkg/store/filenames/filenames.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
LimaVersion = "lima-version" // Lima version used to create instance
3131
CIDataISO = "cidata.iso"
3232
CIDataISODir = "cidata"
33+
CloudConfig = "cloud-config.yaml"
3334
BaseDisk = "basedisk"
3435
DiffDisk = "diffdisk"
3536
Kernel = "kernel"

website/content/en/docs/dev/Internals/_index.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Metadata:
3535
- `protected`: empty file, used by `limactl protect`
3636

3737
cloud-init:
38+
- `cloud-config.yaml`: cloud-init configuration.
3839
- `cidata.iso`: cloud-init ISO9660 image. See [`cidata.iso`](#cidataiso).
3940

4041
disk:

0 commit comments

Comments
 (0)