Skip to content

Commit e1b3c84

Browse files
committed
Refactor Ubuntu images into base templates
Also adds template://default/mounts to setup the default mounts globally. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent 56702e0 commit e1b3c84

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+397
-611
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ jobs:
196196
- name: Validate jsonschema
197197
run: make schema-limayaml.json
198198
- name: Validate templates
199-
run: find -L templates -name '*.yaml' | xargs limactl validate
199+
# Can't validate base templates in `_default` because they have no images
200+
run: find -L templates -name '*.yaml' ! -path '*/_default/*' | xargs limactl validate
200201
- name: Install test dependencies
201202
# QEMU: required by Lima itself
202203
# bash: required by test-templates.sh (OS version of bash is too old)
@@ -208,7 +209,7 @@ jobs:
208209
run: echo "LIMACTL_CREATE_ARGS=${LIMACTL_CREATE_ARGS} --vm-type=qemu" >>$GITHUB_ENV
209210
- name: "Inject `no_timer_check` to kernel cmdline"
210211
# workaround to https://github.com/lima-vm/lima/issues/84
211-
run: ./hack/inject-cmdline-to-template.sh templates/default.yaml no_timer_check
212+
run: ./hack/inject-cmdline-to-template.sh templates/_images/ubuntu.yaml no_timer_check
212213
- name: Cache image used by default.yaml
213214
uses: ./.github/actions/setup_cache_for_template
214215
with:
@@ -365,7 +366,7 @@ jobs:
365366
run: echo "LIMACTL_CREATE_ARGS=${LIMACTL_CREATE_ARGS} --vm-type=qemu --network=lima:shared" >>$GITHUB_ENV
366367
- name: "Inject `no_timer_check` to kernel cmdline"
367368
# workaround to https://github.com/lima-vm/lima/issues/84
368-
run: ./hack/inject-cmdline-to-template.sh templates/default.yaml no_timer_check
369+
run: ./hack/inject-cmdline-to-template.sh templates/_images/ubuntu.yaml no_timer_check
369370
- name: Cache image used by default .yaml
370371
uses: ./.github/actions/setup_cache_for_template
371372
with:

.ls-lint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ ls:
1919
docs:
2020
.md: kebab-case
2121

22+
templates:
23+
# _default and _images have leading underscores
24+
.dir: lowercase
25+
2226
website/content:
2327
.dir: lowercase
2428

