Skip to content

Commit e249752

Browse files
committed
Record lima working directory for instance
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 7225aae commit e249752

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

pkg/instance/create.go

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML
6060
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
6161
return nil, err
6262
}
63+
wd, err := os.Getwd()
64+
if err != nil {
65+
return nil, err
66+
}
67+
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaWorkDir), []byte(wd), 0o644); err != nil {
68+
return nil, err
69+
}
6370
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
6471
return nil, err
6572
}

pkg/store/filenames/filenames.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
const (
2929
LimaYAML = "lima.yaml"
3030
LimaVersion = "lima-version" // Lima version used to create instance
31+
LimaWorkDir = "lima-workdir" // Lima workdir used to create instance
3132
CIDataISO = "cidata.iso"
3233
CIDataISODir = "cidata"
3334
BaseDisk = "basedisk"

pkg/store/instance.go

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ func Inspect(instName string) (*Instance, error) {
8282
if err != nil {
8383
return nil, err
8484
}
85+
// Read working directory from instance directory, if existing
86+
limaWorkDirFile := filepath.Join(instDir, filenames.LimaWorkDir)
87+
if workdir, err := os.ReadFile(limaWorkDirFile); err == nil {
88+
wd := strings.TrimSuffix(string(workdir), "\n")
89+
if err := os.Chdir(wd); err != nil {
90+
logrus.Warnf("failed to change workdir to %q from %q", wd, limaWorkDirFile)
91+
}
92+
} else if !errors.Is(err, os.ErrNotExist) {
93+
inst.Errors = append(inst.Errors, err)
94+
}
8595
// Make sure inst.Dir is set, even when YAML validation fails
8696
inst.Dir = instDir
8797
yamlPath := filepath.Join(instDir, filenames.LimaYAML)

website/content/en/docs/dev/internals/_index.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ An instance directory contains the following files:
3333

3434
Metadata:
3535
- `lima-version`: the Lima version used to create this instance
36+
- `lima-workdir`: the Lima workdir used to create this instance
3637
- `lima.yaml`: the YAML
3738
- `protected`: empty file, used by `limactl protect`
3839

0 commit comments

Comments
 (0)