{"record":{"id":"58b2831ccc16180b","repo":"lima-vm/lima","slug":"failed-to-open-raw-tmp-file-q-w","errorCode":null,"errorMessage":"failed to open raw tmp file %q: %w","messagePattern":"failed to open raw tmp file %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/downloader/downloader.go","lineNumber":482,"sourceCode":"func ensureRawInCache(ctx context.Context, imagePath, format string, originalDigest digest.Digest) (string, digest.Digest, error) {\n\timgConvPath := filepath.Join(filepath.Dir(imagePath), \"imgconv\")\n\tif err := os.MkdirAll(imgConvPath, 0o700); err != nil {\n\t\treturn \"\", \"\", err\n\t}\n\trawImgConvPath := filepath.Join(imgConvPath, \"raw\")\n\n\tlogrus.Infof(\"Converting %s image to raw sparse format in cache: %q\", format, rawImgConvPath)\n\trawPathTmp := filepath.Join(imgConvPath, \"raw.tmp\")\n\tdefer os.Remove(rawPathTmp)\n\tdiskUtil := proxyimgutil.NewDiskUtil(ctx)\n\tif err := diskUtil.Convert(ctx, raw.Type, imagePath, rawPathTmp, nil, false); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to convert %q to raw: %w\", imagePath, err)\n\t}\n\n\t// Ensure the image is sparse to save cache space.\n\trawTmpF, err := os.OpenFile(rawPathTmp, os.O_RDWR, 0o644)\n\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to open raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\tfi, err := rawTmpF.Stat()\n\tif err != nil {\n\t\t_ = rawTmpF.Close()\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to stat raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\tif err := diskUtil.MakeSparse(ctx, rawTmpF, fi.Size()); err != nil {\n\t\tlogrus.WithError(err).Warnf(\"Failed to make %q sparse (non-fatal)\", rawPathTmp)\n\t}\n\tif err := rawTmpF.Close(); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to close raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\n\tif err := os.Remove(rawImgConvPath); err != nil && !errors.Is(err, os.ErrNotExist) {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to remove stale raw image %q: %w\", rawImgConvPath, err)\n\t}\n\tif err := os.Rename(rawPathTmp, rawImgConvPath); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to replace original with raw image: %w\", err)","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/downloader/downloader.go#L464-L500","documentation":"After successfully converting the image to rawPathTmp, ensureRawInCache reopens the temp file with os.OpenFile(O_RDWR) to sparsify it. If opening fails, this error wraps the OS error. Failure here means the conversion result exists but cannot be finalized.","triggerScenarios":"os.OpenFile(rawPathTmp, os.O_RDWR, 0o644) fails right after conversion — typically permission problems on the cache dir, the file being locked/removed by a concurrent process, or a read-only mount.","commonSituations":"Cache shared between users/VMs with mixed ownership; concurrent limactl downloads racing on the same cache entry; containers running as non-root against a root-owned cache.","solutions":["Check ownership/permissions of the cache directory and the imgConv subdir; fix with chown/chmod.","Ensure only one download for the same image runs at a time, or clean the cache of stale raw.tmp files.","Move the cache to a writable local filesystem (adjust CacheDir)."],"exampleFix":"// before: root-owned cache\n$ sudo chown -R $(id -u):$(id -g) ~/.cache/lima\n$ rm -f ~/.cache/lima/images/*/raw.tmp\n// after: reopen succeeds, sparsification proceeds","handlingStrategy":"validation","validationCode":"info, err := os.Stat(cacheDir)\nif err != nil || !info.IsDir() {\n    return fmt.Errorf(\"cache dir not writable: %w\", err)\n}\nif err := unix.Access(cacheDir, unix.W_OK); err != nil {\n    return fmt.Errorf(\"cache dir lacks write permission: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"res, err := d.Download(ctx, local, url)\nif err != nil && strings.Contains(err.Error(), \"failed to open raw tmp file\") {\n    // fix cache ownership/permissions, remove stale raw.tmp, retry\n}","preventionTips":["Keep a single OS user for all Lima operations so cache ownership is uniform.","Avoid concurrent limactl processes operating on the same cache entry.","Do not place CacheDir on read-only or root-owned volumes."],"tags":["downloader","file-io","permissions","cache"],"backgroundTag":"cache-write-failure","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}