cmd/limactl/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ func createStartActionCommon(cmd *cobra.Command, _ []string) (exit bool, err err
374374
if templates, err := templatestore.Templates(); err == nil {
375375
w := cmd.OutOrStdout()
376376
for _, f := range templates {
377-
_, _ = fmt.Fprintln(w, f.Name)
377+
// Don't show internal base templates like `_default/*` and `_images/*`.
378+
if !strings.HasPrefix(f.Name, "_") {
379+
_, _ = fmt.Fprintln(w, f.Name)
380+
}
378381
}
379382
return true, nil
380383
}

hack/common.inc.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ _IPERF3=iperf3
2929
# https://github.com/lima-vm/socket_vmnet/issues/85
3030
[ "$(uname -s)" = "Darwin" ] && _IPERF3="iperf3-darwin"
3131
: "${IPERF3:=$_IPERF3}"
32+
33+
# Setup LIMA_TEMPLATES_PATH because the templates are not installed, but reference base templates
34+
# via template://_images/* and template://_default/*.
35+
templates_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../templates" && pwd)"
36+
: "${LIMA_TEMPLATES_PATH:-$templates_dir}"
37+
export LIMA_TEMPLATES_PATH

hack/test-templates.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ FILE="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
2222
NAME="$(basename -s .yaml "$FILE")"
2323
OS_HOST="$(uname -o)"
2424

25-
# On Windows $HOME of the bash runner, %USERPROFILE% of the host machine and mpunting point in the guest machine
26-
# are all different folders. This will handle path differences, when values are expilictly set.
25+
# On Windows $HOME of the bash runner, %USERPROFILE% of the host machine and mounting point in the guest machine
26+
# are all different folders. This will handle path differences, when values are explicitly set.
2727
HOME_HOST=${HOME_HOST:-$HOME}
2828
HOME_GUEST=${HOME_GUEST:-$HOME}
2929
FILE_HOST=$FILE

pkg/limayaml/limayaml_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func TestDefaultYAML(t *testing.T) {
3737
assert.NilError(t, err)
3838
y.Images = nil // remove default images
3939
y.Mounts = nil // remove default mounts
40+
y.Base = nil // remove default base templates
41+
y.MinimumLimaVersion = nil // remove minimum Lima version
4042
y.MountTypesUnsupported = nil // remove default workaround for kernel 6.9-6.11
4143
t.Log(dumpJSON(t, y))
4244
b, err := Marshal(&y, false)

pkg/limayaml/validate_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package limayaml
55

66
import (
7-
"os"
8-
"runtime"
97
"testing"
108

119
"gotest.tools/v3/assert"
@@ -18,22 +16,6 @@ func TestValidateEmpty(t *testing.T) {
1816
assert.Error(t, err, "field `images` must be set")
1917
}
2018

21-
// Note: can't embed symbolic links, use "os"
22-
23-
func TestValidateDefault(t *testing.T) {
24-
if runtime.GOOS == "windows" {
25-
// FIXME: `assertion failed: error is not nil: field `mounts[1].location` must be an absolute path, got "/tmp/lima"`
26-
t.Skip("Skipping on windows")
27-
}
28-
29-
bytes, err := os.ReadFile("default.yaml")
30-
assert.NilError(t, err)
31-
y, err := Load(bytes, "default.yaml")
32-
assert.NilError(t, err)
33-
err = Validate(y, true)
34-
assert.NilError(t, err)
35-
}
36-
3719
func TestValidateProbes(t *testing.T) {
3820
images := `images: [{"location": "/"}]`
3921
validProbe := `probes: [{"script": "#!foo"}]`

pkg/templatestore/templatestore.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package templatestore
55

66
import (
7+
"errors"
8+
"fmt"
79
"io/fs"
810
"os"
911
"path/filepath"
@@ -20,21 +22,32 @@ type Template struct {
2022
}
2123

2224
func Read(name string) ([]byte, error) {
23-
dir, err := usrlocalsharelima.Dir()
24-
if err != nil {
25-
return nil, err
25+
var pathList []string
26+
if tmplPath := os.Getenv("LIMA_TEMPLATES_PATH"); tmplPath != "" {
27+
pathList = strings.Split(tmplPath, string(filepath.ListSeparator))
28+
} else {
29+
dir, err := usrlocalsharelima.Dir()
30+
if err != nil {
31+
return nil, err
32+
}
33+
pathList = []string{filepath.Join(dir, "templates"), name}
2634
}
2735
ext := filepath.Ext(name)
2836
// Append .yaml extension if name doesn't have an extension, or if it starts with a digit.
2937
// So "docker.sh" would remain unchanged but "ubuntu-24.04" becomes "ubuntu-24.04.yaml".
3038
if len(ext) < 2 || unicode.IsDigit(rune(ext[1])) {
3139
name += ".yaml"
3240
}
33-
filePath, err := securejoin.SecureJoin(filepath.Join(dir, "templates"), name)
34-
if err != nil {
35-
return nil, err
41+
for _, path := range pathList {
42+
filePath, err := securejoin.SecureJoin(path, name)
43+
if err != nil {
44+
return nil, err
45+
}
46+
if b, err := os.ReadFile(filePath); !errors.Is(err, os.ErrNotExist) {
47+
return b, err
48+
}
3649
}
37-
return os.ReadFile(filePath)
50+
return nil, fmt.Errorf("template %q not found", name)
3851
}
3952

4053
const Default = "default"

templates/_default/mounts.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mounts:
2+
- location: "~"
3+
- location: "/tmp/lima"
4+
writable: true

templates/_images/fedora-41.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
images:
2+
- location: "https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2"
3+
arch: "x86_64"
4+
digest: "sha256:6205ae0c524b4d1816dbd3573ce29b5c44ed26c9fbc874fbe48c41c89dd0bac2"
5+
- location: "https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/aarch64/images/Fedora-Cloud-Base-Generic-41-1.4.aarch64.qcow2"
6+
arch: "aarch64"
7+
digest: "sha256:085883b42c7e3b980e366a1fe006cd0ff15877f7e6e984426f3c6c67c7cc2faa"

0 commit comments

Comments
 (0)