{"record":{"id":"9323be6349490e38","repo":"lima-vm/lima","slug":"failed-to-attach-disk-w","errorCode":null,"errorMessage":"failed to attach disk: %w","messagePattern":"failed to attach disk: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/guestpatch/macos/macos_darwin.go","lineNumber":55,"sourceCode":"}\n\n// Patch prepares a macOS guest disk for first boot. It writes the\n// LaunchDaemon plist, init script, and setup markers via a noowners\n// mount, then fixes file ownership by patching APFS inode records\n// directly on the raw disk image. No sudo required.\nfunc Patch(ctx context.Context, disk string) error {\n\tif err := patchWriteGuestFiles(ctx, disk); err != nil {\n\t\treturn err\n\t}\n\treturn patchFixOwnership(ctx, disk)\n}\n\n// patchWriteGuestFiles attaches the disk image, mounts the Data\n// volume with noowners, writes guest files, then detaches.\nfunc patchWriteGuestFiles(ctx context.Context, disk string) error {\n\tattached, err := attachImageWithRetry(ctx, disk, 3)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to attach disk: %w\", err)\n\t}\n\tif attached == nil || attached.Data == \"\" {\n\t\treturn errors.New(\"failed to find data slice in attached disk\")\n\t}\n\tdataDevPath := \"/dev/\" + attached.Data\n\tdefer func() {\n\t\t// Detaching the data slice is enough to detach the whole ASIF.\n\t\tif err := asifutil.DetachASIF(dataDevPath); err != nil {\n\t\t\tlogrus.WithError(err).Warnf(\"failed to detach %#q (%#q)\", dataDevPath, disk)\n\t\t}\n\t}()\n\n\tinstDir := filepath.Dir(disk)\n\tmnt := filepath.Join(instDir, filenames.MntDir)\n\tif err := os.MkdirAll(mnt, 0o755); err != nil {\n\t\treturn fmt.Errorf(\"failed to create mount point %#q: %w\", mnt, err)\n\t}\n\tdefer func() {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/guestpatch/macos/macos_darwin.go#L37-L73","documentation":"patchWriteGuestFiles attaches the instance disk image (ASIF) using attachImageWithRetry before mounting its Data volume to write guest files. If attaching fails after the retries (3 attempts), the underlying error is wrapped with this message.","triggerScenarios":"Patch -> patchWriteGuestFiles when attachImageWithRetry exhausts 3 attempts because the disk image is missing/corrupt, already exclusively attached, or the attach framework (ASIF) returns an error.","commonSituations":"Instance disk already attached to a running VM; disk path wrong or instance deleted; macOS DiskImages framework failing on a corrupt/incomplete image; concurrent patch attempts.","solutions":["Check the wrapped cause for the specific attach failure from attachImageWithRetry.","Ensure the instance is stopped so the disk is not exclusively attached.","Verify the disk path exists and is a valid ASIF/image file (`ls -l`, file integrity).","Avoid concurrent Patch invocations on the same instance; retry after clearing stale attach state."],"exampleFix":"// before: patching a running instance's disk\nlimactl patch inst (while inst is running) -> EBUSY on attach\n// after\nlimactl stop inst && limactl patch inst","handlingStrategy":"retry","validationCode":"// before patching, ensure instance is stopped and disk exists\nif inst.Status != \"Stopped\" {\n\treturn fmt.Errorf(\"instance must be stopped before patching (status=%s)\", inst.Status)\n}\nif _, err := os.Stat(disk); err != nil {\n\treturn fmt.Errorf(\"disk image missing: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := patchWriteGuestFiles(ctx, disk); err != nil {\n\tif strings.Contains(err.Error(), \"failed to attach disk\") {\n\t\t// wait for any stale attach to clear, then retry once\n\t\ttime.Sleep(2 * time.Second)\n\t\treturn patchWriteGuestFiles(ctx, disk)\n\t}\n\treturn err\n}","preventionTips":["Stop the instance before patching so the disk is not exclusively attached.","Serialize patch operations per instance with a lock.","Verify the disk image integrity before attach.","Rely on attachImageWithRetry logs to identify persistent vs transient attach failures."],"tags":["guestpatch","macos","disk-attach","dmg"],"backgroundTag":"disk-attach-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}