Skip to content

Commit 933eda9

Browse files
committed
cidata: move provision scripts to ISO
Signed-off-by: Akihiro Suda <[email protected]>
1 parent a5787df commit 933eda9

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

docs/internal.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Metadata:
1717
- `lima.yaml`: the YAML
1818

1919
cloud-init:
20-
- `cidata.iso`: cloud-init ISO9660 image. (`user-data`, `meta-data`, `lima-guestagent.Linux-<ARCH>`)
20+
- `cidata.iso`: cloud-init ISO9660 image. See [`cidata.iso`](#cidata-iso).
2121

2222
disk:
2323
- `basedisk`: the base image
@@ -58,3 +58,22 @@ The directory contains the following files:
5858

5959
- `$LIMA_INSTANCE`: `lima ...` is expanded to `limactl shell ${LIMA_INSTANCE} ...`.
6060
- Default : `default`
61+
62+
## `cidata.iso`
63+
`cidata.iso` contains the following files:
64+
65+
- `user-data`: [Cloud-init user-data](https://cloudinit.readthedocs.io/en/latest/topics/format.html)
66+
- `meta-data`: [Cloud-init meta-data](https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html)
67+
- `lima-guestagent`: Lima guest agent binary
68+
- `nerdctl-full.tgz`: [`nerdctl-full-<VERSION>-linux-<ARCH>.tar.gz`](https://github.com/containerd/nerdctl/releases)
69+
- `boot/*`: Boot scripts
70+
- `provision.system/*`: Custom provision scripts (system)
71+
- `provision.user/*`: Custom provision scripts (user)
72+
73+
Max file name length = 30
74+
75+
### Volume label
76+
The volume label is "cidata", as defined by [cloud-init NoCloud](https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html).
77+
78+
### Environment variables
79+
- `LIMA_CIDATA_MNT`: the mount point of the disk. `/mnt/lima-cidata`.

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,26 @@ write_files:
3434
CODE=1
3535
fi
3636
done
37+
if [ -d "${LIMA_CIDATA_MNT}"/provision.system ]; then
38+
for f in "${LIMA_CIDATA_MNT}"/provision.system/*; do
39+
echo "Executing $f"
40+
if ! "$f"; then
41+
echo "Failed to execute $f"
42+
CODE=1
43+
fi
44+
done
45+
fi
46+
if [ -d "${LIMA_CIDATA_MNT}"/provision.user ]; then
47+
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
48+
for f in "${LIMA_CIDATA_MNT}"/provision.user/*; do
49+
echo "Executing $f (as user {{.User}})"
50+
if ! sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" "$f"; then
51+
echo "Failed to execute $f (as user {{.User}})"
52+
CODE=1
53+
fi
54+
done
55+
fi
3756
exit "$CODE"
3857
owner: root:root
3958
path: /var/lib/cloud/scripts/per-boot/00-lima.boot.sh
4059
permissions: '0755'
41-
{{- if .Provision}}
42-
# TODO: move Provision scripts to ISO
43-
- content: |
44-
#!/bin/bash
45-
set -eu -o pipefail
46-
{{- range $i, $val := .Provision}}
47-
{{- $script := printf "/var/lib/lima-guestagent/provision-%02d-%s" $i $val.Mode}}
48-
{{- if eq $val.Mode "system"}}
49-
{{$script}}
50-
{{- else}}
51-
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
52-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" {{$script}}
53-
{{- end}}
54-
{{- end}}
55-
owner: root:root
56-
path: /var/lib/cloud/scripts/per-boot/50-execute-provision-scripts.boot.sh
57-
permissions: '0755'
58-
{{- end}}
59-
{{- range $i, $val := .Provision}}
60-
- content: {{printf "%q" $val.Script}}
61-
owner: root:root
62-
path: {{printf "/var/lib/lima-guestagent/provision-%02d-%s" $i $val.Mode}}
63-
permissions: '0755'
64-
{{- end}}

pkg/cidata/cidata.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/user"
1010
"path/filepath"
1111
"strconv"
12+
"strings"
1213

1314
"github.com/AkihiroSuda/lima/pkg/downloader"
1415
"github.com/AkihiroSuda/lima/pkg/iso9660util"
@@ -37,7 +38,6 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
3738
Name: name,
3839
User: u.Username,
3940
UID: uid,
40-
Provision: y.Provision,
4141
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
4242
}
4343

@@ -66,6 +66,18 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
6666
return err
6767
}
6868

69+
for i, f := range y.Provision {
70+
switch f.Mode {
71+
case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser:
72+
layout = append(layout, iso9660util.Entry{
73+
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
74+
Reader: strings.NewReader(f.Script),
75+
})
76+
default:
77+
return errors.Errorf("unknown provision mode %q", f.Mode)
78+
}
79+
}
80+
6981
if guestAgentBinary, err := GuestAgentBinary(y.Arch); err != nil {
7082
return err
7183
} else {

pkg/cidata/template.go

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

1010
"github.com/AkihiroSuda/lima/pkg/iso9660util"
11-
"github.com/AkihiroSuda/lima/pkg/limayaml"
1211

1312
"github.com/AkihiroSuda/lima/pkg/templateutil"
1413
"github.com/containerd/containerd/identifiers"
@@ -30,7 +29,6 @@ type TemplateArgs struct {
3029
UID int
3130
SSHPubKeys []string
3231
Mounts []string // abs path, accessible by the User
33-
Provision []limayaml.Provision
3432
Containerd Containerd
3533
}
3634

0 commit comments

Comments
 (0)