Skip to content

Commit ef78bb6

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 a39d704 commit ef78bb6

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

.yamllint

Lines changed: 4 additions & 0 deletions
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

pkg/cidata/cidata.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ func templateArgs(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLoc
322322
return &args, nil
323323
}
324324

325+
func GenerateCloudConfig(instDir, name string, instConfig *limayaml.LimaYAML) error {
326+
args, err := templateArgs(instDir, name, instConfig, 0, 0, 0, "")
327+
if err != nil {
328+
return err
329+
}
330+
331+
if err := ValidateTemplateArgs(args); err != nil {
332+
return err
333+
}
334+
335+
config, err := ExpandTemplate(args)
336+
if err != nil {
337+
return err
338+
}
339+
340+
return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o444)
341+
}
342+
325343
func GenerateISO9660(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
326344
args, err := templateArgs(instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
327345
if err != nil {

pkg/cidata/cloud-config.yaml

Lines changed: 62 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var templateFS embed.FS
2020

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

23+
//go:embed cloud-config.yaml
24+
var cloudConfigYaml string
25+
2326
type CACerts struct {
2427
RemoveDefaults *bool
2528
Trusted []Cert
@@ -120,6 +123,13 @@ func ValidateTemplateArgs(args *TemplateArgs) error {
120123
return nil
121124
}
122125

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

pkg/cidata/template_test.go

Lines changed: 19 additions & 0 deletions
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/instance/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99

10+
"github.com/lima-vm/lima/pkg/cidata"
1011
"github.com/lima-vm/lima/pkg/driver"
1112
"github.com/lima-vm/lima/pkg/driverutil"
1213
"github.com/lima-vm/lima/pkg/limayaml"
@@ -60,6 +61,9 @@ func Create(ctx context.Context, instName string, instConfig []byte, saveBrokenY
6061
if err := os.WriteFile(filePath, instConfig, 0o644); err != nil {
6162
return nil, err
6263
}
64+
if err := cidata.GenerateCloudConfig(instDir, instName, loadedInstConfig); err != nil {
65+
return nil, err
66+
}
6367
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
6468
return nil, err
6569
}

pkg/store/filenames/filenames.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Metadata:
3737
- `protected`: empty file, used by `limactl protect`
3838

3939
cloud-init:
40+
- `cloud-config.yaml`: cloud-init configuration, for reference only.
4041
- `cidata.iso`: cloud-init ISO9660 image. See [`cidata.iso`](#cidataiso).
4142

4243
Ansible:

0 commit comments

Comments
 (0)