{"record":{"id":"56c37b9d20a01552","repo":"lima-vm/lima","slug":"failed-to-attach-asif-image-q-w","errorCode":null,"errorMessage":"failed to attach ASIF image %#q: %w","messagePattern":"failed to attach ASIF image %#q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go","lineNumber":47,"sourceCode":"\t\t\tif err3 := os.Rename(path+\".asif\", path); err3 != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to rename ASIF image from %#q to %#q: %w\", path+\".asif\", path, err3)\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// NewAttachedASIF creates a new ASIF image file at the specified path with the given size\n// and attaches it, returning the attached device path and an open file handle.\n// The caller is responsible for detaching the ASIF image device when done.\nfunc NewAttachedASIF(path string, size int64) (string, *os.File, error) {\n\tif err := NewASIF(path, size); err != nil {\n\t\treturn \"\", nil, err\n\t}\n\tattachArgs := []string{\"image\", \"attach\", \"--noMount\", path}\n\tout, err := exec.CommandContext(context.Background(), \"diskutil\", attachArgs...).Output()\n\tif err != nil {\n\t\treturn \"\", nil, fmt.Errorf(\"failed to attach ASIF image %#q: %w\", path, err)\n\t}\n\tdevicePath := strings.TrimSpace(string(out))\n\tf, err := os.OpenFile(devicePath, os.O_RDWR, 0o644)\n\tif err != nil {\n\t\t_ = DetachASIF(devicePath)\n\t\treturn \"\", nil, fmt.Errorf(\"failed to open ASIF device %#q: %w\", devicePath, err)\n\t}\n\treturn devicePath, f, err\n}\n\n// DetachASIF detaches the ASIF image device at the specified path.\nfunc DetachASIF(devicePath string) error {\n\tif output, err := exec.CommandContext(context.Background(), \"hdiutil\", \"detach\", devicePath).CombinedOutput(); err != nil {\n\t\treturn fmt.Errorf(\"failed to detach ASIF image %#q: %w: %s\", devicePath, err, output)\n\t}\n\treturn nil\n}\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go#L29-L65","documentation":"NewAttachedASIF attaches an ASIF image with `diskutil image attach --noMount` and captures the device path from stdout. This error wraps a non-zero exit from that attach command. The image exists but could not be attached as a block device.","triggerScenarios":"Calling NewAttachedASIF (via convertTo) when diskutil attach fails: another image already attached with the same identity, diskimages-helper stuck, image corrupt or not a valid ASIF, or the macOS build lacks `diskutil image attach`.","commonSituations":"A previous run crashed without detaching and the system holds a stale attach; running inside a VM/container where diskutil cannot attach images; macOS version mismatch where the subcommand syntax differs.","solutions":["Run `diskutil image list` / `hdiutil info` and detach stale attached images, then retry","Run the attach command manually to see diskutil's stderr","Reboot or kill stale `diskimages-helper` processes if attach persistently fails","Verify the image is a valid ASIF (created successfully by NewASIF) and macOS supports image attach"],"exampleFix":"// cleanup stale attachments before retry\nout, _ := exec.Command(\"diskutil\", \"image\", \"list\", \"-plist\").Output()\n// detach matching image ids, then:\ndevice, f, err := asifutil.NewAttachedASIF(path, size)","handlingStrategy":"retry","validationCode":"// preflight: check for already-attached images\nout, _ := exec.Command(\"diskutil\", \"image\", \"list\").Output()\nif strings.Contains(string(out), filepath.Base(path)) { return errors.New(\"image already attached\") }","typeGuard":null,"tryCatchPattern":"device, f, err := asifutil.NewAttachedASIF(path, size)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to attach\") {\n        time.Sleep(2 * time.Second) // transient attach failures: retry once after cleanup\n    }\n    return err\n}","preventionTips":["Always detach images in defer/cleanup paths to avoid stale attachments","Run attach/detach on the host macOS, not inside a VM or container","Keep macOS updated; verify `diskutil image attach --noMount` works manually"],"tags":["macos","diskutil","attach","asif"],"backgroundTag":"disk-image-attach-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}