Skip to content

Commit

Permalink
Allow loading images by name from imagestore
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Jun 8, 2024
1 parent 203a60e commit 1dca87f
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ binaries: clean \
$(HELPERS) \
$(GUESTAGENT)
cp -aL examples _output/share/lima/templates
cp -aL images _output/share/lima/images
ifneq ($(GOOS),windows)
ln -sf templates _output/share/lima/examples
else
Expand Down
12 changes: 12 additions & 0 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/lima-vm/lima/cmd/limactl/editflags"
"github.com/lima-vm/lima/cmd/limactl/guessarg"
"github.com/lima-vm/lima/pkg/editutil"
"github.com/lima-vm/lima/pkg/imagestore"
"github.com/lima-vm/lima/pkg/ioutilx"
"github.com/lima-vm/lima/pkg/limayaml"
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
Expand Down Expand Up @@ -327,6 +328,17 @@ func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffe
if err != nil {
return nil, err
}
if y.Image != "" {
b, err := imagestore.Read(y.Image)
if err != nil {
return nil, err
}
i, err := limayaml.LoadImage(b, y.Image)
if err != nil {
return nil, err
}
y.Images = append(y.Images, i.Images...)
}
if err := limayaml.Validate(y, true); err != nil {
if !saveBrokenEditorBuffer {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ os: null
# 🟢 Builtin default: "default" (corresponds to the host architecture)
arch: null

# Name of image to use.
image: ""

# OpenStack-compatible disk image.
# 🟢 Builtin default: null (must be specified)
# 🔵 This file: Ubuntu images
Expand Down
15 changes: 15 additions & 0 deletions images/debian-12.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "debian:12"
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud.debian.org/images/cloud/bookworm/20240429-1732/debian-12-genericcloud-amd64-20240429-1732.qcow2"
arch: "x86_64"
digest: "sha512:6cc752d71b390c7fea64b0b598225914a7f4adacd4a33fa366187fac01094648628e0681a109ae9320b9a79aba2832f33395fa13154dad636465b7d9cdbed599"
- location: "https://cloud.debian.org/images/cloud/bookworm/20240429-1732/debian-12-genericcloud-arm64-20240429-1732.qcow2"
arch: "aarch64"
digest: "sha512:59afc40ad0062ca100c9280a281256487348c8aa23b3e70c329a6d6f29b5343b628622e63e0b9b4fc3987dd691d5f3c657233186b3271878d5e0aa0b4d264b06"
# Fallback to the latest release image.
# Hint: run `limactl prune` to invalidate the cache
- location: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
arch: "x86_64"
- location: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-arm64.qcow2"
arch: "aarch64"
15 changes: 15 additions & 0 deletions images/ubuntu-24.04.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "ubuntu:24.04"
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-amd64.img"
arch: "x86_64"
digest: "sha256:32a9d30d18803da72f5936cf2b7b9efcb4d0bb63c67933f17e3bdfd1751de3f3"
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-arm64.img"
arch: "aarch64"
digest: "sha256:c841bac00925d3e6892d979798103a867931f255f28fefd9d5e07e3e22d0ef22"
# Fallback to the latest release image.
# Hint: run `limactl prune` to invalidate the cache
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img"
arch: "x86_64"
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img"
arch: "aarch64"
7 changes: 7 additions & 0 deletions pkg/infoutil/infoutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package infoutil

import (
"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/imagestore"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/templatestore"
Expand All @@ -12,6 +13,8 @@ type Info struct {
Version string `json:"version"`
Templates []templatestore.Template `json:"templates"`
DefaultTemplate *limayaml.LimaYAML `json:"defaultTemplate"`
Images []imagestore.Image `json:"images"`
DefaultImage string `json:"defaultImage"`
LimaHome string `json:"limaHome"`
VMTypes []string `json:"vmTypes"` // since Lima v0.14.2
}
Expand All @@ -34,6 +37,10 @@ func GetInfo() (*Info, error) {
if err != nil {
return nil, err
}
info.Images, err = imagestore.Images()
if err != nil {
return nil, err
}
info.LimaHome, err = dirnames.LimaDir()
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type LimaYAML struct {
VMType *VMType `yaml:"vmType,omitempty" json:"vmType,omitempty"`
OS *OS `yaml:"os,omitempty" json:"os,omitempty"`
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Image string `yaml:"image,omitempty" json:"image,omitempty"`
Images []Image `yaml:"images" json:"images"` // REQUIRED
CPUType CPUType `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
Expand Down Expand Up @@ -44,6 +45,11 @@ type LimaYAML struct {
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
}

type ImageYAML struct {
Name string
Images []Image
}

type (
OS = string
Arch = string
Expand Down
8 changes: 8 additions & 0 deletions pkg/limayaml/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (d *Disk) UnmarshalYAML(value *yamlv3.Node) error {
return nil
}

func LoadImage(b []byte, filePath string) (*ImageYAML, error) {
var y ImageYAML
if err := unmarshalYAML(b, &y, filePath); err != nil {
return nil, err
}
return &y, nil
}

func unmarshalYAML(data []byte, v interface{}, comment string) error {
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
Expand Down
12 changes: 12 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/containerd/containerd/identifiers"
"github.com/lima-vm/lima/pkg/imagestore"

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (../hack/test-templates/alpine-9p-writable.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (alpine.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (fedora.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Colima (v0.6.5)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (experimental/9p.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (experimental/net-user-v2.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (docker.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (../hack/test-templates/test-misc.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (debian.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (opensuse.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests (on Linux) (archlinux.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Unit tests (1.22.x)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Unit tests (1.21.x)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / vz (fedora.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Artifacts Darwin

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Integration tests

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / VMNet test

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / vz (experimental/vz.yaml)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Windows tests

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Upgrade test (v0.15.1)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Upgrade test (v0.15.1)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:

Check failure on line 10 in pkg/store/store.go

View workflow job for this annotation

GitHub Actions / Upgrade test (v0.15.1)

no required module provides package github.com/lima-vm/lima/pkg/imagestore; to add it:
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
Expand Down Expand Up @@ -128,6 +129,17 @@ func LoadYAMLByFilePath(filePath string) (*limayaml.LimaYAML, error) {
if err != nil {
return nil, err
}
if y.Image != "" {
b, err := imagestore.Read(y.Image)
if err != nil {
return nil, err
}
i, err := limayaml.LoadImage(b, y.Image)
if err != nil {
return nil, err
}
y.Images = append(y.Images, i.Images...)
}
if err := limayaml.Validate(y, false); err != nil {
return nil, err
}
Expand Down

0 comments on commit 1dca87f

Please sign in to comment.