From 1dca87fe71aaca018863a46e8b7f36574def775c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sat, 8 Jun 2024 15:04:38 +0200 Subject: [PATCH] Allow loading images by name from imagestore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- Makefile | 1 + cmd/limactl/start.go | 12 ++++++++++++ examples/default.yaml | 3 +++ images/debian-12.yaml | 15 +++++++++++++++ images/ubuntu-24.04.yaml | 15 +++++++++++++++ pkg/infoutil/infoutil.go | 7 +++++++ pkg/limayaml/limayaml.go | 6 ++++++ pkg/limayaml/load.go | 8 ++++++++ pkg/store/store.go | 12 ++++++++++++ 9 files changed, 79 insertions(+) create mode 100644 images/debian-12.yaml create mode 100644 images/ubuntu-24.04.yaml diff --git a/Makefile b/Makefile index 9b4506552a19..dc67de0bfe1e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/limactl/start.go b/cmd/limactl/start.go index 89587af831b9..60bc19a5daaa 100644 --- a/cmd/limactl/start.go +++ b/cmd/limactl/start.go @@ -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" @@ -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 diff --git a/examples/default.yaml b/examples/default.yaml index def8e9dbf3cc..85e04a6173ab 100644 --- a/examples/default.yaml +++ b/examples/default.yaml @@ -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 diff --git a/images/debian-12.yaml b/images/debian-12.yaml new file mode 100644 index 000000000000..fa9437c69ba5 --- /dev/null +++ b/images/debian-12.yaml @@ -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" diff --git a/images/ubuntu-24.04.yaml b/images/ubuntu-24.04.yaml new file mode 100644 index 000000000000..3fc76371bcd4 --- /dev/null +++ b/images/ubuntu-24.04.yaml @@ -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" diff --git a/pkg/infoutil/infoutil.go b/pkg/infoutil/infoutil.go index 19884e3b41ce..8c67a3ccb8d4 100644 --- a/pkg/infoutil/infoutil.go +++ b/pkg/infoutil/infoutil.go @@ -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" @@ -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 } @@ -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 diff --git a/pkg/limayaml/limayaml.go b/pkg/limayaml/limayaml.go index f7bb6da5761e..b2e6ee25df3e 100644 --- a/pkg/limayaml/limayaml.go +++ b/pkg/limayaml/limayaml.go @@ -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"` @@ -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 diff --git a/pkg/limayaml/load.go b/pkg/limayaml/load.go index f9c7e3c4ac5b..ed96a5fd068e 100644 --- a/pkg/limayaml/load.go +++ b/pkg/limayaml/load.go @@ -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) diff --git a/pkg/store/store.go b/pkg/store/store.go index 60c87222d0a4..f7e266d360e8 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/containerd/containerd/identifiers" + "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/store/filenames" @@ -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 }