From 3b335496a46010a179578199c329f6e2dec42618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Mon, 24 Jun 2024 16:57:08 +0200 Subject: [PATCH] Record lima working directory for instance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/instance/create.go | 7 +++++++ pkg/store/filenames/filenames.go | 1 + pkg/store/instance.go | 10 ++++++++++ website/content/en/docs/dev/internals/_index.md | 1 + 4 files changed, 19 insertions(+) diff --git a/pkg/instance/create.go b/pkg/instance/create.go index 8c74e31b9527..534423959cf3 100644 --- a/pkg/instance/create.go +++ b/pkg/instance/create.go @@ -60,6 +60,13 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML if err := os.WriteFile(filePath, yBytes, 0o644); err != nil { return nil, err } + wd, err := os.Getwd() + if err != nil { + return nil, err + } + if err := os.WriteFile(filepath.Join(instDir, filenames.LimaWorkDir), []byte(wd), 0o644); err != nil { + return nil, err + } if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil { return nil, err } diff --git a/pkg/store/filenames/filenames.go b/pkg/store/filenames/filenames.go index f07300a66747..0abde6761bdc 100644 --- a/pkg/store/filenames/filenames.go +++ b/pkg/store/filenames/filenames.go @@ -28,6 +28,7 @@ const ( const ( LimaYAML = "lima.yaml" LimaVersion = "lima-version" // Lima version used to create instance + LimaWorkDir = "lima-workdir" // Lima workdir used to create instance CIDataISO = "cidata.iso" CIDataISODir = "cidata" BaseDisk = "basedisk" diff --git a/pkg/store/instance.go b/pkg/store/instance.go index 1a88a8c63f3c..f5d711493b98 100644 --- a/pkg/store/instance.go +++ b/pkg/store/instance.go @@ -82,6 +82,16 @@ func Inspect(instName string) (*Instance, error) { if err != nil { return nil, err } + // Read working directory from instance directory, if existing + limaWorkDirFile := filepath.Join(instDir, filenames.LimaWorkDir) + if workdir, err := os.ReadFile(limaWorkDirFile); err == nil { + wd := strings.TrimSuffix(string(workdir), "\n") + if err := os.Chdir(wd); err != nil { + logrus.Warnf("failed to change workdir to %q from %q", wd, limaWorkDirFile) + } + } else if !errors.Is(err, os.ErrNotExist) { + inst.Errors = append(inst.Errors, err) + } // Make sure inst.Dir is set, even when YAML validation fails inst.Dir = instDir yamlPath := filepath.Join(instDir, filenames.LimaYAML) diff --git a/website/content/en/docs/dev/internals/_index.md b/website/content/en/docs/dev/internals/_index.md index bc2081006bc4..e3aeea4d51aa 100644 --- a/website/content/en/docs/dev/internals/_index.md +++ b/website/content/en/docs/dev/internals/_index.md @@ -33,6 +33,7 @@ An instance directory contains the following files: Metadata: - `lima-version`: the Lima version used to create this instance +- `lima-workdir`: the Lima workdir used to create this instance - `lima.yaml`: the YAML - `protected`: empty file, used by `limactl protect`